A client gallery is its own module. It is built from an event and its photographs and opens at /cliente/<slug> on the events site.
120 lines
5.4 KiB
TypeScript
120 lines
5.4 KiB
TypeScript
import {
|
|
memberModuleList,
|
|
orgModuleList,
|
|
readModuleList,
|
|
type AppModule,
|
|
type MembershipRole,
|
|
} from "@photoreport/contracts";
|
|
import type { FastifyRequest } from "fastify";
|
|
import { currentUserId } from "../auth/guard.js";
|
|
import type { Db } from "../db.js";
|
|
import { forbidden } from "../lib/errors.js";
|
|
|
|
/** Second path segment under /v1/orgs/:orgId/ that belongs to one module. */
|
|
const BY_SEGMENT: Record<string, AppModule> = {
|
|
"delivery-targets": "delivery",
|
|
deliveries: "delivery",
|
|
galleries: "shop",
|
|
products: "shop",
|
|
bundles: "shop",
|
|
sites: "shop",
|
|
orders: "shop",
|
|
"shop-settings": "shop",
|
|
earnings: "shop",
|
|
affiliates: "shop",
|
|
connect: "shop",
|
|
"my-earnings": "shop",
|
|
"press-accounts": "shop",
|
|
invoices: "shop",
|
|
invoicing: "shop",
|
|
payouts: "shop",
|
|
"print-events": "print",
|
|
printers: "print",
|
|
"print-stations": "print",
|
|
"cloud-sources": "archive",
|
|
"face-review": "faces",
|
|
persons: "faces",
|
|
faces: "faces",
|
|
jersey: "numbers",
|
|
"site-galleries": "siteGallery",
|
|
};
|
|
|
|
const ORG_ROUTE = /^\/v1\/orgs\/([0-9a-f]{8}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{12})\/([^/?]+)/i;
|
|
|
|
/**
|
|
* The desktop finishes a local delivery from any module. Those calls stay open
|
|
* so a person without the Entregar screen can still complete a file already queued.
|
|
*/
|
|
const LOCAL_DELIVERY = /\/deliveries\/local\/|\/local-file(?:\/|$)|\/local-result(?:\/|$)/;
|
|
|
|
/** Which module an organization-scoped URL belongs to. Null: shared, public, or not a module route. */
|
|
export function moduleFromUrl(url: string): { orgId: string; module: AppModule } | null {
|
|
const path = url.split("?")[0] ?? url;
|
|
if (LOCAL_DELIVERY.test(path)) return null;
|
|
const match = ORG_ROUTE.exec(path);
|
|
if (!match?.[1] || !match[2]) return null;
|
|
const module = BY_SEGMENT[match[2]];
|
|
if (!module) return null;
|
|
return { orgId: match[1], module };
|
|
}
|
|
|
|
type IdRoute = { module: AppModule; prefix: string; find: (db: Db, id: string) => Promise<{ organizationId: string } | null> };
|
|
|
|
const ID_ROUTES: IdRoute[] = [
|
|
{ module: "shop", prefix: "/v1/galleries/", find: (db, id) => db.gallery.findUnique({ where: { id }, select: { organizationId: true } }) },
|
|
{ module: "shop", prefix: "/v1/products/", find: (db, id) => db.product.findUnique({ where: { id }, select: { organizationId: true } }) },
|
|
{ module: "shop", prefix: "/v1/bundles/", find: (db, id) => db.bundle.findUnique({ where: { id }, select: { organizationId: true } }) },
|
|
{ module: "shop", prefix: "/v1/sites/", find: (db, id) => db.site.findUnique({ where: { id }, select: { organizationId: true } }) },
|
|
{ module: "shop", prefix: "/v1/orders/", find: (db, id) => db.order.findUnique({ where: { id }, select: { organizationId: true } }) },
|
|
{ module: "shop", prefix: "/v1/press-accounts/", find: (db, id) => db.pressAccount.findUnique({ where: { id }, select: { organizationId: true } }) },
|
|
{ module: "shop", prefix: "/v1/invoices/", find: (db, id) => db.invoice.findUnique({ where: { id }, select: { organizationId: true } }) },
|
|
{ module: "shop", prefix: "/v1/affiliates/", find: (db, id) => db.affiliate.findUnique({ where: { id }, select: { organizationId: true } }) },
|
|
{ module: "shop", prefix: "/v1/payouts/", find: (db, id) => db.payout.findUnique({ where: { id }, select: { organizationId: true } }) },
|
|
{ module: "print", prefix: "/v1/print-events/", find: (db, id) => db.printEvent.findUnique({ where: { id }, select: { organizationId: true } }) },
|
|
{ module: "print", prefix: "/v1/printers/", find: (db, id) => db.printer.findUnique({ where: { id }, select: { organizationId: true } }) },
|
|
{ module: "print", prefix: "/v1/print-stations/", find: (db, id) => db.printStation.findUnique({ where: { id }, select: { organizationId: true } }) },
|
|
{ module: "archive", prefix: "/v1/cloud-sources/", find: (db, id) => db.cloudSource.findUnique({ where: { id }, select: { organizationId: true } }) },
|
|
];
|
|
|
|
const ID_IN_PATH = /([0-9a-f]{8}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{12})/i;
|
|
|
|
/**
|
|
* Module for a URL that names a row instead of an organization.
|
|
* Missing row: null, so the route can answer 404.
|
|
*/
|
|
export async function moduleFromRecord(db: Db, url: string): Promise<{ orgId: string; module: AppModule } | null> {
|
|
const path = url.split("?")[0] ?? url;
|
|
if (path.startsWith("/v1/public/")) return null;
|
|
for (const route of ID_ROUTES) {
|
|
if (!path.startsWith(route.prefix)) continue;
|
|
const id = ID_IN_PATH.exec(path.slice(route.prefix.length))?.[1];
|
|
if (!id) return null;
|
|
const row = await route.find(db, id);
|
|
if (!row) return null;
|
|
return { orgId: row.organizationId, module: route.module };
|
|
}
|
|
return null;
|
|
}
|
|
|
|
/** Refuses the call when this person does not have the module in this organization. */
|
|
export async function requireAppModule(
|
|
db: Db,
|
|
req: FastifyRequest,
|
|
organizationId: string,
|
|
module: AppModule,
|
|
serverModules: readonly AppModule[] | null,
|
|
) {
|
|
const membership = await db.membership.findUnique({
|
|
where: { userId_organizationId: { userId: currentUserId(req), organizationId } },
|
|
include: { organization: { select: { appModules: true } } },
|
|
});
|
|
if (!membership) throw forbidden("insufficient_role");
|
|
const allowed = memberModuleList(
|
|
membership.role as MembershipRole,
|
|
readModuleList(membership.appModules),
|
|
orgModuleList(readModuleList(membership.organization.appModules), serverModules),
|
|
);
|
|
if (!allowed.includes(module)) throw forbidden("module_disabled");
|
|
return membership;
|
|
}
|