feat: fixes and formatter
This commit is contained in:
@@ -1,60 +1,60 @@
|
||||
const admin = require("firebase-admin");
|
||||
const { FieldValue } = require("firebase-admin/firestore");
|
||||
const admin = require('firebase-admin')
|
||||
const { FieldValue } = require('firebase-admin/firestore')
|
||||
|
||||
const ORDER_TYPES = {
|
||||
GIFT: "GIFT",
|
||||
SONG: "SONG",
|
||||
COINS: "COINS",
|
||||
SUBSCRIPTION: "SUBSCRIPTION",
|
||||
};
|
||||
GIFT: 'GIFT',
|
||||
SONG: 'SONG',
|
||||
COINS: 'COINS',
|
||||
SUBSCRIPTION: 'SUBSCRIPTION',
|
||||
}
|
||||
|
||||
const ORDER_STATUS = {
|
||||
PENDING: "PENDING",
|
||||
APPLIED: "APPLIED",
|
||||
REJECTED: "REJECTED",
|
||||
};
|
||||
PENDING: 'PENDING',
|
||||
APPLIED: 'APPLIED',
|
||||
REJECTED: 'REJECTED',
|
||||
}
|
||||
|
||||
const ORDERS_COLLECTION = "orders";
|
||||
const ORDERS_COLLECTION = 'orders'
|
||||
|
||||
const isFiniteNumber = (value) => {
|
||||
if (typeof value === "number" && Number.isFinite(value)) {
|
||||
return true;
|
||||
if (typeof value === 'number' && Number.isFinite(value)) {
|
||||
return true
|
||||
}
|
||||
if (typeof value === "string") {
|
||||
const parsed = Number(value);
|
||||
return Number.isFinite(parsed);
|
||||
if (typeof value === 'string') {
|
||||
const parsed = Number(value)
|
||||
return Number.isFinite(parsed)
|
||||
}
|
||||
return false;
|
||||
};
|
||||
return false
|
||||
}
|
||||
|
||||
const normalizeAmount = (amount) => {
|
||||
if (!isFiniteNumber(amount)) {
|
||||
return null;
|
||||
return null
|
||||
}
|
||||
return Number(amount);
|
||||
};
|
||||
return Number(amount)
|
||||
}
|
||||
|
||||
const createOrderDocument = async ({
|
||||
userId,
|
||||
type,
|
||||
amount,
|
||||
songId = null,
|
||||
createdBy = "system",
|
||||
createdBy = 'system',
|
||||
metadata = {},
|
||||
orderId = null,
|
||||
}) => {
|
||||
if (!userId || typeof userId !== "string") {
|
||||
throw new Error("[orders] Missing userId when creating order");
|
||||
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}"`);
|
||||
throw new Error(`[orders] Invalid order type "${type}"`)
|
||||
}
|
||||
|
||||
const normalizedAmount = normalizeAmount(amount);
|
||||
const normalizedAmount = normalizeAmount(amount)
|
||||
|
||||
if (normalizedAmount === null || normalizedAmount === 0) {
|
||||
throw new Error("[orders] Invalid order amount");
|
||||
throw new Error('[orders] Invalid order amount')
|
||||
}
|
||||
|
||||
const payload = {
|
||||
@@ -63,25 +63,25 @@ const createOrderDocument = async ({
|
||||
amount: normalizedAmount,
|
||||
songId: type === ORDER_TYPES.SONG ? songId || null : null,
|
||||
createdAt: FieldValue.serverTimestamp(),
|
||||
createdBy: createdBy || "system",
|
||||
createdBy: createdBy || 'system',
|
||||
status: ORDER_STATUS.PENDING,
|
||||
metadata: metadata || {},
|
||||
};
|
||||
}
|
||||
|
||||
const collectionRef = admin.firestore().collection(ORDERS_COLLECTION);
|
||||
const orderRef = orderId ? collectionRef.doc(orderId) : collectionRef.doc();
|
||||
const collectionRef = admin.firestore().collection(ORDERS_COLLECTION)
|
||||
const orderRef = orderId ? collectionRef.doc(orderId) : collectionRef.doc()
|
||||
|
||||
if (orderId) {
|
||||
const existingSnapshot = await orderRef.get();
|
||||
const existingSnapshot = await orderRef.get()
|
||||
if (existingSnapshot.exists) {
|
||||
return { orderRef, orderId: orderRef.id };
|
||||
return { orderRef, orderId: orderRef.id }
|
||||
}
|
||||
}
|
||||
|
||||
await orderRef.set(payload);
|
||||
await orderRef.set(payload)
|
||||
|
||||
return { orderRef, orderId: orderRef.id };
|
||||
};
|
||||
return { orderRef, orderId: orderRef.id }
|
||||
}
|
||||
|
||||
module.exports = {
|
||||
ORDER_TYPES,
|
||||
@@ -89,4 +89,4 @@ module.exports = {
|
||||
ORDERS_COLLECTION,
|
||||
createOrderDocument,
|
||||
normalizeAmount,
|
||||
};
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user