User.ts 2.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172
  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.members.edges
  50. const lastCursor = members[members.length - 1]?.cursor
  51. const cursor = `${lastCursor || 'AB'}A`
  52. organization.members.edges.push({
  53. __typename: 'UserEdge',
  54. cursor,
  55. node: userValue as any,
  56. })
  57. organization.members.totalCount += 1
  58. organization.members.pageInfo ??= {} as any
  59. organization.members.pageInfo.startCursor = members[0]?.cursor || cursor
  60. organization.members.pageInfo.endCursor = cursor
  61. } else {
  62. user.organization = {
  63. id: convertToGraphQLId('Organization', 1),
  64. }
  65. }
  66. }
  67. return user
  68. }