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

refactor: enable passing multiple merchant ids #992

Merged
Merged
Show file tree
Hide file tree
Changes from 2 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
15 changes: 10 additions & 5 deletions src/library/zoid/message/validation.js
Original file line number Diff line number Diff line change
Expand Up @@ -68,15 +68,20 @@ export default {
},
merchantId: ({ props: { merchantId } }) => {
if (typeof merchantId !== 'undefined') {
let invalidId;
if (!validateType(Types.STRING, merchantId)) {
logInvalidType('merchantId', Types.STRING, merchantId);
} else if (merchantId.length !== 13 && merchantId.length !== 10) {
logInvalid('merchantId', 'Ensure the correct Merchant ID has been entered.');
} else {
return merchantId;
invalidId = merchantId;
}
const ids = merchantId.toString().split(',');
ids.forEach(id => {
if (id.length !== 13 && id.length !== 10) {
logInvalid('merchantId', 'Ensure the correct Merchant ID has been entered.');
invalidId = id;
}
});
return invalidId ? undefined : merchantId;
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Consider the following which contains a few modifications:

  1. Tightens the scope where invalidId (renamed to isInvalid since we're using it as a boolean) is used to reduce mental overhead
  2. The forEach will potentially log multiple times with the same message if there are multiple bad IDs but we only need it to log once. We can make use of Array.prototype.some to short circuit after a single invalid match. It will return a boolean value.
  3. More closely follows the pattern of other validators by returning the value in an else block after all checks have passed.
            if (!validateType(Types.STRING, merchantId)) {
                logInvalidType('merchantId', Types.STRING, merchantId);
            } else {
                const isInvalid = merchantId.split(',').some(id => id.length !== 13 && id.length !== 10);
                
                if (isInvalid) {
                    logInvalid('merchantId', 'Ensure the correct Merchant ID has been entered.');
                } else {
                    return merchantId;
                }
            }

}

return undefined;
},
customerId: ({ props: { customerId } }) => {
Expand Down
2 changes: 1 addition & 1 deletion src/utils/sdk.js
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@ export function getMerchantConfig() {
export function getAccount() {
if (__MESSAGES__.__TARGET__ === 'SDK') {
// TODO: Should we pass both up if they exist so that nodeweb can create a partner context?
return getMerchantID()[0] || `client-id:${getClientID()}`;
return getMerchantID().join(',') || `client-id:${getClientID()}`;
} else {
return undefined;
}
Expand Down
Loading