GuidedSetupManualInviteColleagues.vue 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124
  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 { NotificationTypes } from '#shared/components/CommonNotifications/types.ts'
  5. import { useNotifications } from '#shared/components/CommonNotifications/useNotifications.ts'
  6. import type { SelectValue } from '#shared/components/CommonSelect/types.ts'
  7. import Form from '#shared/components/Form/Form.vue'
  8. import type { FormSubmitData } from '#shared/components/Form/types.ts'
  9. import { useForm } from '#shared/components/Form/useForm.ts'
  10. import { useObjectAttributeFormData } from '#shared/entities/object-attributes/composables/useObjectAttributeFormData.ts'
  11. import { useObjectAttributes } from '#shared/entities/object-attributes/composables/useObjectAttributes.ts'
  12. import { useUserAddMutation } from '#shared/entities/user/graphql/mutations/add.api.ts'
  13. import { defineFormSchema } from '#shared/form/defineFormSchema.ts'
  14. import {
  15. EnumObjectManagerObjects,
  16. EnumFormUpdaterId,
  17. type UserInput,
  18. } from '#shared/graphql/types.ts'
  19. import MutationHandler from '#shared/server/apollo/handler/MutationHandler.ts'
  20. import {
  21. GroupAccess,
  22. type GroupPermissionReactive,
  23. } from '#desktop/components/Form/fields/FieldGroupPermissions/types.ts'
  24. import GuidedSetupActionFooter from '../../components/GuidedSetupActionFooter.vue'
  25. import { useSystemSetup } from '../../composables/useSystemSetup.ts'
  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. id: 'invite-colleagues',
  82. type: NotificationTypes.Success,
  83. message: __('Invitation sent!'),
  84. })
  85. }
  86. })
  87. }
  88. </script>
  89. <template>
  90. <Form
  91. id="invite-colleagues"
  92. ref="form"
  93. form-class="mb-2.5"
  94. :schema="schema"
  95. :form-updater-id="EnumFormUpdaterId.FormUpdaterUpdaterUserInvite"
  96. use-object-attributes
  97. clear-values-after-submit
  98. @submit="inviteUser"
  99. />
  100. <GuidedSetupActionFooter
  101. :form="form"
  102. :submit-button-text="__('Send Invitation')"
  103. :continue-button-text="__('Finish Setup')"
  104. continue-route="/guided-setup/manual/finish"
  105. />
  106. </template>