useTicketSignature.ts 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114
  1. // Copyright (C) 2012-2024 Zammad Foundation, https://zammad-foundation.org/
  2. import type { TicketById } from '#shared/entities/ticket/types.ts'
  3. import type {
  4. FormHandler,
  5. FormHandlerFunction,
  6. FormValues,
  7. ChangedField,
  8. } from '#shared/components/Form/types.ts'
  9. import { FormHandlerExecution } from '#shared/components/Form/types.ts'
  10. import type { FieldEditorContext } from '#shared/components/Form/fields/FieldEditor/types.ts'
  11. import { useTicketSignatureLazyQuery } from '#shared/graphql/queries/ticketSignature.api.ts'
  12. import {
  13. convertToGraphQLId,
  14. getIdFromGraphQLId,
  15. } from '#shared/graphql/utils.ts'
  16. import { QueryHandler } from '#shared/server/apollo/handler/index.ts'
  17. import type { Ref } from 'vue'
  18. import type {
  19. TicketSignatureQuery,
  20. TicketSignatureQueryVariables,
  21. } from '#shared/graphql/types.ts'
  22. let signatureQuery: QueryHandler<
  23. TicketSignatureQuery,
  24. TicketSignatureQueryVariables
  25. >
  26. export const getTicketSignatureQuery = () => {
  27. if (signatureQuery) return signatureQuery
  28. signatureQuery = new QueryHandler(
  29. useTicketSignatureLazyQuery({ groupId: '' }),
  30. )
  31. return signatureQuery
  32. }
  33. // TODO: can maybe be moved inside ticket entity?
  34. export const useTicketSignature = (ticket?: Ref<TicketById | undefined>) => {
  35. const signatureQuery = getTicketSignatureQuery()
  36. const getValue = (
  37. values: FormValues,
  38. changedField: ChangedField,
  39. name: string,
  40. ) => {
  41. return changedField.name === name ? changedField.newValue : values[name]
  42. }
  43. const signatureHandling = (editorName: string): FormHandler => {
  44. const handleSignature: FormHandlerFunction = (
  45. execution,
  46. reactivity,
  47. data,
  48. ) => {
  49. const { formNode, values, changedField } = data
  50. if (
  51. changedField?.name !== 'group_id' &&
  52. changedField?.name !== 'articleSenderType'
  53. )
  54. return
  55. const editorContext = formNode?.find(editorName, 'name')?.context as
  56. | FieldEditorContext
  57. | undefined
  58. if (!editorContext) return
  59. const groupId = getValue(values, changedField, 'group_id')
  60. if (!groupId) {
  61. editorContext.removeSignature()
  62. return
  63. }
  64. const senderType = getValue(values, changedField, 'articleSenderType')
  65. if (senderType !== 'email-out') {
  66. editorContext.removeSignature?.()
  67. return
  68. }
  69. signatureQuery
  70. .query({
  71. variables: {
  72. groupId: convertToGraphQLId('Group', String(groupId)),
  73. ticketId: ticket?.value?.id,
  74. },
  75. })
  76. .then(({ data: signature }) => {
  77. const body = signature?.ticketSignature?.renderedBody
  78. const id = signature?.ticketSignature?.id
  79. if (!body || !id) {
  80. editorContext.removeSignature()
  81. return
  82. }
  83. editorContext.addSignature({ body, id: getIdFromGraphQLId(id) })
  84. })
  85. }
  86. return {
  87. execution: [
  88. FormHandlerExecution.Initial,
  89. FormHandlerExecution.FieldChange,
  90. ],
  91. callback: handleSignature,
  92. }
  93. }
  94. return {
  95. signatureHandling,
  96. }
  97. }