UNPKG

amocrm-client

Version:
71 lines (59 loc) 4.3 kB
import { ICriteria, IResourceEntityWithEmbedded } from "../../../interfaces/api"; import { TConstructor, TEntityEmbeddedConstructor } from "../../../types"; import { IRequestOptions } from "../../../interfaces/common"; import { ICanGetByIdFactory } from "../../factories/mixins/hasGetById"; import { ResourceCollection } from "../../ResourceCollection"; import { CompanyCriteria } from "../../../interfaces/company"; import { ICompanyFactory, CompanyFactory } from "../../factories/CompanyFactory"; import { Company, ICompany } from "../Company"; import { ICatalogElement } from "../CatalogElement"; import { CatalogCriteria, CatalogElementCriteria } from "../../../interfaces/catalog"; import { Catalog, ICatalog } from "../Catalog"; import { CatalogFactory, ICatalogFactory } from "../../factories/CatalogFactory"; import { CatalogElementDTO } from "../../../dto/catalog.dto"; import { CatalogElementFactory } from "../../factories/CatalogElementFactory"; interface IEmbeddedWithCatalogElements { catalogs: ResourceCollection<ICatalog> | undefined; catalog_elements: ResourceCollection<ICatalogElement> } export interface IHasCatalogsEntity<T extends ICanGetByIdFactory<IResourceEntityWithEmbedded<T>>> extends IResourceEntityWithEmbedded<T> { catalogs(criteria?: CatalogCriteria, options?: IRequestOptions): Promise<ICatalog[] | false> catalogElements(criteria?: Pick<CatalogElementCriteria, "with">, options?: IRequestOptions): Promise<ICatalogElement[]>; } export function hasCompanies<T extends ICanGetByIdFactory<IResourceEntityWithEmbedded<T>>>(Base: TEntityEmbeddedConstructor<T>): TConstructor<IResourceEntityWithEmbedded<T>> { return class HasCatalogs extends Base { async catalogs(criteria?: CatalogCriteria, options?: IRequestOptions): Promise<ResourceCollection<ICatalog> | false> { let _embedded = this._embedded; if (!this._embedded && !this.id) return false; if (this.id && !this._embedded) { const entity = await (this.getFactory() as T).getById(this.id, { with: "catalog_elements" }); if (entity == null) return false; _embedded = entity._embedded; } const embeddedElements = (_embedded as IEmbeddedWithCatalogElements).catalog_elements; if (embeddedElements.size() === 0) return new ResourceCollection(); if ((_embedded as IEmbeddedWithCatalogElements).catalogs != undefined) return (_embedded as IEmbeddedWithCatalogElements).catalogs!; const catalogsIds: number[] = embeddedElements.map((element) => element.metadata != undefined ? element.metadata.catalog_id : element.catalog_id).all(); const catalogFactory: ICatalogFactory = new CatalogFactory(this.getFactory().getRequest()); const data = await (await catalogFactory.get(Object.assign(criteria || {}, { limit: 250 }) as Partial<CatalogCriteria>, options)).getData(); const catalogs = data.filter((catalog) => catalogsIds.includes(catalog.id!)); (this._embedded as IEmbeddedWithCatalogElements).catalogs = catalogs; return catalogs; } async catalogElements(criteria?: ICriteria<any, any>, options?: IRequestOptions) { let _embedded = this._embedded; if (!this._embedded && !this.id) return false; if (this.id && !this._embedded) { const entity = await (this.getFactory() as T).getById(this.id, { with: "catalog_elements" }); if (entity == null) return false; _embedded = entity._embedded; } const embeddedElements = (_embedded as IEmbeddedWithCatalogElements).catalog_elements; if (embeddedElements.size() === 0) return []; if (Object.keys(embeddedElements.first()!).length > 5) return embeddedElements; const catalogElementsIds: number[][] = new ResourceCollection(embeddedElements.pluck("id")).chunk(250).all(); const catalogElements: ResourceCollection<ICompany> = new ResourceCollection<ICompany>([]); const catalogElementFactory: ICompanyFactory = new CatalogElementFactory(this.getFactory().getRequest()); } }; }