Changes Updated
This commit is contained in:
parent
4cf5692386
commit
4edd0c69de
@ -63,10 +63,18 @@ export const checkExpiredMemberships = onSchedule(
|
|||||||
expiringMemberships.map((m) => processExpiringMembership(m.id, m.data))
|
expiringMemberships.map((m) => processExpiringMembership(m.id, m.data))
|
||||||
);
|
);
|
||||||
|
|
||||||
const expiredSuccessful = expiredResults.filter((r) => r.status === "fulfilled").length;
|
const expiredSuccessful = expiredResults.filter(
|
||||||
const expiredFailed = expiredResults.filter((r) => r.status === "rejected").length;
|
(r) => r.status === "fulfilled"
|
||||||
const expiringSuccessful = expiringResults.filter((r) => r.status === "fulfilled").length;
|
).length;
|
||||||
const expiringFailed = expiringResults.filter((r) => r.status === "rejected").length;
|
const expiredFailed = expiredResults.filter(
|
||||||
|
(r) => r.status === "rejected"
|
||||||
|
).length;
|
||||||
|
const expiringSuccessful = expiringResults.filter(
|
||||||
|
(r) => r.status === "fulfilled"
|
||||||
|
).length;
|
||||||
|
const expiringFailed = expiringResults.filter(
|
||||||
|
(r) => r.status === "rejected"
|
||||||
|
).length;
|
||||||
|
|
||||||
logger.info(
|
logger.info(
|
||||||
`Completed processing. Expired - Success: ${expiredSuccessful}, Failed: ${expiredFailed}. Expiring - Success: ${expiringSuccessful}, Failed: ${expiringFailed}`
|
`Completed processing. Expired - Success: ${expiredSuccessful}, Failed: ${expiredFailed}. Expiring - Success: ${expiringSuccessful}, Failed: ${expiringFailed}`
|
||||||
@ -139,7 +147,10 @@ async function findMembershipsExpiringIn10Days(): 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 isExpiringIn10Days = await checkIfMembershipExpiringIn10Days(doc.id, data);
|
const isExpiringIn10Days = await checkIfMembershipExpiringIn10Days(
|
||||||
|
doc.id,
|
||||||
|
data
|
||||||
|
);
|
||||||
if (isExpiringIn10Days) {
|
if (isExpiringIn10Days) {
|
||||||
return { id: doc.id, data };
|
return { id: doc.id, data };
|
||||||
}
|
}
|
||||||
@ -241,11 +252,11 @@ async function checkIfMembershipExpiringIn10Days(
|
|||||||
startDate,
|
startDate,
|
||||||
data.subscription.frequency
|
data.subscription.frequency
|
||||||
);
|
);
|
||||||
|
|
||||||
const now = new Date();
|
const now = new Date();
|
||||||
const tenDaysFromNow = new Date();
|
const tenDaysFromNow = new Date();
|
||||||
tenDaysFromNow.setDate(now.getDate() + 10);
|
tenDaysFromNow.setDate(now.getDate() + 10);
|
||||||
|
|
||||||
const isExpiringIn10Days = expiryDate > now && expiryDate <= tenDaysFromNow;
|
const isExpiringIn10Days = expiryDate > now && expiryDate <= tenDaysFromNow;
|
||||||
|
|
||||||
if (isExpiringIn10Days) {
|
if (isExpiringIn10Days) {
|
||||||
@ -381,7 +392,10 @@ async function processExpiringMembership(
|
|||||||
logger.info(`Processing expiring membership ${membershipId}`);
|
logger.info(`Processing expiring membership ${membershipId}`);
|
||||||
await sendPlanExpiringNotification(membershipId, membershipData);
|
await sendPlanExpiringNotification(membershipId, membershipData);
|
||||||
} catch (error) {
|
} catch (error) {
|
||||||
logger.error(`Error processing expiring membership ${membershipId}:`, error);
|
logger.error(
|
||||||
|
`Error processing expiring membership ${membershipId}:`,
|
||||||
|
error
|
||||||
|
);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -394,19 +408,6 @@ async function sendPlanExpiredNotification(
|
|||||||
const gymOwnerId = await getGymOwnerId(membershipData.gymId);
|
const gymOwnerId = await getGymOwnerId(membershipData.gymId);
|
||||||
const gymName = await getGymName(membershipData.gymId);
|
const gymName = await getGymName(membershipData.gymId);
|
||||||
|
|
||||||
const existing = await app
|
|
||||||
.firestore()
|
|
||||||
.collection("notifications")
|
|
||||||
.where("type", "==", "plan_expired")
|
|
||||||
.where("data.membershipId", "==", membershipId)
|
|
||||||
.limit(1)
|
|
||||||
.get();
|
|
||||||
|
|
||||||
if (!existing.empty) {
|
|
||||||
logger.info(`Notification already sent for ${membershipId}, skipping...`);
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
|
|
||||||
let expiryDate: Date | undefined;
|
let expiryDate: Date | undefined;
|
||||||
let formattedDate = "Unknown Date";
|
let formattedDate = "Unknown Date";
|
||||||
|
|
||||||
@ -422,7 +423,26 @@ async function sendPlanExpiredNotification(
|
|||||||
month: "long",
|
month: "long",
|
||||||
day: "numeric",
|
day: "numeric",
|
||||||
});
|
});
|
||||||
|
}
|
||||||
|
|
||||||
|
const existing = await app
|
||||||
|
.firestore()
|
||||||
|
.collection("notifications")
|
||||||
|
.where("type", "==", "plan_expired")
|
||||||
|
.where("data.membershipId", "==", membershipId)
|
||||||
|
.where(
|
||||||
|
"data.expiryDate",
|
||||||
|
"==",
|
||||||
|
expiryDate
|
||||||
|
? admin.firestore.Timestamp.fromDate(expiryDate)
|
||||||
|
: admin.firestore.Timestamp.fromDate(new Date())
|
||||||
|
)
|
||||||
|
.limit(1)
|
||||||
|
.get();
|
||||||
|
|
||||||
|
if (!existing.empty) {
|
||||||
|
logger.info(`Notification already sent for ${membershipId}, skipping...`);
|
||||||
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
await app
|
await app
|
||||||
@ -435,13 +455,13 @@ async function sendPlanExpiredNotification(
|
|||||||
notificationSent: false,
|
notificationSent: false,
|
||||||
timestamp: admin.firestore.FieldValue.serverTimestamp(),
|
timestamp: admin.firestore.FieldValue.serverTimestamp(),
|
||||||
read: false,
|
read: false,
|
||||||
readBy: [],
|
readBy: [],
|
||||||
data: {
|
data: {
|
||||||
planName: membershipData.subscription?.name || "Unknown Plan",
|
planName: membershipData.subscription?.name || "Unknown Plan",
|
||||||
clientName,
|
clientName,
|
||||||
membershipId,
|
membershipId,
|
||||||
gymName,
|
gymName,
|
||||||
ownerId: gymOwnerId,
|
ownerId: gymOwnerId,
|
||||||
formattedExpiryDate: formattedDate,
|
formattedExpiryDate: formattedDate,
|
||||||
expiryDate: expiryDate
|
expiryDate: expiryDate
|
||||||
? admin.firestore.Timestamp.fromDate(expiryDate)
|
? admin.firestore.Timestamp.fromDate(expiryDate)
|
||||||
@ -466,19 +486,6 @@ async function sendPlanExpiringNotification(
|
|||||||
const gymOwnerId = await getGymOwnerId(membershipData.gymId);
|
const gymOwnerId = await getGymOwnerId(membershipData.gymId);
|
||||||
const gymName = await getGymName(membershipData.gymId);
|
const gymName = await getGymName(membershipData.gymId);
|
||||||
|
|
||||||
const existing = await app
|
|
||||||
.firestore()
|
|
||||||
.collection("notifications")
|
|
||||||
.where("type", "==", "plan_expiring_soon")
|
|
||||||
.where("data.membershipId", "==", membershipId)
|
|
||||||
.limit(1)
|
|
||||||
.get();
|
|
||||||
|
|
||||||
if (!existing.empty) {
|
|
||||||
logger.info(`Expiring notification already sent for ${membershipId}, skipping...`);
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
|
|
||||||
let expiryDate: Date | undefined;
|
let expiryDate: Date | undefined;
|
||||||
let formattedDate = "Unknown Date";
|
let formattedDate = "Unknown Date";
|
||||||
let daysUntilExpiry = 10;
|
let daysUntilExpiry = 10;
|
||||||
@ -495,12 +502,34 @@ async function sendPlanExpiringNotification(
|
|||||||
month: "long",
|
month: "long",
|
||||||
day: "numeric",
|
day: "numeric",
|
||||||
});
|
});
|
||||||
|
|
||||||
const now = new Date();
|
const now = new Date();
|
||||||
const timeDiff = expiryDate.getTime() - now.getTime();
|
const timeDiff = expiryDate.getTime() - now.getTime();
|
||||||
daysUntilExpiry = Math.ceil(timeDiff / (1000 * 3600 * 24));
|
daysUntilExpiry = Math.ceil(timeDiff / (1000 * 3600 * 24));
|
||||||
}
|
}
|
||||||
|
|
||||||
|
const existing = await app
|
||||||
|
.firestore()
|
||||||
|
.collection("notifications")
|
||||||
|
.where("type", "==", "plan_expiring_soon")
|
||||||
|
.where("data.membershipId", "==", membershipId)
|
||||||
|
.where(
|
||||||
|
"data.expiryDate",
|
||||||
|
"==",
|
||||||
|
expiryDate
|
||||||
|
? admin.firestore.Timestamp.fromDate(expiryDate)
|
||||||
|
: admin.firestore.Timestamp.fromDate(new Date())
|
||||||
|
)
|
||||||
|
.limit(1)
|
||||||
|
.get();
|
||||||
|
|
||||||
|
if (!existing.empty) {
|
||||||
|
logger.info(
|
||||||
|
`Expiring notification already sent for ${membershipId}, skipping...`
|
||||||
|
);
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
await app
|
await app
|
||||||
.firestore()
|
.firestore()
|
||||||
.collection("notifications")
|
.collection("notifications")
|
||||||
@ -511,13 +540,13 @@ async function sendPlanExpiringNotification(
|
|||||||
notificationSent: false,
|
notificationSent: false,
|
||||||
timestamp: admin.firestore.FieldValue.serverTimestamp(),
|
timestamp: admin.firestore.FieldValue.serverTimestamp(),
|
||||||
read: false,
|
read: false,
|
||||||
readBy: [],
|
readBy: [],
|
||||||
data: {
|
data: {
|
||||||
planName: membershipData.subscription?.name || "Unknown Plan",
|
planName: membershipData.subscription?.name || "Unknown Plan",
|
||||||
clientName,
|
clientName,
|
||||||
membershipId,
|
membershipId,
|
||||||
gymName,
|
gymName,
|
||||||
ownerId: gymOwnerId,
|
ownerId: gymOwnerId,
|
||||||
formattedExpiryDate: formattedDate,
|
formattedExpiryDate: formattedDate,
|
||||||
expiryDate: expiryDate
|
expiryDate: expiryDate
|
||||||
? admin.firestore.Timestamp.fromDate(expiryDate)
|
? admin.firestore.Timestamp.fromDate(expiryDate)
|
||||||
@ -530,7 +559,10 @@ async function sendPlanExpiringNotification(
|
|||||||
`Expiring notification sent for membership ${membershipId} (expires on ${formattedDate}, ${daysUntilExpiry} days remaining)`
|
`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
|
||||||
|
);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -577,4 +609,4 @@ async function getGymName(gymId: string): Promise<string> {
|
|||||||
logger.error(`Error getting gym name for gym ${gymId}:`, error);
|
logger.error(`Error getting gym name for gym ${gymId}:`, error);
|
||||||
return "Unknown Gym";
|
return "Unknown Gym";
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
Loading…
Reference in New Issue
Block a user