useTicketEdit.ts 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180
  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 || !form.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. }
  77. form.value?.resetForm(
  78. {
  79. values: initialTicketValue.value,
  80. object: ticket.value,
  81. },
  82. {
  83. resetDirty: false,
  84. },
  85. )
  86. },
  87. { immediate: true },
  88. )
  89. const isTicketFormGroupValid = computed(() => {
  90. const ticketGroup = form.value?.formNode?.at('ticket')
  91. return !!ticketGroup?.context?.state.valid
  92. })
  93. const { attributesLookup: ticketObjectAttributesLookup } =
  94. useObjectAttributes(EnumObjectManagerObjects.Ticket)
  95. const processArticle = (
  96. formId: string,
  97. article: TicketArticleReceivedFormValues | undefined,
  98. ) => {
  99. if (!article) return null
  100. const contentType =
  101. getNodeByName(formId, 'body')?.context?.contentType || 'text/html'
  102. if (contentType === 'text/html') {
  103. article.body = populateEditorNewLines(article.body)
  104. }
  105. return {
  106. type: article.articleType,
  107. body: article.body,
  108. internal: article.internal,
  109. cc: article.cc,
  110. to: article.to,
  111. subject: article.subject,
  112. subtype: article.subtype,
  113. inReplyTo: article.inReplyTo,
  114. contentType,
  115. attachments: convertFilesToAttachmentInput(formId, article.attachments),
  116. security: article.security,
  117. timeUnit: article.timeUnit,
  118. accountedTimeTypeId: article.accountedTimeTypeId,
  119. }
  120. }
  121. const editTicket = async (
  122. formData: FormSubmitData,
  123. meta?: TicketUpdateMetaInput,
  124. ) => {
  125. if (!ticket.value || !form.value) return undefined
  126. if (!formData.owner_id) {
  127. formData.owner_id = 1
  128. }
  129. const { internalObjectAttributeValues, additionalObjectAttributeValues } =
  130. useObjectAttributeFormData(ticketObjectAttributesLookup.value, formData)
  131. const formArticle = formData.article as
  132. | TicketArticleReceivedFormValues
  133. | undefined
  134. const article = processArticle(form.value.formId, formArticle)
  135. const ticketMeta = meta || {}
  136. return mutationUpdate.send({
  137. ticketId: ticket.value.id,
  138. input: {
  139. ...internalObjectAttributeValues,
  140. objectAttributeValues: additionalObjectAttributeValues,
  141. article,
  142. } as TicketUpdateInput,
  143. meta: ticketMeta,
  144. })
  145. }
  146. return {
  147. initialTicketValue,
  148. isTicketFormGroupValid,
  149. editTicket,
  150. }
  151. }