useTicketEdit.ts 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121
  1. // Copyright (C) 2012-2023 Zammad Foundation, https://zammad-foundation.org/
  2. import type { ComputedRef, ShallowRef } from 'vue'
  3. import { computed, reactive, watch } from 'vue'
  4. import { pick } from 'lodash-es'
  5. import type { FormValues, FormRef, FormData } from '@shared/components/Form'
  6. import { useObjectAttributeFormData } from '@shared/entities/object-attributes/composables/useObjectAttributeFormData'
  7. import { useObjectAttributes } from '@shared/entities/object-attributes/composables/useObjectAttributes'
  8. import type { TicketUpdateInput } from '@shared/graphql/types'
  9. import { EnumObjectManagerObjects } from '@shared/graphql/types'
  10. import { MutationHandler } from '@shared/server/apollo/handler'
  11. import type { TicketById } from '@shared/entities/ticket/types'
  12. import type { FileUploaded } from '@shared/components/Form/fields/FieldFile/types'
  13. import type { SecurityValue } from '@shared/components/Form/fields/FieldSecurity/types'
  14. import { getNode } from '@formkit/core'
  15. import { useTicketUpdateMutation } from '../graphql/mutations/update.api'
  16. interface ArticleFormValues {
  17. articleType: string
  18. body: string
  19. internal: boolean
  20. cc?: string[]
  21. subtype?: string
  22. inReplyTo?: string
  23. to?: string[]
  24. subject?: string
  25. attachments?: FileUploaded[]
  26. contentType?: string
  27. security?: SecurityValue
  28. }
  29. export const useTicketEdit = (
  30. ticket: ComputedRef<TicketById | undefined>,
  31. form: ShallowRef<FormRef | undefined>,
  32. ) => {
  33. const initialTicketValue = reactive<FormValues>({})
  34. const mutationUpdate = new MutationHandler(useTicketUpdateMutation({}))
  35. watch(ticket, (ticket) => {
  36. if (!ticket) {
  37. return
  38. }
  39. const ticketId = initialTicketValue.id || ticket.id
  40. const { internalId: ownerInternalId } = ticket.owner
  41. initialTicketValue.id = ticket.id
  42. // show Zammad user as empty
  43. initialTicketValue.owner_id = ownerInternalId === 1 ? null : ownerInternalId
  44. form.value?.resetForm(initialTicketValue, ticket, {
  45. // don't reset to new values, if user changes something
  46. // if ticket is different, it's probably navigation to another ticket,
  47. // so we can safely reset the form
  48. resetDirty: ticketId !== ticket.id,
  49. })
  50. })
  51. const isTicketFormGroupValid = computed(() => {
  52. const ticketGroup = form.value?.formNode?.at('ticket')
  53. return !!ticketGroup?.context?.state.valid
  54. })
  55. const { attributesLookup: ticketObjectAttributesLookup } =
  56. useObjectAttributes(EnumObjectManagerObjects.Ticket)
  57. const processArticle = (
  58. formId: string,
  59. article: ArticleFormValues | undefined,
  60. ) => {
  61. if (!article) return null
  62. const attachments = article.attachments || []
  63. const files = attachments.map((file) =>
  64. pick(file, ['content', 'name', 'type']),
  65. )
  66. const contentType = getNode('body')?.context?.contentType || 'text/html'
  67. return {
  68. type: article.articleType,
  69. body: article.body,
  70. internal: article.internal,
  71. cc: article.cc,
  72. to: article.to,
  73. subject: article.subject,
  74. subtype: article.subtype,
  75. inReplyTo: article.inReplyTo,
  76. contentType,
  77. attachments: attachments.length ? { files, formId } : null,
  78. security: article.security,
  79. }
  80. }
  81. const editTicket = async (formData: FormData) => {
  82. if (!ticket.value) return undefined
  83. if (!formData.owner_id) {
  84. formData.owner_id = 1
  85. }
  86. const { internalObjectAttributeValues, additionalObjectAttributeValues } =
  87. useObjectAttributeFormData(ticketObjectAttributesLookup.value, formData)
  88. const formArticle = formData.article as ArticleFormValues | undefined
  89. const article = processArticle(formData.formId, formArticle)
  90. return mutationUpdate.send({
  91. ticketId: ticket.value.id,
  92. input: {
  93. ...internalObjectAttributeValues,
  94. objectAttributeValues: additionalObjectAttributeValues,
  95. article,
  96. } as TicketUpdateInput,
  97. })
  98. }
  99. return {
  100. initialTicketValue,
  101. isTicketFormGroupValid,
  102. editTicket,
  103. }
  104. }