expiry-notification #78

Merged
dhanshas merged 14 commits from expiry-notification into dev 2025-08-04 07:30:27 +00:00
Showing only changes of commit 29b06a6666 - Show all commits

View File

@ -12,7 +12,6 @@ interface MembershipData {
status: string; status: string;
subscription?: { subscription?: {
name: string; name: string;
duration: number;
frequency: string; frequency: string;
assignedAt: admin.firestore.Timestamp; assignedAt: admin.firestore.Timestamp;
}; };
@ -88,10 +87,8 @@ async function findExpiredMemberships(): Promise<
function checkIfMembershipExpired(data: MembershipData): boolean { function checkIfMembershipExpired(data: MembershipData): boolean {
try { try {
// Critical update: Use the assignedAt timestamp from the subscription object
if ( if (
!data.subscription || !data.subscription ||
!data.subscription.duration ||
!data.subscription.frequency || !data.subscription.frequency ||
!data.subscription.assignedAt !data.subscription.assignedAt
) { ) {
@ -106,7 +103,6 @@ function checkIfMembershipExpired(data: MembershipData): boolean {
).toDate(); ).toDate();
const expiryDate = calculateExpiryDate( const expiryDate = calculateExpiryDate(
startDate, startDate,
data.subscription.duration,
data.subscription.frequency data.subscription.frequency
); );
const now = new Date(); const now = new Date();
@ -118,27 +114,23 @@ function checkIfMembershipExpired(data: MembershipData): boolean {
} }
} }
function calculateExpiryDate( function calculateExpiryDate(startDate: Date, frequency: string): Date {
startDate: Date,
duration: number,
frequency: string
): Date {
const expiry = new Date(startDate); const expiry = new Date(startDate);
switch (frequency.toLowerCase()) { switch (frequency.toLowerCase()) {
case "monthly": case "monthly":
expiry.setMonth(expiry.getMonth() + duration); expiry.setMonth(expiry.getMonth() + 1);
break; break;
case "quarterly": case "quarterly":
expiry.setMonth(expiry.getMonth() + 3 * duration); expiry.setMonth(expiry.getMonth() + 3);
break; break;
case "half-yearly": case "half-yearly":
expiry.setMonth(expiry.getMonth() + 6 * duration); expiry.setMonth(expiry.getMonth() + 6);
break; break;
case "yearly": case "yearly":
expiry.setFullYear(expiry.getFullYear() + duration); expiry.setFullYear(expiry.getFullYear() + 1);
break; break;
default: default:
expiry.setMonth(expiry.getMonth() + duration); expiry.setMonth(expiry.getMonth() + 1);
} }
return expiry; return expiry;
} }