UNPKG

@apihawk/billia-sdk

Version:

The ApiHawk Billia SDK

488 lines (487 loc) 16.6 kB
"use strict"; var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) { return new (P || (P = Promise))(function (resolve, reject) { function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } } function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } } function step(result) { result.done ? resolve(result.value) : new P(function (resolve) { resolve(result.value); }).then(fulfilled, rejected); } step((generator = generator.apply(thisArg, _arguments || [])).next()); }); }; Object.defineProperty(exports, "__esModule", { value: true }); const qs = require("qs"); const billia_sdk_service_base_1 = require("../lib/billia-sdk-service-base"); const to_rest_resource_1 = require("./common/to-rest-resource"); class BilliaSDKCustomer extends billia_sdk_service_base_1.BilliaSDKServiceBase { /** * Fetches customer profiles. * * @param session user session */ getProfiles(session) { return __awaiter(this, void 0, void 0, function* () { return this.api .call({ url: '/customer/profile?page_size=-1', method: 'GET', session, headers: { Accept: 'application/hal+json', 'Content-Type': 'application/json', 'Accept-Response': 'Advanced' } }) .then((result) => { if (result._embedded && result._embedded.customer_profile) { return Promise.resolve(result._embedded.customer_profile.sort((a, b) => { if (a.type === 'master' && b.type === 'slave') { return -1; } if (a.type === 'slave' && b.type === 'master') { return 1; } return 0; })); } else { return Promise.resolve([]); } }); }); } /** * Fetches a customer profile by ID. * * @param session user session * @param profileId profile ID */ getProfileById(session, profileId) { return __awaiter(this, void 0, void 0, function* () { return this.api.call({ url: `/customer/profile/${profileId}`, method: 'GET', session, headers: { Accept: 'application/hal+json', 'Content-Type': 'application/json', 'Accept-Response': 'Advanced' } }); }); } /** * Creates a customer profile. * * @param session user session * @param profileData the new profile information */ createProfile(session, profileData) { return __awaiter(this, void 0, void 0, function* () { const validationError = this.validate(profileData, [ 'customer', 'new-customer-profile' ]); if (validationError) { throw validationError; } return this.api .call({ url: '/customer/profile', method: 'POST', body: profileData, session, headers: { Accept: 'application/hal+json', 'Content-Type': 'application/json' } }) .then((profile) => { profile.email = []; profile.company = []; return profile; }); }); } /** * Updates a customer profile. * * @param session user session * @param profileId customer profile ID * @param data profile information */ updateProfile(session, profileId, data) { return __awaiter(this, void 0, void 0, function* () { const validationError = this.validate(data, [ 'customer', 'edit-customer-profile' ]); if (validationError) { throw validationError; } return this.api.call({ url: `/customer/profile/${profileId}`, method: 'PATCH', body: data, session, headers: { Accept: 'application/hal+json', 'Content-Type': 'application/json' } }); }); } /** * Deactivates a customer profile. * * @param session user session * @param profileId customer profile ID */ deactivateProfile(session, profileId) { return __awaiter(this, void 0, void 0, function* () { return this.api.call({ url: `/customer/profile/${profileId}`, method: 'DELETE', session, headers: { Accept: 'application/hal+json', 'Content-Type': 'application/json' } }); }); } /** * Creates customer profile email. * * @param session user session * @param data email address data */ createCustomerProfileEmail(session, data) { return __awaiter(this, void 0, void 0, function* () { const validationError = this.validate(data, [ 'customer', 'new-customer-profile-email' ]); if (validationError) { throw validationError; } return this.api.call({ url: '/customer/profile-email', method: 'POST', body: data, session, headers: { Accept: 'application/hal+json', 'Content-Type': 'application/json' } }); }); } /** * Updates a customer profile email. * * @param session user session * @param emailId customer email address ID * @param data information to update */ updateCustomerProfileEmail(session, emailId, data) { return __awaiter(this, void 0, void 0, function* () { const validationError = this.validate(data, [ 'customer', 'edit-customer-profile-email' ]); if (validationError) { throw validationError; } return this.api.call({ url: `/customer/profile-email/${emailId}`, method: 'PATCH', body: data, session, headers: { Accept: 'application/hal+json', 'Content-Type': 'application/json' } }); }); } /** * Removes a customer profile email address. * * @param session user session * @param emailId the customer profile email address ID */ removeCustomerProfileEmail(session, emailId) { return __awaiter(this, void 0, void 0, function* () { return this.api .call({ url: `/customer/profile-email/${emailId}`, method: 'DELETE', session, headers: { Accept: 'application/hal+json', 'Content-Type': 'application/json' } }) .then(() => true); }); } /** * Creates a customer profile company. * * @param session user session * @param data the company data */ createCustomerProfileCompany(session, data) { return __awaiter(this, void 0, void 0, function* () { const validationError = this.validate(data, [ 'customer', 'new-customer-profile-company' ]); if (validationError) { throw validationError; } return this.api.call({ url: '/customer/profile-company', method: 'POST', body: data, session, headers: { Accept: 'application/hal+json', 'Content-Type': 'application/json' } }); }); } /** * Update customer profile company * @param session * @param {number} companyId * @param {ICustomerProfileCompany} data * @returns {Promise<any>} */ updateCustomerProfileCompany(session, companyId, data) { return __awaiter(this, void 0, void 0, function* () { const validationError = this.validate(data, [ 'customer', 'edit-customer-profile-company' ]); if (validationError) { throw validationError; } return this.api.call({ url: `/customer/profile-company/${companyId}`, method: 'PATCH', body: data, session, headers: { Accept: 'application/hal+json', 'Content-Type': 'application/json' } }); }); } /** * Removes a customer profile company. * * @param session user session * @param companyId the customer profile company ID */ removeCustomerProfileCompany(session, companyId) { return __awaiter(this, void 0, void 0, function* () { return this.api .call({ url: `/customer/profile-company/${companyId}`, method: 'DELETE', session, headers: { Accept: 'application/hal+json', 'Content-Type': 'application/json' } }) .then(() => true); }); } /** * Validates company VAT. * * @param session user session */ validateVat(session, vat) { return __awaiter(this, void 0, void 0, function* () { return this.api.call({ url: `/customer/validate-vat/${encodeURIComponent(vat)}`, method: 'GET', session, headers: { Accept: 'application/hal+json', 'Content-Type': 'application/json', 'Accept-Response': 'Advanced' } }); }); } /** * Lists customer tasks which were processed by Gear. * * @param session user session * @param query query options */ getGearTasks(session, query = {}) { return __awaiter(this, void 0, void 0, function* () { const q = { page: query.page || 1, page_size: query.page_size || 25 }; if (query.sort_by) { q.order = [{ field: query.sort_by, sort: query.sort_order || 'ASC' }]; } const response = yield this.api.call({ url: `/customer_gear_task`, method: 'GET', session, query: q, headers: { Accept: 'application/hal+json', 'Content-Type': 'application/json', 'Accept-Response': 'Advanced' } }); const restResource = to_rest_resource_1.toRestResource(response, 'customer_gear_task'); return restResource; }); } /** * Get My Domains. * * @author Anton Katsarov * @param {Object} session */ getMyDomains(session) { const promises = []; let cart; promises.push(this.api .call({ url: '/billing/cart', method: 'GET', session, headers: { Accept: 'application/hal+json', 'Content-Type': 'application/json', 'Accept-Response': 'Advanced' } }) .then((data) => { cart = data._embedded.cart; const categories = Array.from(cart.reduce((acc, item) => { acc.add(item.product.category_id); return acc; }, new Set())); if (categories.length) { const filter = qs.stringify({ where: [ { field: 'id', where: 'and', type: 'in', values: categories } ] }); return this.api.call({ url: '/billing/category?page_size=-1&' + filter, method: 'GET', headers: { Accept: 'application/hal+json', 'Content-Type': 'application/json', 'Accept-Response': 'Advanced' } }); } else { return Promise.resolve([]); } }) .then((categories) => { if (categories && categories._embedded) { const categoryLayouts = categories._embedded.catalog_category.reduce((acc, category) => { acc[category.id] = category.layout; return acc; }, {}); return Promise.resolve(cart .filter((item) => { const relatedProductsInCartCount = cart.filter((i) => i.descriptor === item.descriptor && i.product.product_id !== item.product.product_id).length; return (categoryLayouts[item.product.category_id] === 'domain' && relatedProductsInCartCount === 0); }) .map((item) => item.descriptor)); } else { return Promise.resolve([]); } })); let products; promises.push(this.api .call({ url: '/customer/product', method: 'GET', session, headers: { Accept: 'application/hal+json', 'Content-Type': 'application/json', 'Accept-Response': 'Advanced' } }) .then((result) => { products = result._embedded.customer_product; const ids = Array.from(products.reduce((acc, product) => { acc.add(product.product.category_id); return acc; }, new Set())); if (ids.length) { const filter = qs.stringify({ where: [ { field: 'id', where: 'and', type: 'in', values: ids } ] }); return this.api.call({ url: '/billing/category?page_size=-1&' + filter, method: 'GET', headers: { Accept: 'application/hal+json', 'Content-Type': 'application/json', 'Accept-Response': 'Advanced' } }); } else { return Promise.resolve(null); } }) .then((categories) => { if (categories) { const categoryLayouts = categories._embedded.catalog_category.reduce((acc, category) => { acc[category.id] = category.layout; return acc; }, {}); return Promise.resolve(products .filter((item) => categoryLayouts[item.product.category_id] === 'domain') .map((item) => item.descriptor)); } else { return Promise.resolve(null); } })); return Promise.all(promises).then((results) => { return Promise.resolve({ cart: results[0], products: results[1] }); }); } } exports.BilliaSDKCustomer = BilliaSDKCustomer;