UNPKG

amocrm-client

Version:
44 lines (39 loc) 1.8 kB
import { TFactoryConstructor } from "../../../types"; import { ICollectionResponse, IEntityAttributes, IResourceEntity, IResourceFactory } from "../../../interfaces/api"; import { IRequestOptions, Moment } from "../../../interfaces/common"; export interface IUpdateResult { id: number; updated_at: Date; request_id: string; } export interface IEntityUpdateAttributes extends IEntityAttributes { updated_at?: number; } export interface IHasUpdateFactory<T extends IResourceEntity<IResourceFactory<T>>> extends IResourceFactory<T> { update(criteria: (object | T)[], options?: IRequestOptions): Promise<T[]> } export function hasUpdate<T extends IResourceEntity<IResourceFactory<T>>>(Base: TFactoryConstructor<T>): TFactoryConstructor<T> { return class HasUpdate extends Base implements IResourceFactory<T> { async update(criteria: (object | T)[], options?: IRequestOptions): Promise<T[]> { const url = this.getUrl(); const requestCriteria = this.getEntityCriteria(criteria); const request = this.getRequest(); const { data } = await request.patch<ICollectionResponse<IUpdateResult>>(url, requestCriteria, options); const response = this.getEmbedded(data); const result = response.map((attributes, index: number) => { const entityCriteria = criteria[index]; const instance = entityCriteria instanceof this.getEntityClass() ? entityCriteria : this.from(entityCriteria); instance.id = attributes.id; instance.updated_at = attributes.updated_at; return instance; }); return result; } }; }