fix usage value

This commit is contained in:
KMKoushik
2025-03-23 08:47:11 +11:00
parent 2d90f08b42
commit 66a9b73269
2 changed files with 16 additions and 8 deletions

View File

@@ -1,5 +1,10 @@
import { EmailUsageType, Plan } from "@prisma/client"; import { EmailUsageType, Plan } from "@prisma/client";
export const USAGE_UNIT_PRICE: Record<EmailUsageType, number> = {
[EmailUsageType.MARKETING]: 0.001,
[EmailUsageType.TRANSACTIONAL]: 0.0004,
};
/** /**
* Unit price for unsend * Unit price for unsend
* 1 marketing email = 1 unit * 1 marketing email = 1 unit
@@ -7,6 +12,9 @@ import { EmailUsageType, Plan } from "@prisma/client";
*/ */
export const UNIT_PRICE = 0.001; export const UNIT_PRICE = 0.001;
export const TRANSACTIONAL_UNIT_CONVERSION =
UNIT_PRICE / USAGE_UNIT_PRICE[EmailUsageType.TRANSACTIONAL];
/** /**
* Number of credits per plan * Number of credits per plan
* Credits are the units of measurement for email sending capacity. * Credits are the units of measurement for email sending capacity.
@@ -17,11 +25,6 @@ export const PLAN_CREDIT_UNITS = {
[Plan.BASIC]: 10_000, [Plan.BASIC]: 10_000,
}; };
export const USAGE_UNIT_PRICE: Record<EmailUsageType, number> = {
[EmailUsageType.MARKETING]: 0.001,
[EmailUsageType.TRANSACTIONAL]: 0.0004,
};
/** /**
* Gets timestamp for usage reporting, set to yesterday's date * Gets timestamp for usage reporting, set to yesterday's date
* Converts to Unix timestamp (seconds since epoch) * Converts to Unix timestamp (seconds since epoch)
@@ -55,12 +58,17 @@ export function getUsageUinits(
marketingUsage: number, marketingUsage: number,
transactionUsage: number transactionUsage: number
) { ) {
return marketingUsage + Math.floor(transactionUsage / 4); return (
marketingUsage +
Math.floor(transactionUsage / TRANSACTIONAL_UNIT_CONVERSION)
);
} }
export function getCost(usage: number, type: EmailUsageType) { export function getCost(usage: number, type: EmailUsageType) {
const calculatedUsage = const calculatedUsage =
type === EmailUsageType.MARKETING ? usage : Math.floor(usage / 4); type === EmailUsageType.MARKETING
? usage
: Math.floor(usage / TRANSACTIONAL_UNIT_CONVERSION);
return calculatedUsage * UNIT_PRICE; return calculatedUsage * UNIT_PRICE;
} }

View File

@@ -71,7 +71,7 @@ const worker = new Worker(
await usageQueue.upsertJobScheduler( await usageQueue.upsertJobScheduler(
"daily-usage-report", "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", tz: "UTC",
}, },
{ {