-
Notifications
You must be signed in to change notification settings - Fork 245
Expand file tree
/
Copy pathfireTrackWebhooks.ts
More file actions
119 lines (111 loc) · 3.36 KB
/
Copy pathfireTrackWebhooks.ts
File metadata and controls
119 lines (111 loc) · 3.36 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
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
import {
type Feature,
type FullCustomer,
type FullSubject,
fullSubjectToFullCustomer,
} from "@autumn/shared";
import type { AutumnContext } from "@/honoUtils/HonoEnv.js";
import { buildEvaluationSubject } from "@/internal/balances/check/buildEvaluationSubject.js";
import { checkUsageAlerts } from "@/internal/balances/usageAlerts/check/checkUsageAlerts.js";
import { getApiCustomerBase } from "@/internal/customers/cusUtils/apiCusUtils/getApiCustomerBase.js";
import { getApiEntityBase } from "@/internal/entities/entityUtils/apiEntityUtils/getApiEntityBase.js";
import { checkLimitReached } from "./checkLimitReached.js";
import { handleThresholdReached } from "./handleThresholdReached.js";
export const fireTrackWebhooks = ({
ctx,
oldFullSubject,
newFullSubject,
oldFullCus: oldFullCusInput,
newFullCus: newFullCusInput,
feature,
entityId,
featuresFromMutationLogs,
eventProperties,
}: {
ctx: AutumnContext;
oldFullSubject?: FullSubject;
newFullSubject?: FullSubject;
oldFullCus?: FullCustomer;
newFullCus?: FullCustomer;
feature: Feature;
entityId?: string;
featuresFromMutationLogs?: Feature[];
eventProperties?: Record<string, unknown> | null;
}) => {
const oldFullCus = oldFullSubject
? fullSubjectToFullCustomer({ fullSubject: oldFullSubject })
: oldFullCusInput;
const newFullCus = newFullSubject
? fullSubjectToFullCustomer({ fullSubject: newFullSubject })
: newFullCusInput;
if (!oldFullCus || !newFullCus) return;
handleThresholdReached({
ctx,
oldFullCus,
newFullCus,
feature,
}).catch((error) => {
ctx.logger.error(`[fireTrackWebhooks] handleThresholdReached: ${error}`);
});
const featuresForUsageAlertsAndLimit =
featuresFromMutationLogs && featuresFromMutationLogs.length > 0
? featuresFromMutationLogs
: [feature];
for (const affectedFeature of featuresForUsageAlertsAndLimit) {
checkUsageAlerts({
ctx,
oldFullCus,
newFullCus,
oldFullSubject,
newFullSubject,
feature: affectedFeature,
entityId,
}).catch((error) => {
ctx.logger.error(`[fireTrackWebhooks] checkUsageAlerts: ${error}`);
});
}
(async () => {
let oldEvalSubject: Awaited<ReturnType<typeof buildEvaluationSubject>>;
let newEvalSubject: Awaited<ReturnType<typeof buildEvaluationSubject>>;
if (oldFullSubject && newFullSubject) {
[oldEvalSubject, newEvalSubject] = await Promise.all([
buildEvaluationSubject({ ctx, fullSubject: oldFullSubject, entityId }),
buildEvaluationSubject({ ctx, fullSubject: newFullSubject, entityId }),
]);
} else {
const entity = entityId
? newFullCus.entities?.find((e) => e.id === entityId)
: undefined;
const buildSubject = async (fullCus: FullCustomer) => {
if (entity) {
const { apiEntity } = await getApiEntityBase({
ctx,
entity,
fullCus,
});
return apiEntity;
}
const { apiCustomer } = await getApiCustomerBase({ ctx, fullCus });
return apiCustomer;
};
[oldEvalSubject, newEvalSubject] = await Promise.all([
buildSubject(oldFullCus),
buildSubject(newFullCus),
]);
}
for (const affectedFeature of featuresForUsageAlertsAndLimit) {
await checkLimitReached({
ctx,
oldEvalSubject,
newEvalSubject,
newFullCus,
newFullSubject,
feature: affectedFeature,
entityId,
eventProperties,
});
}
})().catch((error) => {
ctx.logger.error(`[fireTrackWebhooks] checkLimitReached: ${error}`);
});
};