Closer/functions/dist/dates/onDateReflectionRevealed.js

99 lines
5.2 KiB
JavaScript
Raw Normal View History

"use strict";
var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
if (k2 === undefined) k2 = k;
var desc = Object.getOwnPropertyDescriptor(m, k);
if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
desc = { enumerable: true, get: function() { return m[k]; } };
}
Object.defineProperty(o, k2, desc);
}) : (function(o, m, k, k2) {
if (k2 === undefined) k2 = k;
o[k2] = m[k];
}));
var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) {
Object.defineProperty(o, "default", { enumerable: true, value: v });
}) : function(o, v) {
o["default"] = v;
});
var __importStar = (this && this.__importStar) || (function () {
var ownKeys = function(o) {
ownKeys = Object.getOwnPropertyNames || function (o) {
var ar = [];
for (var k in o) if (Object.prototype.hasOwnProperty.call(o, k)) ar[ar.length] = k;
return ar;
};
return ownKeys(o);
};
return function (mod) {
if (mod && mod.__esModule) return mod;
var result = {};
if (mod != null) for (var k = ownKeys(mod), i = 0; i < k.length; i++) if (k[i] !== "default") __createBinding(result, mod, k[i]);
__setModuleDefault(result, mod);
return result;
};
})();
Object.defineProperty(exports, "__esModule", { value: true });
exports.onDateReflectionRevealed = void 0;
const admin = __importStar(require("firebase-admin"));
refactor(functions): B1 migrate 13 Firestore triggers to v2 + harden Migrate all Firestore triggers off the v1 API to firebase-functions/v2/firestore (onDocumentCreated/Updated/Written); context.params→event.params, snap→event.data, change.before/after→event.data.before/after. Region stays us-central1 (global option). Hardening folded in (all reuse in-repo patterns): - Adopt the shared sendPushToUser()/getUserTokens() helper in every trigger, removing ~7 copied token readers and the copied send/prune blocks. FCM tokens are no longer logged in plaintext anywhere here (redacted inside push.ts). - console.* → firebase-functions/logger (structured). - Idempotency: new claimOnce() (atomic create-if-absent marker under couples/{id}/notif_marks) dedupes at-least-once redelivery on the non-idempotent senders (onAnswerWritten/Revealed, onMessageWritten, onCoupleLeave, onEntitlementChanged, onDateReflectionWritten/Revealed, onDateHistoryCreated). Fail-open. onGameSessionUpdate/ onGamePartFinished/notifyOnDateMatch already had transactional claim-flags — preserved. - onRestoreRequested: the plan's "claim" is implemented as the existing 60s time-WINDOW (lastRestorePartnerAlertAt), not a permanent recipientUid marker — a permanent marker would wrongly block legitimate re-requests (restore docs are deleted+recreated by design). Faithful port of onGameSessionUpdate/onGamePartFinished (broad wildcard + allowlist kept); the trigger split and read reorder are deferred to B6 as separate commits. Build clean; 70 tests green (+ new idempotency.test.ts, push token/prune tests). Emulator discovery loads all triggers as v2 in us-central1. firebase-functions still v5.1.1 (v6 bump deferred to B6). dist rebuilt. Co-Authored-By: Claude Opus 4.8 <[email protected]>
2026-07-07 23:46:17 -05:00
const firestore_1 = require("firebase-functions/v2/firestore");
const quietHours_1 = require("../notifications/quietHours");
refactor(functions): B1 migrate 13 Firestore triggers to v2 + harden Migrate all Firestore triggers off the v1 API to firebase-functions/v2/firestore (onDocumentCreated/Updated/Written); context.params→event.params, snap→event.data, change.before/after→event.data.before/after. Region stays us-central1 (global option). Hardening folded in (all reuse in-repo patterns): - Adopt the shared sendPushToUser()/getUserTokens() helper in every trigger, removing ~7 copied token readers and the copied send/prune blocks. FCM tokens are no longer logged in plaintext anywhere here (redacted inside push.ts). - console.* → firebase-functions/logger (structured). - Idempotency: new claimOnce() (atomic create-if-absent marker under couples/{id}/notif_marks) dedupes at-least-once redelivery on the non-idempotent senders (onAnswerWritten/Revealed, onMessageWritten, onCoupleLeave, onEntitlementChanged, onDateReflectionWritten/Revealed, onDateHistoryCreated). Fail-open. onGameSessionUpdate/ onGamePartFinished/notifyOnDateMatch already had transactional claim-flags — preserved. - onRestoreRequested: the plan's "claim" is implemented as the existing 60s time-WINDOW (lastRestorePartnerAlertAt), not a permanent recipientUid marker — a permanent marker would wrongly block legitimate re-requests (restore docs are deleted+recreated by design). Faithful port of onGameSessionUpdate/onGamePartFinished (broad wildcard + allowlist kept); the trigger split and read reorder are deferred to B6 as separate commits. Build clean; 70 tests green (+ new idempotency.test.ts, push token/prune tests). Emulator discovery loads all triggers as v2 in us-central1. firebase-functions still v5.1.1 (v6 bump deferred to B6). dist rebuilt. Co-Authored-By: Claude Opus 4.8 <[email protected]>
2026-07-07 23:46:17 -05:00
const push_1 = require("../notifications/push");
const idempotency_1 = require("../notifications/idempotency");
const log_1 = require("../log");
/**
* Fires when a partner OPENS (reveals) the shared date reflections their own reflection metadata doc
* flips `isRevealed` false true (`couples/{coupleId}/date_reflections/{dateId}/answers/{userId}`).
* Notifies the OTHER partner that their reflection was read, mirroring daily's `onAnswerRevealed`:
* generic copy (no decrypted content, no name the app renders the real name in-app), gated on
* `notifPartnerAnswered` + quiet hours (push suppressed in quiet hours, but the in-app record is kept).
*/
refactor(functions): B1 migrate 13 Firestore triggers to v2 + harden Migrate all Firestore triggers off the v1 API to firebase-functions/v2/firestore (onDocumentCreated/Updated/Written); context.params→event.params, snap→event.data, change.before/after→event.data.before/after. Region stays us-central1 (global option). Hardening folded in (all reuse in-repo patterns): - Adopt the shared sendPushToUser()/getUserTokens() helper in every trigger, removing ~7 copied token readers and the copied send/prune blocks. FCM tokens are no longer logged in plaintext anywhere here (redacted inside push.ts). - console.* → firebase-functions/logger (structured). - Idempotency: new claimOnce() (atomic create-if-absent marker under couples/{id}/notif_marks) dedupes at-least-once redelivery on the non-idempotent senders (onAnswerWritten/Revealed, onMessageWritten, onCoupleLeave, onEntitlementChanged, onDateReflectionWritten/Revealed, onDateHistoryCreated). Fail-open. onGameSessionUpdate/ onGamePartFinished/notifyOnDateMatch already had transactional claim-flags — preserved. - onRestoreRequested: the plan's "claim" is implemented as the existing 60s time-WINDOW (lastRestorePartnerAlertAt), not a permanent recipientUid marker — a permanent marker would wrongly block legitimate re-requests (restore docs are deleted+recreated by design). Faithful port of onGameSessionUpdate/onGamePartFinished (broad wildcard + allowlist kept); the trigger split and read reorder are deferred to B6 as separate commits. Build clean; 70 tests green (+ new idempotency.test.ts, push token/prune tests). Emulator discovery loads all triggers as v2 in us-central1. firebase-functions still v5.1.1 (v6 bump deferred to B6). dist rebuilt. Co-Authored-By: Claude Opus 4.8 <[email protected]>
2026-07-07 23:46:17 -05:00
exports.onDateReflectionRevealed = (0, firestore_1.onDocumentUpdated)('couples/{coupleId}/date_reflections/{dateId}/answers/{userId}', async (event) => {
var _a, _b, _c, _d, _e, _f, _g;
const { coupleId, dateId, userId } = event.params;
const before = ((_b = (_a = event.data) === null || _a === void 0 ? void 0 : _a.before.data()) !== null && _b !== void 0 ? _b : {});
const after = ((_d = (_c = event.data) === null || _c === void 0 ? void 0 : _c.after.data()) !== null && _d !== void 0 ? _d : {});
// Only on the false → true reveal transition.
if (before.isRevealed === true || after.isRevealed !== true)
return;
const db = admin.firestore();
const coupleDoc = await db.collection('couples').doc(coupleId).get();
if (!coupleDoc.exists)
return;
refactor(functions): B1 migrate 13 Firestore triggers to v2 + harden Migrate all Firestore triggers off the v1 API to firebase-functions/v2/firestore (onDocumentCreated/Updated/Written); context.params→event.params, snap→event.data, change.before/after→event.data.before/after. Region stays us-central1 (global option). Hardening folded in (all reuse in-repo patterns): - Adopt the shared sendPushToUser()/getUserTokens() helper in every trigger, removing ~7 copied token readers and the copied send/prune blocks. FCM tokens are no longer logged in plaintext anywhere here (redacted inside push.ts). - console.* → firebase-functions/logger (structured). - Idempotency: new claimOnce() (atomic create-if-absent marker under couples/{id}/notif_marks) dedupes at-least-once redelivery on the non-idempotent senders (onAnswerWritten/Revealed, onMessageWritten, onCoupleLeave, onEntitlementChanged, onDateReflectionWritten/Revealed, onDateHistoryCreated). Fail-open. onGameSessionUpdate/ onGamePartFinished/notifyOnDateMatch already had transactional claim-flags — preserved. - onRestoreRequested: the plan's "claim" is implemented as the existing 60s time-WINDOW (lastRestorePartnerAlertAt), not a permanent recipientUid marker — a permanent marker would wrongly block legitimate re-requests (restore docs are deleted+recreated by design). Faithful port of onGameSessionUpdate/onGamePartFinished (broad wildcard + allowlist kept); the trigger split and read reorder are deferred to B6 as separate commits. Build clean; 70 tests green (+ new idempotency.test.ts, push token/prune tests). Emulator discovery loads all triggers as v2 in us-central1. firebase-functions still v5.1.1 (v6 bump deferred to B6). dist rebuilt. Co-Authored-By: Claude Opus 4.8 <[email protected]>
2026-07-07 23:46:17 -05:00
const userIds = ((_f = (_e = coupleDoc.data()) === null || _e === void 0 ? void 0 : _e.userIds) !== null && _f !== void 0 ? _f : []);
if (!userIds.includes(userId))
return;
const partnerId = userIds.find((u) => u !== userId);
if (!partnerId)
return;
const partnerUserDoc = await db.collection('users').doc(partnerId).get();
refactor(functions): B1 migrate 13 Firestore triggers to v2 + harden Migrate all Firestore triggers off the v1 API to firebase-functions/v2/firestore (onDocumentCreated/Updated/Written); context.params→event.params, snap→event.data, change.before/after→event.data.before/after. Region stays us-central1 (global option). Hardening folded in (all reuse in-repo patterns): - Adopt the shared sendPushToUser()/getUserTokens() helper in every trigger, removing ~7 copied token readers and the copied send/prune blocks. FCM tokens are no longer logged in plaintext anywhere here (redacted inside push.ts). - console.* → firebase-functions/logger (structured). - Idempotency: new claimOnce() (atomic create-if-absent marker under couples/{id}/notif_marks) dedupes at-least-once redelivery on the non-idempotent senders (onAnswerWritten/Revealed, onMessageWritten, onCoupleLeave, onEntitlementChanged, onDateReflectionWritten/Revealed, onDateHistoryCreated). Fail-open. onGameSessionUpdate/ onGamePartFinished/notifyOnDateMatch already had transactional claim-flags — preserved. - onRestoreRequested: the plan's "claim" is implemented as the existing 60s time-WINDOW (lastRestorePartnerAlertAt), not a permanent recipientUid marker — a permanent marker would wrongly block legitimate re-requests (restore docs are deleted+recreated by design). Faithful port of onGameSessionUpdate/onGamePartFinished (broad wildcard + allowlist kept); the trigger split and read reorder are deferred to B6 as separate commits. Build clean; 70 tests green (+ new idempotency.test.ts, push token/prune tests). Emulator discovery loads all triggers as v2 in us-central1. firebase-functions still v5.1.1 (v6 bump deferred to B6). dist rebuilt. Co-Authored-By: Claude Opus 4.8 <[email protected]>
2026-07-07 23:46:17 -05:00
const partnerData = partnerUserDoc.data();
if ((partnerData === null || partnerData === void 0 ? void 0 : partnerData.notifPartnerAnswered) === false) {
log_1.logger.log(`[onDateReflectionRevealed] ${partnerId} has partner notifications off`);
return;
}
// Dedupe redelivery (at-least-once) before writing the in-app record or sending.
if (!(await (0, idempotency_1.claimOnce)((0, idempotency_1.notifMark)(db, coupleId, `date-reveal-${dateId}-${userId}`)))) {
log_1.logger.log(`[onDateReflectionRevealed] already notified for ${userId} on ${dateId}; skipping`);
return;
}
const title = 'Your partner opened your reflection ✨';
const body = 'Open to see what you each wrote.';
const type = 'date_reflection_opened';
// In-app record (→ Together feed) — written regardless of quiet hours.
await db.collection('users').doc(partnerId).collection('notification_queue').add({
type, title, body, read: false, createdAt: admin.firestore.FieldValue.serverTimestamp(),
});
refactor(functions): B1 migrate 13 Firestore triggers to v2 + harden Migrate all Firestore triggers off the v1 API to firebase-functions/v2/firestore (onDocumentCreated/Updated/Written); context.params→event.params, snap→event.data, change.before/after→event.data.before/after. Region stays us-central1 (global option). Hardening folded in (all reuse in-repo patterns): - Adopt the shared sendPushToUser()/getUserTokens() helper in every trigger, removing ~7 copied token readers and the copied send/prune blocks. FCM tokens are no longer logged in plaintext anywhere here (redacted inside push.ts). - console.* → firebase-functions/logger (structured). - Idempotency: new claimOnce() (atomic create-if-absent marker under couples/{id}/notif_marks) dedupes at-least-once redelivery on the non-idempotent senders (onAnswerWritten/Revealed, onMessageWritten, onCoupleLeave, onEntitlementChanged, onDateReflectionWritten/Revealed, onDateHistoryCreated). Fail-open. onGameSessionUpdate/ onGamePartFinished/notifyOnDateMatch already had transactional claim-flags — preserved. - onRestoreRequested: the plan's "claim" is implemented as the existing 60s time-WINDOW (lastRestorePartnerAlertAt), not a permanent recipientUid marker — a permanent marker would wrongly block legitimate re-requests (restore docs are deleted+recreated by design). Faithful port of onGameSessionUpdate/onGamePartFinished (broad wildcard + allowlist kept); the trigger split and read reorder are deferred to B6 as separate commits. Build clean; 70 tests green (+ new idempotency.test.ts, push token/prune tests). Emulator discovery loads all triggers as v2 in us-central1. firebase-functions still v5.1.1 (v6 bump deferred to B6). dist rebuilt. Co-Authored-By: Claude Opus 4.8 <[email protected]>
2026-07-07 23:46:17 -05:00
if ((0, quietHours_1.recipientInQuietHours)(partnerData)) {
log_1.logger.log(`[onDateReflectionRevealed] ${partnerId} in quiet hours — push suppressed (in-app kept)`);
return;
}
refactor(functions): B1 migrate 13 Firestore triggers to v2 + harden Migrate all Firestore triggers off the v1 API to firebase-functions/v2/firestore (onDocumentCreated/Updated/Written); context.params→event.params, snap→event.data, change.before/after→event.data.before/after. Region stays us-central1 (global option). Hardening folded in (all reuse in-repo patterns): - Adopt the shared sendPushToUser()/getUserTokens() helper in every trigger, removing ~7 copied token readers and the copied send/prune blocks. FCM tokens are no longer logged in plaintext anywhere here (redacted inside push.ts). - console.* → firebase-functions/logger (structured). - Idempotency: new claimOnce() (atomic create-if-absent marker under couples/{id}/notif_marks) dedupes at-least-once redelivery on the non-idempotent senders (onAnswerWritten/Revealed, onMessageWritten, onCoupleLeave, onEntitlementChanged, onDateReflectionWritten/Revealed, onDateHistoryCreated). Fail-open. onGameSessionUpdate/ onGamePartFinished/notifyOnDateMatch already had transactional claim-flags — preserved. - onRestoreRequested: the plan's "claim" is implemented as the existing 60s time-WINDOW (lastRestorePartnerAlertAt), not a permanent recipientUid marker — a permanent marker would wrongly block legitimate re-requests (restore docs are deleted+recreated by design). Faithful port of onGameSessionUpdate/onGamePartFinished (broad wildcard + allowlist kept); the trigger split and read reorder are deferred to B6 as separate commits. Build clean; 70 tests green (+ new idempotency.test.ts, push token/prune tests). Emulator discovery loads all triggers as v2 in us-central1. firebase-functions still v5.1.1 (v6 bump deferred to B6). dist rebuilt. Co-Authored-By: Claude Opus 4.8 <[email protected]>
2026-07-07 23:46:17 -05:00
const senderAvatar = (_g = (await db.collection('users').doc(userId).get()).data()) === null || _g === void 0 ? void 0 : _g.photoUrl;
await (0, push_1.sendPushToUser)(db, admin.messaging(), partnerId, {
notification: { title, body },
data: Object.assign({ type, couple_id: coupleId, date_id: dateId }, (typeof senderAvatar === 'string' && senderAvatar.length > 0
? { sender_avatar_url: senderAvatar }
: {})),
refactor(functions): B1 migrate 13 Firestore triggers to v2 + harden Migrate all Firestore triggers off the v1 API to firebase-functions/v2/firestore (onDocumentCreated/Updated/Written); context.params→event.params, snap→event.data, change.before/after→event.data.before/after. Region stays us-central1 (global option). Hardening folded in (all reuse in-repo patterns): - Adopt the shared sendPushToUser()/getUserTokens() helper in every trigger, removing ~7 copied token readers and the copied send/prune blocks. FCM tokens are no longer logged in plaintext anywhere here (redacted inside push.ts). - console.* → firebase-functions/logger (structured). - Idempotency: new claimOnce() (atomic create-if-absent marker under couples/{id}/notif_marks) dedupes at-least-once redelivery on the non-idempotent senders (onAnswerWritten/Revealed, onMessageWritten, onCoupleLeave, onEntitlementChanged, onDateReflectionWritten/Revealed, onDateHistoryCreated). Fail-open. onGameSessionUpdate/ onGamePartFinished/notifyOnDateMatch already had transactional claim-flags — preserved. - onRestoreRequested: the plan's "claim" is implemented as the existing 60s time-WINDOW (lastRestorePartnerAlertAt), not a permanent recipientUid marker — a permanent marker would wrongly block legitimate re-requests (restore docs are deleted+recreated by design). Faithful port of onGameSessionUpdate/onGamePartFinished (broad wildcard + allowlist kept); the trigger split and read reorder are deferred to B6 as separate commits. Build clean; 70 tests green (+ new idempotency.test.ts, push token/prune tests). Emulator discovery loads all triggers as v2 in us-central1. firebase-functions still v5.1.1 (v6 bump deferred to B6). dist rebuilt. Co-Authored-By: Claude Opus 4.8 <[email protected]>
2026-07-07 23:46:17 -05:00
android: { notification: { channelId: 'partner_activity' } },
}, partnerData);
});
//# sourceMappingURL=onDateReflectionRevealed.js.map