* refactor: apply biome formatting to packages/features (batch 1 - small subdirs) Format small subdirectories in packages/features: di, flags, holidays, oauth, settings, users, assignment-reason, selectedCalendar, hashedLink, host, form, form-builder, availability, data-table, pbac, schedules, troubleshooter, eventtypes, calendar-subscription, and root-level files. Also includes straggler apps/web BookEventForm.tsx. Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com> * refactor: apply biome formatting to packages/features (batch 2 - medium subdirs) Format medium subdirectories in packages/features: auth, credentials, calendars, routing-forms, routing-trace, attributes, watchlist, calAIPhone, tasker, and webhooks. Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com> * refactor: apply biome formatting to packages/features (batch 3 - bookings + insights) Format bookings and insights subdirectories in packages/features. Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com> * refactor: apply biome formatting to packages/features (batch 4 - ee) Format packages/features/ee subdirectory covering billing, workflows, organizations, teams, managed-event-types, round-robin, dsync, integration-attribute-sync, and payments. Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com> * refactor: apply biome formatting to packages/features (batch 5 - booking-audit part 1) Format booking-audit di, actions, common, dto, repository, and types subdirectories in packages/features/booking-audit. Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com> * refactor: apply biome formatting to packages/features (batch 6 - booking-audit part 2) Format booking-audit service subdirectory in packages/features/booking-audit. Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com> --------- Co-authored-by: Claude Opus 4.6 <noreply@anthropic.com>
81 lines
2.5 KiB
TypeScript
81 lines
2.5 KiB
TypeScript
import { getBillingProviderService } from "@calcom/ee/billing/di/containers/Billing";
|
|
import { HighWaterMarkService } from "@calcom/features/ee/billing/service/highWaterMark/HighWaterMarkService";
|
|
import logger from "@calcom/lib/logger";
|
|
import type { Logger } from "tslog";
|
|
|
|
const defaultLogger = logger.getSubLogger({ prefix: ["hwm-webhook-utils"] });
|
|
|
|
export function extractPeriodStartFromInvoice(
|
|
linesData: Array<{ period?: { start: number; end: number } }>
|
|
): number | undefined {
|
|
if (!linesData || linesData.length === 0) {
|
|
return undefined;
|
|
}
|
|
return linesData[0]?.period?.start;
|
|
}
|
|
|
|
export function validateInvoiceLinesForHwm(
|
|
linesData: Array<{ period?: { start: number; end: number } }>,
|
|
subscriptionId: string,
|
|
log: Logger<unknown> = defaultLogger
|
|
): { isValid: boolean; periodStart?: number } {
|
|
if (!linesData || linesData.length === 0) {
|
|
log.warn(`Invoice has no line items for subscription ${subscriptionId}, cannot process HWM`);
|
|
return { isValid: false };
|
|
}
|
|
|
|
const periodStart = linesData[0]?.period?.start;
|
|
if (!periodStart) {
|
|
log.warn(`Invoice line item missing period.start for subscription ${subscriptionId}, cannot process HWM`);
|
|
return { isValid: false };
|
|
}
|
|
|
|
return { isValid: true, periodStart };
|
|
}
|
|
|
|
export interface HwmResetResult {
|
|
success: boolean;
|
|
updated?: boolean;
|
|
error?: string;
|
|
}
|
|
|
|
export async function handleHwmResetAfterRenewal(
|
|
subscriptionId: string,
|
|
periodStartTimestamp: number | undefined,
|
|
log: Logger<unknown> = defaultLogger
|
|
): Promise<HwmResetResult> {
|
|
if (!periodStartTimestamp) {
|
|
log.warn(`No period start timestamp for subscription ${subscriptionId}, skipping HWM reset`);
|
|
return { success: false, error: "No period start timestamp" };
|
|
}
|
|
|
|
const newPeriodStart = new Date(periodStartTimestamp * 1000);
|
|
const billingProviderService = getBillingProviderService();
|
|
const highWaterMarkService = new HighWaterMarkService({
|
|
logger: log,
|
|
billingService: billingProviderService,
|
|
});
|
|
|
|
try {
|
|
const updated = await highWaterMarkService.resetSubscriptionAfterRenewal({
|
|
subscriptionId,
|
|
newPeriodStart,
|
|
});
|
|
|
|
log.info("HWM reset after invoice paid", {
|
|
subscriptionId,
|
|
newPeriodStart,
|
|
updated,
|
|
});
|
|
|
|
return { success: true, updated };
|
|
} catch (error) {
|
|
const errorMessage = error instanceof Error ? error.message : String(error);
|
|
log.error("Failed to reset HWM after invoice paid", {
|
|
subscriptionId,
|
|
error: errorMessage,
|
|
});
|
|
return { success: false, error: errorMessage };
|
|
}
|
|
}
|