pay
This commit is contained in:
@@ -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 }
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user