feature/fitlien-828 (#121)
Some checks failed
Deploy FitLien services to Dev / Deploy to Dev (push) Failing after 42s
Some checks failed
Deploy FitLien services to Dev / Deploy to Dev (push) Failing after 42s
Reviewed-on: #121 Co-authored-by: Sharon Dcruz <sharondcruz@cosq.net> Co-committed-by: Sharon Dcruz <sharondcruz@cosq.net>
This commit is contained in:
parent
b6d222f0c3
commit
a8cb7dce8c
@ -18,6 +18,8 @@ interface MembershipData {
|
|||||||
};
|
};
|
||||||
isPartialPayment?: boolean;
|
isPartialPayment?: boolean;
|
||||||
remainingAmount?: number;
|
remainingAmount?: number;
|
||||||
|
daysUntilExpiry?: number;
|
||||||
|
expirationDate?: admin.firestore.Timestamp;
|
||||||
createdAt?: admin.firestore.Timestamp;
|
createdAt?: admin.firestore.Timestamp;
|
||||||
updatedAt?: admin.firestore.Timestamp;
|
updatedAt?: admin.firestore.Timestamp;
|
||||||
[key: string]: any;
|
[key: string]: any;
|
||||||
@ -42,6 +44,7 @@ interface MinimalCacheEntry {
|
|||||||
phoneNumber?: string | null;
|
phoneNumber?: string | null;
|
||||||
alternateContact?: string | null;
|
alternateContact?: string | null;
|
||||||
renewalDate?: string | null;
|
renewalDate?: string | null;
|
||||||
|
expirationDate?: string | null;
|
||||||
createdAt?: string | null;
|
createdAt?: string | null;
|
||||||
daysUntilExpiry?: number | null;
|
daysUntilExpiry?: number | null;
|
||||||
hasPersonalTraining: boolean;
|
hasPersonalTraining: boolean;
|
||||||
@ -389,44 +392,10 @@ async function generateMinimalCacheEntry(
|
|||||||
logger.error(`Error getting fields for user ${userId}:`, error);
|
logger.error(`Error getting fields for user ${userId}:`, error);
|
||||||
}
|
}
|
||||||
|
|
||||||
let renewalDate: Date | null = null;
|
const daysUntilExpiry = membershipData.daysUntilExpiry || null;
|
||||||
let daysUntilExpiry: number | null = null;
|
|
||||||
|
|
||||||
if (membershipData.subscription) {
|
const displayName = firstName.length === 0 ? 'Unknown' :
|
||||||
try {
|
(lastName.length === 0 ? firstName : `${firstName} ${lastName}`);
|
||||||
const paymentsSnapshot = await app
|
|
||||||
.firestore()
|
|
||||||
.collection("membership_payments")
|
|
||||||
.where("membershipId", "==", membershipId)
|
|
||||||
.orderBy("dateTimestamp", "desc")
|
|
||||||
.limit(1)
|
|
||||||
.get();
|
|
||||||
|
|
||||||
if (!paymentsSnapshot.empty) {
|
|
||||||
const latestPayment = paymentsSnapshot.docs[0].data() as PaymentData;
|
|
||||||
if (latestPayment.dateTimestamp) {
|
|
||||||
const paymentDate = latestPayment.dateTimestamp.toDate();
|
|
||||||
renewalDate = calculateRenewalDateTimeFromPayment(
|
|
||||||
membershipData.subscription,
|
|
||||||
paymentDate
|
|
||||||
);
|
|
||||||
|
|
||||||
if (renewalDate && membershipData.status === "ACTIVE") {
|
|
||||||
const now = new Date();
|
|
||||||
const difference = Math.floor(
|
|
||||||
(renewalDate.getTime() - now.getTime()) / (1000 * 60 * 60 * 24)
|
|
||||||
);
|
|
||||||
daysUntilExpiry = difference > 0 ? difference : null;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
} catch (error) {
|
|
||||||
logger.error(
|
|
||||||
`Error getting renewal date for membership ${membershipId}:`,
|
|
||||||
error
|
|
||||||
);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
const displayName = firstName.length === 0 ? 'Unknown' :
|
const displayName = firstName.length === 0 ? 'Unknown' :
|
||||||
(lastName.length === 0 ? firstName : `${firstName} ${lastName}`);
|
(lastName.length === 0 ? firstName : `${firstName} ${lastName}`);
|
||||||
@ -443,7 +412,10 @@ async function generateMinimalCacheEntry(
|
|||||||
email: email || null,
|
email: email || null,
|
||||||
phoneNumber: phoneNumber || null,
|
phoneNumber: phoneNumber || null,
|
||||||
alternateContact: alternateContact || null,
|
alternateContact: alternateContact || null,
|
||||||
renewalDate: renewalDate ? renewalDate.toISOString() : null,
|
renewalDate: null,
|
||||||
|
expirationDate: membershipData.expirationDate
|
||||||
|
? membershipData.expirationDate.toDate().toISOString()
|
||||||
|
: null,
|
||||||
createdAt: membershipData.createdAt
|
createdAt: membershipData.createdAt
|
||||||
? membershipData.createdAt.toDate().toISOString()
|
? membershipData.createdAt.toDate().toISOString()
|
||||||
: null,
|
: null,
|
||||||
@ -461,38 +433,3 @@ async function generateMinimalCacheEntry(
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
function calculateRenewalDateTimeFromPayment(
|
|
||||||
subscription: { frequency?: string },
|
|
||||||
paymentDate: Date
|
|
||||||
): Date {
|
|
||||||
const frequency = subscription.frequency || "Monthly";
|
|
||||||
|
|
||||||
switch (frequency) {
|
|
||||||
case "Monthly":
|
|
||||||
return new Date(
|
|
||||||
paymentDate.getFullYear(),
|
|
||||||
paymentDate.getMonth() + 1,
|
|
||||||
paymentDate.getDate()
|
|
||||||
);
|
|
||||||
case "Quarterly":
|
|
||||||
return new Date(
|
|
||||||
paymentDate.getFullYear(),
|
|
||||||
paymentDate.getMonth() + 3,
|
|
||||||
paymentDate.getDate()
|
|
||||||
);
|
|
||||||
case "Half-yearly":
|
|
||||||
return new Date(
|
|
||||||
paymentDate.getFullYear(),
|
|
||||||
paymentDate.getMonth() + 6,
|
|
||||||
paymentDate.getDate()
|
|
||||||
);
|
|
||||||
case "Yearly":
|
|
||||||
return new Date(
|
|
||||||
paymentDate.getFullYear() + 1,
|
|
||||||
paymentDate.getMonth(),
|
|
||||||
paymentDate.getDate()
|
|
||||||
);
|
|
||||||
default:
|
|
||||||
return new Date(Date.now() + 30 * 24 * 60 * 60 * 1000);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
Loading…
Reference in New Issue
Block a user