GuidedSetupManualInviteColleagues.vue 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121
  1. <!-- Copyright (C) 2012-2024 Zammad Foundation, https://zammad-foundation.org/ -->
  2. <!-- eslint-disable @typescript-eslint/no-unused-vars -->
  3. <script setup lang="ts">
  4. import Form from '#shared/components/Form/Form.vue'
  5. import { useForm } from '#shared/components/Form/useForm.ts'
  6. import { defineFormSchema } from '#shared/form/defineFormSchema.ts'
  7. import { useNotifications } from '#shared/components/CommonNotifications/useNotifications.ts'
  8. import { NotificationTypes } from '#shared/components/CommonNotifications/types.ts'
  9. import { useUserAddMutation } from '#shared/entities/user/graphql/mutations/add.api.ts'
  10. import MutationHandler from '#shared/server/apollo/handler/MutationHandler.ts'
  11. import { useObjectAttributes } from '#shared/entities/object-attributes/composables/useObjectAttributes.ts'
  12. import { useObjectAttributeFormData } from '#shared/entities/object-attributes/composables/useObjectAttributeFormData.ts'
  13. import type { FormSubmitData } from '#shared/components/Form/types.ts'
  14. import {
  15. GroupAccess,
  16. type GroupPermissionReactive,
  17. } from '#desktop/components/Form/fields/FieldGroupPermissions/types.ts'
  18. import type { SelectValue } from '#shared/components/CommonSelect/types.ts'
  19. import {
  20. EnumObjectManagerObjects,
  21. EnumFormUpdaterId,
  22. type UserInput,
  23. } from '#shared/graphql/types.ts'
  24. import { useSystemSetup } from '../../composables/useSystemSetup.ts'
  25. import GuidedSetupActionFooter from '../../components/GuidedSetupActionFooter.vue'
  26. const { setBoxSize, setTitle } = useSystemSetup()
  27. setBoxSize?.('large')
  28. setTitle(__('Invite Colleagues'))
  29. const { form } = useForm()
  30. const { notify } = useNotifications()
  31. const schema = defineFormSchema([
  32. {
  33. screen: 'invite_agent',
  34. object: EnumObjectManagerObjects.User,
  35. },
  36. ])
  37. const transformGroupPermissions = (value: GroupPermissionReactive[]) =>
  38. value.reduce(
  39. (groupPermissions, row) => {
  40. if (!row.groups) return groupPermissions
  41. groupPermissions.push(
  42. ...(row.groups as unknown as SelectValue[]).map((groupInternalId) => ({
  43. groupInternalId,
  44. accessType: Object.keys(row.groupAccess).reduce((accesses, key) => {
  45. if (row.groupAccess[key as GroupAccess])
  46. accesses.push(key as GroupAccess)
  47. return accesses
  48. }, [] as GroupAccess[]),
  49. })),
  50. )
  51. return groupPermissions
  52. },
  53. [] as {
  54. groupInternalId: SelectValue
  55. accessType: GroupAccess[]
  56. }[],
  57. )
  58. const { attributesLookup } = useObjectAttributes(EnumObjectManagerObjects.User)
  59. const inviteUser = async (formData: FormSubmitData) => {
  60. // TODO: Try to move this value transformation into the relevant field.
  61. if (formData.group_ids) {
  62. formData.group_ids = transformGroupPermissions(
  63. formData.group_ids as unknown as GroupPermissionReactive[],
  64. )
  65. }
  66. const { internalObjectAttributeValues, additionalObjectAttributeValues } =
  67. useObjectAttributeFormData(attributesLookup.value, formData)
  68. const input: UserInput = {
  69. ...internalObjectAttributeValues,
  70. objectAttributeValues: additionalObjectAttributeValues,
  71. }
  72. const userAdd = new MutationHandler(useUserAddMutation())
  73. return userAdd
  74. .send({
  75. input,
  76. sendInvite: true,
  77. })
  78. .then(async (result) => {
  79. if (result?.userAdd?.user) {
  80. notify({
  81. type: NotificationTypes.Success,
  82. message: __('Invitation sent!'),
  83. })
  84. }
  85. })
  86. }
  87. </script>
  88. <template>
  89. <Form
  90. id="invite-colleagues"
  91. ref="form"
  92. form-class="mb-2.5"
  93. :schema="schema"
  94. :form-updater-id="EnumFormUpdaterId.FormUpdaterUpdaterUserInvite"
  95. use-object-attributes
  96. clear-values-after-submit
  97. @submit="inviteUser"
  98. />
  99. <GuidedSetupActionFooter
  100. :form="form"
  101. :submit-button-text="__('Send Invitation')"
  102. :continue-button-text="__('Finish Setup')"
  103. continue-route="/guided-setup/manual/finish"
  104. />
  105. </template>