Support public class lookup, user provisioning, installment pricing, and admin approval before final enrollment.
95 lines
3.0 KiB
JavaScript
95 lines
3.0 KiB
JavaScript
'use strict';
|
|
|
|
const { FULL_PAYMENT_DISCOUNT, CLASS_REQUEST_DEPOSIT_RATE } = require('../constants/registration');
|
|
|
|
const getFinalTuition = (cls = {}) => {
|
|
const tuitionFee = Math.max(0, Number(cls.tuitionFee) || 0);
|
|
const classDiscount = cls.hasDiscount ? Math.max(0, Number(cls.discount) || 0) : 0;
|
|
return Math.max(0, tuitionFee - Math.min(classDiscount, tuitionFee));
|
|
};
|
|
|
|
const calculateMidpointDueDate = (cls = {}, course = {}) => {
|
|
const startDate = cls.startDate ? new Date(cls.startDate) : null;
|
|
if (!startDate || Number.isNaN(startDate.getTime())) return null;
|
|
|
|
const sectionCount = Math.max(1, Number(course.sectionCount) || 1);
|
|
const sessionsPerWeek = Math.max(1, (cls.days || []).length || 1);
|
|
const totalWeeks = sectionCount / sessionsPerWeek;
|
|
const weeksUntilDue = totalWeeks / 2;
|
|
|
|
const due = new Date(startDate);
|
|
due.setDate(due.getDate() + Math.round(weeksUntilDue * 7));
|
|
return due;
|
|
};
|
|
|
|
const calculateEnrollmentPricing = (cls = {}, course = {}, paymentPlan = 'full') => {
|
|
const tuitionFee = getFinalTuition(cls);
|
|
|
|
if (paymentPlan === 'installments') {
|
|
const totalAmount = tuitionFee;
|
|
const amountDueNow = Math.ceil(totalAmount / 2);
|
|
const secondInstallmentAmount = Math.max(0, totalAmount - amountDueNow);
|
|
|
|
return {
|
|
type: 'enrollment',
|
|
paymentPlan: 'installments',
|
|
tuitionFee,
|
|
classDiscount: cls.hasDiscount ? Math.max(0, Number(cls.discount) || 0) : 0,
|
|
paymentDiscount: 0,
|
|
totalAmount,
|
|
amountDueNow,
|
|
secondInstallmentAmount,
|
|
secondInstallmentDueDate: calculateMidpointDueDate(cls, course)
|
|
};
|
|
}
|
|
|
|
const paymentDiscount = Math.min(FULL_PAYMENT_DISCOUNT, tuitionFee);
|
|
const totalAmount = Math.max(0, tuitionFee - paymentDiscount);
|
|
|
|
return {
|
|
type: 'enrollment',
|
|
paymentPlan: 'full',
|
|
tuitionFee,
|
|
classDiscount: cls.hasDiscount ? Math.max(0, Number(cls.discount) || 0) : 0,
|
|
paymentDiscount,
|
|
totalAmount,
|
|
amountDueNow: totalAmount,
|
|
secondInstallmentAmount: 0,
|
|
secondInstallmentDueDate: null
|
|
};
|
|
};
|
|
|
|
const calculateClassRequestPricing = (cls = {}) => {
|
|
const tuitionFee = getFinalTuition(cls);
|
|
const amountDueNow = Math.ceil(tuitionFee * CLASS_REQUEST_DEPOSIT_RATE);
|
|
|
|
return {
|
|
type: 'class_request',
|
|
paymentPlan: 'deposit',
|
|
tuitionFee,
|
|
classDiscount: cls.hasDiscount ? Math.max(0, Number(cls.discount) || 0) : 0,
|
|
paymentDiscount: 0,
|
|
totalAmount: amountDueNow,
|
|
amountDueNow,
|
|
secondInstallmentAmount: 0,
|
|
secondInstallmentDueDate: null
|
|
};
|
|
};
|
|
|
|
const calculateRegistrationPricing = (cls = {}, course = {}, { type = 'enrollment', paymentPlan = 'full' } = {}) => {
|
|
if (type === 'class_request') {
|
|
return calculateClassRequestPricing(cls);
|
|
}
|
|
return calculateEnrollmentPricing(cls, course, paymentPlan);
|
|
};
|
|
|
|
module.exports = {
|
|
FULL_PAYMENT_DISCOUNT,
|
|
CLASS_REQUEST_DEPOSIT_RATE,
|
|
getFinalTuition,
|
|
calculateMidpointDueDate,
|
|
calculateEnrollmentPricing,
|
|
calculateClassRequestPricing,
|
|
calculateRegistrationPricing
|
|
};
|