Open-source manufacturing ERP, MES and QMS. Quoting, MRP, inventory, shop floor, quality and lot/serial traceability on one Postgres schema, with a REST API and MCP server. Self-host or use Carbon Cloud.
TypeScript ai-agentsbill-of-materialserpinventory-managementleanmanufacturingmanufacturing-execution-systemmcp-servermesmrpopen-source-erppostgresqlproduction-planningqmsquality-management-systemreact-routerself-hostedsupabasetraceabilitytypescript
feat: add edge function for triggering notifications
Brad Barbin305148f971a3
diff --git a/packages/database/supabase/functions/trigger/index.ts b/packages/database/supabase/functions/trigger/index.tsnew file mode 100644index 0000000000..d579cff923--- /dev/null+++ b/packages/database/supabase/functions/trigger/index.ts@@ -0,0 +1,93 @@+import { serve } from "https://deno.land/[email protected]/http/server.ts";+import { z } from "https://deno.land/x/[email protected]/mod.ts";+import { tasks } from "npm:@trigger.dev/[email protected]/v3";+import type { notifyTask } from "../../../../../apps/erp/app/trigger/notify.ts";++import { corsHeaders } from "../lib/headers.ts";+import { getSupabaseServiceRole } from "../lib/supabase.ts";++const recipientValidator = z.discriminatedUnion("type", [+ z.object({+ type: z.literal("user"),+ userId: z.string(),+ }),+ z.object({+ type: z.literal("group"),+ groupId: z.string(),+ }),+ z.object({+ type: z.literal("users"),+ userIds: z.array(z.string()),+ }),+]);++const payloadValidator = z.discriminatedUnion("type", [+ z.object({+ type: z.literal("notify"),+ event: z.enum([+ "quote-assignment",+ "sales-rfq-assignment",+ "sales-order-assignment",+ ]),+ documentId: z.string(),+ companyId: z.string(),+ recipient: recipientValidator,+ }),+]);++serve(async (req: Request) => {+ if (req.method === "OPTIONS") {+ return new Response("ok", { headers: corsHeaders });+ }+ const payload = await req.json();++ try {+ const validatedPayload = payloadValidator.parse(payload);+ const { type, ...data } = validatedPayload;++ console.log({+ function: "trigger",+ type,+ ...data,+ });++ // verify that the request is authorized by an API key or service role+ await getSupabaseServiceRole(+ req.headers.get("Authorization"),+ req.headers.get("carbon-key") ?? "",+ data.companyId+ );++ switch (type) {+ case "notify": {+ await tasks.trigger<typeof notifyTask>("notify", {+ companyId: data.companyId,+ documentId: data.documentId,+ event: data.event,+ recipient: data.recipient,+ from: "sytem",+ });+ break;+ }++ default:+ throw new Error(`Invalid type ${type}`);+ }++ return new Response(+ JSON.stringify({+ success: true,+ }),+ {+ headers: { ...corsHeaders, "Content-Type": "application/json" },+ status: 200,+ }+ );+ } catch (err) {+ console.error(err);+ return new Response(JSON.stringify(err), {+ headers: { ...corsHeaders, "Content-Type": "application/json" },+ status: 500,+ });+ }+});