Closer/functions/dist/dates/createDateMatch.js

88 lines
3.5 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.createDateMatchOnMutualLove = void 0;
const functions = __importStar(require("firebase-functions"));
const admin = __importStar(require("firebase-admin"));
const LOVE = 'love';
/**
* Creates a revealed date match when both partners have swiped LOVE on the
* same date idea.
*
* Trigger: couples/{coupleId}/date_swipes/{dateIdeaId} (onWrite)
*
* The `date_matches` collection is server-write-only — Firestore rules deny all
* client writes (`allow create, update, delete: if false`). This trigger is
* therefore the single source of truth for match creation. The client only
* records swipes and observes `date_matches` for the result.
*
* Idempotency: the match document id is the date idea id and creation runs in a
* transaction, so repeated swipes on the same idea and concurrent invocations
* never produce a duplicate match.
*/
exports.createDateMatchOnMutualLove = functions.firestore
.document('couples/{coupleId}/date_swipes/{dateIdeaId}')
.onWrite(async (change, context) => {
var _a;
const after = change.after.data();
if (!after)
return; // swipe document was deleted
const actions = ((_a = after.actions) !== null && _a !== void 0 ? _a : {});
const lovedBy = Object.entries(actions)
.filter(([, entry]) => (entry === null || entry === void 0 ? void 0 : entry.action) === LOVE)
.map(([uid]) => uid)
.sort();
// A match needs both partners to have loved the same idea.
if (lovedBy.length < 2)
return;
const { coupleId, dateIdeaId } = context.params;
const db = admin.firestore();
const matchRef = db
.collection('couples')
.doc(coupleId)
.collection('date_matches')
.doc(dateIdeaId);
await db.runTransaction(async (tx) => {
const existing = await tx.get(matchRef);
if (existing.exists)
return; // already matched — no-op
tx.set(matchRef, {
dateIdeaId,
matchedBy: lovedBy,
revealedAt: admin.firestore.FieldValue.serverTimestamp(),
});
});
});
//# sourceMappingURL=createDateMatch.js.map