UNPKG

amocrm-client

Version:
57 lines 2.83 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.hasContacts = void 0; const tslib_1 = require("tslib"); const ResourceCollection_1 = require("../../ResourceCollection"); const ContactFactory_1 = require("../../factories/ContactFactory"); function hasContacts(Base) { return class HasContacts extends Base { contacts(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: "contacts" }); if (entity == null) return false; _embedded = entity._embedded; } const embeddedContacts = _embedded.contacts; if (embeddedContacts.size() === 0) return []; if (Object.keys(embeddedContacts.first() || {}).length > 5) return embeddedContacts; const contactIds = new ResourceCollection_1.ResourceCollection(embeddedContacts.pluck('id')).chunk(250).all(); const contacts = new ResourceCollection_1.ResourceCollection([]); const contactFactory = new ContactFactory_1.ContactFactory(this.getFactory().getRequest()); for (const ids of contactIds) { const mergedCriteria = Object.assign(criteria || {}, { filter: { id: ids }, limit: 250 }); const pagination = yield contactFactory.get(mergedCriteria, options); contacts.add(pagination.getData().all()); if (!pagination.hasNext() || pagination.getData().size() < 250) continue; let hasNextPage = true; do { const nextPage = yield pagination.next(); if (nextPage != false) contacts.add(nextPage); else hasNextPage = false; } while (hasNextPage); } contacts.each((contact) => { embeddedContacts.each((embeddedContact) => { if (embeddedContact.id == contact.id) { contact.is_main = embeddedContact.is_main; } }); }); this._embedded.contacts = contacts; return contacts; }); } }; } exports.hasContacts = hasContacts; //# sourceMappingURL=hasContacts.js.map