"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.onAnswerRevealed = void 0; const functions = __importStar(require("firebase-functions")); const admin = __importStar(require("firebase-admin")); const quietHours_1 = require("../notifications/quietHours"); /** * Firestore trigger: when one partner OPENS (reveals) the shared answers — i.e. their own * daily answer doc flips isRevealed false → true — notify the other partner that they've * looked, so the pair stays in sync ("[Name] just opened your answers"). * * Path: couples/{coupleId}/daily_question/{date}/answers/{userId} */ exports.onAnswerRevealed = functions.firestore .document('couples/{coupleId}/daily_question/{date}/answers/{userId}') .onUpdate(async (change, context) => { var _a, _b, _c, _d, _e, _f; const { coupleId, date, userId } = context.params; const before = change.before.data(); const after = change.after.data(); // Only fire 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) { console.warn(`[onAnswerRevealed] couple ${coupleId} not found`); return; } const userIds = ((_b = (_a = coupleDoc.data()) === null || _a === void 0 ? void 0 : _a.userIds) !== null && _b !== void 0 ? _b : []); if (!userIds.includes(userId)) { console.warn(`[onAnswerRevealed] revealer ${userId} not a member of ${coupleId}`); return; } const partnerId = userIds.find((uid) => uid !== userId); if (!partnerId) { console.warn(`[onAnswerRevealed] no partner for couple ${coupleId}`); return; } // Partner FCM tokens (legacy field + multi-device subcollection). const tokens = []; const partnerUserDoc = await db.collection('users').doc(partnerId).get(); if (partnerUserDoc.exists) { const legacyToken = (_c = partnerUserDoc.data()) === null || _c === void 0 ? void 0 : _c.fcmToken; if (typeof legacyToken === 'string' && legacyToken.length > 0) tokens.push(legacyToken); } const tokenSnapshot = await db.collection('users').doc(partnerId).collection('fcmTokens').get(); tokenSnapshot.docs.forEach((doc) => { var _a; const t = (_a = doc.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) { console.log(`[onAnswerRevealed] no FCM tokens for partner ${partnerId}`); return; } // Respect the same partner-activity opt-out as the answered ping. if (((_d = partnerUserDoc.data()) === null || _d === void 0 ? void 0 : _d.notifPartnerAnswered) === false) { console.log(`[onAnswerRevealed] partner ${partnerId} has partner-activity notifications off`); return; } // M-001: honor the recipient's quiet-hours window ("no notifications" promise). Fail-open. if ((0, quietHours_1.recipientInQuietHours)(partnerUserDoc.data())) { console.log(`[onAnswerRevealed] partner ${partnerId} is in quiet hours — suppressing`); return; } const questionId = typeof after.questionId === 'string' ? after.questionId : ''; const revealerDoc = await db.collection('users').doc(userId).get(); const revealerName = ((_e = revealerDoc.data()) === null || _e === void 0 ? void 0 : _e.displayName) || 'Your partner'; const revealerAvatar = (_f = revealerDoc.data()) === null || _f === void 0 ? void 0 : _f.photoUrl; const payload = { notification: { title: `${revealerName} opened your answers`, body: 'Open to see what you each said.', }, data: Object.assign({ type: 'partner_opened_answer', couple_id: coupleId, question_id: questionId, date }, (typeof revealerAvatar === 'string' && revealerAvatar.length > 0 ? { sender_avatar_url: revealerAvatar } : {})), }; const sendResults = await Promise.allSettled(tokens.map((token) => admin.messaging().send(Object.assign(Object.assign({}, payload), { token, android: { notification: { channelId: 'partner_activity' } } })))); const failures = []; sendResults.forEach((r, i) => { if (r.status === 'rejected') failures.push(`${tokens[i]}: ${String(r.reason)}`); }); if (failures.length > 0) console.error('[onAnswerRevealed] some notifications failed:', failures); console.log(`[onAnswerRevealed] notified ${partnerId} that ${userId} opened couple ${coupleId}`); }); //# sourceMappingURL=onAnswerRevealed.js.map