Skip to content

Commit

Permalink
better error handling
Browse files Browse the repository at this point in the history
  • Loading branch information
lelemm committed Jan 2, 2025
1 parent 263014a commit fa957ee
Show file tree
Hide file tree
Showing 2 changed files with 163 additions and 141 deletions.
301 changes: 160 additions & 141 deletions src/app-pluggyai/app-pluggyai.js
Original file line number Diff line number Diff line change
Expand Up @@ -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,
},
});
}),
);

Expand All @@ -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;
}),
);
Expand Down
3 changes: 3 additions & 0 deletions src/app-pluggyai/pluggyai-service.js
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down Expand Up @@ -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;
}
}
},
Expand Down

0 comments on commit fa957ee

Please sign in to comment.