useTicketChangeCustomer.ts 2.0 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273
  1. // Copyright (C) 2012-2024 Zammad Foundation, https://zammad-foundation.org/
  2. import {
  3. NotificationTypes,
  4. useNotifications,
  5. } from '#shared/components/CommonNotifications/index.ts'
  6. import type { FormSubmitData } from '#shared/components/Form/types.ts'
  7. import { useTicketCustomerUpdateMutation } from '#shared/entities/ticket/graphql/mutations/customerUpdate.api.ts'
  8. import type {
  9. TicketById,
  10. TicketCustomerUpdateFormData,
  11. } from '#shared/entities/ticket/types.ts'
  12. import UserError from '#shared/errors/UserError.ts'
  13. import type { TicketCustomerUpdateInput } from '#shared/graphql/types.ts'
  14. import { convertToGraphQLId } from '#shared/graphql/utils.ts'
  15. import { MutationHandler } from '#shared/server/apollo/handler/index.ts'
  16. import type { Ref } from 'vue'
  17. export const useTicketChangeCustomer = (
  18. ticket: Ref<TicketById>,
  19. options?: { onSuccess: () => void },
  20. ) => {
  21. const { notify } = useNotifications()
  22. const changeCustomerMutation = new MutationHandler(
  23. useTicketCustomerUpdateMutation(),
  24. )
  25. const changeCustomer = async (
  26. formData: FormSubmitData<TicketCustomerUpdateFormData>,
  27. ) => {
  28. const input = {
  29. customerId: convertToGraphQLId('User', formData.customer_id),
  30. } as TicketCustomerUpdateInput
  31. if (formData.organization_id) {
  32. input.organizationId = convertToGraphQLId(
  33. 'Organization',
  34. formData.organization_id,
  35. )
  36. }
  37. try {
  38. const result = await changeCustomerMutation.send({
  39. ticketId: ticket.value.id,
  40. input,
  41. })
  42. if (result) {
  43. options?.onSuccess?.()
  44. notify({
  45. id: 'ticket-customer-updated',
  46. type: NotificationTypes.Success,
  47. message: __('Ticket customer updated successfully.'),
  48. })
  49. return result
  50. }
  51. } catch (errors) {
  52. if (errors instanceof UserError) {
  53. notify({
  54. id: 'ticket-customer-update-error',
  55. message: errors.generalErrors[0],
  56. type: NotificationTypes.Error,
  57. })
  58. }
  59. }
  60. }
  61. return { changeCustomer }
  62. }