Closer/functions/dist/dates/onDateHistoryCreated.js

85 lines
4.1 KiB
JavaScript
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

"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 admin = __importStar(require("firebase-admin"));
const firestore_1 = require("firebase-functions/v2/firestore");
const quietHours_1 = require("../notifications/quietHours");
const push_1 = require("../notifications/push");
const idempotency_1 = require("../notifications/idempotency");
const log_1 = require("../log");
/**
* 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 = (0, firestore_1.onDocumentCreated)('couples/{coupleId}/date_history/{dateId}', async (event) => {
var _a, _b, _c;
const { coupleId, dateId } = event.params;
const snap = event.data;
if (!snap)
return;
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();
const partnerData = partnerUserDoc.data();
if ((partnerData === null || partnerData === void 0 ? void 0 : partnerData.notifPartnerAnswered) === false)
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-logged-${dateId}`)))) {
log_1.logger.log(`[onDateHistoryCreated] already notified for date ${dateId}; skipping`);
return;
}
const title = 'You went on a date 💜';
const body = 'Reflect on it together while its 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)(partnerData))
return;
await (0, push_1.sendPushToUser)(db, admin.messaging(), partnerId, {
notification: { title, body },
data: { type: 'date_logged', couple_id: coupleId, date_id: dateId },
android: { notification: { channelId: 'partner_activity' } },
}, partnerData);
});
//# sourceMappingURL=onDateHistoryCreated.js.map