From f6b199c4c95e1e23b51c9e94c2b386c1186ceeaf Mon Sep 17 00:00:00 2001 From: Bastien Jorge Date: Tue, 25 Jun 2019 12:34:08 +0200 Subject: [PATCH] Feat/assert name (#4) * add: assert_name() method * Bump 1.8.0 * add test --- lib/__tests__/sepa.test.js | 40 ++++++++++++++++++++++++++++++++++++-- lib/sepa.js | 16 ++++++++++++++- package.json | 2 +- 3 files changed, 54 insertions(+), 4 deletions(-) diff --git a/lib/__tests__/sepa.test.js b/lib/__tests__/sepa.test.js index 687e18c..00fc0bd 100644 --- a/lib/__tests__/sepa.test.js +++ b/lib/__tests__/sepa.test.js @@ -30,7 +30,7 @@ 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'; @@ -38,7 +38,7 @@ describe('SEPA lib', () => { const tx = paymentInfo.createTransaction(); tx.mandateSignatureDate = new Date('2014-02-01'); - + tx.creditorName = 'AMAZON'; tx.creditorIBAN = 'FR6130002056460000061183D58'; tx.creditorBIC = 'CRLYFRPPCRL'; @@ -67,4 +67,40 @@ describe('SEPA lib', () => { // Correctly build the initiatorName expect(xmlString).toMatch(/1557856M<\/Id>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: ""'); + }); }); diff --git a/lib/sepa.js b/lib/sepa.js index c0cd7ed..5fb7a62 100644 --- a/lib/sepa.js +++ b/lib/sepa.js @@ -201,7 +201,7 @@ /** * Used by italian bank and mandatory for them. */ - cucNumber: '', + cucNumber: '', controlSum: 0, batchBooking: false, grouping: 'MIXD', @@ -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'); @@ -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)) { diff --git a/package.json b/package.json index 665237e..48ce41b 100644 --- a/package.json +++ b/package.json @@ -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"