autocompletion-external-data-source.ts 2.0 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162
  1. // Copyright (C) 2012-2024 Zammad Foundation, https://zammad-foundation.org/
  2. import { getNodeByName } from '#shared/components/Form/utils.ts'
  3. import type { FieldResolverModule } from '#shared/entities/object-attributes/types/resolver.ts'
  4. import { EnumObjectManagerObjects } from '#shared/graphql/types.ts'
  5. import { ensureGraphqlId } from '#shared/graphql/utils.ts'
  6. import type { ObjectLike } from '#shared/types/utils.ts'
  7. import FieldResolver from '../FieldResolver.ts'
  8. import type { JsonValue } from 'type-fest'
  9. export class FieldResolverAutocompletionExternalDataSource extends FieldResolver {
  10. fieldType = 'externalDataSource'
  11. public fieldTypeAttributes() {
  12. return {
  13. props: {
  14. clearable: !!this.attributeConfig.nulloption,
  15. noOptionsLabelTranslation: !this.attributeConfig.translate,
  16. object: this.object,
  17. searchTemplateRenderContext: (
  18. formId: string,
  19. entityObject: ObjectLike,
  20. ) => {
  21. const templateRenderContext: Record<string, JsonValue> = {}
  22. switch (this.object) {
  23. case EnumObjectManagerObjects.Ticket:
  24. if (entityObject) {
  25. templateRenderContext.customerId = entityObject.customer?.id
  26. }
  27. if (!templateRenderContext.customerId) {
  28. const node = getNodeByName(formId, 'customer_id')
  29. const value = node?.value as string
  30. if (value) {
  31. templateRenderContext.customerId = ensureGraphqlId(
  32. 'User',
  33. value,
  34. )
  35. }
  36. }
  37. return templateRenderContext
  38. case EnumObjectManagerObjects.User:
  39. case EnumObjectManagerObjects.Organization:
  40. case EnumObjectManagerObjects.Group:
  41. default:
  42. return templateRenderContext
  43. }
  44. },
  45. },
  46. }
  47. }
  48. }
  49. export default <FieldResolverModule>{
  50. type: 'autocompletion_ajax_external_data_source',
  51. resolver: FieldResolverAutocompletionExternalDataSource,
  52. }