objectAttributes.ts 1.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566
  1. // Copyright (C) 2012-2024 Zammad Foundation, https://zammad-foundation.org/
  2. import { defineStore } from 'pinia'
  3. import { ref } from 'vue'
  4. import type { EnumObjectManagerObjects } from '#shared/graphql/types.ts'
  5. import log from '#shared/utils/log.ts'
  6. import type {
  7. EntityStaticObjectAttributes,
  8. ObjectAttribute,
  9. ObjectAttributesObject,
  10. } from '../types/store.ts'
  11. const staticObjectAttributesEntityModules: Record<
  12. string,
  13. EntityStaticObjectAttributes
  14. > = import.meta.glob(['../../*/stores/objectAttributes.ts'], {
  15. eager: true,
  16. import: 'staticObjectAttributes',
  17. })
  18. export const entitiesStaticObjectAttributes = Object.values(
  19. staticObjectAttributesEntityModules,
  20. )
  21. export const staticObjectAttributesByEntity =
  22. entitiesStaticObjectAttributes.reduce<
  23. Record<EnumObjectManagerObjects, ObjectAttribute[]>
  24. >(
  25. (result, entityItem) => {
  26. result[entityItem.name] = entityItem.attributes
  27. return result
  28. },
  29. {} as Record<EnumObjectManagerObjects, ObjectAttribute[]>,
  30. )
  31. export const useObjectAttributesStore = defineStore('objectAttributes', () => {
  32. const objectAttributesObjectLookup = ref<
  33. Record<string, ObjectAttributesObject>
  34. >({})
  35. const getObjectAttributesForObject = (object: EnumObjectManagerObjects) => {
  36. const objectAttributesObject = objectAttributesObjectLookup.value[object]
  37. if (!objectAttributesObject) {
  38. log.error(
  39. `Please load the form object attributes first, the object "${object}" does not exists in the store.`,
  40. )
  41. }
  42. return objectAttributesObject
  43. }
  44. const setObjectAttributesForObject = (
  45. object: EnumObjectManagerObjects,
  46. data: ObjectAttributesObject,
  47. ) => {
  48. objectAttributesObjectLookup.value[object] = data
  49. }
  50. return {
  51. objectAttributesObjectLookup,
  52. setObjectAttributesForObject,
  53. getObjectAttributesForObject,
  54. }
  55. })