merge dev2.0

This commit is contained in:
archer
2023-03-23 23:07:24 +08:00
4 changed files with 80 additions and 40 deletions

View File

@@ -12,30 +12,34 @@ export const pushBill = async ({
chatId: string;
textLen: number;
}) => {
await connectToDatabase();
const modelItem = ModelList.find((item) => item.model === modelName);
if (!modelItem) return;
const price = modelItem.price * textLen;
let billId;
try {
// 插入 Bill 记录
const res = await Bill.create({
userId,
chatId,
textLen,
price
});
billId = res._id;
await connectToDatabase();
// 扣费
await User.findByIdAndUpdate(userId, {
$inc: { balance: -price }
});
const modelItem = ModelList.find((item) => item.model === modelName);
if (!modelItem) return;
const price = modelItem.price * textLen;
let billId;
try {
// 插入 Bill 记录
const res = await Bill.create({
userId,
chatId,
textLen,
price
});
billId = res._id;
// 扣费
await User.findByIdAndUpdate(userId, {
$inc: { balance: -price }
});
} catch (error) {
billId && Bill.findByIdAndDelete(billId);
}
} catch (error) {
billId && Bill.findByIdAndDelete(billId);
console.log(error);
}
};