diff --git a/src/app-pluggyai/app-pluggyai.js b/src/app-pluggyai/app-pluggyai.js index e4e441a34..00e02e42f 100644 --- a/src/app-pluggyai/app-pluggyai.js +++ b/src/app-pluggyai/app-pluggyai.js @@ -27,25 +27,34 @@ app.post( app.post( '/accounts', handleError(async (req, res) => { - await pluggyaiService.setToken(); - const itemIds = secretsService - .get(SecretName.pluggyai_itemIds) - .split(',') - .map((item) => item.trim()); - - let accounts = []; + try { + await pluggyaiService.setToken(); + const itemIds = secretsService + .get(SecretName.pluggyai_itemIds) + .split(',') + .map((item) => item.trim()); + + let accounts = []; + + for (const item of itemIds) { + const partial = await pluggyaiService.getAccountsByItemId(item); + accounts = accounts.concat(partial.results); + } - for (const item of itemIds) { - const partial = await pluggyaiService.getAccountsByItemId(item); - accounts = accounts.concat(partial.results); + res.send({ + status: 'ok', + data: { + accounts: accounts, + }, + }); + } catch (error) { + res.send({ + status: 'ok', + data: { + error: error.message, + }, + }); } - - res.send({ - status: 'ok', - data: { - accounts: accounts, - }, - }); }), ); @@ -54,143 +63,153 @@ app.post( handleError(async (req, res) => { const { accountId, startDate, endDate } = req.body; - let transactions = []; - await pluggyaiService.setToken(); - let result = await pluggyaiService.getTransactionsByAccountId( - accountId, - startDate, - endDate, - 500, - 1, - ); - transactions = transactions.concat(result.results); - const totalPages = result.totalPages; - while (result.page != totalPages) { - result = await pluggyaiService.getTransactionsByAccountId( + try { + let transactions = []; + await pluggyaiService.setToken(); + let result = await pluggyaiService.getTransactionsByAccountId( accountId, startDate, endDate, 500, - result.page + 1, + 1, ); transactions = transactions.concat(result.results); - } - - const account = await pluggyaiService.getAccountById(accountId); - - let startingBalance = parseInt( - Math.trunc(account.balance * 100).toString(), - ); - if (account.type === 'CREDIT') { - startingBalance = -startingBalance; - } - const date = getDate(new Date(account.updatedAt)); - - const balances = [ - { - balanceAmount: { - amount: startingBalance, - currency: account.currencyCode, - }, - balanceType: 'expected', - referenceDate: date, - }, - { - balanceAmount: { - amount: startingBalance, - currency: account.currencyCode, - }, - balanceType: 'interimAvailable', - referenceDate: date, - }, - ]; - - const all = []; - const booked = []; - const pending = []; - - for (const trans of transactions) { - const newTrans = {}; - - let dateToUse = 0; - - if (trans.status === 'PENDING') { - newTrans.booked = false; - } else { - newTrans.booked = true; + const totalPages = result.totalPages; + while (result.page != totalPages) { + result = await pluggyaiService.getTransactionsByAccountId( + accountId, + startDate, + endDate, + 500, + result.page + 1, + ); + transactions = transactions.concat(result.results); } - dateToUse = trans.date; - const transactionDate = new Date(dateToUse); + const account = await pluggyaiService.getAccountById(accountId); - if (transactionDate < startDate) { - continue; + let startingBalance = parseInt( + Math.trunc(account.balance * 100).toString(), + ); + if (account.type === 'CREDIT') { + startingBalance = -startingBalance; } - - newTrans.date = getDate(transactionDate); - - newTrans.payeeName = ''; - if ( - trans.merchant && - (trans.merchant.name || trans.merchant.businessName) - ) { - newTrans.payeeName = trans.merchant.name || trans.merchant.businessName; - } else if ( - trans.type === 'DEBIT' && - trans.paymentData && - trans.paymentData.receiver && - trans.paymentData.receiver.name - ) { - newTrans.payeeName = trans.paymentData.receiver.name; - } else if ( - trans.type === 'CREDIT' && - trans.paymentData && - trans.paymentData.payer && - trans.paymentData.payer.name - ) { - newTrans.payeeName = trans.paymentData.payer.name; - } else if ( - trans.type === 'DEBIT' && - trans.paymentData && - trans.paymentData.receiver && - trans.paymentData.receiver.documentNumber && - trans.paymentData.receiver.documentNumber.value - ) { - newTrans.payeeName = trans.paymentData.receiver.documentNumber.value; - } else if ( - trans.type === 'CREDIT' && - trans.paymentData && - trans.paymentData.payer && - trans.paymentData.payer.documentNumber && - trans.paymentData.payer.documentNumber.value - ) { - newTrans.payeeName = trans.paymentData.receiver.documentNumber.value; + const date = getDate(new Date(account.updatedAt)); + + const balances = [ + { + balanceAmount: { + amount: startingBalance, + currency: account.currencyCode, + }, + balanceType: 'expected', + referenceDate: date, + }, + { + balanceAmount: { + amount: startingBalance, + currency: account.currencyCode, + }, + balanceType: 'interimAvailable', + referenceDate: date, + }, + ]; + + const all = []; + const booked = []; + const pending = []; + + for (const trans of transactions) { + const newTrans = {}; + + let dateToUse = 0; + + if (trans.status === 'PENDING') { + newTrans.booked = false; + } else { + newTrans.booked = true; + } + dateToUse = trans.date; + + const transactionDate = new Date(dateToUse); + + if (transactionDate < startDate) { + continue; + } + + newTrans.date = getDate(transactionDate); + + newTrans.payeeName = ''; + if ( + trans.merchant && + (trans.merchant.name || trans.merchant.businessName) + ) { + newTrans.payeeName = + trans.merchant.name || trans.merchant.businessName; + } else if ( + trans.type === 'DEBIT' && + trans.paymentData && + trans.paymentData.receiver && + trans.paymentData.receiver.name + ) { + newTrans.payeeName = trans.paymentData.receiver.name; + } else if ( + trans.type === 'CREDIT' && + trans.paymentData && + trans.paymentData.payer && + trans.paymentData.payer.name + ) { + newTrans.payeeName = trans.paymentData.payer.name; + } else if ( + trans.type === 'DEBIT' && + trans.paymentData && + trans.paymentData.receiver && + trans.paymentData.receiver.documentNumber && + trans.paymentData.receiver.documentNumber.value + ) { + newTrans.payeeName = trans.paymentData.receiver.documentNumber.value; + } else if ( + trans.type === 'CREDIT' && + trans.paymentData && + trans.paymentData.payer && + trans.paymentData.payer.documentNumber && + trans.paymentData.payer.documentNumber.value + ) { + newTrans.payeeName = trans.paymentData.receiver.documentNumber.value; + } + + newTrans.remittanceInformationUnstructured = trans.descriptionRaw; + newTrans.transactionAmount = { + amount: account.type === 'BANK' ? trans.amount : -trans.amount, + currency: trans.currencyCode, + }; + newTrans.transactionId = trans.id; + newTrans.valueDate = newTrans.bookingDate; //always undefined? + + if (newTrans.booked) { + booked.push(newTrans); + } else { + pending.push(newTrans); + } + all.push(newTrans); } - newTrans.remittanceInformationUnstructured = trans.descriptionRaw; - newTrans.transactionAmount = { - amount: account.type === 'BANK' ? trans.amount : -trans.amount, - currency: trans.currencyCode, - }; - newTrans.transactionId = trans.id; - newTrans.valueDate = newTrans.bookingDate; //always undefined? - - if (newTrans.booked) { - booked.push(newTrans); - } else { - pending.push(newTrans); - } - all.push(newTrans); + res.send({ + status: 'ok', + data: { + balances, + startingBalance, + transactions: { all, booked, pending }, + }, + }); + } catch (error) { + res.send({ + status: 'ok', + data: { + error: error.message, + }, + }); } - - res.send({ - status: 'ok', - data: { - balances, - startingBalance, - transactions: { all, booked, pending }, - }, - }); return; }), ); diff --git a/src/app-pluggyai/pluggyai-service.js b/src/app-pluggyai/pluggyai-service.js index 1d59bc289..b453ceb36 100644 --- a/src/app-pluggyai/pluggyai-service.js +++ b/src/app-pluggyai/pluggyai-service.js @@ -51,6 +51,8 @@ export const pluggyaiService = { res.on('end', () => { if (res.statusCode === 403) { reject(new Error('Forbidden')); + } else if (res.statusCode === 401) { + reject(new Error('Unauthorized')); } else { try { const results = JSON.parse(data); @@ -98,6 +100,7 @@ export const pluggyaiService = { secretsService.set(SecretName.pluggyai_apiKey, pluggyApiKey); } catch (error) { console.error(`Error getting apiKey for Pluggy.ai account: ${error}`); + throw error; } } },