Skip to content

Commit

Permalink
Npm Package Update - For 99 return addenda record not getting trance …
Browse files Browse the repository at this point in the history
…number fix
  • Loading branch information
pradeepvish1213 committed Oct 26, 2023
1 parent 8800506 commit 585a10f
Show file tree
Hide file tree
Showing 6 changed files with 189 additions and 36 deletions.
4 changes: 3 additions & 1 deletion lib/file/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -118,7 +118,9 @@ File.prototype.generateBatches = function (done1) {
async.each(batch._entries, function (entry, done3) {
if (Object.keys(entry.fields).indexOf('sequenceNumberWithinBatch') === -1) entry.fields.traceNumber.value = (entry.fields.traceNumber.value ? entry.fields.traceNumber.value : self.header.immediateOrigin.value.slice(0, 8)) + utils.pad(addendaCount, 7, false, '0');
async.each(entry._addendas, function (addenda, done4) {
addenda.fields.entryDetailSequenceNumber.value = entry.fields.traceNumber.value.slice(0 - addenda.fields.entryDetailSequenceNumber.width);
let keys = Object.keys(addenda.fields);
if (keys.indexOf('traceNumber') !== -1) addenda.fields.traceNumber.value = entry.fields.traceNumber.value || ''
if (keys.indexOf('entryDetailSequenceNumber') !== -1) addenda.fields.entryDetailSequenceNumber.value = entry.fields.traceNumber.value.slice(0 - addenda.fields.entryDetailSequenceNumber.width);
done4()
})

Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{
"name": "sp-nacha",
"description": "NACHA file format is a set of instructions that, when uploaded into a bank portal, successfully initiates a batch of ACH payments. NACHA file format is the protocol for structuring those instructions in a way that successfully initiates the payments. NACHA is a highly customizable Node.js module exposing a high & low-level API for generating ACH files for use within the ACH network.",
"version": "1.1.4",
"version": "1.1.5",
"keywords": [
"Seerportal Nacha",
"Dataseers Nacha",
Expand Down
11 changes: 11 additions & 0 deletions test/ach_file/ACH121042882PEIN2023102614495272.ach
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
101 011002725 1210428822310261449A094101BERKSHIRE BANK WELLS FARGO BANK NA
5220Dataseers FF4 US 1022337788PPDAccounting 231025 1121042880000000
6230110027252234532 000010000045678654 Bank Client 551121042880000000
799R0401120175000000023102401100272011201750000000 011201750000000 011201750011121042880000000
822000000200011002720000000000000000001000001022337788 121042880000000
9000001000001000000010001100272000000000000000000100000
9999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999
9999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999
9999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999
9999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999
9999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999
16 changes: 16 additions & 0 deletions test/entry.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ let chai = require('chai')
, EntryAddendaReturn = require('../lib/entry-addenda-return')
, {GenerateAchFile} = require('./generateFile')
, Moment = require('moment')
const {GenerateReturnAchFile} = require("./generateReturnACHFile");
describe('Entry', function () {
describe('Create Entry', function () {
it('should create an entry successfully', function () {
Expand Down Expand Up @@ -120,4 +121,19 @@ describe('Entry', function () {
})
}).timeout(10000);
})

describe('Generate Return ACH File', async function () {
it('should create an Return ACH file successfully', function (done) {
GenerateReturnAchFile([], './test/ach_file').then((result) => {
expect(result.error).to.equal(false);
expect(result.message).to.equal('Successfully writing file.');
return done()
}).catch(error => {
console.log('==Return ACH=========', error)
expect(true).to.be.false;
expect(error.message).to.equal('Successfully writing file.');
return done();
})
}).timeout(10000);
})
});
34 changes: 0 additions & 34 deletions test/generateFile.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,40 +3,6 @@ const path = require('path')
const FS = require('fs')
const NachaAimPoint = require('../index')

let data = [{
"id": 1,
"immediateDestination": "325272306",
"immediateOrigin": "041215663",
"immediateDestinationName": "TONGA'S FCU",
"immediateOriginName": "SUTTON BANK",
"standardEntryClassCode": "PPD",
"referenceCode": " ",
"batchChildren": [{
"id": 1,
"companyName": "SSA TREAS 310",
"companyIdentification": "9101036669",
"serviceClassCode": "220",
"standardEntryClassCode": "PPD",
"companyEntryDescription": "NA",
"companyDescriptiveDate": Moment(NachaAimPoint.Utils.computeBusinessDay(8)).format('MMM D'),
"effectiveEntryDate": Moment('2023-08-30 05:30:00').toDate(),
"settlementDate": "2023-08-30 05:30:00",
"originatingDFI": "041215663",
"entryChildren": [{
"id": 1,
"receivingDFI": "325272306",
"DFIAccount": "1347569324910",
"amount": "697",
"idNumber": "",
"individualName": "Demo",
"discretionaryData": "00",
"transactionCode": "22",
"transactionType": "Credit",
"paymentRelatedInformation": "N1YUNNAN L SPEARS N1YUNNAN L SPEARS 34501116885"
}]
}]
}]

let dataAddenda = [{
"id": 1,
"immediateDestination": "011002725",
Expand Down
158 changes: 158 additions & 0 deletions test/generateReturnACHFile.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,158 @@
const Moment = require('moment')
const path = require('path')
const FS = require('fs')
const NachaAimPoint = require('../index')

let dataAddenda = [{
"id": 90,
"standardEntryClassCode": "PPD",
"immediateDestination": "011002725",
"immediateOrigin": "121042882",
"immediateDestinationName": "BERKSHIRE BANK",
"immediateOriginName": "WELLS FARGO BANK NA",
"referenceCode": " ",
"batchChildren": [{
"id": 90,
"companyName": "Dataseers",
"companyIdentification": "1022337788",
"serviceClassCode": "220",
"standardEntryClassCode": "PPD",
"companyDiscretionaryData": "FF4 US",
"companyEntryDescription": "Accounting",
"companyDescriptiveDate": "",
"effectiveEntryDate": Moment("2023-10-25T00:00:00.000Z").toDate(),
"originatingDFI": "121042882",
"entryChildren": [{
"id": 90,
"transactionCode": "23",
"receivingDFI": "011002725",
"DFIAccount": "2234532",
"amount": "1000",
"idNumber": "45678654",
"individualName": "Bank Client",
"discretionaryData": "55",
"transactionType": "Credit",
"addendaRecords": [{
"id": 105,
"queued_transaction_id": 90,
"addenda_type_code": "99",
"payment_related_information": "011201750000000 011201750000000 011201750011",
"ach_return_code": "R04",
"original_entry_trace_number": "011201750000000",
"date_of_death": "2023-10-24",
"created_at": "2023-10-26 10:59:05",
"modified_at": "2023-10-26 10:59:05"
}]
}]
}],
"recordCount": 1
}]


async function GenerateReturnAchFile(queuedTransaction = [], fileFullPath = './') {
return new Promise((resolve, reject) => {
let totalRunsFile = [];
queuedTransaction = queuedTransaction.length > 0 ? queuedTransaction : dataAddenda;
queuedTransaction.forEach(({batchChildren, id, ...restFieldFile}) => {
try {
let Nacha2AimPointFile = new NachaAimPoint.File(restFieldFile);
let totalBatchNumber = batchChildren.length;
let totalCreditAmount = 0
let totalDebitAmount = 0;
let successRecords = []
let errorBatchEntryRecords = []
batchChildren.forEach(({entryChildren, id, ...restField}) => {
let batch = new NachaAimPoint.Batch(restField);
entryChildren.forEach(({transactionType, addendaRecords, id, ...entry}) => {
if (transactionType === 'Credit') {
totalCreditAmount = totalCreditAmount + parseInt(entry.amount)
} else {
totalDebitAmount = totalDebitAmount + parseInt(entry.amount)
}

try {
let entryRecord = new NachaAimPoint.Entry(entry);
if (addendaRecords && addendaRecords.length > 0) {
addendaRecords.forEach(addenda => {
if (addenda.addenda_type_code === '99') {
let object = {
addendaTypeCode: addenda.addenda_type_code,
returnReasonCode: addenda.ach_return_code,
originalEntryTraceNumber: addenda.original_entry_trace_number,
dateOfDeath: Moment(addenda.date_of_death).toDate(),
originalReceivingDFI: restFieldFile.immediateDestination,
addendaInformation: addenda.payment_related_information
}
let addendaEntry = new NachaAimPoint.ReturnEntryAddenda(object);
entryRecord.addReturnAddenda(addendaEntry);
} else {
let object = {
addendaTypeCode: addenda.addenda_type_code,
paymentRelatedInformation: addenda.payment_related_information
}
let addendaEntry = new NachaAimPoint.EntryAddenda(object);
entryRecord.addAddenda(addendaEntry);
}
})
}
batch.addEntry(entryRecord);
successRecords.push({row_id: id, message: 'Success', error: false})
} catch (e) {
return reject({row_id: id, message: e.message, error: true})
}
})
try {
Nacha2AimPointFile.addBatch(batch);
} catch (e) {
return reject({row_id: id, message: e.message, error: true})
}
})
let fileName = `ACH${restFieldFile.immediateOrigin}PEIN${Moment().format('YYYYMMDDHHmmssSS')}.ach`;
if (successRecords.length > 0) {
Nacha2AimPointFile.generateFile(function (result) {
FS.writeFile(path.join(fileFullPath, fileName), result, function (error) {
if (error) {
totalRunsFile.push({
message: error.message ? error.message : error.stack, error: true
})
return reject(totalRunsFile)
}
if (errorBatchEntryRecords.length === 0) {
return resolve({
error: false,
message: 'Successfully writing file.',
fileName,
totalBatchNumber,
totalDebitAmount,
totalCreditAmount,
endTime: Moment().format('YYYY-MM-DD HH:mm:ss'),
successRecords,
errorBatchEntryRecords
})
}
});
});
}
if (errorBatchEntryRecords.length > 0) {
return resolve({
error: true,
fileName,
message: 'Queued transaction has some error',
totalBatchNumber,
totalDebitAmount,
totalCreditAmount,
endTime: Moment().format('YYYY-MM-DD HH:mm:ss'),
successRecords,
errorBatchEntryRecords
})
}
} catch (error) {
return reject({
row_id: id, message: error.message ? error.message : error.stack, error: true
})
}
})
})
}

module.exports = {GenerateReturnAchFile}

0 comments on commit 585a10f

Please sign in to comment.