diff --git a/functions/dist/dates/onDateHistoryCreated.js b/functions/dist/dates/onDateHistoryCreated.js new file mode 100644 index 00000000..63db1ec9 --- /dev/null +++ b/functions/dist/dates/onDateHistoryCreated.js @@ -0,0 +1,92 @@ +"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.onDateHistoryCreated = void 0; +const functions = __importStar(require("firebase-functions")); +const admin = __importStar(require("firebase-admin")); +const quietHours_1 = require("../notifications/quietHours"); +/** + * Fires when a date is logged as completed (`couples/{coupleId}/date_history/{dateId}` created). Nudges + * the OTHER partner (not the one who logged it) to add their reflection — so the reflect→reveal loop + * starts even if the logger doesn't reflect right away. Gated on `notifPartnerAnswered` + quiet hours. + */ +exports.onDateHistoryCreated = functions.firestore + .document('couples/{coupleId}/date_history/{dateId}') + .onCreate(async (snap, context) => { + var _a, _b, _c, _d, _e; + const { coupleId, dateId } = context.params; + const db = admin.firestore(); + const addedBy = (_a = snap.data()) === null || _a === void 0 ? void 0 : _a.addedBy; + const coupleDoc = await db.collection('couples').doc(coupleId).get(); + if (!coupleDoc.exists) + return; + const userIds = ((_c = (_b = coupleDoc.data()) === null || _b === void 0 ? void 0 : _b.userIds) !== null && _c !== void 0 ? _c : []); + const partnerId = userIds.find((u) => u !== addedBy); + if (!partnerId) + return; + const partnerUserDoc = await db.collection('users').doc(partnerId).get(); + if (((_d = partnerUserDoc.data()) === null || _d === void 0 ? void 0 : _d.notifPartnerAnswered) === false) + return; + const title = 'You went on a date 💜'; + const body = 'Reflect on it together while it’s fresh.'; + await db.collection('users').doc(partnerId).collection('notification_queue').add({ + type: 'date_logged', title, body, read: false, createdAt: admin.firestore.FieldValue.serverTimestamp(), + }); + if ((0, quietHours_1.recipientInQuietHours)(partnerUserDoc.data())) + return; + 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: { type: 'date_logged', couple_id: coupleId, date_id: dateId }, + }; + 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(`[onDateHistoryCreated] FCM failed for ${tokens[i]}:`, r.reason); + }); +}); +//# sourceMappingURL=onDateHistoryCreated.js.map \ No newline at end of file diff --git a/functions/dist/dates/onDateHistoryCreated.js.map b/functions/dist/dates/onDateHistoryCreated.js.map new file mode 100644 index 00000000..fd16587d --- /dev/null +++ b/functions/dist/dates/onDateHistory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AE,IAAI,EAAE;QAC7B,IAAI,EAAE,EAAE,IAAI,EAAE,aAAa,EAAE,SAAS,EAAE,QAAQ,EAAE,OAAO,EAAE,MAAM,EAAE;KACpE,CAAA;IACD,MAAM,OAAO,GAAG,MAAM,OAAO,CAAC,UAAU,CACtC,MAAM,CAAC,GAAG,CAAC,CAAC,KAAK,EAAE,EAAE,CACnB,KAAK,CAAC,SAAS,EAAE,CAAC,IAAI,CAAC,gCAClB,OAAO,KACV,KAAK,EACL,OAAO,EAAE,EAAE,YAAY,EAAE,EAAE,SAAS,EAAE,kBAAkB,EAAE,EAAE,GAClC,CAAC,CAC9B,CACF,CAAA;IACD,OAAO,CAAC,OAAO,CAAC,CAAC,CAAC,EAAE,CAAC,EAAE,EAAE;QACvB,IAAI,CAAC,CAAC,MAAM,KAAK,UAAU;YAAE,OAAO,CAAC,IAAI,CAAC,yCAAyC,MAAM,CAAC,CAAC,CAAC,GAAG,EAAE,CAAC,CAAC,MAAM,CAAC,CAAA;IAC5G,CAAC,CAAC,CAAA;AACJ,CAAC,CAAC,CAAA"} \ No newline at end of file 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,QAAQ,CAAC,CAAC,GAAG,EAAE,CAAA;IACpE,IAAI,CAAC,SAAS,CAAC,MAAM;QAAE,OAAM;IAC7B,MAAM,OAAO,GAAG,CAAC,MAAA,MAAA,SAAS,CAAC,IAAI,EAAE,0CAAE,OAAO,mCAAI,EAAE,CAAa,CAAA;IAC7D,IAAI,CAAC,OAAO,CAAC,QAAQ,CAAC,MAAM,CAAC;QAAE,OAAM;IACrC,MAAM,SAAS,GAAG,OAAO,CAAC,IAAI,CAAC,CAAC,CAAC,EAAE,EAAE,CAAC,CAAC,KAAK,MAAM,CAAC,CAAA;IACnD,IAAI,CAAC,SAAS;QAAE,OAAM;IAEtB,MAAM,cAAc,GAAG,MAAM,EAAE,CAAC,UAAU,CAAC,OAAO,CAAC,CAAC,GAAG,CAAC,SAAS,CAAC,CAAC,GAAG,EAAE,CAAA;IACxE,yCAAyC;IACzC,IAAI,CAAA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ACR;KACF,CAAA;IACD,MAAM,OAAO,GAAG,MAAM,OAAO,CAAC,UAAU,CACtC,MAAM,CAAC,GAAG,CAAC,CAAC,KAAK,EAAE,EAAE,CACnB,KAAK,CAAC,SAAS,EAAE,CAAC,IAAI,CAAC,gCAClB,OAAO,KACV,KAAK,EACL,OAAO,EAAE,EAAE,YAAY,EAAE,EAAE,SAAS,EAAE,kBAAkB,EAAE,EAAE,GAClC,CAAC,CAC9B,CACF,CAAA;IACD,OAAO,CAAC,OAAO,CAAC,CAAC,CAAC,EAAE,CAAC,EAAE,EAAE;QACvB,IAAI,CAAC,CAAC,MAAM,KAAK,UAAU;YAAE,OAAO,CAAC,IAAI,CAAC,4CAA4C,MAAM,CAAC,CAAC,CAAC,GAAG,EAAE,CAAC,CAAC,MAAM,CAAC,CAAA;IAC/G,CAAC,CAAC,CAAA;AACJ,CAAC,CAAC,CAAA"} \ No newline at end of file diff --git a/functions/dist/index.js b/functions/dist/index.js index 5df51bda..a0ebf5cf 100644 --- a/functions/dist/index.js +++ b/functions/dist/index.js @@ -33,7 +33,7 @@ var __importStar = (this && this.__importStar) || (function () { }; })(); Object.defineProperty(exports, "__esModule", { value: true }); -exports.wrapReleaseKeyCallable = exports.onGamePartFinished = exports.onGameSessionUpdate = exports.onUserDelete = exports.scheduledOutcomesReminder = exports.submitOutcomeCallable = exports.createInviteCallable = exports.acceptInviteCallable = exports.leaveCoupleCallable = exports.onCoupleLeave = exports.onMessageWritten = exports.onAnswerRevealed = exports.onAnswerWritten = exports.assignDailyQuestionCallable = exports.assignDailyQuestion = exports.notifyOnDateMatch = exports.checkDeviceIntegrity = exports.sendReengagementReminder = exports.sendStreakReminder = exports.sendDailyQuestionProactiveReminder = exports.unlockDueMemoryCapsules = exports.sendChallengeDayReminders = exports.sendThinkingOfYouCallable = exports.sendGentleReminderCallable = exports.sendPartnerAnsweredNotification = exports.sendDailyQuestionReminder = exports.onEntitlementChanged = exports.syncEntitlement = exports.revenueCatWebhook = void 0; +exports.wrapReleaseKeyCallable = exports.onGamePartFinished = exports.onGameSessionUpdate = exports.onUserDelete = exports.scheduledOutcomesReminder = exports.submitOutcomeCallable = exports.createInviteCallable = exports.acceptInviteCallable = exports.leaveCoupleCallable = exports.onCoupleLeave = exports.onMessageWritten = exports.onAnswerRevealed = exports.onAnswerWritten = exports.assignDailyQuestionCallable = exports.assignDailyQuestion = exports.onDateHistoryCreated = exports.onDateReflectionWritten = exports.notifyOnDateMatch = exports.checkDeviceIntegrity = exports.sendReengagementReminder = exports.sendStreakReminder = exports.sendDailyQuestionProactiveReminder = exports.unlockDueMemoryCapsules = exports.sendChallengeDayReminders = exports.sendThinkingOfYouCallable = exports.sendGentleReminderCallable = exports.sendPartnerAnsweredNotification = exports.sendDailyQuestionReminder = exports.onEntitlementChanged = exports.syncEntitlement = exports.revenueCatWebhook = void 0; const admin = __importStar(require("firebase-admin")); // Initialize the Admin SDK once for every function in this codebase. // Handlers call admin.firestore()/messaging() lazily at invocation time, so a @@ -67,6 +67,10 @@ var checkDeviceIntegrity_1 = require("./security/checkDeviceIntegrity"); Object.defineProperty(exports, "checkDeviceIntegrity", { enumerable: true, get: function () { return checkDeviceIntegrity_1.checkDeviceIntegrity; } }); var createDateMatch_1 = require("./dates/createDateMatch"); Object.defineProperty(exports, "notifyOnDateMatch", { enumerable: true, get: function () { return createDateMatch_1.notifyOnDateMatch; } }); +var onDateReflectionWritten_1 = require("./dates/onDateReflectionWritten"); +Object.defineProperty(exports, "onDateReflectionWritten", { enumerable: true, get: function () { return onDateReflectionWritten_1.onDateReflectionWritten; } }); +var onDateHistoryCreated_1 = require("./dates/onDateHistoryCreated"); +Object.defineProperty(exports, "onDateHistoryCreated", { enumerable: true, get: function () { return onDateHistoryCreated_1.onDateHistoryCreated; } }); var assignDailyQuestion_1 = require("./questions/assignDailyQuestion"); Object.defineProperty(exports, "assignDailyQuestion", { enumerable: true, get: function () { return assignDailyQuestion_1.assignDailyQuestion; } }); Object.defineProperty(exports, "assignDailyQuestionCallable", { enumerable: true, get: function () { return assignDailyQuestion_1.assignDailyQuestionCallable; } }); diff --git a/functions/dist/index.js.map b/functions/dist/index.js.map index d632c95e..41d997ed 100644 --- a/functions/dist/index.js.map +++ b/functions/dist/index.js.map @@ -1 +1 @@ -{"version":3,"file":"index.js","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":";;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;AAAA,sDAAuC;AAEvC,qEAAqE;AACrE,8EAA8E;AAC9E,gFAAgF;AAChF,IAAI,KAAK,CAAC,IAAI,CAAC,MAAM,KAAK,CAAC,EAAE,CAAC;IAC5B,KAAK,CAAC,aAAa,EAAE,CAAA;AACvB,CAAC;AAED,iEAA+D;AAAtD,sHAAA,iBAAiB,OAAA;AAC1B,6DAA2D;AAAlD,kHAAA,eAAe,OAAA;AACxB,uEAAqE;AAA5D,4HAAA,oBAAoB,OAAA;AAC7B,uDAGkC;AAFhC,sHAAA,yBAAyB,OAAA;AACzB,4HAAA,+BAA+B,OAAA;AAEjC,yFAAuF;AAA9E,wIAAA,0BAA0B,OAAA;AACnC,uFAAqF;AAA5E,sIAAA,yBAAyB,OAAA;AAClC,+DAGsC;AAFpC,0HAAA,yBAAyB,OAAA;AACzB,wHAAA,uBAAuB,OAAA;AAEzB,+EAA0F;AAAjF,2IAAA,kCAAkC,OAAA;AAC3C,iEAAmE;AAA1D,oHAAA,kBAAkB,OAAA;AAC3B,6DAAuE;AAA9D,wHAAA,wBAAwB,OAAA;AACjC,wEAAsE;AAA7D,4HAAA,oBAAoB,OAAA;AAC7B,2DAA2D;AAAlD,oHAAA,iBAAiB,OAAA;AAC1B,uEAGwC;AAFtC,0HAAA,mBAAmB,OAAA;AACnB,kIAAA,2BAA2B,OAAA;AAE7B,+DAA6D;AAApD,kHAAA,eAAe,OAAA;AACxB,iEAA+D;AAAtD,oHAAA,gBAAgB,OAAA;AACzB,iEAA+D;AAAtD,oHAAA,gBAAgB,OAAA;AACzB,yDAAuD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AAwB,OAAA;AACjC,wEAAsE;AAA7D,4HAAA,oBAAoB,OAAA;AAC7B,2DAA2D;AAAlD,oHAAA,iBAAiB,OAAA;AAC1B,2EAAyE;AAAhE,kIAAA,uBAAuB,OAAA;AAChC,qEAAmE;AAA1D,4HAAA,oBAAoB,OAAA;AAC7B,uEAGwC;AAFtC,0HAAA,mBAAmB,OAAA;AACnB,kIAAA,2BAA2B,OAAA;AAE7B,+DAA6D;AAApD,kHAAA,eAAe,OAAA;AACxB,iEAA+D;AAAtD,oHAAA,gBAAgB,OAAA;AACzB,iEAA+D;AAAtD,oHAAA,gBAAgB,OAAA;AACzB,yDAAuD;AAA9C,8GAAA,aAAa,OAAA;AACtB,qEAAmE;AAA1D,0HAAA,mBAAmB,OAAA;AAC5B,uEAAqE;AAA5D,4HAAA,oBAAoB,OAAA;AAC7B,uEAAqE;AAA5D,4HAAA,oBAAoB,OAAA;AAC7B,yEAAuE;AAA9D,8HAAA,qBAAqB,OAAA;AAC9B,iFAA+E;AAAtE,sIAAA,yBAAyB,OAAA;AAClC,qDAAmD;AAA1C,4GAAA,YAAY,OAAA;AACrB,mEAAqF;AAA5E,0HAAA,mBAAmB,OAAA;AAAE,yHAAA,kBAAkB,OAAA;AAEhD,8EAA4E;AAAnE,gIAAA,sBAAsB,OAAA;AAE/B,oFAAoF;AACpF,uEAAuE;AACvE,iFAAiF;AACjF,0DAA0D"} \ No newline at end of file diff --git a/functions/src/dates/onDateHistoryCreated.ts b/functions/src/dates/onDateHistoryCreated.ts new file mode 100644 index 00000000..0c9af9b6 --- /dev/null +++ b/functions/src/dates/onDateHistoryCreated.ts @@ -0,0 +1,61 @@ +import * as functions from 'firebase-functions' +import * as admin from 'firebase-admin' +import { recipientInQuietHours } from '../notifications/quietHours' + +/** + * Fires when a date is logged as completed (`couples/{coupleId}/date_history/{dateId}` created). Nudges + * the OTHER partner (not the one who logged it) to add their reflection — so the reflect→reveal loop + * starts even if the logger doesn't reflect right away. Gated on `notifPartnerAnswered` + quiet hours. + */ +export const onDateHistoryCreated = functions.firestore + .document('couples/{coupleId}/date_history/{dateId}') + .onCreate(async (snap, context) => { + const { coupleId, dateId } = context.params as { coupleId: string; dateId: string } + const db = admin.firestore() + + const addedBy = snap.data()?.addedBy as string | undefined + const coupleDoc = await db.collection('couples').doc(coupleId).get() + if (!coupleDoc.exists) return + const userIds = (coupleDoc.data()?.userIds ?? []) as string[] + const partnerId = userIds.find((u) => u !== addedBy) + if (!partnerId) return + + const partnerUserDoc = await db.collection('users').doc(partnerId).get() + if (partnerUserDoc.data()?.notifPartnerAnswered === false) return + + const title = 'You went on a date 💜' + const body = 'Reflect on it together while it’s fresh.' + + await db.collection('users').doc(partnerId).collection('notification_queue').add({ + type: 'date_logged', title, body, read: false, createdAt: admin.firestore.FieldValue.serverTimestamp(), + }) + + if (recipientInQuietHours(partnerUserDoc.data())) return + + const tokens: string[] = [] + const legacy = partnerUserDoc.data()?.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) => { + const t = d.data()?.token + if (typeof t === 'string' && t.length > 0 && !tokens.includes(t)) tokens.push(t) + }) + if (tokens.length === 0) return + + const payload: admin.messaging.MessagingPayload = { + notification: { title, body }, + data: { type: 'date_logged', couple_id: coupleId, date_id: dateId }, + } + const results = await Promise.allSettled( + tokens.map((token) => + admin.messaging().send({ + ...payload, + token, + android: { notification: { channelId: 'partner_activity' } }, + } as admin.messaging.Message) + ) + ) + results.forEach((r, i) => { + if (r.status === 'rejected') console.warn(`[onDateHistoryCreated] FCM failed for ${tokens[i]}:`, r.reason) + }) + }) diff --git a/functions/src/dates/onDateReflectionWritten.ts b/functions/src/dates/onDateReflectionWritten.ts new file mode 100644 index 00000000..ee39cf49 --- /dev/null +++ b/functions/src/dates/onDateReflectionWritten.ts @@ -0,0 +1,94 @@ +import * as functions from 'firebase-functions' +import * as admin from 'firebase-admin' +import { recipientInQuietHours } from '../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). + */ +export const onDateReflectionWritten = functions.firestore + .document('couples/{coupleId}/date_reflections/{dateId}/answers/{userId}') + .onCreate(async (_snap, context) => { + const { coupleId, dateId, userId } = context.params as { + coupleId: string + dateId: string + userId: string + } + const db = admin.firestore() + + const coupleDoc = await db.collection('couples').doc(coupleId).get() + if (!coupleDoc.exists) return + const userIds = (coupleDoc.data()?.userIds ?? []) as string[] + 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 (partnerUserDoc.data()?.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 (recipientInQuietHours(partnerUserDoc.data())) { + console.log(`[onDateReflectionWritten] ${partnerId} in quiet hours — push suppressed (in-app kept)`) + return + } + + const senderAvatar = (await db.collection('users').doc(userId).get()).data()?.photoUrl + + const tokens: string[] = [] + const legacy = partnerUserDoc.data()?.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) => { + const t = d.data()?.token + if (typeof t === 'string' && t.length > 0 && !tokens.includes(t)) tokens.push(t) + }) + if (tokens.length === 0) return + + const payload: admin.messaging.MessagingPayload = { + notification: { title, body }, + data: { + 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({ + ...payload, + token, + android: { notification: { channelId: 'partner_activity' } }, // E-OBS + } as admin.messaging.Message) + ) + ) + results.forEach((r, i) => { + if (r.status === 'rejected') console.warn(`[onDateReflectionWritten] FCM failed for ${tokens[i]}:`, r.reason) + }) + }) diff --git a/functions/src/index.ts b/functions/src/index.ts index 1b098388..61c59cc9 100644 --- a/functions/src/index.ts +++ b/functions/src/index.ts @@ -25,6 +25,8 @@ export { sendStreakReminder } from './notifications/streakReminder' export { sendReengagementReminder } from './notifications/reengagement' export { checkDeviceIntegrity } from './security/checkDeviceIntegrity' export { notifyOnDateMatch } from './dates/createDateMatch' +export { onDateReflectionWritten } from './dates/onDateReflectionWritten' +export { onDateHistoryCreated } from './dates/onDateHistoryCreated' export { assignDailyQuestion, assignDailyQuestionCallable,