feat: implement billing account management and cycle synchronization logic with accompanying tests
This commit is contained in:
@@ -18,6 +18,28 @@ const AVAILABLE_PRODUCTS = ['monthly_pro', 'yearly_pro', 'topup_small', 'topup_m
|
||||
|
||||
const nowIso = () => new Date().toISOString();
|
||||
|
||||
const asIsoDate = (value) => {
|
||||
if (value == null || value === '') return null;
|
||||
if (value instanceof Date) {
|
||||
return Number.isNaN(value.getTime()) ? null : value.toISOString();
|
||||
}
|
||||
if (typeof value === 'number' && Number.isFinite(value)) {
|
||||
return new Date(value).toISOString();
|
||||
}
|
||||
if (typeof value === 'string') {
|
||||
const trimmed = value.trim();
|
||||
if (!trimmed) return null;
|
||||
if (/^\d+$/.test(trimmed)) {
|
||||
return new Date(Number(trimmed)).toISOString();
|
||||
}
|
||||
const parsed = new Date(trimmed);
|
||||
if (!Number.isNaN(parsed.getTime())) {
|
||||
return parsed.toISOString();
|
||||
}
|
||||
}
|
||||
return null;
|
||||
};
|
||||
|
||||
const startOfUtcMonth = (date) => {
|
||||
return new Date(Date.UTC(date.getUTCFullYear(), date.getUTCMonth(), 1, 0, 0, 0, 0));
|
||||
};
|
||||
@@ -73,17 +95,19 @@ const runInTransaction = async (db, worker) => {
|
||||
|
||||
const normalizeAccountRow = (row) => {
|
||||
if (!row) return null;
|
||||
const now = new Date();
|
||||
const { cycleStartedAt: defaultCycleStartedAt, cycleEndsAt: defaultCycleEndsAt } = getCycleBounds(now);
|
||||
return {
|
||||
userId: String(row.userId),
|
||||
plan: row.plan === 'pro' ? 'pro' : 'free',
|
||||
provider: typeof row.provider === 'string' && row.provider ? row.provider : 'revenuecat',
|
||||
cycleStartedAt: String(row.cycleStartedAt),
|
||||
cycleEndsAt: String(row.cycleEndsAt),
|
||||
cycleStartedAt: asIsoDate(row.cycleStartedAt) || defaultCycleStartedAt.toISOString(),
|
||||
cycleEndsAt: asIsoDate(row.cycleEndsAt) || defaultCycleEndsAt.toISOString(),
|
||||
monthlyAllowance: Number(row.monthlyAllowance) || FREE_MONTHLY_CREDITS,
|
||||
usedThisCycle: Number(row.usedThisCycle) || 0,
|
||||
topupBalance: Number(row.topupBalance) || 0,
|
||||
renewsAt: row.renewsAt ? String(row.renewsAt) : null,
|
||||
updatedAt: row.updatedAt ? String(row.updatedAt) : nowIso(),
|
||||
renewsAt: asIsoDate(row.renewsAt),
|
||||
updatedAt: asIsoDate(row.updatedAt) || now.toISOString(),
|
||||
};
|
||||
};
|
||||
|
||||
@@ -238,25 +262,6 @@ const buildBillingSummary = (account) => {
|
||||
};
|
||||
};
|
||||
|
||||
const asIsoDate = (value) => {
|
||||
if (value == null || value === '') return null;
|
||||
if (typeof value === 'number' && Number.isFinite(value)) {
|
||||
return new Date(value).toISOString();
|
||||
}
|
||||
if (typeof value === 'string') {
|
||||
const trimmed = value.trim();
|
||||
if (!trimmed) return null;
|
||||
if (/^\d+$/.test(trimmed)) {
|
||||
return new Date(Number(trimmed)).toISOString();
|
||||
}
|
||||
const parsed = new Date(trimmed);
|
||||
if (!Number.isNaN(parsed.getTime())) {
|
||||
return parsed.toISOString();
|
||||
}
|
||||
}
|
||||
return null;
|
||||
};
|
||||
|
||||
const isSupportedTopupProduct = (productId) => {
|
||||
return typeof productId === 'string'
|
||||
&& productId.startsWith('topup_')
|
||||
|
||||
Reference in New Issue
Block a user