UNPKG

internetmarke

Version:

A node implementation to use the Internetmarke web service of Deutsche Post.

188 lines (187 loc) 8.26 kB
"use strict"; var __decorate = (this && this.__decorate) || function (decorators, target, key, desc) { var c = arguments.length, r = c < 3 ? target : desc === null ? desc = Object.getOwnPropertyDescriptor(target, key) : desc, d; if (typeof Reflect === "object" && typeof Reflect.decorate === "function") r = Reflect.decorate(decorators, target, key, desc); else for (var i = decorators.length - 1; i >= 0; i--) if (d = decorators[i]) r = (c < 3 ? d(r) : c > 3 ? d(target, key, r) : d(target, key)) || r; return c > 3 && r && Object.defineProperty(target, key, r), r; }; var __param = (this && this.__param) || function (paramIndex, decorator) { return function (target, key) { decorator(target, key, paramIndex); } }; var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) { function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); } 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) : adopt(result.value).then(fulfilled, rejected); } step((generator = generator.apply(thisArg, _arguments || [])).next()); }); }; Object.defineProperty(exports, "__esModule", { value: true }); exports.ProductService = exports.WSDL = void 0; const inversify_1 = require("inversify"); const soap_1 = require("soap"); const types_1 = require("../di/types"); const Soap_1 = require("../services/Soap"); const catalog_1 = require("./catalog"); const date_1 = require("./date"); const Error_1 = require("./Error"); const product_1 = require("./product"); exports.WSDL = 'https://prodws.deutschepost.de:8443/ProdWSProvider_1_1/prodws?wsdl'; /** * The implementation of the ProdWS service that handles product information */ let ProductService = class ProductService extends Soap_1.SoapService { constructor(client, catalogStore, productStore, getLogger, getSoapClient) { super(getSoapClient); this.client = client; this.catalogStore = catalogStore; this.productStore = productStore; this.wsdl = exports.WSDL; this.log = getLogger('prodws'); } /** * Initializes the product service and makes it ready to use with the * registered client account. * * @param options Product service options to manipulate the default behaviour. */ init(options) { return __awaiter(this, void 0, void 0, function* () { if (!options.client) { throw new Error_1.ClientError('Missing client credentials for ProdWS service init.'); } const ttl = undefined !== options.ttl ? options.ttl : 7 * 24 * 3600; this.client.setCredentials(options.client); yield this.checkSoapClient(); yield this.catalogStore.init('catalog-list.json', this.updateCatalogs.bind(this), ttl); yield this.productStore.init('product-list.json', this.updateProducts.bind(this), ttl); }); } /** * Retrieves the list of available catalogs from the service. */ getCatalogList() { return __awaiter(this, void 0, void 0, function* () { yield this.checkServiceInit('Cannot get catalog list before initializing product service'); return this.catalogStore.getList(); }); } /** * Retrieves the catalog with the given name if existing. * * @param id The name of the catalog that should be retrieved. */ getCatalog(name) { return __awaiter(this, void 0, void 0, function* () { yield this.checkServiceInit('Cannot get catalog before initializing product service'); return this.catalogStore.getItem(name); }); } /** * Retrieves the list of available products from the service. * * @param date An optional date that loads the product list at the given date. */ getProductList(date) { return __awaiter(this, void 0, void 0, function* () { yield this.checkServiceInit('Cannot get product list before initializing product service'); // only use cache for the latest product data if (!date) { return this.productStore.getList(); } return this.updateProducts(date).then(products => Object.values(products)); }); } /** * Retrieves the product with the given id if existing. * * @param id The id of the product that should be retrieved. */ getProduct(id) { return __awaiter(this, void 0, void 0, function* () { yield this.checkServiceInit('Cannot get product before initializing product service'); return this.productStore.getItem(id); }); } initSoapClient() { const options = { hasTimeStamp: false, hasTokenCreated: false }; const security = new soap_1.WSSecurity(this.client.getUsername(), this.client.getPassword(), options); this.soapClient.setSecurity(security); } updateCatalogs() { return __awaiter(this, void 0, void 0, function* () { const payload = { mandantID: this.client.getId() }; // pay.load.subMandatId payload.catalogProperties = true; return this.soapClient .getCatalogListAsync(payload) .then(([response]) => { if (!response || 'true' !== response.attributes.success) { return {}; } const catalogs = {}; response.Response.catalogList.catalog.forEach((data) => { const catalog = (0, catalog_1.parseCatalog)(data); if (catalog) { catalogs[catalog.id] = catalog; } }); return catalogs; }) .catch((e) => { var _a; this.log('getCatalogList', ((_a = e.root) === null || _a === void 0 ? void 0 : _a.Envelope.Body.Fault) || e); // throw new SoapError(e.root.Envelope.Body.Fault.faultstring); }); }); } updateProducts(date) { return __awaiter(this, void 0, void 0, function* () { const payload = { mandantID: this.client.getId() }; if (date) { payload.timestamp = { attributes: (0, date_1.formatDate)(date) }; } payload.dedicatedProducts = true; payload.responseMode = 0; return this.soapClient .getProductListAsync(payload) .then(([response]) => { if (!response || 'true' !== response.attributes.success) { return {}; } const products = {}; response.Response.salesProductList.SalesProduct.forEach((data) => { const product = (0, product_1.parseSalesProduct)(data); if (product) { products[product.id] = product; } }); return products; }) .catch((e) => { var _a; this.log('getProductList', ((_a = e.root) === null || _a === void 0 ? void 0 : _a.Envelope.Body.Fault) || e); // throw new SoapError(e.root.Envelope.Body.Fault.faultstring); }); }); } }; ProductService = __decorate([ (0, inversify_1.injectable)(), __param(0, (0, inversify_1.inject)(types_1.TYPES.Client)), __param(1, (0, inversify_1.inject)(types_1.TYPES.CatalogStore)), __param(2, (0, inversify_1.inject)(types_1.TYPES.ProductStore)), __param(3, (0, inversify_1.inject)(types_1.TYPES.LoggerFactory)), __param(4, (0, inversify_1.inject)(types_1.TYPES.SoapClientFactory)) ], ProductService); exports.ProductService = ProductService;