utils.ts 2.1 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374
  1. // Copyright (C) 2012-2025 Zammad Foundation, https://zammad-foundation.org/
  2. import type { ObjectAttribute } from '#shared/entities/object-attributes/types/store.ts'
  3. import { useEntity } from '#shared/entities/useEntity.ts'
  4. import type { ObjectAttributeValue } from '#shared/graphql/types.ts'
  5. import { i18n } from '#shared/i18n.ts'
  6. import type { EntityObject } from '#shared/types/entity.ts'
  7. import type { ObjectLike } from '#shared/types/utils.ts'
  8. import { camelize } from '#shared/utils/formatter.ts'
  9. import { edgesToArray } from '#shared/utils/helpers.ts'
  10. import type { Dictionary } from 'ts-essentials'
  11. export const getValue = (
  12. key: string,
  13. object: ObjectLike,
  14. attributesObject: Dictionary<ObjectAttributeValue>,
  15. attribute: ObjectAttribute,
  16. ) => {
  17. if (attribute.dataOption?.relation) {
  18. const entityName = attribute.dataOption.relation
  19. const belongsTo = attribute.dataOption.belongs_to || camelize(entityName)
  20. const entity = useEntity(entityName)
  21. const entityObject = object[belongsTo]
  22. if (entity && entityObject) {
  23. // Special handling for array relations (e.g. secondary organizations)
  24. if ('edges' in entityObject) {
  25. return edgesToArray(entityObject)
  26. .map((item) => entity.display(item as EntityObject))
  27. .join(', ')
  28. }
  29. return entity.display(entityObject)
  30. }
  31. }
  32. if (key in attributesObject) {
  33. return attributesObject[key].value
  34. }
  35. if (key in object) {
  36. return object[key]
  37. }
  38. return object[camelize(key)]
  39. }
  40. export const isEmpty = (value: unknown) => {
  41. if (Array.isArray(value)) {
  42. return value.length === 0
  43. }
  44. if (value && typeof value === 'object') {
  45. return Object.keys(value).length === 0
  46. }
  47. return value === null || value === undefined || value === ''
  48. }
  49. export const getLink = (
  50. name: string,
  51. attributesObject: Dictionary<ObjectAttributeValue>,
  52. ) => {
  53. const attribute = attributesObject[name]
  54. return attribute?.renderedLink || null
  55. }
  56. export const translateOption = (attribute: ObjectAttribute, str?: string) => {
  57. if (!str) return ''
  58. if (attribute.dataOption?.translate) {
  59. return i18n.t(str)
  60. }
  61. return str
  62. }