39 lines
1.1 KiB
JavaScript
39 lines
1.1 KiB
JavaScript
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,
|
|
}
|