From d8e9fc5c25ed542a317fafcba286d04d6f38b8b6 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jo=C3=A3o=20Conde?= <16060539+joao-conde@users.noreply.github.com> Date: Fri, 13 Oct 2023 15:52:07 +0100 Subject: [PATCH] feat: create waybill and invoice for shpments --- CHANGELOG.md | 2 +- src/js/api/shipment.js | 72 ++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 73 insertions(+), 1 deletion(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index ae8c54ec..cc98f237 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -9,7 +9,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0 ### Added -* +* Added waybill and invoice creation methods to the `ShipmentAPI` - [#4853](https://github.com/ripe-tech/ripe-core/issues/4853) ### Changed diff --git a/src/js/api/shipment.js b/src/js/api/shipment.js index d657b117..995bf437 100644 --- a/src/js/api/shipment.js +++ b/src/js/api/shipment.js @@ -307,3 +307,75 @@ ripe.Ripe.prototype.deleteShipmentP = function(number, options) { }); }); }; + +/** + * Creates a shipping waybill for the shipment with the provided number. + * + * @param {Number} number The number of the shipment to create the waybill for. + * @param {Object} options An object of options to configure the request. + * @param {Function} callback Function with the result of the request. + * @returns {XMLHttpRequest} The XMLHttpRequest instance of the API request. + */ +ripe.Ripe.prototype.createWaybillShipment = function(number, options, callback) { + callback = typeof options === "function" ? options : callback; + options = typeof options === "function" || options === undefined ? {} : options; + const url = `${this.url}shipments/${number}/waybill`; + options = Object.assign(options, { + url: url, + method: "POST", + auth: true + }); + options = this._build(options); + return this._cacheURL(options.url, options, callback); +}; + +/** + * Creates a shipping waybill for the shipment with the provided number. + * + * @param {Number} number The number of the shipment to create the waybill for. + * @param {Object} options An object of options to configure the request. + * @returns {Promise} The contents of the note instance that was created. + */ +ripe.Ripe.prototype.createWaybillShipmentP = function(number, options) { + return new Promise((resolve, reject) => { + this.createWaybillShipment(number, options, (result, isValid, request) => { + isValid ? resolve(result) : reject(new ripe.RemoteError(request, null, result)); + }); + }); +}; + +/** + * Creates an invoice for the shipment with the provided number. + * + * @param {Number} number The number of the shipment to create the invoice for. + * @param {Object} options An object of options to configure the request. + * @param {Function} callback Function with the result of the request. + * @returns {XMLHttpRequest} The XMLHttpRequest instance of the API request. + */ +ripe.Ripe.prototype.createInvoiceShipment = function(number, options, callback) { + callback = typeof options === "function" ? options : callback; + options = typeof options === "function" || options === undefined ? {} : options; + const url = `${this.url}shipments/${number}/invoice`; + options = Object.assign(options, { + url: url, + method: "POST", + auth: true + }); + options = this._build(options); + return this._cacheURL(options.url, options, callback); +}; + +/** + * Creates an invoice for the shipment with the provided number. + * + * @param {Number} number The number of the shipment to create the invoice for. + * @param {Object} options An object of options to configure the request. + * @returns {Promise} The contents of the note instance that was created. + */ +ripe.Ripe.prototype.createInvoiceShipmentP = function(number, options) { + return new Promise((resolve, reject) => { + this.createInvoiceShipment(number, options, (result, isValid, request) => { + isValid ? resolve(result) : reject(new ripe.RemoteError(request, null, result)); + }); + }); +};