Skip to content

Commit

Permalink
feat: Account for new UPS endpoint in carrier account create/update f…
Browse files Browse the repository at this point in the history
…unctions (#455)

- Add init-examples-submodule Make step
- Account for new UPS endpoint, payload schema when creating/updating carrier account
  • Loading branch information
nwithan8 authored Jul 12, 2024
1 parent 3cb195f commit 5fb49ca
Show file tree
Hide file tree
Showing 10 changed files with 1,377 additions and 100 deletions.
5 changes: 5 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,10 @@
# CHANGELOG

## Next Release

- Routes `UpsAccount`, `UpsMailInnovationsAccount`, and `UpsSurepostAccount` create/update requests to the new `/ups_oauth_registrations` endpoint
- Starting `2024-08-05`, UPS accounts will require a new payload to register or update. See [UPS OAuth 2.0 Update](https://support.easypost.com/hc/en-us/articles/26635027512717-UPS-OAuth-2-0-Update) for more details

## v7.3.1 (2024-06-21)

- Removes the unusable CarbonOffset types
Expand Down
7 changes: 6 additions & 1 deletion Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -26,12 +26,17 @@ eslint:
eslint-fix:
npm run lintFix

## init-examples-submodule - Initialize the examples submodule
init-examples-submodule:
git submodule init
git submodule update

## install-styleguide - Install the styleguide (Unix only)
install-styleguide: | update-examples-submodule
sh examples/symlink_directory_files.sh examples/style_guides/node .

## install - Install project dependencies (Unix only)
install: | update-examples-submodule
install: | init-examples-submodule
npm install

## lint - Lint the project
Expand Down
7 changes: 5 additions & 2 deletions src/constants.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,11 @@ import Utils from './utils/util';
* A class containing constants used throughout the EasyPost Node.js client library.
*/
export default class Constants {
static get CARRIER_ACCOUNTS_WITH_CUSTOM_WORKFLOWS() {
return ['FedexAccount', 'FedexSmartpostAccount', 'UpsAccount'];
static get CARRIER_ACCOUNTS_WITH_CUSTOM_CREATE_WORKFLOWS() {
return ['FedexAccount', 'FedexSmartpostAccount'];
}
static get UPS_OAUTH_CARRIER_TYPES() {
return ['UpsAccount', 'UpsMailInnovationsAccount', 'UpsSurepostAccount'];
}
static EXTERNAL_API_CALL_FAILED = 'Communication with %s failed, please try again later';
static INVALID_API_KEY_TYPE = 'Invalid API key type.';
Expand Down
48 changes: 40 additions & 8 deletions src/services/carrier_account_service.js
Original file line number Diff line number Diff line change
Expand Up @@ -26,8 +26,7 @@ export default (easypostClient) =>
}

const endpoint = this._selectCarrierAccountCreationEndpoint(carrierAccountType);

const wrappedParams = { carrier_account: params };
const wrappedParams = this._wrapCarrierAccountParams(carrierAccountType, params);

return this._create(endpoint, wrappedParams);
}
Expand All @@ -40,13 +39,15 @@ export default (easypostClient) =>
* @returns {CarrierAccount} - The updated carrier account.
*/
static async update(id, params) {
const url = `carrier_accounts/${id}`;
const wrappedParams = {
carrier_account: params,
};
const carrierAccount = await this.retrieve(id);

const carrierAccountType = carrierAccount.type;

const endpoint = this._selectCarrierAccountUpdateEndpoint(carrierAccountType, id);
const wrappedParams = this._wrapCarrierAccountParams(carrierAccountType, params);

try {
const response = await easypostClient._patch(url, wrappedParams);
const response = await easypostClient._patch(endpoint, wrappedParams);

return this._convertToEasyPostObject(response.body, wrappedParams);
} catch (e) {
Expand Down Expand Up @@ -79,12 +80,43 @@ export default (easypostClient) =>
* @returns {string} - The endpoint to be used for the carrier account creation request.
*/
static _selectCarrierAccountCreationEndpoint(carrierAccountType) {
if (Constants.CARRIER_ACCOUNTS_WITH_CUSTOM_WORKFLOWS.includes(carrierAccountType)) {
if (Constants.CARRIER_ACCOUNTS_WITH_CUSTOM_CREATE_WORKFLOWS.includes(carrierAccountType)) {
return 'carrier_accounts/register';
}
if (Constants.UPS_OAUTH_CARRIER_TYPES.includes(carrierAccountType)) {
return 'ups_oauth_registrations';
}
return 'carrier_accounts';
}

/**
* Returns the correct carrier_account endpoint when updating a record based on the type.
* @private
* @param {string} carrierAccountType - The type of carrier account to be updated.
* @param {string} carrierAccountId - The ID of the carrier account to be updated.
* @returns {string} - The endpoint to be used for the carrier account update request.
*/
static _selectCarrierAccountUpdateEndpoint(carrierAccountType, carrierAccountId) {
if (Constants.UPS_OAUTH_CARRIER_TYPES.includes(carrierAccountType)) {
return `ups_oauth_registrations/${carrierAccountId}`;
}
return `carrier_accounts/${carrierAccountId}`;
}

/**
* Wraps the carrier account parameters in the correct format based on the type.
* @private
* @param {string} carrierAccountType - The type of carrier account to be created.
* @param {Object} params - The parameters for the carrier account to be created.
* @returns {Object} - The wrapped carrier account parameters.
*/
static _wrapCarrierAccountParams(carrierAccountType, params) {
if (Constants.UPS_OAUTH_CARRIER_TYPES.includes(carrierAccountType)) {
return { ups_oauth_registrations: params };
}
return { carrier_account: params };
}

/**
* Retrieve all {@link CarrierAccount carrier accounts} associated with the current authenticated user.
* See {@link https://www.easypost.com/docs/api/node#list-all-carrier-accounts EasyPost API Documentation} for more information.
Expand Down
Loading

0 comments on commit 5fb49ca

Please sign in to comment.