-
-
Notifications
You must be signed in to change notification settings - Fork 10
Expand file tree
/
Copy pathbilling.init.js
More file actions
46 lines (43 loc) · 1.84 KB
/
Copy pathbilling.init.js
File metadata and controls
46 lines (43 loc) · 1.84 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
/**
* Module dependencies
*/
import config from '../../config/index.js';
import AnalyticsService from '../../lib/services/analytics.js';
import billingEvents from './lib/events.js';
import BillingPlanService from './services/billing.plan.service.js';
/**
* Billing module initialisation.
* Wires cross-module integrations that depend on services from lib.
*
* @param {import('express').Application} app - Express application instance
* @returns {Promise<void>}
*/
// eslint-disable-next-line no-unused-vars
export default async (app) => {
// Warn at startup if any pack is missing a valid priceUsd — refundPartial fallback will be inaccurate
if (config.billing?.packs?.length) {
for (const pack of config.billing.packs) {
if (typeof pack.priceUsd !== 'number' || pack.priceUsd <= 0) {
console.warn(`[billing] pack '${pack.packId}' missing valid priceUsd; refundPartial fallback will be inaccurate`);
}
}
}
// Update analytics group properties when a subscription plan changes
billingEvents.on('plan.changed', ({ organizationId, newPlan }) => {
try {
AnalyticsService.groupIdentify('company', String(organizationId), { plan: newPlan });
} catch (_) { /* analytics must not break billing flow */ }
});
try {
const { seeded, skipped } = await BillingPlanService.ensureSeeded();
if (seeded > 0) {
console.info(`[billing] seeded ${seeded} plan(s) from config.billing.planDefinitions (skipped ${skipped} already active)`);
}
} catch (err) {
console.error('[billing] ensureSeeded failed:', err);
// Fail fast when meterMode is enabled: a seeding failure means quota resolution
// will return 0 for all plans, silently gating all metered operations.
// Surfacing the crash here prevents a deploy from succeeding in a broken state.
if (config?.billing?.meterMode) throw err;
}
};