useTicketSignature.ts 3.1 KB

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