- Hub client: register and heartbeat on protocol v2 with the site's Ed25519 key (sent on the heartbeat until the Hub has it); falls back to v1 for a day if the Hub refuses v2, so the licence always refreshes. - crosscode.link/1 Connect / Disconnect, both directions, trusted through the Hub's cached linkedProducts (ProductLink, LinkNonce). - GET /api/feed/v1/events from FeedEntry (go-live, closing and selling out are changes; deactivated events go in deleted), event.changed to connected products, and showing their events (ImportedEvent). - /api/v2/linked-products, /api/v2/linked-events; /events/:id/full gains crosscodeEvent and priceRangeCents for the JSON-LD. - Contracts: FEED_PATHS, typesVersions for @crosscode/contracts/signing. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
123 lines
4.8 KiB
JavaScript
123 lines
4.8 KiB
JavaScript
const { PrismaClient } = require('@prisma/client');
|
|
const { PrismaPg } = require('@prisma/adapter-pg');
|
|
|
|
// Prisma 7 talks to Postgres through the node-postgres driver adapter instead of
|
|
// the old Rust engine. pg has no connect timeout by default; 5s matches Prisma 5.
|
|
function createBaseClient() {
|
|
const adapter = new PrismaPg({
|
|
connectionString: process.env.DATABASE_URL,
|
|
connectionTimeoutMillis: 5000,
|
|
});
|
|
return new PrismaClient({ adapter });
|
|
}
|
|
|
|
/**
|
|
* Outbound webhooks (utils/webhooks.js) notice what happens here, whichever code path does it:
|
|
* registrations made, paid or cancelled, payments and refunds, check-ins, new accounts, events
|
|
* becoming public. Noticing only queues; the record is read again before anything is sent, so a
|
|
* transaction that rolls back sends nothing. A failure here never affects the write itself.
|
|
*/
|
|
function withWebhooks(base) {
|
|
const notice = (type, id) => {
|
|
try { require('../utils/webhooks').notice(type, id); } catch (e) { console.warn('[webhooks] notice:', e?.message || e); }
|
|
};
|
|
const isPublic = e => !!e && e.isActive !== false && !e.isHidden;
|
|
// The status before an update (read outside the caller's transaction: the committed value).
|
|
const before = (model, where, select) => (where?.id ? base[model].findUnique({ where: { id: where.id }, select }).catch(() => null) : Promise.resolve(null));
|
|
|
|
return base.$extends({
|
|
query: {
|
|
registration: {
|
|
async create({ args, query }) {
|
|
const r = await query(args);
|
|
if (r?.id) {
|
|
notice('registration.created', r.id);
|
|
if (r.status === 'paid') notice('registration.paid', r.id);
|
|
}
|
|
return r;
|
|
},
|
|
async update({ args, query }) {
|
|
const status = args?.data?.status;
|
|
if (status !== 'paid' && status !== 'cancelled') return query(args);
|
|
const prev = await before('registration', args.where, { status: true });
|
|
const r = await query(args);
|
|
if (r?.id && prev && prev.status !== status) notice(status === 'paid' ? 'registration.paid' : 'registration.cancelled', r.id);
|
|
return r;
|
|
},
|
|
},
|
|
payment: {
|
|
async create({ args, query }) {
|
|
const p = await query(args);
|
|
// Applying an earlier donation to an order isn't new money (see cashupUtils.isDonationLeg).
|
|
const leg = p && !p.isDonation && p.originalPaymentId && p.amount > 0;
|
|
if (p?.id && !leg && p.amount !== 0) notice(p.amount < 0 ? 'payment.refunded' : 'payment.received', p.id);
|
|
return p;
|
|
},
|
|
},
|
|
ticketUsage: {
|
|
async create({ args, query }) {
|
|
const u = await query(args);
|
|
if (u?.id) notice('ticket.checked_in', u.id);
|
|
return u;
|
|
},
|
|
},
|
|
user: {
|
|
async create({ args, query }) {
|
|
const u = await query(args);
|
|
if (u?.id) notice('user.created', u.id);
|
|
return u;
|
|
},
|
|
},
|
|
event: {
|
|
async create({ args, query }) {
|
|
const e = await query(args);
|
|
if (e?.id && isPublic(e)) notice('event.published', e.id);
|
|
return e;
|
|
},
|
|
async update({ args, query }) {
|
|
const touches = args?.data && ('isActive' in args.data || 'isHidden' in args.data);
|
|
if (!touches) return query(args);
|
|
const prev = await before('event', args.where, { isActive: true, isHidden: true });
|
|
const e = await query(args);
|
|
if (e?.id && prev && !isPublic(prev) && isPublic(e)) notice('event.published', e.id);
|
|
return e;
|
|
},
|
|
},
|
|
},
|
|
});
|
|
}
|
|
|
|
/**
|
|
* The event feed (2.4.0, src/feed/entries.ts): a write to anything a linked product would see
|
|
* (an event, its tickets, sessions, series or campus, or a registration that can fill it up)
|
|
* refreshes the feed a few seconds later. Only starts a timer; never affects the write.
|
|
*/
|
|
const FEED_MODELS = new Set(['Event', 'EventOption', 'OptionVariant', 'EarlyBirdTier', 'EventSession', 'EventSeries', 'Campus', 'Registration', 'RegistrationOption', 'WaitlistEntry']);
|
|
const WRITES = new Set(['create', 'createMany', 'createManyAndReturn', 'update', 'updateMany', 'updateManyAndReturn', 'upsert', 'delete', 'deleteMany']);
|
|
|
|
function withFeed(client) {
|
|
const touched = () => {
|
|
try { require('../feed/entries').changedSoon(); } catch (e) { console.warn('[feed] changedSoon:', e?.message || e); }
|
|
};
|
|
return client.$extends({
|
|
query: {
|
|
$allModels: {
|
|
async $allOperations({ model, operation, args, query }) {
|
|
const result = await query(args);
|
|
if (FEED_MODELS.has(model) && WRITES.has(operation)) touched();
|
|
return result;
|
|
},
|
|
},
|
|
},
|
|
});
|
|
}
|
|
|
|
function createPrismaClient() {
|
|
return withFeed(withWebhooks(createBaseClient()));
|
|
}
|
|
|
|
const prisma = createPrismaClient();
|
|
|
|
module.exports = prisma;
|
|
module.exports.createPrismaClient = createPrismaClient;
|