INNER CODE UNIT · JavaScript
handleRenewal
adeekshith/watomatic · functions/index.js:1216
async function handleRenewal(userId, subscription, eventTimeMillis) {
logger.info('Subscription renewed', { userId });
const lineItem = subscription.lineItems?.[0];
const expiryTime = lineItem?.expiryTime ? new Date(lineItem.expiryTime).getTime() : 0;
// CRITICAL FIX #3: Store renewal as a separate document keyed by expiryTime
// This ensures idempotency - same renewal processed multiple times = same document
const renewalKey = `renewal_${expiryTime}`;
const renewalRef = admin.firestore()
.collection('users')
.doc(userId)
.collection('renewal_events')
.doc(renewalKey);
// Check if this renewal was already recorded
const existingRenewal = await renewalRef.get();
if (existingRenewal.exists) {