Skip to content

Commit d1ec778

Browse files
committed
make stripe billing to use set instead of increment
1 parent 4c38743 commit d1ec778

File tree

1 file changed

+16
-17
lines changed

1 file changed

+16
-17
lines changed

helicone-cron/src/managers/UsageManager.ts

Lines changed: 16 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -43,24 +43,20 @@ export class UsageManager {
4343
endDate.setMinutes(endDate.getMinutes() - 5);
4444

4545
try {
46-
if (!org.latestEndTime) {
47-
// Get current period start date from stripe
48-
const subscription = await this.stripeClient.getSubscriptionById(
49-
org.stripeSubscriptionId
50-
);
51-
52-
if (subscription.error || !subscription.data) {
53-
throw new Error(
54-
`Failed to get subscription from Stripe. ${subscription.error}`
55-
);
56-
}
46+
// Get current period start date from stripe
47+
const subscription = await this.stripeClient.getSubscriptionById(
48+
org.stripeSubscriptionId
49+
);
5750

58-
subscription.data.start_date;
59-
startDate = new Date(subscription.data.current_period_start * 1000);
60-
} else {
61-
startDate = new Date(org.latestEndTime);
51+
if (subscription.error || !subscription.data) {
52+
throw new Error(
53+
`Failed to get subscription from Stripe. ${subscription.error}`
54+
);
6255
}
6356

57+
subscription.data.start_date;
58+
startDate = new Date(subscription.data.current_period_start * 1000);
59+
6460
const requestCountResult =
6561
await this.requestResponseStore.getRequestCountByOrgId(
6662
org.orgId,
@@ -81,6 +77,7 @@ export class UsageManager {
8177
stripeSubscriptionItemId: org.stripeSubscriptionItemId,
8278
quantity: requestQuantity,
8379
startDate: startDate,
80+
endDate: endDate,
8481
});
8582

8683
if (usageRecordErr || !usageRecord) {
@@ -122,19 +119,21 @@ export class UsageManager {
122119
stripeSubscriptionItemId,
123120
quantity,
124121
startDate,
122+
endDate,
125123
}: {
126124
orgId: string;
127125
stripeSubscriptionItemId: string;
128126
quantity: number;
129127
startDate: Date;
128+
endDate: Date;
130129
}): Promise<Result<string, string>> {
131130
try {
132131
const usageRecord = await this.stripeClient.addUsageRecord({
133132
subscriptionItemId: stripeSubscriptionItemId,
134133
quantity: quantity,
135134
timestamp: "now",
136-
action: "increment",
137-
idempotencyKey: `${orgId}-${startDate.toISOString()}`,
135+
action: "set",
136+
idempotencyKey: `${orgId}-${startDate.toISOString()}-${endDate.toISOString()}`,
138137
});
139138

140139
if (usageRecord.error || !usageRecord.data) {

0 commit comments

Comments
 (0)