plan-expiry-in-two-days (#83)
All checks were successful
Deploy FitLien services to Dev / Deploy to Dev (push) Successful in 4m20s
All checks were successful
Deploy FitLien services to Dev / Deploy to Dev (push) Successful in 4m20s
Reviewed-on: #83 Reviewed-by: Dhansh A S <dhanshas@cosq.net> Co-authored-by: Sharon Dcruz <sharondcruz@cosq.net> Co-committed-by: Sharon Dcruz <sharondcruz@cosq.net>
This commit is contained in:
parent
237dd8a263
commit
4cf5692386
@ -44,7 +44,7 @@ export const checkExpiredMemberships = onSchedule(
|
|||||||
|
|
||||||
try {
|
try {
|
||||||
const expiredMemberships = await findExpiredMemberships();
|
const expiredMemberships = await findExpiredMemberships();
|
||||||
const expiringMemberships = await findMembershipsExpiringIn2Days();
|
const expiringMemberships = await findMembershipsExpiringIn10Days();
|
||||||
|
|
||||||
if (expiredMemberships.length === 0 && expiringMemberships.length === 0) {
|
if (expiredMemberships.length === 0 && expiringMemberships.length === 0) {
|
||||||
logger.info("No expired or expiring memberships found.");
|
logger.info("No expired or expiring memberships found.");
|
||||||
@ -52,7 +52,7 @@ export const checkExpiredMemberships = onSchedule(
|
|||||||
}
|
}
|
||||||
|
|
||||||
logger.info(
|
logger.info(
|
||||||
`Found ${expiredMemberships.length} expired memberships and ${expiringMemberships.length} memberships expiring in 2 days to process.`
|
`Found ${expiredMemberships.length} expired memberships and ${expiringMemberships.length} memberships expiring in 10 days to process.`
|
||||||
);
|
);
|
||||||
|
|
||||||
const expiredResults = await Promise.allSettled(
|
const expiredResults = await Promise.allSettled(
|
||||||
@ -119,7 +119,7 @@ async function findExpiredMemberships(): Promise<
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
async function findMembershipsExpiringIn2Days(): Promise<
|
async function findMembershipsExpiringIn10Days(): Promise<
|
||||||
Array<{ id: string; data: MembershipData }>
|
Array<{ id: string; data: MembershipData }>
|
||||||
> {
|
> {
|
||||||
try {
|
try {
|
||||||
@ -139,8 +139,8 @@ async function findMembershipsExpiringIn2Days(): Promise<
|
|||||||
const batchResults = await Promise.allSettled(
|
const batchResults = await Promise.allSettled(
|
||||||
batch.map(async (doc) => {
|
batch.map(async (doc) => {
|
||||||
const data = doc.data() as MembershipData;
|
const data = doc.data() as MembershipData;
|
||||||
const isExpiringIn2Days = await checkIfMembershipExpiringIn2Days(doc.id, data);
|
const isExpiringIn10Days = await checkIfMembershipExpiringIn10Days(doc.id, data);
|
||||||
if (isExpiringIn2Days) {
|
if (isExpiringIn10Days) {
|
||||||
return { id: doc.id, data };
|
return { id: doc.id, data };
|
||||||
}
|
}
|
||||||
return null;
|
return null;
|
||||||
@ -156,7 +156,7 @@ async function findMembershipsExpiringIn2Days(): Promise<
|
|||||||
|
|
||||||
return expiring;
|
return expiring;
|
||||||
} catch (error) {
|
} catch (error) {
|
||||||
logger.error("Error finding memberships expiring in 2 days:", error);
|
logger.error("Error finding memberships expiring in 10 days:", error);
|
||||||
throw error;
|
throw error;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@ -214,7 +214,7 @@ async function checkIfMembershipExpired(
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
async function checkIfMembershipExpiringIn2Days(
|
async function checkIfMembershipExpiringIn10Days(
|
||||||
membershipId: string,
|
membershipId: string,
|
||||||
data: MembershipData
|
data: MembershipData
|
||||||
): Promise<boolean> {
|
): Promise<boolean> {
|
||||||
@ -243,21 +243,21 @@ async function checkIfMembershipExpiringIn2Days(
|
|||||||
);
|
);
|
||||||
|
|
||||||
const now = new Date();
|
const now = new Date();
|
||||||
const twoDaysFromNow = new Date();
|
const tenDaysFromNow = new Date();
|
||||||
twoDaysFromNow.setDate(now.getDate() + 2);
|
tenDaysFromNow.setDate(now.getDate() + 10);
|
||||||
|
|
||||||
const isExpiringIn2Days = expiryDate > now && expiryDate <= twoDaysFromNow;
|
const isExpiringIn10Days = expiryDate > now && expiryDate <= tenDaysFromNow;
|
||||||
|
|
||||||
if (isExpiringIn2Days) {
|
if (isExpiringIn10Days) {
|
||||||
logger.info(
|
logger.info(
|
||||||
`Membership ${membershipId} will expire on ${expiryDate.toISOString()} (within 2 days)`
|
`Membership ${membershipId} will expire on ${expiryDate.toISOString()} (within 10 days)`
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
return isExpiringIn2Days;
|
return isExpiringIn10Days;
|
||||||
} catch (error) {
|
} catch (error) {
|
||||||
logger.error(
|
logger.error(
|
||||||
`Error checking 2-day expiry for membership ${membershipId}:`,
|
`Error checking 10-day expiry for membership ${membershipId}:`,
|
||||||
error
|
error
|
||||||
);
|
);
|
||||||
return false;
|
return false;
|
||||||
@ -481,6 +481,7 @@ async function sendPlanExpiringNotification(
|
|||||||
|
|
||||||
let expiryDate: Date | undefined;
|
let expiryDate: Date | undefined;
|
||||||
let formattedDate = "Unknown Date";
|
let formattedDate = "Unknown Date";
|
||||||
|
let daysUntilExpiry = 10;
|
||||||
|
|
||||||
const payments = await getPaymentsForMembership(membershipId);
|
const payments = await getPaymentsForMembership(membershipId);
|
||||||
if (payments.length > 0) {
|
if (payments.length > 0) {
|
||||||
@ -494,6 +495,10 @@ async function sendPlanExpiringNotification(
|
|||||||
month: "long",
|
month: "long",
|
||||||
day: "numeric",
|
day: "numeric",
|
||||||
});
|
});
|
||||||
|
|
||||||
|
const now = new Date();
|
||||||
|
const timeDiff = expiryDate.getTime() - now.getTime();
|
||||||
|
daysUntilExpiry = Math.ceil(timeDiff / (1000 * 3600 * 24));
|
||||||
}
|
}
|
||||||
|
|
||||||
await app
|
await app
|
||||||
@ -517,12 +522,12 @@ async function sendPlanExpiringNotification(
|
|||||||
expiryDate: expiryDate
|
expiryDate: expiryDate
|
||||||
? admin.firestore.Timestamp.fromDate(expiryDate)
|
? admin.firestore.Timestamp.fromDate(expiryDate)
|
||||||
: admin.firestore.Timestamp.fromDate(new Date()),
|
: admin.firestore.Timestamp.fromDate(new Date()),
|
||||||
daysUntilExpiry: 2,
|
daysUntilExpiry: daysUntilExpiry,
|
||||||
},
|
},
|
||||||
});
|
});
|
||||||
|
|
||||||
logger.info(
|
logger.info(
|
||||||
`Expiring notification sent for membership ${membershipId} (expires on ${formattedDate})`
|
`Expiring notification sent for membership ${membershipId} (expires on ${formattedDate}, ${daysUntilExpiry} days remaining)`
|
||||||
);
|
);
|
||||||
} catch (error) {
|
} catch (error) {
|
||||||
logger.error(`Error sending expiring notification for ${membershipId}:`, error);
|
logger.error(`Error sending expiring notification for ${membershipId}:`, error);
|
||||||
|
|||||||
@ -240,14 +240,14 @@ function prepareNotificationMessage(
|
|||||||
title = notification.data?.title || "Plan Expired";
|
title = notification.data?.title || "Plan Expired";
|
||||||
body =
|
body =
|
||||||
notification.data?.message ||
|
notification.data?.message ||
|
||||||
`${notification.data?.clientName}'s subscription for plan ${notification.data?.planName} has expired.`;
|
`${notification.data?.clientName}'s membership subscription for ${notification.data?.planName} has expired.`;
|
||||||
break;
|
break;
|
||||||
|
|
||||||
case "plan_expiring_soon":
|
case "plan_expiring_soon":
|
||||||
title = notification.data?.title || "Plan Expiring Soon";
|
title = notification.data?.title || "Plan Expiring Soon";
|
||||||
body =
|
body =
|
||||||
notification.data?.message ||
|
notification.data?.message ||
|
||||||
`${notification.data?.clientName}'s subscription for plan ${notification.data?.planName} will expire on ${notification.data?.formattedExpiryDate}.`;
|
`${notification.data?.clientName}'s membership subscription for ${notification.data?.planName} will expire on ${notification.data?.formattedExpiryDate}.`;
|
||||||
break;
|
break;
|
||||||
|
|
||||||
case "schedule_update":
|
case "schedule_update":
|
||||||
|
|||||||
Loading…
Reference in New Issue
Block a user