useOrganizationDetail.ts 2.7 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697
  1. // Copyright (C) 2012-2024 Zammad Foundation, https://zammad-foundation.org/
  2. import { storeToRefs } from 'pinia'
  3. import { computed, ref, type Ref } from 'vue'
  4. import type {
  5. OrganizationUpdatesSubscriptionVariables,
  6. OrganizationUpdatesSubscription,
  7. } from '#shared/graphql/types.ts'
  8. import { convertToGraphQLId } from '#shared/graphql/utils.ts'
  9. import { QueryHandler } from '#shared/server/apollo/handler/index.ts'
  10. import type { GraphQLHandlerError } from '#shared/types/error.ts'
  11. import { normalizeEdges } from '#shared/utils/helpers.ts'
  12. import { useOrganizationQuery } from '../graphql/queries/organization.api.ts'
  13. import { OrganizationUpdatesDocument } from '../graphql/subscriptions/organizationUpdates.api.ts'
  14. import { useOrganizationObjectAttributesStore } from '../stores/objectAttributes.ts'
  15. import type { WatchQueryFetchPolicy } from '@apollo/client/core'
  16. export const useOrganizationDetail = (
  17. internalId: Ref<number | undefined>,
  18. errorCallback?: (error: GraphQLHandlerError) => boolean,
  19. fetchPolicy?: WatchQueryFetchPolicy,
  20. ) => {
  21. const organizationId = computed(() => {
  22. if (!internalId.value) return
  23. return convertToGraphQLId('Organization', internalId.value)
  24. })
  25. const fetchMembersCount = ref<Maybe<number>>(3)
  26. const organizationQuery = new QueryHandler(
  27. useOrganizationQuery(
  28. () => ({
  29. organizationInternalId: internalId.value,
  30. membersCount: 3,
  31. }),
  32. () => ({
  33. enabled: Boolean(internalId.value),
  34. fetchPolicy,
  35. }),
  36. ),
  37. {
  38. errorCallback,
  39. },
  40. )
  41. organizationQuery.subscribeToMore<
  42. OrganizationUpdatesSubscriptionVariables,
  43. OrganizationUpdatesSubscription
  44. >(() => ({
  45. document: OrganizationUpdatesDocument,
  46. variables: {
  47. organizationId: organizationId.value!,
  48. membersCount: fetchMembersCount.value,
  49. },
  50. }))
  51. const organizationResult = organizationQuery.result()
  52. const loading = organizationQuery.loading()
  53. const organization = computed(() => organizationResult.value?.organization)
  54. const loadAllMembers = () => {
  55. const organizationInternalId = organization.value?.internalId
  56. if (!organizationInternalId) {
  57. return
  58. }
  59. organizationQuery
  60. .refetch({
  61. organizationInternalId,
  62. membersCount: null,
  63. })
  64. .then(() => {
  65. fetchMembersCount.value = null
  66. })
  67. }
  68. const { viewScreenAttributes } = storeToRefs(
  69. useOrganizationObjectAttributesStore(),
  70. )
  71. const organizationMembers = computed(
  72. () => normalizeEdges(organization.value?.allMembers) || [],
  73. )
  74. return {
  75. loading,
  76. organizationQuery,
  77. organization,
  78. objectAttributes: viewScreenAttributes,
  79. organizationMembers,
  80. loadAllMembers,
  81. }
  82. }