useTicketFormOrganizationHandler.ts 3.6 KB

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