-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathapplication.handlers.ts
More file actions
68 lines (61 loc) · 2.67 KB
/
application.handlers.ts
File metadata and controls
68 lines (61 loc) · 2.67 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
import { ApplicationChangeset, IApplicationRepository } from "@grants-stack-indexer/repository";
import { performanceLogger } from "@grants-stack-indexer/shared";
import { ChangesetHandler } from "../types/index.js";
/**
* Collection of handlers for application-related operations.
* Each handler corresponds to a specific Application changeset type.
*/
export type ApplicationHandlers = {
[K in ApplicationChangeset["type"]]: ChangesetHandler<K>;
};
/**
* Creates handlers for managing application-related operations.
*
* @param repository - The application repository instance used for database operations
* @returns An object containing all application-related handlers
*/
export const createApplicationHandlers = (
repository: IApplicationRepository,
): ApplicationHandlers => ({
InsertApplication: (async (changeset, txConnection): Promise<void> => {
await repository.insertApplication(changeset.args, txConnection);
}) satisfies ChangesetHandler<"InsertApplication">,
UpdateApplication: (async (changeset, txConnection): Promise<void> => {
const { chainId, roundId, applicationId, application } = changeset.args;
await repository.updateApplication(
{ chainId, roundId, id: applicationId },
application,
txConnection,
);
}) satisfies ChangesetHandler<"UpdateApplication">,
IncrementApplicationDonationStats: (async (changeset, txConnection): Promise<void> => {
const startTime = performance.now();
const { chainId, roundId, applicationId, amountInUsd } = changeset.args;
await repository.incrementApplicationDonationStats(
{ chainId, roundId, id: applicationId },
amountInUsd,
txConnection,
);
const endTime = performance.now();
const duration = endTime - startTime;
// Get current application stats for logging
const application = await repository.getApplicationById(applicationId, chainId, roundId);
performanceLogger.logMetric({
timestamp: new Date().toISOString(),
eventType: "Application",
operation: "IncrementApplicationDonationStats",
duration,
totalTime: duration,
chainId,
roundId,
applicationId,
amountInUsd,
uniqueDonorsCount: application?.uniqueDonorsCount,
totalDonationsCount: application?.totalDonationsCount,
details: {
totalAmountDonatedInUsd: application?.totalAmountDonatedInUsd,
status: application?.status,
},
});
}) satisfies ChangesetHandler<"IncrementApplicationDonationStats">,
});