Skip to content

Commit 7a9d857

Browse files
committed
Fix potential duplicate emails
1 parent 8a2f4e3 commit 7a9d857

File tree

3 files changed

+19
-20
lines changed

3 files changed

+19
-20
lines changed

apps/server/src/email.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -103,7 +103,7 @@ const sendEmail = async (claim: Claims) => {
103103

104104
if (process.env.NODE_ENV !== "production") {
105105
console.log("Not running prod, logging email params");
106-
console.log("Email params: ", emailParams);
106+
console.log("Email params: ", { ...emailParams, html: "<truncated>" });
107107

108108
return;
109109
}
Lines changed: 12 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1,24 +1,21 @@
11
import { ClaimStatus, prisma } from "@repo/database";
22
import { sendEmail } from "./email";
33
import { notifyPaymentSubscribers } from "./notifyPaymentSubscribers";
4-
import type { Invoice__Output } from "./protos/generated/lnrpc/Invoice";
54

6-
export const handleIncomingPayment = async ({ paymentRequest, state }: Invoice__Output) => {
7-
if (state === "SETTLED") {
8-
const claim = await prisma.claims.findUnique({
9-
where: { paymentRequest, status: ClaimStatus.AWAITING_PAYMENT },
10-
});
5+
export const handleIncomingPayment = async (paymentRequest: string) => {
6+
const claim = await prisma.claims.findUnique({
7+
where: { paymentRequest, status: ClaimStatus.AWAITING_PAYMENT },
8+
});
119

12-
if (!claim) return;
10+
if (!claim) return;
1311

14-
await prisma.claims.update({
15-
where: { paymentRequest },
16-
data: { status: ClaimStatus.PAID },
17-
});
12+
await prisma.claims.update({
13+
where: { paymentRequest },
14+
data: { status: ClaimStatus.PAID },
15+
});
1816

19-
notifyPaymentSubscribers(paymentRequest);
17+
notifyPaymentSubscribers(paymentRequest);
2018

21-
// todo: handle multiple platforms
22-
await sendEmail(claim);
23-
}
19+
// todo: handle multiple platforms
20+
await sendEmail(claim);
2421
};

apps/server/src/subscribeInvoices.ts

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -42,12 +42,14 @@ export const subscribeInvoices = () => {
4242
stream.on("data", (data: Invoice__Output) => {
4343
reconnectAttempts = 0;
4444

45-
if (data.state === "SETTLED") {
46-
lastSettleIndex = Number(data.settleIndex);
45+
const settleIndex = Number(data.settleIndex);
46+
47+
if (settleIndex && settleIndex > lastSettleIndex) {
48+
lastSettleIndex = settleIndex;
4749
saveLastSettleIndex(lastSettleIndex);
48-
}
4950

50-
handleIncomingPayment(data);
51+
handleIncomingPayment(data.paymentRequest);
52+
}
5153
});
5254

5355
stream.on("error", (err: unknown) => {

0 commit comments

Comments
 (0)