add orders collecrtion for coins and subscriptions page

This commit is contained in:
Thomas Demirdjian
2025-11-05 11:18:43 +01:00
parent ffaccd71a7
commit a7371323c4
17 changed files with 1613 additions and 228 deletions
+90
View File
@@ -0,0 +1,90 @@
const admin = require("firebase-admin");
const ORDER_TYPES = {
GIFT: "GIFT",
SONG: "SONG",
COINS: "COINS",
};
const ORDER_STATUS = {
PENDING: "PENDING",
APPLIED: "APPLIED",
REJECTED: "REJECTED",
};
const ORDERS_COLLECTION = "orders";
const isFiniteNumber = (value) => {
if (typeof value === "number" && Number.isFinite(value)) {
return true;
}
if (typeof value === "string") {
const parsed = Number(value);
return Number.isFinite(parsed);
}
return false;
};
const normalizeAmount = (amount) => {
if (!isFiniteNumber(amount)) {
return null;
}
return Number(amount);
};
const createOrderDocument = async ({
userId,
type,
amount,
songId = null,
createdBy = "system",
metadata = {},
orderId = null,
}) => {
if (!userId || typeof userId !== "string") {
throw new Error("[orders] Missing userId when creating order");
}
if (!Object.values(ORDER_TYPES).includes(type)) {
throw new Error(`[orders] Invalid order type "${type}"`);
}
const normalizedAmount = normalizeAmount(amount);
if (normalizedAmount === null || normalizedAmount === 0) {
throw new Error("[orders] Invalid order amount");
}
const payload = {
userId,
type,
amount: normalizedAmount,
songId: type === ORDER_TYPES.SONG ? songId || null : null,
createdAt: admin.firestore.FieldValue.serverTimestamp(),
createdBy: createdBy || "system",
status: ORDER_STATUS.PENDING,
metadata: metadata || {},
};
const collectionRef = admin.firestore().collection(ORDERS_COLLECTION);
const orderRef = orderId ? collectionRef.doc(orderId) : collectionRef.doc();
if (orderId) {
const existingSnapshot = await orderRef.get();
if (existingSnapshot.exists) {
return { orderRef, orderId: orderRef.id };
}
}
await orderRef.set(payload);
return { orderRef, orderId: orderRef.id };
};
module.exports = {
ORDER_TYPES,
ORDER_STATUS,
ORDERS_COLLECTION,
createOrderDocument,
normalizeAmount,
};