-
Notifications
You must be signed in to change notification settings - Fork 42
feat: implement automated email and push notification service #162
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
base: master
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -9,6 +9,7 @@ import { xdr } from "@stellar/stellar-sdk"; | |
| import contractService from "./contract.service"; | ||
| import { prisma } from "./database.service"; | ||
| import websocketService from "./websocket.service"; | ||
| import notificationService, { NotificationType, NotificationChannel } from "./notification.service"; | ||
| import { env } from "../config/env"; | ||
| import Decimal from "decimal.js"; | ||
| import eventMonitoringService from "./event-monitoring.service"; | ||
|
|
@@ -415,6 +416,57 @@ export class LoanService { | |
| throw new NotFoundError("Loan not found"); | ||
| } | ||
|
|
||
| const outstandingAfter = outstandingBefore.minus(amount); | ||
| let nextStatus: LoanStatus = loan.status; | ||
| if (outstandingAfter.eq(ZERO)) { | ||
| nextStatus = "REPAID"; | ||
| } else if (loan.status === "PENDING") { | ||
| nextStatus = "ACTIVE"; | ||
| } | ||
|
|
||
| if (nextStatus !== loan.status) { | ||
| await tx.loan.update({ | ||
| where: { id: loanId }, | ||
| data: { status: nextStatus }, | ||
| }); | ||
|
|
||
| websocketService.broadcastLoanUpdated(loanId, nextStatus); | ||
|
|
||
| // Send notification if loan is approved (moved to ACTIVE) | ||
| if (nextStatus === "ACTIVE") { | ||
| await notificationService.sendNotification({ | ||
| userId: loan.borrowerId, | ||
| type: NotificationType.LOAN_APPROVED, | ||
| channel: NotificationChannel.BOTH, | ||
| data: { | ||
| loanId, | ||
| amount: loan.amount.toString(), | ||
| assetCode: loan.assetCode, | ||
| interestRate: loan.interestRate.toString(), | ||
| dueDate: loan.dueDate?.toISOString() || "N/A", | ||
| dashboardUrl: `${process.env.FRONTEND_URL || "https://stellovault.com"}/loans/${loanId}`, | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Use the The file imports - dashboardUrl: `${process.env.FRONTEND_URL || "https://stellovault.com"}/loans/${loanId}`,
+ dashboardUrl: `${env.frontendUrl || "https://stellovault.com"}/loans/${loanId}`,🤖 Prompt for AI Agents |
||
| }, | ||
| }); | ||
| } | ||
|
Comment on lines
+434
to
+450
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Move notification dispatch outside the database transaction to avoid blocking and potential rollback. The
The notification should be queued after the transaction commits successfully. 🛠️ Proposed fix: dispatch notification after transaction- if (nextStatus !== loan.status) {
- await tx.loan.update({
- where: { id: loanId },
- data: { status: nextStatus },
- });
-
- websocketService.broadcastLoanUpdated(loanId, nextStatus);
-
- // Send notification if loan is approved (moved to ACTIVE)
- if (nextStatus === "ACTIVE") {
- await notificationService.sendNotification({
- userId: loan.borrowerId,
- type: NotificationType.LOAN_APPROVED,
- channel: NotificationChannel.BOTH,
- data: {
- loanId,
- amount: loan.amount.toString(),
- assetCode: loan.assetCode,
- interestRate: loan.interestRate.toString(),
- dueDate: loan.dueDate?.toISOString() || "N/A",
- dashboardUrl: `${process.env.FRONTEND_URL || "https://stellovault.com"}/loans/${loanId}`,
- },
- });
- }
- }
+ let shouldNotifyApproval = false;
+ let notificationData: { borrowerId: string; loanId: string; amount: string; assetCode: string; interestRate: string; dueDate: string } | null = null;
+
+ if (nextStatus !== loan.status) {
+ await tx.loan.update({
+ where: { id: loanId },
+ data: { status: nextStatus },
+ });
+
+ websocketService.broadcastLoanUpdated(loanId, nextStatus);
+
+ if (nextStatus === "ACTIVE") {
+ shouldNotifyApproval = true;
+ notificationData = {
+ borrowerId: loan.borrowerId,
+ loanId,
+ amount: loan.amount.toString(),
+ assetCode: loan.assetCode,
+ interestRate: loan.interestRate.toString(),
+ dueDate: loan.dueDate?.toISOString() || "N/A",
+ };
+ }
+ }Then after the transaction block (after line 272), add: // Send notification after transaction commits
if (shouldNotifyApproval && notificationData) {
notificationService.sendNotification({
userId: notificationData.borrowerId,
type: NotificationType.LOAN_APPROVED,
channel: NotificationChannel.BOTH,
data: {
...notificationData,
dashboardUrl: `${env.frontendUrl || "https://stellovault.com"}/loans/${notificationData.loanId}`,
},
}).catch(err => console.error("Failed to send loan approval notification:", err));
}🤖 Prompt for AI Agents |
||
| } | ||
|
|
||
| const updatedLoan = await tx.loan.findUnique({ | ||
| where: { id: loanId }, | ||
| include: { repayments: true, borrower: true, lender: true }, | ||
| }); | ||
| if (!updatedLoan) { | ||
| throw new NotFoundError("Loan not found"); | ||
| } | ||
|
|
||
| return { | ||
| repayment, | ||
| outstandingBefore: outstandingBefore.toString(), | ||
| outstandingAfter: outstandingAfter.toString(), | ||
| fullyRepaid: outstandingAfter.eq(ZERO), | ||
| loan: updatedLoan, | ||
| }; | ||
| }, { isolationLevel: "Serializable" }); | ||
| } | ||
| return { | ||
| repayment, | ||
| paymentSession: selectedPaymentSession, | ||
|
|
||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Fix dotenv-linter warnings on new env keys (likely CI blocker).
Line 6 and Line 7 introduce
QuoteCharacterwarnings, and key order triggersUnorderedKey. This can keep CI red.Proposed fix
📝 Committable suggestion
🧰 Tools
🪛 dotenv-linter (4.0.0)
[warning] 6-6: [QuoteCharacter] The value has quote characters (', ")
(QuoteCharacter)
[warning] 7-7: [QuoteCharacter] The value has quote characters (', ")
(QuoteCharacter)
[warning] 7-7: [UnorderedKey] The FROM_EMAIL key should go before the SENDGRID_API_KEY key
(UnorderedKey)
🤖 Prompt for AI Agents