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.

fix(slack): read the vaulted access_token in send-slack and the interactive route

Sidwebworks60dac0f0aff7
diff --git a/apps/erp/app/routes/api+/integrations.slack.interactive.ts b/apps/erp/app/routes/api+/integrations.slack.interactive.ts
index cb2a9aaf41..01f5b3de80 100644
--- a/apps/erp/app/routes/api+/integrations.slack.interactive.ts
+++ b/apps/erp/app/routes/api+/integrations.slack.interactive.ts
@@ -1,6 +1,7 @@
import { ERP_URL } from "@carbon/auth";
import { getCarbonServiceRole } from "@carbon/auth/client.server";
import type { Database } from "@carbon/database";
+import { resolveIntegrationSecrets } from "@carbon/ee";
import {
createIssueSlackThread,
createSlackWebClient,
@@ -101,8 +102,22 @@ export async function action({ request }: ActionFunctionArgs) {
};
}
- const { companyId, metadata } = integration.data?.[0];
- const slackToken = (metadata as any)?.access_token as string;
+ const { companyId, metadata, secretRef } = integration.data?.[0];
+ // Secret material (access_token) lives in Supabase Vault; merge it back so
+ // we read the same shape as before. Fails closed when the vault ref is gone.
+ let slackToken: string | undefined;
+ try {
+ const resolved = (await resolveIntegrationSecrets(
+ serviceRole,
+ companyId,
+ "slack",
+ metadata,
+ secretRef
+ )) as { access_token?: string };
+ slackToken = resolved.access_token;
+ } catch (error) {
+ logger.error("Failed to resolve Slack integration secret", { error });
+ }
if (!slackToken) {
logger.error("Slack token not found");
diff --git a/packages/jobs/src/inngest/functions/notifications/send-slack.ts b/packages/jobs/src/inngest/functions/notifications/send-slack.ts
index 3007a0ddac..fa332fcddf 100644
--- a/packages/jobs/src/inngest/functions/notifications/send-slack.ts
+++ b/packages/jobs/src/inngest/functions/notifications/send-slack.ts
@@ -1,4 +1,5 @@
import { getCarbonServiceRole } from "@carbon/auth/client.server";
+import { resolveIntegrationSecrets } from "@carbon/ee";
import { getSlackClient } from "@carbon/lib/slack.server";
import { inngest } from "../../client";
@@ -15,20 +16,33 @@ export const sendSlackFunction = inngest.createFunction(
const client = getCarbonServiceRole();
const { data, error } = await client
.from("companyIntegration")
- .select("active, metadata")
+ .select("active, metadata, secretRef")
.eq("companyId", companyId)
.eq("id", "slack")
.maybeSingle();
if (error || !data?.active) return null;
- const metadata = data.metadata as { access_token?: string } | null;
+ // Secret material (access_token) lives in Supabase Vault; merge it back
+ // so we read the same shape as before. `client` is service-role.
+ const metadata = (await resolveIntegrationSecrets(
+ client,
+ companyId,
+ "slack",
+ data.metadata,
+ data.secretRef
+ )) as { access_token?: string } | null;
return metadata?.access_token ?? null;
});
+ // The channel id only exists in the company's linked workspace, so without
+ // that workspace's token there is nothing valid to post — the old env-token
+ // fallback sent the id into Carbon's own workspace (channel_not_found).
+ if (!accessToken) {
+ return { success: false, skipped: "slack-integration-not-linked" };
+ }
+
await step.run("post-message", async () => {
- // Per-company token if the company has Slack linked, else fall back to
- // the env token (legacy single-workspace setups). Client is a no-op on
- // localhost — see slack.server.ts.
- const slack = getSlackClient(accessToken ?? undefined);
+ // Client is a no-op on localhost — see slack.server.ts.
+ const slack = getSlackClient(accessToken);
await slack.sendMessage({ blocks, channel, text });
});
diff --git a/packages/lib/src/slack.server.ts b/packages/lib/src/slack.server.ts
index 5a3433c5ba..8bec832f6b 100644
--- a/packages/lib/src/slack.server.ts
+++ b/packages/lib/src/slack.server.ts
@@ -32,7 +32,7 @@ class SlackClient {
blocks
});
} catch (error) {
- log.error("Error sending Slack message", { error });
+ log.error("Error sending Slack message", { channel, error });
}
}
}