refactor: validation

This commit is contained in:
prafull-opensignlabs
2024-09-05 16:59:47 +05:30
parent d885570dd8
commit e2eddb6362
10 changed files with 302 additions and 96 deletions
@@ -7,8 +7,10 @@ export default async function saveSubscription(request, response) {
const Next_billing_date = request.body?.data?.subscription?.next_billing_at;
const planCode = request.body?.data?.subscription?.plan?.plan_code;
const addons = request.body?.data?.subscription?.addons || [];
const event = request.body?.data?.event_type || '';
const credits = planCredits?.[planCode] || 0;
let existAddon = 0;
const isTeamPlan = planCode?.includes('team');
let newAddons = 0;
if (addons?.length > 0) {
let allowedUsersMonthly = 0;
let allowedUsersYearly = 0;
@@ -22,11 +24,11 @@ export default async function saveSubscription(request, response) {
}
});
if (allowedUsersMonthly > 0 || allowedUsersYearly > 0) {
existAddon = allowedUsersMonthly + allowedUsersYearly + 1;
newAddons = allowedUsersMonthly + allowedUsersYearly + 1;
}
} else {
if (planCode === 'teams-yearly' || planCode === 'teams-monthly' || planCode === 'team-weekly') {
existAddon = 1;
newAddons = 1;
}
}
try {
@@ -42,6 +44,7 @@ export default async function saveSubscription(request, response) {
});
const subscription = await subcriptionCls.first({ useMasterKey: true });
if (subscription) {
const _resSub = JSON.parse(JSON.stringify(subscription));
const updateSubscription = new Parse.Object('contracts_Subscriptions');
updateSubscription.id = subscription.id;
updateSubscription.set('SubscriptionId', SubscriptionId);
@@ -52,27 +55,81 @@ export default async function saveSubscription(request, response) {
updateSubscription.unset('Next_billing_date');
}
updateSubscription.set('PlanCode', planCode);
if (existAddon > 0) {
updateSubscription.set('AllowedUsers', parseInt(existAddon));
if (newAddons > 0) {
updateSubscription.set('AllowedUsers', parseInt(newAddons));
}
let existAddon = 0;
let allowedUsersMonthly = 0;
let allowedUsersYearly = 0;
_resSub.SubscriptionDetails?.data?.subscription?.addons?.forEach(item => {
if (item.addon_code === 'extra-teams-users-monthly') {
allowedUsersMonthly += item.quantity;
} else if (item.addon_code === 'extra-teams-users-yearly') {
allowedUsersYearly += item.quantity;
} else if (item.addon_code === 'extra-users') {
allowedUsersMonthly += item.quantity;
}
});
if (allowedUsersMonthly > 0 || allowedUsersYearly > 0) {
existAddon = allowedUsersMonthly + allowedUsersYearly + 1; // + 1 is Admin user
} else {
if (
planCode === 'teams-yearly' ||
planCode === 'teams-monthly' ||
planCode === 'team-weekly'
) {
existAddon = 1; // 1 is Admin user
}
}
const isSameAsPrevPlan = subscription?.get('PlanCode') === planCode;
if (isSameAsPrevPlan) {
const planCredits = subscription?.get('PlanCredits');
const existAllowedCredits = subscription?.get('AllowedCredits') || 0;
if (planCredits) {
updateSubscription.set('AllowedCredits', planCredits);
const oldAddons = existAddon;
const substractedAddon = newAddons - oldAddons;
if (isTeamPlan && substractedAddon > 0) {
const newCredits = existAllowedCredits + substractedAddon * planCredits;
updateSubscription.set('AllowedCredits', newCredits);
if (event === 'subscription_created') {
updateSubscription.set('PlanCredits', credits);
}
} else if (isTeamPlan) {
const existCredits = existAddon * planCredits;
updateSubscription.set('AllowedCredits', existCredits);
if (event === 'subscription_created') {
updateSubscription.set('PlanCredits', credits);
}
} else {
updateSubscription.set('AllowedCredits', planCredits);
if (event === 'subscription_created') {
updateSubscription.set('PlanCredits', credits);
}
}
} else {
if (credits > 0) {
updateSubscription.set('AllowedCredits', credits);
updateSubscription.set('PlanCredits', credits);
if (isTeamPlan) {
const newCredits = newAddons * credits;
updateSubscription.set('AllowedCredits', newCredits);
updateSubscription.set('PlanCredits', credits);
} else {
updateSubscription.set('AllowedCredits', credits);
updateSubscription.set('PlanCredits', credits);
}
}
}
} else {
if (credits > 0) {
updateSubscription.set('AllowedCredits', credits);
updateSubscription.set('PlanCredits', credits);
if (isTeamPlan) {
const newCredits = newAddons * credits;
updateSubscription.set('AllowedCredits', newCredits);
updateSubscription.set('PlanCredits', credits);
} else {
updateSubscription.set('AllowedCredits', credits);
updateSubscription.set('PlanCredits', credits);
}
}
}
await updateSubscription.save(null, { useMasterKey: true });
return response.status(200).json({ status: 'update subscription!' });
} else {
@@ -100,11 +157,18 @@ export default async function saveSubscription(request, response) {
createSubscription.unset('Next_billing_date');
}
createSubscription.set('PlanCode', planCode);
if (existAddon > 0) {
createSubscription.set('AllowedUsers', parseInt(existAddon));
if (newAddons > 0) {
createSubscription.set('AllowedUsers', parseInt(newAddons));
}
if (credits > 0) {
createSubscription.set('AllowedCredits', credits);
if (isTeamPlan) {
const totalCredits = parseInt(newAddons) * credits;
createSubscription.set('AllowedCredits', totalCredits);
createSubscription.set('PlanCredits', credits);
} else {
createSubscription.set('AllowedCredits', credits);
createSubscription.set('PlanCredits', credits);
}
}
await createSubscription.save(null, { useMasterKey: true });
return response.status(200).json({ status: 'create subscription!' });