Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix borrowers update debt #1095

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
38 changes: 23 additions & 15 deletions services/microcredit/src/borrowers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ export async function updateBorrowers(): Promise<void> {
return subgraphMicroCreditBorrowers
.findOne({ where: { userId: user.id } })
.then(subgraphBorrower => {
if(subgraphBorrower)
if (subgraphBorrower)
return subgraphBorrower.update(values, { transaction: t });
return subgraphMicroCreditBorrowers.create({ userId: user.id, ...values }, { transaction: t });
})
Expand All @@ -74,25 +74,33 @@ export async function updateCurrentDebt(): Promise<void> {
const t = await database.sequelize.transaction();

try {
// get borrowers that did not repay for more that 3 weeks
const threeWeeksAgo = new Date();
threeWeeksAgo.setDate(threeWeeksAgo.getDate() - 21);
// get borrowers that did not repay for more that 3 days
const threeDaysAgo = new Date();
threeDaysAgo.setDate(threeDaysAgo.getDate() - 3);

const borrowers = await subgraphMicroCreditBorrowers.findAll({
include: [{
model: appUser,
as: 'user',
}],
where: {
lastRepayment: {
[Op.lt]: threeWeeksAgo.getTime() / 1000 | 0
},
updatedAt: {
[Op.lt]: threeWeeksAgo
},
lastDebt: {
[Op.gt]: 0
}
[Op.or]: [
{
lastRepayment: {
[Op.lt]: threeDaysAgo.getTime() / 1000 | 0
}
},
{
updatedAt: {
[Op.lt]: threeDaysAgo
}
},
{
lastDebt: {
[Op.gt]: 0
}
}
]
}
});

Expand All @@ -105,7 +113,7 @@ export async function updateCurrentDebt(): Promise<void> {
const { loansLength } = await microCreditContract.walletMetadata(borrower.user!.address);

// get last loan
const { currentDebt } = await microCreditContract.userLoans(borrower.user!.address, loansLength-1);
const { currentDebt } = await microCreditContract.userLoans(borrower.user!.address, loansLength - 1);
const currentDebtFormated = new BigNumber(currentDebt.toString()).dividedBy(new BigNumber(10).pow(18)).toNumber();
return subgraphMicroCreditBorrowers.update({
lastDebt: currentDebtFormated,
Expand All @@ -122,7 +130,7 @@ export async function updateCurrentDebt(): Promise<void> {
await Promise.all(promises);

await t.commit();
utils.Logger.info('Current debt updated!');
utils.Logger.info('Current debt updated!');
} catch (error) {
await t.rollback();
utils.slack.sendSlackMessage('🚨 Error to update current debt', config.slack.lambdaChannel);
Expand Down
Loading