Skip to content

Commit

Permalink
Feat/assert name (#4)
Browse files Browse the repository at this point in the history
* add: assert_name() method
* Bump 1.8.0
* add test
  • Loading branch information
bj0rge authored Jun 25, 2019
1 parent da91269 commit f6b199c
Show file tree
Hide file tree
Showing 3 changed files with 54 additions and 4 deletions.
40 changes: 38 additions & 2 deletions lib/__tests__/sepa.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -30,15 +30,15 @@ describe('SEPA lib', () => {
// If set to true, banks will group all payements with the same supplier and date
// We do not want that
paymentInfo.batchBooking = false;

paymentInfo.debtorIBAN = 'FR6130002056460000061183D58';
paymentInfo.debtorBIC = 'CRLYFRPPCRL';
paymentInfo.debtorName = 'AMAZON';
paymentInfo.debtorId = 'DE98ZZZ09999999999';

const tx = paymentInfo.createTransaction();
tx.mandateSignatureDate = new Date('2014-02-01');

tx.creditorName = 'AMAZON';
tx.creditorIBAN = 'FR6130002056460000061183D58';
tx.creditorBIC = 'CRLYFRPPCRL';
Expand Down Expand Up @@ -67,4 +67,40 @@ describe('SEPA lib', () => {
// Correctly build the initiatorName
expect(xmlString).toMatch(/<Id><OrgId><Othr><Id>1557856M<\/Id><Issr>CBI<\/Issr><\/Othr><\/OrgId><\/Id>/);
});

it('should reject when creditor name is empty', () => {
const doc = new Sepa.Document(SEPA_TYPE);

doc.grpHdr.id = 'ID_1';
doc.grpHdr.created = new Date();
doc.grpHdr.initiatorName = 'SPENDESK';

const paymentInfo = doc.createPaymentInfo();
paymentInfo.requestedExecutionDate = new Date();
// If set to true, banks will group all payements with the same supplier and date
// We do not want that
paymentInfo.batchBooking = false;

paymentInfo.debtorIBAN = 'FR6130002056460000061183D58';
paymentInfo.debtorBIC = 'CRLYFRPPCRL';
paymentInfo.debtorName = 'AMAZON';
paymentInfo.debtorId = 'DE98ZZZ09999999999';

const tx = paymentInfo.createTransaction();
tx.mandateSignatureDate = new Date('2014-02-01');

tx.creditorName = '';
tx.creditorIBAN = 'FR6130002056460000061183D58';
tx.creditorBIC = 'CRLYFRPPCRL';
tx.amount = 30;
tx.currency = 'EUR';
tx.end2endId = '123';
paymentInfo.addTransaction(tx);

doc.addPaymentInfo(paymentInfo);


const docToString = () => doc.toString();
expect(docToString).toThrow('creditor has invalid name: ""');
});
});
16 changes: 15 additions & 1 deletion lib/sepa.js
Original file line number Diff line number Diff line change
Expand Up @@ -201,7 +201,7 @@
/**
* Used by italian bank and mandatory for them.
*/
cucNumber: '',
cucNumber: '',
controlSum: 0,
batchBooking: false,
grouping: 'MIXD',
Expand Down Expand Up @@ -778,6 +778,13 @@
message = 'debitor country is too long.';
}

try {
assert_name(name, pullFrom);
} catch (error) {
message = pullFrom + ' has invalid name: "' + name + '"';
invalidFields.push(pullFrom + 'Name');
}

try {
assert_iban(iban, pullFrom + 'IBAN');
assert_fixed(bic.length, [0, 8, 11], pullFrom + 'BIC');
Expand Down Expand Up @@ -830,6 +837,13 @@
}
}

/** assert that |str| is not empty */
function assert_name(str, member) {
if (!str || !str.trim().length) {
throw new Error(member + ' has invalid name: "' + str + '"');
}
}

/** assert that |str| is a creditor id */
function assert_cid(str, member) {
if (!validateCreditorID(str)) {
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
"description": "Create SEPA XML for business transactions, fork of https://github.com/kewisch/sepa.js",
"license": "MPL-2.0",
"main": "lib/sepa.js",
"version": "1.7.0",
"version": "1.8.0",
"author": "Spendesk",
"repository": {
"url": "https://github.com/Spendesk/sepa.js"
Expand Down

0 comments on commit f6b199c

Please sign in to comment.