amocrm-client
Version:
JS Library for AmoCRM
22 lines (20 loc) • 1.07 kB
text/typescript
import { ICriteria, IResourceEntity } from "../../../interfaces/api";
import { TConstructor, TEntityConstructor } from "../../../types";
import { IRequestOptions } from "../../../interfaces/common";
import { ICanGetByIdFactory, } from "../../factories/mixins/hasGetById";
export interface IHasFetchEntity<T extends ICanGetByIdFactory<IResourceEntity<T>>> {
fetch(criteria?: ICriteria<any, any>, options?: IRequestOptions): Promise<IHasFetchEntity<T> | false | null>;
}
export function hasFetch<T extends ICanGetByIdFactory<IResourceEntity<T>>>(Base: TEntityConstructor<T>): TConstructor<IResourceEntity<T>> {
return class HasFetch extends Base {
async fetch(criteria?: ICriteria<any, any>, options?: IRequestOptions) {
if (this.isNew() && this.updated_at != undefined) {
return false;
}
const id = <number>this.id;
const factory = this.getFactory();
const entity = await factory.getById(id, criteria, options);
return entity;
}
};
}