Skip to content

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.

feat: add proxy endpoint in mes for cors requests

Brad Barbin49a2015d334a
diff --git a/apps/erp/app/routes/api+/messaging.notify.ts b/apps/erp/app/routes/api+/messaging.notify.ts
index 347ea21659..da63eefc4f 100644
--- a/apps/erp/app/routes/api+/messaging.notify.ts
+++ b/apps/erp/app/routes/api+/messaging.notify.ts
@@ -1,4 +1,4 @@
-import { DOMAIN, error } from "@carbon/auth";
+import { error } from "@carbon/auth";
import { requirePermissions } from "@carbon/auth/auth.server";
import { flash } from "@carbon/auth/session.server";
import { NotificationEvent } from "@carbon/notifications";
@@ -19,19 +19,7 @@ export const messagingNotifySchema = z.discriminatedUnion("type", [
}),
]);
-const corsHeaders = {
- "Access-Control-Allow-Origin": `https://mes.${DOMAIN}`,
- "Access-Control-Allow-Methods": "POST",
- "Access-Control-Allow-Headers": "Content-Type",
- "Access-Control-Allow-Credentials": "true",
-};
-
export async function action({ request }: ActionFunctionArgs) {
- // Handle CORS preflight requests
- if (request.method === "OPTIONS") {
- return new Response(null, { headers: corsHeaders });
- }
-
const { client, companyId, userId } = await requirePermissions(request, {});
const payload = messagingNotifySchema.safeParse(await request.json());
@@ -52,12 +40,6 @@ export async function action({ request }: ActionFunctionArgs) {
const makeMethodId = data.data?.jobMakeMethod?.id;
const materialId = data.data?.jobMakeMethod?.parentMaterialId;
- console.log({
- assignee,
- jobId,
- makeMethodId,
- materialId,
- });
if (assignee) {
const notificationEvent = getNotificationEvent("jobOperationNote");
if (notificationEvent) {
@@ -79,37 +61,17 @@ export async function action({ request }: ActionFunctionArgs) {
break;
default:
- const flashedResponse = await flash(
- request,
- error(null, "Invalid payload")
- );
return json(
{ success: false },
- {
- ...flashedResponse,
- headers: {
- ...corsHeaders,
- ...flashedResponse.headers,
- },
- }
+ await flash(request, error(null, "Invalid payload"))
);
}
- return json({ success: true }, { headers: corsHeaders });
+ return json({ success: true });
} else {
- const flashedResponse = await flash(
- request,
- error(null, "Failed to notify user")
- );
return json(
{ success: false },
- {
- ...flashedResponse,
- headers: {
- ...corsHeaders,
- ...flashedResponse.headers,
- },
- }
+ await flash(request, error(null, "Failed to notify user"))
);
}
}
diff --git a/apps/mes/app/routes/x+/proxy.$.tsx b/apps/mes/app/routes/x+/proxy.$.tsx
new file mode 100644
index 0000000000..9f8bbecd11
--- /dev/null
+++ b/apps/mes/app/routes/x+/proxy.$.tsx
@@ -0,0 +1,25 @@
+import type { ActionFunctionArgs } from "@vercel/remix";
+import { ERP_URL } from "~/utils/path";
+
+export async function action({ request, params }: ActionFunctionArgs) {
+ const path = params["*"];
+
+ // Create new headers without host header to avoid connection refused
+ const headers = new Headers(request.headers);
+ headers.delete("host");
+ headers.delete("origin");
+
+ try {
+ const response = await fetch(`${ERP_URL}/${path}`, {
+ method: request.method,
+ body: request.body,
+ headers,
+ duplex: "half", // Add duplex option when sending body
+ });
+
+ return response;
+ } catch (error) {
+ console.error("Proxy request failed:", error);
+ return new Response("Proxy request failed", { status: 500 });
+ }
+}
diff --git a/apps/mes/app/utils/path.ts b/apps/mes/app/utils/path.ts
index f13890da57..e3704f2ce4 100644
--- a/apps/mes/app/utils/path.ts
+++ b/apps/mes/app/utils/path.ts
@@ -28,7 +28,7 @@ export const path = {
forgotPassword: `${ERP_URL}/forgot-password`,
healthcheck: "/healthcheck",
issue: `${x}/issue`,
- messagingNotify: `${ERP_URL}/api/messaging/notify`,
+ messagingNotify: `${x}/proxy/api/messaging/notify`,
operation: (id: string) => generatePath(`${x}/operation/${id}`),
operations: `${x}/operations?saved=1`,
location: `${x}/location`,