full
This commit is contained in:
38
utils/freeCreationLimit.js
Normal file
38
utils/freeCreationLimit.js
Normal file
@@ -0,0 +1,38 @@
|
||||
const FREE_LIFETIME_CREATION_LIMIT = 100
|
||||
|
||||
const canUseFreeCreation = (user) => {
|
||||
const used = user?.lifetime_free_creations || 0
|
||||
return used < FREE_LIFETIME_CREATION_LIMIT
|
||||
}
|
||||
|
||||
const remainingFreeCreations = (user) => {
|
||||
const used = user?.lifetime_free_creations || 0
|
||||
return Math.max(0, FREE_LIFETIME_CREATION_LIMIT - used)
|
||||
}
|
||||
|
||||
const consumeFreeCreation = async (user) => {
|
||||
if (!canUseFreeCreation(user)) {
|
||||
return {
|
||||
ok: false,
|
||||
message: `سقف ${FREE_LIFETIME_CREATION_LIMIT} ثبت رایگان (پروژه، بیلبورد و درخواست) به پایان رسیده است.`,
|
||||
}
|
||||
}
|
||||
|
||||
user.lifetime_free_creations = (user.lifetime_free_creations || 0) + 1
|
||||
await user.save()
|
||||
|
||||
return { ok: true }
|
||||
}
|
||||
|
||||
const freeCreationLimitResponse = () => ({
|
||||
error: true,
|
||||
message: `سقف ${FREE_LIFETIME_CREATION_LIMIT} ثبت رایگان (پروژه، بیلبورد و درخواست) به پایان رسیده است.`,
|
||||
})
|
||||
|
||||
module.exports = {
|
||||
FREE_LIFETIME_CREATION_LIMIT,
|
||||
canUseFreeCreation,
|
||||
remainingFreeCreations,
|
||||
consumeFreeCreation,
|
||||
freeCreationLimitResponse,
|
||||
}
|
||||
Reference in New Issue
Block a user