UNPKG

amocrm-client

Version:
31 lines (26 loc) 1.66 kB
import { IResourceCollection, IResourceEntity, IResourceFactory } from "../../../interfaces/api"; import { ICustomFieldValue } from "../../../interfaces/custom_field"; import { TConstructor, TEntityConstructor } from "../../../types"; import { ResourceCollection } from "../../ResourceCollection"; interface IResourceEntityWithCustomFields<T extends IResourceFactory<IResourceEntity<T>>> extends IResourceEntity<T> { custom_fields_values: IResourceCollection<ICustomFieldValue>; } export interface IHasCustomFieldsEntity<T extends IResourceFactory<IResourceEntityWithCustomFields<T>>> extends IResourceEntityWithCustomFields<T> { cf(identity: number): ResourceCollection<ICustomFieldValue>; cf(identity: number): ICustomFieldValue | null; cf(identity: number, value: any): ICustomFieldValue; } export function hasCustomFields<T extends IResourceFactory<IHasCustomFieldsEntity<T>>>(Base: TEntityConstructor<T>): TConstructor<IResourceEntityWithCustomFields<T>> { return class HasCustomFields extends Base { custom_fields_values: IResourceCollection<ICustomFieldValue>; cf(...args: any) { if (args.length === 0) return this.custom_fields_values; let custom_field = this.custom_fields_values.findWhere({ field_id: args[0] }); if (!custom_field && args.length == 1) return null; else if (!custom_field) custom_field = { field_id: args[0] as number, values: [] }; if (args[1] != undefined) custom_field.values = args[1]; this.custom_fields_values.add([custom_field]); return custom_field; } }; }