useTicketSignature.ts 3.0 KB

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