UNPKG

amocrm-client

Version:
49 lines 2.44 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.hasCustomers = void 0; const tslib_1 = require("tslib"); const ResourceCollection_1 = require("../../ResourceCollection"); const CustomerFactory_1 = require("../../factories/CustomerFactory"); function hasCustomers(Base) { return class HasCompanies extends Base { customers(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.customers; if (embeddedCompanies.size() === 0) return []; if (Object.keys(embeddedCompanies.first()).length > 5) return embeddedCompanies; const customerIds = new ResourceCollection_1.ResourceCollection(embeddedCompanies.pluck("id")).chunk(250).all(); const customers = new ResourceCollection_1.ResourceCollection([]); const companyFactory = new CustomerFactory_1.CustomerFactory(this.getFactory().getRequest()); for (const ids of customerIds) { const pagination = yield companyFactory.get({ filter: { id: ids }, limit: 250 }, options); customers.add(pagination.getData()); if (!pagination.hasNext() || pagination.getData().size() < 250) continue; let hasNextPage = true; do { const nextPage = yield pagination.next(); if (nextPage != false) customers.add(nextPage); else hasNextPage = false; } while (hasNextPage); } this._embedded.customers = customers; return customers; }); } }; } exports.hasCustomers = hasCustomers; //# sourceMappingURL=hasCustomers.js.map