Skip to content

Commit

Permalink
Merge pull request #140 from mojaloop/error-handling-remove-logging
Browse files Browse the repository at this point in the history
Added some error handling and removed some unnecessary logging.
  • Loading branch information
aaronreynoza authored Jan 25, 2021
2 parents 4592626 + 9aad8c0 commit da6c76c
Show file tree
Hide file tree
Showing 17 changed files with 90 additions and 30 deletions.
45 changes: 38 additions & 7 deletions typology-11/src/rules/co-located-parties.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,13 +8,17 @@ const getPayeesFromILPsArray = async (outgoingTransfersClient: RedisClient, ILPS
}))
);

return accountsTransfers.reduce((payeesAcc: any, payee: any) => {
if (accountsTransfers == undefined || accountsTransfers.length == 0) return undefined;

const payeeNames = accountsTransfers.reduce((payeesAcc: any, payee: any) => {
if (payee === null) return payeesAcc;
const payeeTransfers = JSON.parse(payee);
if (payeeTransfers.length < 1) return payeesAcc;
const payeeNames = payeeTransfers.map((transfer: any) => transfer.ILPDestinationAccountAddress);
return [...payeesAcc, ...payeeNames];
}, []);

return { payeeNames, accountsTransfers };
}

const removeDuplicates = (names: any) => names.filter((c: any, index: number) => {
Expand All @@ -25,30 +29,57 @@ const removeValuesFromSecondArray = (firstArr: any, names: any) => names.filter(
return firstArr.indexOf(c) === index;
});

const deg2rad = (num: number): number => (num * Math.PI) / 180;

const distance = (location1: number[], location2: number[], useMiles = false): number => {
const coreRadius = 6371; // km
const dLat: number = deg2rad(location2[0] - location1[0]);
const dLon: number = deg2rad(location2[1] - location1[1]);
const a: number = Math.sin(dLat / 2)
* Math.sin(dLat / 2)
+ Math.cos(deg2rad(location1[0]))
* Math.cos(deg2rad(location2[0]))
* Math.sin(dLon / 2)
* Math.sin(dLon / 2);
const c: number = 2 * Math.atan2(Math.sqrt(a), Math.sqrt(1 - a));
let d: number = Math.round(coreRadius * c);
if (useMiles) { // returns miles instead of kilometers
d = Math.round(d * 0.621371192);
}
return d;
};

const handleCoLocatedParties = async (transfer: any,
outgoingTransfersClient: RedisClient,
historicalData: any) => {
if (historicalData == undefined || historicalData.length == 0) return false;

const payeesList = historicalData.map((transfer: any) => transfer.ILPDestinationAccountAddress);
payeesList.push(transfer.ILPDestinationAccountAddress);
const uniqueNames = removeDuplicates(payeesList);

// get Payees names - tier 1
const tier1Payees = await getPayeesFromILPsArray(outgoingTransfersClient, uniqueNames);
payeesList.push(tier1Payees);
uniqueNames.push(removeDuplicates(tier1Payees));
if (tier1Payees != undefined) {
payeesList.push(tier1Payees.payeeNames);
uniqueNames.push(removeDuplicates(tier1Payees.payeeNames));
}

// get Payees names - tier 2
const uniqueTier2Names = removeValuesFromSecondArray(uniqueNames, tier1Payees);
const tier2Payees = await getPayeesFromILPsArray(outgoingTransfersClient, removeDuplicates(uniqueTier2Names));
payeesList.push(tier2Payees);
uniqueNames.push(removeDuplicates(tier2Payees));
if (tier2Payees != undefined) {
payeesList.push(tier2Payees.payeeNames);
uniqueNames.push(removeDuplicates(tier2Payees.payeeNames));
}

// get Payees names - tier 3
const uniqueTier3Names = removeValuesFromSecondArray(uniqueNames, tier2Payees);
const tier3Payees = await getPayeesFromILPsArray(outgoingTransfersClient, removeDuplicates(uniqueTier3Names));
payeesList.push(tier3Payees);
uniqueNames.push(removeDuplicates(tier3Payees));
if (tier3Payees != undefined) {
payeesList.push(tier3Payees.payeeNames);
uniqueNames.push(removeDuplicates(tier3Payees.payeeNames));
}

if (payeesList.length < 8) return false;
const uniqueEntitiesPercentage = uniqueNames.length / payeesList.length * 100;
Expand Down
2 changes: 1 addition & 1 deletion typology-11/src/rules/transaction-divergence.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
const handleTransactionDivergence = (transaction: any, payeeHistoricalSendData: any) => {
if (payeeHistoricalSendData.length < 10) return false;
if (payeeHistoricalSendData == undefined || payeeHistoricalSendData.length < 10) return false;

const transactionDate = new Date(transaction.HTTPTransactionDate);
// Count transaction over the last 8 hours
Expand Down
2 changes: 1 addition & 1 deletion typology-11/src/rules/transaction-mirroring.ts
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ const handleTransactionMirroring = (
payeeHistoricalSendData: any,
payeeHistoricalReceiveData: any,
): boolean => {
if (payeeHistoricalSendData.length < 1) return false;
if (payeeHistoricalSendData == undefined || payeeHistoricalSendData.length < 1) return false;
const { HTTPTransactionDate } = transfer;
const currentTransferDate = new Date(HTTPTransactionDate);

Expand Down
23 changes: 17 additions & 6 deletions typology-11/src/rules/transactions-between-parties.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,9 @@ const getTransfersFromILPsArray = async (outgoingTransfersClient: RedisClient, I
resolve(get(outgoingTransfersClient, ILP));
}))
);

if (accountsTransfers == undefined || accountsTransfers.length == 0) return undefined;

const payees = accountsTransfers.reduce((payeesAcc: any, payee: any) => {
if (payee === null) return payeesAcc;
const payeeTransfers = JSON.parse(payee);
Expand All @@ -31,26 +34,34 @@ const handleTransactionsBetweenParties = async (
outgoingTransfersClient: RedisClient,
historicalData: any
) => {
if (historicalData == undefined || historicalData.length == 0) return false;

const payeesList = historicalData.map((transfer: any) => transfer.ILPDestinationAccountAddress);
payeesList.push(transfer.d);
const uniqueNames = removeDuplicates(payeesList);

// get Payess names - tier 1
const tier1Payees = await getTransfersFromILPsArray(outgoingTransfersClient, uniqueNames);
payeesList.push(tier1Payees.payees);
uniqueNames.push(removeDuplicates(tier1Payees.payees));
if (tier1Payees != undefined) {
payeesList.push(tier1Payees.payees);
uniqueNames.push(removeDuplicates(tier1Payees.payees));
}

// get Payess names - tier 2
const uniqueTier2Names = removeValuesFromSecondArray(uniqueNames, tier1Payees.payees);
const tier2Payees = await getTransfersFromILPsArray(outgoingTransfersClient, removeDuplicates(uniqueTier2Names));
payeesList.push(tier2Payees.payees);
uniqueNames.push(removeDuplicates(tier2Payees.payees));
if (tier2Payees != undefined) {
payeesList.push(tier2Payees.payees);
uniqueNames.push(removeDuplicates(tier2Payees.payees));
}

// get Payess names - tier 3
const uniqueTier3Names = removeValuesFromSecondArray(uniqueNames, tier2Payees.payees);
const tier3Payees = await getTransfersFromILPsArray(outgoingTransfersClient, removeDuplicates(uniqueTier3Names));
payeesList.push(tier3Payees.payees);
uniqueNames.push(removeDuplicates(tier3Payees.payees));
if (tier3Payees != undefined) {
payeesList.push(tier3Payees.payees);
uniqueNames.push(removeDuplicates(tier3Payees.payees));
}

if (payeesList.length < 8) return false;
const uniqueEntitiesPercentage = uniqueNames.length / payeesList.length * 100;
Expand Down
2 changes: 1 addition & 1 deletion typology-214/src/rules/account-dormancy.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
const handleAccountDormancy = (transfer: any, payeeHistoricalReceiveData: any, payeeHistoricalSendData: any): boolean => {
if (payeeHistoricalReceiveData.length < 1 && payeeHistoricalSendData.length < 1) return false;
if (payeeHistoricalReceiveData.length < 1 && payeeHistoricalReceiveData.length < 1) return false;
const currentTransferDate = new Date(transfer.HTTPTransactionDate);
const lastTransferReceivedDate = payeeHistoricalReceiveData.length >= 1 ? payeeHistoricalReceiveData.reduce((latestDate: any, transaction: any) => {
if (!latestDate) {
Expand Down
3 changes: 1 addition & 2 deletions typology-214/src/rules/large-transaction-payer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,7 @@ const handleLargeTransactionPayer = (
transfer: any,
historicalData: any,
): boolean => {
if (!historicalData || historicalData.length === 0)
return false;
if (historicalData == undefined || historicalData.length === 0) return false;

const tranAmt = transfer.Amount;
let amounts: number[] = [];
Expand Down
2 changes: 2 additions & 0 deletions typology-214/src/rules/newPayeeTransfer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@ const handleNewPayeeTransfer = (
message: any,
): boolean => {
const { historicalData, transfer } = message;
if (historicalData == undefined || historicalData.length < 1) return false;

const ILPCount = historicalData
.filter((transaction: any) => transfer.ILPDestinationAccountAddress !== transaction.ILPDestinationAccountAddress);
return ILPCount.length <= 0;
Expand Down
4 changes: 2 additions & 2 deletions typology-214/src/rules/transaction-divergence.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
// move to typology 11

const handleTransactionDivergence = (transaction: any, payeeHistoricalSendData: any) => {
if (payeeHistoricalSendData.length < 10) return false;

if (payeeHistoricalSendData == undefined || payeeHistoricalSendData.length < 10) return false;
const transactionDate = new Date(transaction.HTTPTransactionDate);
// Count transaction over the last 8 hours
const latestTransactions = payeeHistoricalSendData.filter((transfer: any) => {
Expand Down
3 changes: 2 additions & 1 deletion typology-214/src/rules/transaction-mirroring.ts
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,8 @@ const handleTransactionMirroring = (
payeeHistoricalSendData: any,
payeeHistoricalReceiveData: any,
): boolean => {
if (payeeHistoricalSendData.length < 1) return false;
if (payeeHistoricalSendData == undefined || payeeHistoricalSendData.length < 1) return false;

const { HTTPTransactionDate } = transfer;
const currentTransferDate = new Date(HTTPTransactionDate);

Expand Down
22 changes: 16 additions & 6 deletions typology-214/src/rules/transactions-between-parties.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,8 @@ const getPayeesFromILPsArray = async (outgoingTransfersClient: RedisClient, ILPS
}))
);

if (accountsTransfers == undefined || accountsTransfers.length == 0) return undefined;

return accountsTransfers.reduce((payeesAcc: any, payee: any) => {
if (payee === null) return payeesAcc;
const payeeTransfers = JSON.parse(payee);
Expand All @@ -33,26 +35,34 @@ const handleTransactionsBetweenParties = async (
outgoingTransfersClient: RedisClient,
historicalData: any
) => {
if (historicalData == undefined || historicalData.length < 1) return false;

const payeesList = historicalData.map((transfer: any) => transfer.ILPDestinationAccountAddress);
payeesList.push(transfer.ILPDestinationAccountAddress);
const uniqueNames = removeDuplicates(payeesList);

// get Payess names - tier 1
const tier1Payees = await getPayeesFromILPsArray(outgoingTransfersClient, uniqueNames);
payeesList.push(tier1Payees);
uniqueNames.push(removeDuplicates(tier1Payees));
if (tier1Payees != undefined && tier1Payees.length > 0) {
payeesList.push(tier1Payees);
uniqueNames.push(removeDuplicates(tier1Payees));
}

// get Payess names - tier 2
const uniqueTier2Names = removeValuesFromSecondArray(uniqueNames, tier1Payees);
const tier2Payees = await getPayeesFromILPsArray(outgoingTransfersClient, removeDuplicates(uniqueTier2Names));
payeesList.push(tier2Payees);
uniqueNames.push(removeDuplicates(tier2Payees));
if (tier2Payees != undefined && tier2Payees.length > 0) {
payeesList.push(tier2Payees);
uniqueNames.push(removeDuplicates(tier2Payees));
}

// get Payess names - tier 3
const uniqueTier3Names = removeValuesFromSecondArray(uniqueNames, tier2Payees);
const tier3Payees = await getPayeesFromILPsArray(outgoingTransfersClient, removeDuplicates(uniqueTier3Names));
payeesList.push(tier3Payees);
uniqueNames.push(removeDuplicates(tier3Payees));
if (tier3Payees != undefined && tier3Payees.length > 0) {
payeesList.push(tier3Payees);
uniqueNames.push(removeDuplicates(tier3Payees));
}

if (payeesList.length < 8) return false;
const uniqueEntitiesPercentage = uniqueNames.length / payeesList.length * 100;
Expand Down
2 changes: 2 additions & 0 deletions typology-27/src/rules/exceptionallyLargeTransfer.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
const handleExceptionallyLargeTransfer = (
message: any,
): boolean => {
if(message.historicalData == undefined || message.historicalData.length == 0) return false;

const { amount } = message.transfer.Amount;
const biggestTransactionAmount = Math.max(message.historicalData
.map((transaction: any) => transaction.Amount));
Expand Down
1 change: 1 addition & 0 deletions typology-27/src/rules/newPayeeTransfer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ const handleNewPayeeTransfer = (
message: any,
): boolean => {
const { historicalData, transfer } = message;
if(historicalData == undefined || historicalData.length == 0) return false;
const ILPCount = historicalData
.filter((transaction: any) => transfer.ILPDestinationAccountAddress !== transaction.ILPDestinationAccountAddress);
return ILPCount.length <= 0;
Expand Down
1 change: 1 addition & 0 deletions typology-27/src/rules/recentSimSwap.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ const handleSimSwap = (
const { historicalData } = message;

if (historicalData == undefined
|| historicalData.length == 0
|| historicalData[0] == undefined) {
return false;
}
Expand Down
1 change: 1 addition & 0 deletions typology-28/src/rules/benfordsLaw.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ const handleBenfordsLaw = (
historicalData: any,
): boolean => {
if (historicalData == undefined
|| historicalData.length == 0
|| historicalData[0] == undefined) {
return false;
}
Expand Down
2 changes: 1 addition & 1 deletion typology-28/src/rules/transaction-convergence.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
const handleTransactionConvergence = (transaction: any, payeeHistoricalSendData: any) => {
if (payeeHistoricalSendData.length < 10) return false;
if (payeeHistoricalSendData == undefined || payeeHistoricalSendData.length < 10) return false;

const transactionDate = new Date(transaction.HTTPTransactionDate);
// Count transaction over the last 8 hours
Expand Down
2 changes: 1 addition & 1 deletion typology-28/src/rules/transaction-divergence.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
const handleTransactionDivergence = (transaction: any, payeeHistoricalSendData: any) => {
if (payeeHistoricalSendData.length < 10) return false;
if (payeeHistoricalSendData == undefined || payeeHistoricalSendData.length < 10) return false;

const transactionDate = new Date(transaction.HTTPTransactionDate);
// Count transaction over the last 8 hours
Expand Down
3 changes: 2 additions & 1 deletion typology-28/src/rules/transaction-mirroring.ts
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,8 @@ const handleTransactionMirroring = (
payeeHistoricalSendData: any,
payeeHistoricalReceiveData: any,
): boolean => {
if (payeeHistoricalSendData.length < 1) return false;
if (payeeHistoricalSendData == undefined || payeeHistoricalSendData.length < 1) return false;
if (payeeHistoricalReceiveData == undefined || payeeHistoricalReceiveData.length < 1) return false;
const { HTTPTransactionDate } = transfer;
const currentTransferDate = new Date(HTTPTransactionDate);

Expand Down

0 comments on commit da6c76c

Please sign in to comment.