entity-data-mapper.service.ts 1.4 KB

123456789101112131415161718192021222324252627282930313233343536
  1. import { Inject, Injectable } from '@nestjs/common';
  2. import { CustomFields, VendureEntity } from '@vendure/core';
  3. import { DASHBOARD_PLUGIN_OPTIONS } from '../constants';
  4. import { DashboardPluginOptions } from '../types';
  5. import { EntityDataMapper } from './entity-data-mapper.interface';
  6. import { ProductDataMapper } from './product.data-mapper';
  7. import { VendureEntityDataMapper } from './vendure-entity.data-mapper';
  8. @Injectable()
  9. export class EntityDataMapperService {
  10. defaultMappers: Map<keyof CustomFields | string, EntityDataMapper> = new Map([
  11. ['Product', new ProductDataMapper()],
  12. ]);
  13. constructor(
  14. @Inject(DASHBOARD_PLUGIN_OPTIONS) private readonly dashboardPluginOptions: DashboardPluginOptions,
  15. ) {}
  16. async map(entityName: string, entity: VendureEntity) {
  17. const mappers =
  18. this.dashboardPluginOptions.globalSearch?.entityDataMappers ??
  19. new Map<string, EntityDataMapper>();
  20. let mapperForEntity: EntityDataMapper | undefined;
  21. if (!mappers.has(entityName)) {
  22. mapperForEntity = mappers.get(entityName);
  23. } else if (this.defaultMappers.has(entityName)) {
  24. mapperForEntity = this.defaultMappers.get(entityName);
  25. }
  26. if (!mapperForEntity) {
  27. mapperForEntity = new VendureEntityDataMapper();
  28. }
  29. return mapperForEntity!.map(entity);
  30. }
  31. }