useTicketEdit.ts 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183
  1. // Copyright (C) 2012-2024 Zammad Foundation, https://zammad-foundation.org/
  2. import { isEqual } from 'lodash-es'
  3. import { computed, ref, watch } from 'vue'
  4. import { populateEditorNewLines } from '#shared/components/Form/fields/FieldEditor/utils.ts'
  5. import type {
  6. FormValues,
  7. FormRef,
  8. FormSubmitData,
  9. } from '#shared/components/Form/types.ts'
  10. import { getNodeByName } from '#shared/components/Form/utils.ts'
  11. import { useObjectAttributeFormData } from '#shared/entities/object-attributes/composables/useObjectAttributeFormData.ts'
  12. import { useObjectAttributes } from '#shared/entities/object-attributes/composables/useObjectAttributes.ts'
  13. import { useTicketUpdateMutation } from '#shared/entities/ticket/graphql/mutations/update.api.ts'
  14. import type { TicketById } from '#shared/entities/ticket/types.ts'
  15. import type { TicketArticleFormValues } from '#shared/entities/ticket-article/action/plugins/types.ts'
  16. import type {
  17. TicketUpdateInput,
  18. TicketUpdateMetaInput,
  19. } from '#shared/graphql/types.ts'
  20. import { EnumObjectManagerObjects } from '#shared/graphql/types.ts'
  21. import { MutationHandler } from '#shared/server/apollo/handler/index.ts'
  22. import type { PartialRequired } from '#shared/types/utils.ts'
  23. import { convertFilesToAttachmentInput } from '#shared/utils/files.ts'
  24. import type { ComputedRef, ShallowRef } from 'vue'
  25. type TicketArticleReceivedFormValues = PartialRequired<
  26. TicketArticleFormValues,
  27. // form always has these values
  28. 'articleType' | 'body' | 'internal'
  29. >
  30. const TICKET_FORM_RELEVANT_KEYS = [
  31. 'id',
  32. 'group',
  33. 'owner',
  34. 'state',
  35. 'pending_time',
  36. 'priority',
  37. 'customer',
  38. 'organization',
  39. 'objectAttributeValues',
  40. ]
  41. export const useTicketEdit = (
  42. ticket: ComputedRef<TicketById | undefined>,
  43. form: ShallowRef<FormRef | undefined>,
  44. ) => {
  45. const initialTicketValue = ref<FormValues>()
  46. const mutationUpdate = new MutationHandler(useTicketUpdateMutation({}))
  47. const ticketFormRelatedData = computed<Partial<TicketById>>(
  48. (currentTicketFormRelatedData) => {
  49. if (!ticket.value) return {}
  50. const newTicketFormRelatedData = (
  51. TICKET_FORM_RELEVANT_KEYS as Array<keyof TicketById>
  52. ).reduce<Partial<TicketById>>((relevantData, key) => {
  53. if (!ticket.value || !(key in ticket.value)) return relevantData
  54. relevantData[key] = ticket.value[key]
  55. return relevantData
  56. }, {})
  57. if (
  58. currentTicketFormRelatedData &&
  59. isEqual(newTicketFormRelatedData, currentTicketFormRelatedData)
  60. ) {
  61. return currentTicketFormRelatedData
  62. }
  63. return newTicketFormRelatedData
  64. },
  65. )
  66. watch(
  67. ticketFormRelatedData,
  68. () => {
  69. if (!ticket.value) {
  70. return
  71. }
  72. const { internalId: ownerInternalId } = ticket.value.owner
  73. initialTicketValue.value = {
  74. id: ticket.value.id,
  75. owner_id: ownerInternalId === 1 ? null : ownerInternalId,
  76. isDefaultFollowUpStateSet: undefined, // the default value for reset situations.
  77. }
  78. if (!form.value?.formInitialSettled) return
  79. form.value?.resetForm(
  80. {
  81. values: initialTicketValue.value,
  82. object: ticket.value,
  83. },
  84. {
  85. resetDirty: false,
  86. },
  87. )
  88. },
  89. { immediate: true },
  90. )
  91. const isTicketFormGroupValid = computed(() => {
  92. const ticketGroup = form.value?.formNode?.at('ticket')
  93. return !!ticketGroup?.context?.state.valid
  94. })
  95. const { attributesLookup: ticketObjectAttributesLookup } =
  96. useObjectAttributes(EnumObjectManagerObjects.Ticket)
  97. const processArticle = (
  98. formId: string,
  99. article: TicketArticleReceivedFormValues | undefined,
  100. ) => {
  101. if (!article) return null
  102. const contentType =
  103. getNodeByName(formId, 'body')?.context?.contentType || 'text/html'
  104. if (contentType === 'text/html') {
  105. article.body = populateEditorNewLines(article.body)
  106. }
  107. return {
  108. type: article.articleType,
  109. body: article.body,
  110. internal: article.internal,
  111. cc: article.cc,
  112. to: article.to,
  113. subject: article.subject,
  114. subtype: article.subtype,
  115. inReplyTo: article.inReplyTo,
  116. contentType,
  117. attachments: convertFilesToAttachmentInput(formId, article.attachments),
  118. security: article.security,
  119. timeUnit: article.timeUnit,
  120. accountedTimeTypeId: article.accountedTimeTypeId,
  121. }
  122. }
  123. const editTicket = async (
  124. formData: FormSubmitData,
  125. meta?: TicketUpdateMetaInput,
  126. ) => {
  127. if (!ticket.value || !form.value) return undefined
  128. if (!formData.owner_id) {
  129. formData.owner_id = 1
  130. }
  131. const { internalObjectAttributeValues, additionalObjectAttributeValues } =
  132. useObjectAttributeFormData(ticketObjectAttributesLookup.value, formData)
  133. const formArticle = formData.article as
  134. | TicketArticleReceivedFormValues
  135. | undefined
  136. const article = processArticle(form.value.formId, formArticle)
  137. const ticketMeta = meta || {}
  138. return mutationUpdate.send({
  139. ticketId: ticket.value.id,
  140. input: {
  141. ...internalObjectAttributeValues,
  142. objectAttributeValues: additionalObjectAttributeValues,
  143. article,
  144. } as TicketUpdateInput,
  145. meta: ticketMeta,
  146. })
  147. }
  148. return {
  149. initialTicketValue,
  150. isTicketFormGroupValid,
  151. editTicket,
  152. }
  153. }