UNPKG

amocrm-client

Version:
50 lines 2.53 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.hasCompanies = void 0; const tslib_1 = require("tslib"); const ResourceCollection_1 = require("../../ResourceCollection"); const CompanyFactory_1 = require("../../factories/CompanyFactory"); function hasCompanies(Base) { return class HasCompanies extends Base { comapanies(criteria, options) { return tslib_1.__awaiter(this, void 0, void 0, function* () { let _embedded = this._embedded; if (!this._embedded && !this.id) return false; if (this.id && !this._embedded) { const entity = yield this.getFactory().getById(this.id, { with: "companies" }); if (entity == null) return false; _embedded = entity._embedded; } const embeddedCompanies = _embedded.companies; if (embeddedCompanies.size() === 0) return []; if (Object.keys(embeddedCompanies.first()).length > 5) return embeddedCompanies; const companyIds = new ResourceCollection_1.ResourceCollection(embeddedCompanies.pluck("id")).chunk(250).all(); const companies = new ResourceCollection_1.ResourceCollection([]); const companyFactory = new CompanyFactory_1.CompanyFactory(this.getFactory().getRequest()); for (const ids of companyIds) { const mergedCriteria = Object.assign(criteria || {}, { filter: { id: ids }, limit: 250 }); const pagination = yield companyFactory.get(mergedCriteria, options); companies.add(pagination.getData().all()); if (!pagination.hasNext() || pagination.getData().size() < 250) continue; let hasNextPage = true; do { const nextPage = yield pagination.next(); if (nextPage != false) companies.add(nextPage); else hasNextPage = false; } while (hasNextPage); } this._embedded.companies = companies; return companies; }); } }; } exports.hasCompanies = hasCompanies; //# sourceMappingURL=hasCompanies.js.map