diff --git a/functions/dist/dates/onDateReflectionWritten.js b/functions/dist/dates/onDateReflectionWritten.js new file mode 100644 index 00000000..ddb838c7 --- /dev/null +++ b/functions/dist/dates/onDateReflectionWritten.js @@ -0,0 +1,113 @@ +"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.onDateReflectionWritten = void 0; +const functions = __importStar(require("firebase-functions")); +const admin = __importStar(require("firebase-admin")); +const quietHours_1 = require("../notifications/quietHours"); +/** + * Fires when a partner writes their post-date reflection + * (`couples/{coupleId}/date_reflections/{dateId}/answers/{userId}`). Notifies the OTHER partner — "your + * turn to reflect" if they haven't yet, or "ready to reveal together" if both now have. Without this the + * second partner never learns to reflect and the mutual reveal stalls. Mirrors `onAnswerWritten`: + * 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). + */ +exports.onDateReflectionWritten = functions.firestore + .document('couples/{coupleId}/date_reflections/{dateId}/answers/{userId}') + .onCreate(async (_snap, context) => { + var _a, _b, _c, _d, _e; + const { coupleId, dateId, userId } = context.params; + const db = admin.firestore(); + const coupleDoc = await db.collection('couples').doc(coupleId).get(); + if (!coupleDoc.exists) + return; + const userIds = ((_b = (_a = coupleDoc.data()) === null || _a === void 0 ? void 0 : _a.userIds) !== null && _b !== void 0 ? _b : []); + if (!userIds.includes(userId)) + return; + const partnerId = userIds.find((u) => u !== userId); + if (!partnerId) + return; + const partnerUserDoc = await db.collection('users').doc(partnerId).get(); + // Partner-activity opt-out (default on). + if (((_c = partnerUserDoc.data()) === null || _c === void 0 ? void 0 : _c.notifPartnerAnswered) === false) { + console.log(`[onDateReflectionWritten] ${partnerId} has partner notifications off`); + return; + } + // Did this complete the pair? If the recipient already reflected, both are done → reveal-ready. + const partnerReflection = await db + .collection('couples').doc(coupleId) + .collection('date_reflections').doc(dateId) + .collection('answers').doc(partnerId).get(); + const bothReflected = partnerReflection.exists; + const title = bothReflected ? 'Your date reflections are ready ✨' : 'Your partner reflected on your date 💭'; + const body = bothReflected ? 'Open to reveal them together.' : 'Add yours to reveal together.'; + const type = bothReflected ? 'date_reflection_ready' : 'date_reflection_partner'; + // 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(), + }); + // Quiet hours: keep the in-app record, suppress the disruptive push. + if ((0, quietHours_1.recipientInQuietHours)(partnerUserDoc.data())) { + console.log(`[onDateReflectionWritten] ${partnerId} in quiet hours — push suppressed (in-app kept)`); + return; + } + const senderAvatar = (_d = (await db.collection('users').doc(userId).get()).data()) === null || _d === void 0 ? void 0 : _d.photoUrl; + const tokens = []; + const legacy = (_e = partnerUserDoc.data()) === null || _e === void 0 ? void 0 : _e.fcmToken; + if (typeof legacy === 'string' && legacy.length > 0) + tokens.push(legacy); + const tokenSnap = await db.collection('users').doc(partnerId).collection('fcmTokens').get(); + tokenSnap.docs.forEach((d) => { + var _a; + const t = (_a = d.data()) === null || _a === void 0 ? void 0 : _a.token; + if (typeof t === 'string' && t.length > 0 && !tokens.includes(t)) + tokens.push(t); + }); + if (tokens.length === 0) + return; + const payload = { + notification: { title, body }, + data: Object.assign({ type, couple_id: coupleId, date_id: dateId }, (typeof senderAvatar === 'string' && senderAvatar.length > 0 + ? { sender_avatar_url: senderAvatar } + : {})), + }; + const results = await Promise.allSettled(tokens.map((token) => admin.messaging().send(Object.assign(Object.assign({}, payload), { token, android: { notification: { channelId: 'partner_activity' } } })))); + results.forEach((r, i) => { + if (r.status === 'rejected') + console.warn(`[onDateReflectionWritten] FCM failed for ${tokens[i]}:`, r.reason); + }); +}); +//# sourceMappingURL=onDateReflectionWritten.js.map \ No newline at end of file diff --git a/functions/dist/dates/onDateReflectionWritten.js.map b/functions/dist/dates/onDateReflectionWritten.js.map new file mode 100644 index 00000000..d5e5ede6 --- /dev/null +++ b/functions/dist/dates/onDateReflectionWritten.js.map @@ -0,0 +1 @@ +{"version":3,"file":"onDateReflectionWritten.js","sourceRoot":"","sources":["../../src/dates/onDateReflectionWritten.ts"],"names":[],"mappings":";;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;AAAA,8DAA+C;AAC/C,sDAAuC;AACvC,4DAAmE;AAEnE;;;;;;;GAOG;AACU,QAAA,uBAAuB,GAAG,SAAS,CAAC,SAAS;KACvD,QAAQ,CAAC,+DAA+D,CAAC;KACzE,QAAQ,CAAC,KAAK,EAAE,KAAK,EAAE,OAAO,EAAE,EAAE;;IACjC,MAAM,EAAE,QAAQ,EAAE,MAAM,EAAE,MAAM,EAAE,GAAG,OAAO,CAAC,MAI5C,CAAA;IACD,MAAM,EAAE,GAAG,KAAK,CAAC,SAAS,EAAE,CAAA;IAE5B,MAAM,SAAS,GAAG,MAAM,EAAE,CAAC,UAAU,CAAC,SAAS,CAAC,CAAC,GAAG,CAAC,Q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