2026-06-27 16:35:41 -05:00
|
|
|
"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.onEntitlementChanged = void 0;
|
|
|
|
|
const functions = __importStar(require("firebase-functions"));
|
|
|
|
|
const admin = __importStar(require("firebase-admin"));
|
|
|
|
|
/**
|
|
|
|
|
* Notifies the OTHER partner when a user GAINS premium, so couple-shared premium unlock isn't silent
|
|
|
|
|
* for the non-subscriber. (Future.md: `subscription_entitlement_changed`.)
|
|
|
|
|
*
|
|
|
|
|
* Path: users/{userId}/entitlements/premium (onWrite)
|
|
|
|
|
* Edge-triggered: fires only on the inactive→active transition, so renewals / repeated writes don't
|
|
|
|
|
* re-notify. Skips if the partner already had premium (the couple was already unlocked).
|
|
|
|
|
*/
|
|
|
|
|
function isActive(data) {
|
|
|
|
|
if (!data)
|
|
|
|
|
return false;
|
|
|
|
|
if (data.premium !== true)
|
|
|
|
|
return false;
|
|
|
|
|
const expiresAt = data.expiresAt;
|
|
|
|
|
if (expiresAt && expiresAt.toMillis() <= Date.now())
|
|
|
|
|
return false;
|
|
|
|
|
return true;
|
|
|
|
|
}
|
|
|
|
|
async function collectTokens(db, userId) {
|
|
|
|
|
var _a;
|
|
|
|
|
const tokens = [];
|
|
|
|
|
const userDoc = await db.collection('users').doc(userId).get();
|
|
|
|
|
const legacy = (_a = userDoc.data()) === null || _a === void 0 ? void 0 : _a.fcmToken;
|
|
|
|
|
if (typeof legacy === 'string' && legacy.length > 0)
|
|
|
|
|
tokens.push(legacy);
|
|
|
|
|
const tokSnap = await db.collection('users').doc(userId).collection('fcmTokens').get();
|
|
|
|
|
tokSnap.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);
|
|
|
|
|
});
|
|
|
|
|
return tokens;
|
|
|
|
|
}
|
|
|
|
|
exports.onEntitlementChanged = functions.firestore
|
|
|
|
|
.document('users/{userId}/entitlements/premium')
|
|
|
|
|
.onWrite(async (change, context) => {
|
2026-06-30 02:38:31 -05:00
|
|
|
var _a, _b;
|
2026-06-27 16:35:41 -05:00
|
|
|
const { userId } = context.params;
|
|
|
|
|
const before = isActive(change.before.data());
|
|
|
|
|
const after = isActive(change.after.data());
|
|
|
|
|
if (before || !after)
|
|
|
|
|
return; // only a genuine inactive→active gain
|
|
|
|
|
const db = admin.firestore();
|
|
|
|
|
const messaging = admin.messaging();
|
|
|
|
|
// Resolve this user's couple + partner.
|
|
|
|
|
const coupleSnap = await db
|
|
|
|
|
.collection('couples')
|
|
|
|
|
.where('userIds', 'array-contains', userId)
|
|
|
|
|
.limit(1)
|
|
|
|
|
.get();
|
|
|
|
|
if (coupleSnap.empty) {
|
|
|
|
|
console.log(`[onEntitlementChanged] no couple for ${userId}`);
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
const coupleDoc = coupleSnap.docs[0];
|
|
|
|
|
const coupleId = coupleDoc.id;
|
|
|
|
|
const userIds = ((_b = (_a = coupleDoc.data()) === null || _a === void 0 ? void 0 : _a.userIds) !== null && _b !== void 0 ? _b : []);
|
|
|
|
|
const partnerId = userIds.find((id) => id !== userId);
|
|
|
|
|
if (!partnerId) {
|
|
|
|
|
console.log(`[onEntitlementChanged] no partner for ${userId} in ${coupleId}`);
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
// If the partner already has premium the couple was already unlocked — nothing new to announce.
|
|
|
|
|
const partnerEnt = await db.doc(`users/${partnerId}/entitlements/premium`).get();
|
|
|
|
|
if (isActive(partnerEnt.data())) {
|
|
|
|
|
console.log(`[onEntitlementChanged] partner ${partnerId} already premium; skip`);
|
|
|
|
|
return;
|
|
|
|
|
}
|
2026-06-30 02:38:31 -05:00
|
|
|
// displayName is E2EE in users/{uid}, so use a generic label (this copy is stored server-side in
|
|
|
|
|
// notification_queue + the push, neither of which can decrypt).
|
2026-06-27 16:35:41 -05:00
|
|
|
const payload = {
|
|
|
|
|
type: 'subscription_entitlement_changed',
|
|
|
|
|
title: 'Premium unlocked ✨',
|
2026-06-30 02:38:31 -05:00
|
|
|
body: 'Your partner upgraded — you both have Premium now.',
|
2026-06-27 16:35:41 -05:00
|
|
|
};
|
|
|
|
|
// In-app record for the partner.
|
|
|
|
|
await db
|
|
|
|
|
.collection('users')
|
|
|
|
|
.doc(partnerId)
|
|
|
|
|
.collection('notification_queue')
|
|
|
|
|
.add(Object.assign(Object.assign({}, payload), { read: false, createdAt: admin.firestore.FieldValue.serverTimestamp() }));
|
|
|
|
|
const tokens = await collectTokens(db, partnerId);
|
|
|
|
|
if (tokens.length === 0) {
|
|
|
|
|
console.log(`[onEntitlementChanged] no FCM tokens for ${partnerId}`);
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
const message = {
|
|
|
|
|
token: tokens[0],
|
|
|
|
|
notification: { title: payload.title, body: payload.body },
|
|
|
|
|
android: { notification: { channelId: 'partner_activity' } },
|
|
|
|
|
data: { type: payload.type, couple_id: coupleId },
|
|
|
|
|
};
|
|
|
|
|
const results = await Promise.allSettled(tokens.map((token) => messaging.send(Object.assign(Object.assign({}, message), { token }))));
|
|
|
|
|
results.forEach((r, i) => {
|
|
|
|
|
if (r.status === 'rejected') {
|
|
|
|
|
console.warn(`[onEntitlementChanged] send failed ${tokens[i]}: ${String(r.reason)}`);
|
|
|
|
|
}
|
|
|
|
|
});
|
|
|
|
|
console.log(`[onEntitlementChanged] notified ${partnerId} of couple premium (${coupleId})`);
|
|
|
|
|
});
|
|
|
|
|
//# sourceMappingURL=onEntitlementChanged.js.map
|