useTicketFormOrganizationHandler.ts 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125
  1. // Copyright (C) 2012-2023 Zammad Foundation, https://zammad-foundation.org/
  2. import { getNode } from '@formkit/core'
  3. import { FormHandlerExecution } from '@shared/components/Form'
  4. import type { FormHandlerFunction, FormHandler } from '@shared/components/Form'
  5. import { useSessionStore } from '@shared/stores/session'
  6. import type { Organization, Scalars } from '@shared/graphql/types'
  7. import type { AutoCompleteCustomerOption } from '@shared/components/Form/fields/FieldCustomer'
  8. import type { UserData } from '@shared/types/store' // TODO: remove this import
  9. import type {
  10. FormSchemaField,
  11. ReactiveFormSchemData,
  12. ChangedField,
  13. } from '@shared/components/Form/types'
  14. import { getAutoCompleteOption } from '@shared/entities/organization/utils/getAutoCompleteOption'
  15. // TODO: needs to be aligned, when auto completes has a final state.
  16. export const useTicketFormOganizationHandler = (): FormHandler => {
  17. const executeHandler = (
  18. execution: FormHandlerExecution,
  19. schemaData: ReactiveFormSchemData,
  20. changedField?: ChangedField,
  21. ) => {
  22. if (!schemaData.fields.organization_id) return false
  23. if (
  24. execution === FormHandlerExecution.FieldChange &&
  25. (!changedField || changedField.name !== 'customer_id')
  26. ) {
  27. return false
  28. }
  29. return true
  30. }
  31. const handleOrganizationField: FormHandlerFunction = (
  32. execution,
  33. formNode,
  34. values,
  35. changeFields,
  36. updateSchemaDataField,
  37. schemaData,
  38. changedField,
  39. initialEntityObject,
  40. // eslint-disable-next-line sonarjs/cognitive-complexity
  41. ) => {
  42. if (!executeHandler(execution, schemaData, changedField)) return
  43. const session = useSessionStore()
  44. const organizationField: Partial<FormSchemaField> = {
  45. show: false,
  46. required: false,
  47. }
  48. const setCustomer = (): Maybe<UserData> | undefined => {
  49. if (session.hasPermission('ticket.agent')) {
  50. if (changedField?.newValue) {
  51. return (
  52. getNode('customer_id')?.context?.optionValueLookup as Record<
  53. number,
  54. AutoCompleteCustomerOption
  55. >
  56. )[changedField.newValue as number].user as UserData
  57. }
  58. if (
  59. execution === FormHandlerExecution.FieldChange ||
  60. !values.customer_id ||
  61. !initialEntityObject
  62. )
  63. return undefined
  64. return initialEntityObject.customer
  65. }
  66. return session.user
  67. }
  68. const setOrganizationField = (
  69. customerId: Scalars['ID'],
  70. organization?: Maybe<Partial<Organization>>,
  71. ) => {
  72. if (!organization) return
  73. organizationField.show = true
  74. organizationField.required = true
  75. const currentValueOption = getAutoCompleteOption(organization)
  76. // Some information can be changed during the next user interactions, so update only the current schema data.
  77. updateSchemaDataField({
  78. name: 'organization_id',
  79. props: {
  80. defaultFilter: '*',
  81. options: [currentValueOption],
  82. additionalQueryParams: {
  83. customerId,
  84. },
  85. },
  86. value: currentValueOption.value,
  87. })
  88. }
  89. const customer = setCustomer()
  90. if (customer?.hasSecondaryOrganizations) {
  91. setOrganizationField(
  92. customer.id,
  93. execution === FormHandlerExecution.Initial && initialEntityObject
  94. ? initialEntityObject.organization
  95. : (customer.organization as Organization),
  96. )
  97. }
  98. // This values should be fixed, until the user change something in the customer_id field.
  99. changeFields.value.organization_id = {
  100. ...(changeFields.value.organization_id || {}),
  101. ...organizationField,
  102. }
  103. }
  104. return {
  105. execution: [FormHandlerExecution.Initial, FormHandlerExecution.FieldChange],
  106. callback: handleOrganizationField,
  107. }
  108. }