7e73d675d7
- Add strategyName property to ISeatBillingStrategy interface and all implementations - Log which strategy handled the invoice.upcoming webhook (not just whether it applied) - Always log strategy result (not just when applied=true) for better observability - Rename highWaterMarkApplied to strategyApplied since applied covers multiple strategies Co-Authored-By: unknown <> Co-authored-by: Devin AI <158243242+devin-ai-integration[bot]@users.noreply.github.com>
58 lines
2.0 KiB
TypeScript
58 lines
2.0 KiB
TypeScript
import logger from "@calcom/lib/logger";
|
|
import type { HighWaterMarkRepository } from "../../repository/highWaterMark/HighWaterMarkRepository";
|
|
import type { HighWaterMarkService } from "../highWaterMark/HighWaterMarkService";
|
|
import type { SeatChangeContext } from "./ISeatBillingStrategy";
|
|
import { BaseSeatBillingStrategy } from "./ISeatBillingStrategy";
|
|
|
|
const log = logger.getSubLogger({ prefix: ["HighWaterMarkStrategy"] });
|
|
|
|
export interface IHighWaterMarkStrategyDeps {
|
|
highWaterMarkRepository: HighWaterMarkRepository;
|
|
highWaterMarkService: HighWaterMarkService;
|
|
}
|
|
|
|
export class HighWaterMarkStrategy extends BaseSeatBillingStrategy {
|
|
readonly strategyName = "HighWaterMark";
|
|
|
|
constructor(private readonly deps: IHighWaterMarkStrategyDeps) {
|
|
super();
|
|
}
|
|
|
|
async onSeatChange(context: SeatChangeContext): Promise<void> {
|
|
if (context.changeType !== "addition") return;
|
|
|
|
const billing = await this.deps.highWaterMarkRepository.getByTeamId(context.teamId);
|
|
if (!billing) return;
|
|
|
|
const periodStart = billing.highWaterMarkPeriodStart || billing.subscriptionStart;
|
|
if (!periodStart) return;
|
|
|
|
const result = await this.deps.highWaterMarkRepository.updateIfHigher({
|
|
teamId: context.teamId,
|
|
isOrganization: billing.isOrganization,
|
|
newSeatCount: context.membershipCount,
|
|
periodStart,
|
|
});
|
|
|
|
if (result.updated) {
|
|
log.info(`High water mark updated for team ${context.teamId}`, {
|
|
previousHighWaterMark: result.previousHighWaterMark,
|
|
newHighWaterMark: context.membershipCount,
|
|
});
|
|
}
|
|
}
|
|
|
|
override async onInvoiceUpcoming(subscriptionId: string): Promise<{ applied: boolean }> {
|
|
const applied = await this.deps.highWaterMarkService.applyHighWaterMarkToSubscription(subscriptionId);
|
|
return { applied };
|
|
}
|
|
|
|
override async onRenewalPaid(subscriptionId: string, periodStart: Date): Promise<{ reset: boolean }> {
|
|
const reset = await this.deps.highWaterMarkService.resetSubscriptionAfterRenewal({
|
|
subscriptionId,
|
|
newPeriodStart: periodStart,
|
|
});
|
|
return { reset };
|
|
}
|
|
}
|