forked from StellarSplit/StellarSplit
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsettlement.processor.ts
More file actions
86 lines (77 loc) · 2.74 KB
/
Copy pathsettlement.processor.ts
File metadata and controls
86 lines (77 loc) · 2.74 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
import { Process, Processor } from "@nestjs/bull";
import { Logger } from "@nestjs/common";
import { InjectRepository } from "@nestjs/typeorm";
import { Repository, LessThan } from "typeorm";
import { SettlementSuggestion } from "./entities/settlement-suggestions.entity";
import { SettlementService } from "./settlement.service";
import { EmailService } from "../email/email.service";
import { User } from "../entities/user.entity";
@Processor("settlement-tasks")
export class SettlementProcessor {
private readonly logger = new Logger(SettlementProcessor.name);
constructor(
@InjectRepository(User)
private readonly userRepo: Repository<User>,
@InjectRepository(SettlementSuggestion)
private readonly suggestionRepo: Repository<SettlementSuggestion>,
private readonly settlementService: SettlementService,
private readonly emailService: EmailService,
) {}
/**
* Weekly Cron Job: Runs every Sunday at Midnight
* Analyzes net positions and sends a digest to users who owe money.
*/
@Process("send-weekly-settlement-digest")
async handleWeeklyDigest() {
this.logger.log("Starting weekly settlement digest generation...");
// Fetch all users (In production, batch this using a cursor/limit)
const users = await this.userRepo.find();
for (const user of users) {
try {
const isSnoozed = await this.settlementService.isSnoozed(user.id);
if (isSnoozed) continue;
const position = await this.settlementService.calculateNetPosition(
(user as any).walletAddress,
);
if (position.net < 0) {
await this.emailQueueWeeklyDigest(user, position);
}
} catch (error: any) {
this.logger.error(
`Failed to process digest for user ${user.id}: ${error.message}`,
);
}
}
}
/**
* Cleanup Job: Runs hourly
* Removes expired settlement suggestions to keep the DB performant.
*/
@Process("cleanup-expired-suggestions")
async handleCleanup() {
const result = await this.suggestionRepo.delete({
expiresAt: LessThan(new Date()),
wasActedOn: false,
});
this.logger.log(
`Cleaned up ${result.affected} expired settlement suggestions.`,
);
}
/**
* Helper to interface with your existing EmailService logic
*/
private async emailQueueWeeklyDigest(user: User, position: any) {
await (this.emailService as any).emailQueue.add("sendEmail", {
to: user.email,
type: "settlement_digest",
context: {
userName: user.email.split("@")[0],
totalOwed: Math.abs(position.owes),
totalOwedToYou: position.owed,
netPosition: position.net,
currency: "XLM",
actionLink: `${process.env.FRONTEND_URL}/settlements`,
},
});
}
}