User.ts 2.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273
  1. // Copyright (C) 2012-2024 Zammad Foundation, https://zammad-foundation.org/
  2. import { faker } from '@faker-js/faker'
  3. import type { Organization, User } from '#shared/graphql/types.ts'
  4. import { convertToGraphQLId } from '#shared/graphql/utils.ts'
  5. import type { DeepPartial } from '#shared/types/utils.ts'
  6. import { getStoredMockedObject } from '../builders/index.ts'
  7. export default (
  8. parent: any | undefined,
  9. userValue: User | undefined,
  10. ): DeepPartial<User> => {
  11. const firstname = faker.person.firstName()
  12. const lastname = faker.person.lastName()
  13. const user: DeepPartial<User> = {
  14. firstname,
  15. lastname,
  16. fullname: `${firstname} ${lastname}`,
  17. image: faker.image.dataUri(),
  18. imageSource: null,
  19. email: faker.internet.email(),
  20. fax: null,
  21. login: faker.internet.userName(),
  22. phone: '+49 #### ######'.replace(/#+/g, (m) =>
  23. faker.string.numeric(m.length),
  24. ),
  25. outOfOffice: null,
  26. outOfOfficeStartAt: null,
  27. outOfOfficeEndAt: null,
  28. outOfOfficeReplacement: null,
  29. objectAttributeValues: [],
  30. createdBy: null,
  31. secondaryOrganizations: {
  32. edges: [],
  33. totalCount: 0,
  34. },
  35. updatedBy: null,
  36. policy: {
  37. update: true,
  38. destroy: true,
  39. },
  40. authorizations: [],
  41. }
  42. if (parent?.__typename === 'Organization') {
  43. user.organization = parent
  44. } else if (userValue) {
  45. const organization = getStoredMockedObject<Organization>('Organization', 1)
  46. if (organization) {
  47. // if the organization already exists, add the user to it
  48. user.organization = organization
  49. const members = organization.allMembers.edges
  50. const lastCursor = members[members.length - 1]?.cursor
  51. const cursor = `${lastCursor || 'AB'}A`
  52. organization.allMembers.edges.push({
  53. __typename: 'UserEdge',
  54. cursor,
  55. node: userValue as any,
  56. })
  57. organization.allMembers.totalCount += 1
  58. organization.allMembers.pageInfo ??= {} as any
  59. organization.allMembers.pageInfo.startCursor =
  60. members[0]?.cursor || cursor
  61. organization.allMembers.pageInfo.endCursor = cursor
  62. } else {
  63. user.organization = {
  64. id: convertToGraphQLId('Organization', 1),
  65. }
  66. }
  67. }
  68. return user
  69. }