This commit is contained in:
payacom
2026-10-06 17:08:44 +03:30
parent fade8543e2
commit 367416d419
14 changed files with 6087 additions and 5937 deletions

View File

@@ -1,3 +1,4 @@
const UserModel = require('../models/UserModel')
const FREE_LIFETIME_CREATION_LIMIT = 100
const canUseFreeCreation = (user) => {
@@ -18,8 +19,20 @@ const consumeFreeCreation = async (user) => {
}
}
// updateOne + $inc (اتمیک، فقط همین فیلد) به‌جای user.save() — save() کل سند رو
// ری‌ولیدیت می‌کنه و برای کاربرهایی با مقدار نامعتبر در فیلدهای دیگه (مثلاً
// income_range) با ValidationError شکست می‌خوره
const res = await UserModel.updateOne(
{ _id: user._id, lifetime_free_creations: { $lt: FREE_LIFETIME_CREATION_LIMIT } },
{ $inc: { lifetime_free_creations: 1 } }
)
if (!res.modifiedCount) {
return {
ok: false,
message: `سقف ${FREE_LIFETIME_CREATION_LIMIT} ثبت رایگان (پروژه، بیلبورد و درخواست) به پایان رسیده است.`,
}
}
user.lifetime_free_creations = (user.lifetime_free_creations || 0) + 1
await user.save()
return { ok: true }
}