diff --git a/apps/web/src/lib/usage.ts b/apps/web/src/lib/usage.ts index 7db8407..23e8d04 100644 --- a/apps/web/src/lib/usage.ts +++ b/apps/web/src/lib/usage.ts @@ -1,5 +1,10 @@ import { EmailUsageType, Plan } from "@prisma/client"; +export const USAGE_UNIT_PRICE: Record = { + [EmailUsageType.MARKETING]: 0.001, + [EmailUsageType.TRANSACTIONAL]: 0.0004, +}; + /** * Unit price for unsend * 1 marketing email = 1 unit @@ -7,6 +12,9 @@ import { EmailUsageType, Plan } from "@prisma/client"; */ export const UNIT_PRICE = 0.001; +export const TRANSACTIONAL_UNIT_CONVERSION = + UNIT_PRICE / USAGE_UNIT_PRICE[EmailUsageType.TRANSACTIONAL]; + /** * Number of credits per plan * Credits are the units of measurement for email sending capacity. @@ -17,11 +25,6 @@ export const PLAN_CREDIT_UNITS = { [Plan.BASIC]: 10_000, }; -export const USAGE_UNIT_PRICE: Record = { - [EmailUsageType.MARKETING]: 0.001, - [EmailUsageType.TRANSACTIONAL]: 0.0004, -}; - /** * Gets timestamp for usage reporting, set to yesterday's date * Converts to Unix timestamp (seconds since epoch) @@ -55,12 +58,17 @@ export function getUsageUinits( marketingUsage: number, transactionUsage: number ) { - return marketingUsage + Math.floor(transactionUsage / 4); + return ( + marketingUsage + + Math.floor(transactionUsage / TRANSACTIONAL_UNIT_CONVERSION) + ); } export function getCost(usage: number, type: EmailUsageType) { const calculatedUsage = - type === EmailUsageType.MARKETING ? usage : Math.floor(usage / 4); + type === EmailUsageType.MARKETING + ? usage + : Math.floor(usage / TRANSACTIONAL_UNIT_CONVERSION); return calculatedUsage * UNIT_PRICE; } diff --git a/apps/web/src/server/jobs/usage-job.ts b/apps/web/src/server/jobs/usage-job.ts index 45a2115..6812d73 100644 --- a/apps/web/src/server/jobs/usage-job.ts +++ b/apps/web/src/server/jobs/usage-job.ts @@ -71,7 +71,7 @@ const worker = new Worker( await usageQueue.upsertJobScheduler( "daily-usage-report", { - pattern: "0 0 * * *", // Run every day at 12 AM + pattern: "0 */12 * * *", // Run every 12 hours (at 00:00, 12:00 UTC) tz: "UTC", }, {