118 lines
5.4 KiB
JavaScript
118 lines
5.4 KiB
JavaScript
"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.onCoupleLeave = void 0;
|
|
const admin = __importStar(require("firebase-admin"));
|
|
const firestore_1 = require("firebase-functions/v2/firestore");
|
|
const push_1 = require("../notifications/push");
|
|
const idempotency_1 = require("../notifications/idempotency");
|
|
const log_1 = require("../log");
|
|
/**
|
|
* Firestore trigger that notifies the remaining partner when a user's coupleId
|
|
* field is cleared (i.e. the user left the couple or was removed).
|
|
*
|
|
* Path: users/{userId}
|
|
* Condition: previous coupleId was non-empty and new coupleId is null/missing.
|
|
*/
|
|
exports.onCoupleLeave = (0, firestore_1.onDocumentUpdated)('users/{userId}', async (event) => {
|
|
var _a, _b, _c, _d, _e, _f;
|
|
const { userId } = event.params;
|
|
const previousData = (_b = (_a = event.data) === null || _a === void 0 ? void 0 : _a.before.data()) !== null && _b !== void 0 ? _b : {};
|
|
const currentData = (_d = (_c = event.data) === null || _c === void 0 ? void 0 : _c.after.data()) !== null && _d !== void 0 ? _d : {};
|
|
const previousCoupleId = previousData.coupleId;
|
|
const currentCoupleId = currentData.coupleId;
|
|
// Only act when coupleId transitions from a real value to null/empty.
|
|
if (!previousCoupleId || typeof previousCoupleId !== 'string') {
|
|
return;
|
|
}
|
|
if (currentCoupleId) {
|
|
return;
|
|
}
|
|
const db = admin.firestore();
|
|
const coupleDoc = await db.collection('couples').doc(previousCoupleId).get();
|
|
if (!coupleDoc.exists) {
|
|
log_1.logger.warn(`[onCoupleLeave] couple ${previousCoupleId} not found`);
|
|
return;
|
|
}
|
|
const userIds = ((_f = (_e = coupleDoc.data()) === null || _e === void 0 ? void 0 : _e.userIds) !== null && _f !== void 0 ? _f : []);
|
|
const partnerId = userIds.find((uid) => uid !== userId);
|
|
if (!partnerId) {
|
|
log_1.logger.warn(`[onCoupleLeave] no partner found for couple ${previousCoupleId}`);
|
|
return;
|
|
}
|
|
// Make sure the partner is still paired in this couple.
|
|
// If both users are leaving simultaneously, avoid duplicate/phantom notifications.
|
|
const partnerUserDoc = await db.collection('users').doc(partnerId).get();
|
|
const partnerData = partnerUserDoc.data();
|
|
const partnerCoupleId = partnerData === null || partnerData === void 0 ? void 0 : partnerData.coupleId;
|
|
if (partnerCoupleId !== previousCoupleId) {
|
|
log_1.logger.log(`[onCoupleLeave] partner ${partnerId} is no longer in couple ${previousCoupleId}; skipping notification`);
|
|
return;
|
|
}
|
|
// Dedupe redelivery of this leave (at-least-once) so the partner isn't double-notified and the
|
|
// in-app record isn't duplicated.
|
|
if (!(await (0, idempotency_1.claimOnce)((0, idempotency_1.notifMark)(db, previousCoupleId, `leave-${userId}`)))) {
|
|
log_1.logger.log(`[onCoupleLeave] already notified partner of ${userId} leaving; skipping`);
|
|
return;
|
|
}
|
|
const notificationPayload = {
|
|
type: 'partner_left',
|
|
title: 'Your partner has left',
|
|
body: 'You are no longer paired. Tap to create a new invite.',
|
|
};
|
|
// Write an in-app notification record for the partner.
|
|
// This is read-only denied for clients; the Cloud Function writes it.
|
|
await db
|
|
.collection('users')
|
|
.doc(partnerId)
|
|
.collection('notification_queue')
|
|
.add(Object.assign(Object.assign({}, notificationPayload), { read: false, createdAt: admin.firestore.FieldValue.serverTimestamp() }));
|
|
const res = await (0, push_1.sendPushToUser)(db, admin.messaging(), partnerId, {
|
|
notification: {
|
|
title: notificationPayload.title,
|
|
body: notificationPayload.body,
|
|
},
|
|
android: { notification: { channelId: 'partner_activity' } }, // E-OBS
|
|
data: {
|
|
type: notificationPayload.type,
|
|
},
|
|
}, partnerData);
|
|
if (res.sent === 0 && res.failed === 0) {
|
|
log_1.logger.log(`[onCoupleLeave] no FCM tokens for partner ${partnerId}`);
|
|
return;
|
|
}
|
|
log_1.logger.log(`[onCoupleLeave] notified partner ${partnerId} that user ${userId} left couple ${previousCoupleId}`);
|
|
});
|
|
//# sourceMappingURL=onCoupleLeave.js.map
|