Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: shipment attachment API #524

Merged
merged 1 commit into from
Oct 17, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,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)
* Added shipment attachment methods to the `ShipmentAPI` - [#4853](https://github.com/ripe-tech/ripe-core/issues/4853)

### Changed

Expand Down
119 changes: 119 additions & 0 deletions src/js/api/shipment.js
Original file line number Diff line number Diff line change
Expand Up @@ -308,6 +308,125 @@ ripe.Ripe.prototype.deleteShipmentP = function(number, options) {
});
};

/**
* Returns all the attachments of a shipment.
*
* @param {Number} number The number of the shipment to find by.
* @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.attachmentsShipment = function(number, options, callback) {
callback = typeof options === "function" ? options : callback;
options = typeof options === "function" || options === undefined ? {} : options;
const url = `${this.url}shipments/${number}/attachments`;
options = Object.assign(options, {
url: url,
method: "GET",
auth: true
});
options = this._build(options);
return this._cacheURL(options.url, options, callback);
};

/**
* Returns all the attachments of a shipment.
*
* @param {Number} number The number of the shipment to find by.
* @param {Object} options An object of options to configure the request.
* @returns {Promise} An object containing all the attachments of a shipment.
*/
ripe.Ripe.prototype.attachmentsShipmentP = function(number, options) {
return new Promise((resolve, reject) => {
this.attachmentsShipment(number, options, (result, isValid, request) => {
isValid ? resolve(result) : reject(new ripe.RemoteError(request, null, result));
});
});
};

/**
* Adds an attachment to a shipment.
*
* @param {Number} number The number of the shipment where the attachment will be added.
* @param {Object} file The attachment file to be added to the shipment.
* @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.createAttachmentShipment = function(number, file, options, callback) {
callback = typeof options === "function" ? options : callback;
options = typeof options === "function" || options === undefined ? {} : options;
const url = `${this.url}shipments/${number}/attachments`;
const dataM = { file: file };
if (options.name !== undefined) dataM.name = options.name;
if (options.meta !== undefined) dataM.meta = JSON.stringify(options.meta);
options = Object.assign(options, {
url: url,
method: "POST",
dataM: dataM,
auth: true
});
options = this._build(options);
return this._cacheURL(options.url, options, callback);
};

/**
* Adds an attachment to a shipment.
*
* @param {Number} number The number of the shipment where the attachment will be added.
* @param {Object} file The attachment file to be added to the shipment.
* @param {Object} options An object of options to configure the request.
* @param {Function} callback Function with the result of the request.
* @returns {Promise} The attachment added to the shipment.
*/
ripe.Ripe.prototype.createAttachmentShipmentP = function(number, file, options) {
return new Promise((resolve, reject) => {
this.createAttachmentShipment(number, file, options, (result, isValid, request) => {
isValid ? resolve(result) : reject(new ripe.RemoteError(request, null, result));
});
});
};

/**
* Returns the attachment of a shipment requested by ID.
*
* @param {Number} number The number of the shipment to find by.
* @param {Number} attachmentId The ID of the attachment of the shipment to find by.
* @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.attachmentShipment = function(number, attachmentId, options, callback) {
callback = typeof options === "function" ? options : callback;
options = typeof options === "function" || options === undefined ? {} : options;
const url = `${this.url}shipments/${number}/attachments/${attachmentId}`;
options = Object.assign(options, {
url: url,
method: "GET",
auth: true,
cached: false
});
options = this._build(options);
return this._cacheURL(options.url, options, callback);
};

/**
* Returns the attachment of a shipment requested by ID.
*
* @param {Number} number The number of the shipment to find by.
* @param {Number} attachmentId The ID of the attachment of the shipment to find by.
* @param {Object} options An object of options to configure the request.
* @param {Function} callback Function with the result of the request.
* @returns {Promise} The attachment file of an shipment requested by ID.
*/
ripe.Ripe.prototype.attachmentShipmentP = function(number, attachmentId, options) {
return new Promise((resolve, reject) => {
this.attachmentShipment(number, attachmentId, options, (result, isValid, request) => {
isValid ? resolve(result) : reject(new ripe.RemoteError(request, null, result));
});
});
};

/**
* Creates a shipping waybill for the shipment with the provided number.
*
Expand Down
Loading