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
refactor: rename recordId to documentId in notifications and related types
naveenkash6cb046abe1b3
diff --git a/apps/erp/app/components/Layout/Topbar/Notifications.tsx b/apps/erp/app/components/Layout/Topbar/Notifications.tsxindex be442da5d4..28a8b9c2d2 100644--- a/apps/erp/app/components/Layout/Topbar/Notifications.tsx+++ b/apps/erp/app/components/Layout/Topbar/Notifications.tsx@@ -469,7 +469,7 @@ function DigestNotification({{children.map((child) => (<GenericNotificationkey={child._id}- id={child.payload.recordId as string}+ id={child.payload.documentId as string}createdAt={child.createdAt}description={child.payload.description as string}event={child.payload.event as NotificationEvent}@@ -627,7 +627,7 @@ const Notifications = () => {return (<GenericNotificationkey={notification._id}- id={notification.payload.recordId as string}+ id={notification.payload.documentId as string}createdAt={notification.createdAt}description={notification.payload.description as string}event={event}@@ -715,7 +715,7 @@ const Notifications = () => {return (<GenericNotificationkey={notification._id}- id={notification.payload.recordId as string}+ id={notification.payload.documentId as string}createdAt={notification.createdAt}description={notification.payload.description as string}event={event}diff --git a/apps/erp/app/types/index.ts b/apps/erp/app/types/index.tsindex 872f871b9e..8dc5607f47 100644--- a/apps/erp/app/types/index.ts+++ b/apps/erp/app/types/index.ts@@ -38,7 +38,7 @@ export type Notification = {seen: boolean;createdAt: string;payload: {- recordId?: string;+ documentId?: string;description?: string;event?: string;from?: string;diff --git a/packages/database/src/types.ts b/packages/database/src/types.tsindex 46a4ea8299..13ef1bb7a2 100644--- a/packages/database/src/types.ts+++ b/packages/database/src/types.ts@@ -23863,14 +23863,13 @@ export type Database = {createdAt: stringdescription: string | nulldigestedInto: string | null+ documentId: string | nulldocumentType: string | nullevent: stringfrom: string | null- href: string | nullid: stringpayload: JsonreadAt: string | null- recordId: string | nullseenAt: string | nulltitle: stringtopic: string@@ -23881,14 +23880,13 @@ export type Database = {createdAt?: stringdescription?: string | nulldigestedInto?: string | null+ documentId?: string | nulldocumentType?: string | nullevent: stringfrom?: string | null- href?: string | nullid?: stringpayload?: JsonreadAt?: string | null- recordId?: string | nullseenAt?: string | nulltitle: stringtopic: string@@ -23899,14 +23897,13 @@ export type Database = {createdAt?: stringdescription?: string | nulldigestedInto?: string | null+ documentId?: string | nulldocumentType?: string | nullevent?: stringfrom?: string | null- href?: string | nullid?: stringpayload?: JsonreadAt?: string | null- recordId?: string | nullseenAt?: string | nulltitle?: stringtopic?: string@@ -36235,6 +36232,90 @@ export type Database = {},]}+ searchIndex_ErvqEAYmCKsdXYKx4cufpT: {+ Row: {+ createdAt: string+ description: string | null+ entityId: string+ entityType: string+ id: number+ link: string+ metadata: Json | null+ searchVector: unknown+ tags: string[] | null+ title: string+ updatedAt: string | null+ }+ Insert: {+ createdAt?: string+ description?: string | null+ entityId: string+ entityType: string+ id?: number+ link: string+ metadata?: Json | null+ searchVector?: unknown+ tags?: string[] | null+ title: string+ updatedAt?: string | null+ }+ Update: {+ createdAt?: string+ description?: string | null+ entityId?: string+ entityType?: string+ id?: number+ link?: string+ metadata?: Json | null+ searchVector?: unknown+ tags?: string[] | null+ title?: string+ updatedAt?: string | null+ }+ Relationships: []+ }+ searchIndex_LLSW6e3aZazf2qceCYoeRa: {+ Row: {+ createdAt: string+ description: string | null+ entityId: string+ entityType: string+ id: number+ link: string+ metadata: Json | null+ searchVector: unknown+ tags: string[] | null+ title: string+ updatedAt: string | null+ }+ Insert: {+ createdAt?: string+ description?: string | null+ entityId: string+ entityType: string+ id?: number+ link: string+ metadata?: Json | null+ searchVector?: unknown+ tags?: string[] | null+ title: string+ updatedAt?: string | null+ }+ Update: {+ createdAt?: string+ description?: string | null+ entityId?: string+ entityType?: string+ id?: number+ link?: string+ metadata?: Json | null+ searchVector?: unknown+ tags?: string[] | null+ title?: string+ updatedAt?: string | null+ }+ Relationships: []+ }searchIndexRegistry: {Row: {companyId: string@@ -51468,14 +51549,14 @@ export type Database = {},{foreignKeyName: "partner_id_fkey"- columns: ["id"]+ columns: ["supplierLocationId"]isOneToOne: falsereferencedRelation: "supplierLocation"referencedColumns: ["id"]},{foreignKeyName: "partner_id_fkey"- columns: ["supplierLocationId"]+ columns: ["id"]isOneToOne: falsereferencedRelation: "supplierLocation"referencedColumns: ["id"]@@ -52959,14 +53040,14 @@ export type Database = {Relationships: [{foreignKeyName: "address_countryCode_fkey"- columns: ["customerCountryCode"]+ columns: ["supplierCountryCode"]isOneToOne: falsereferencedRelation: "country"referencedColumns: ["alpha2"]},{foreignKeyName: "address_countryCode_fkey"- columns: ["supplierCountryCode"]+ columns: ["customerCountryCode"]isOneToOne: falsereferencedRelation: "country"referencedColumns: ["alpha2"]@@ -56377,14 +56458,14 @@ export type Database = {Relationships: [{foreignKeyName: "address_countryCode_fkey"- columns: ["invoiceCountryCode"]+ columns: ["customerCountryCode"]isOneToOne: falsereferencedRelation: "country"referencedColumns: ["alpha2"]},{foreignKeyName: "address_countryCode_fkey"- columns: ["customerCountryCode"]+ columns: ["invoiceCountryCode"]isOneToOne: falsereferencedRelation: "country"referencedColumns: ["alpha2"]@@ -56949,14 +57030,14 @@ export type Database = {Relationships: [{foreignKeyName: "address_countryCode_fkey"- columns: ["paymentCountryCode"]+ columns: ["customerCountryCode"]isOneToOne: falsereferencedRelation: "country"referencedColumns: ["alpha2"]},{foreignKeyName: "address_countryCode_fkey"- columns: ["customerCountryCode"]+ columns: ["paymentCountryCode"]isOneToOne: falsereferencedRelation: "country"referencedColumns: ["alpha2"]diff --git a/packages/database/supabase/functions/lib/types.ts b/packages/database/supabase/functions/lib/types.tsindex 46a4ea8299..13ef1bb7a2 100644--- a/packages/database/supabase/functions/lib/types.ts+++ b/packages/database/supabase/functions/lib/types.ts@@ -23863,14 +23863,13 @@ export type Database = {createdAt: stringdescription: string | nulldigestedInto: string | null+ documentId: string | nulldocumentType: string | nullevent: stringfrom: string | null- href: string | nullid: stringpayload: JsonreadAt: string | null- recordId: string | nullseenAt: string | nulltitle: stringtopic: string@@ -23881,14 +23880,13 @@ export type Database = {createdAt?: stringdescription?: string | nulldigestedInto?: string | null+ documentId?: string | nulldocumentType?: string | nullevent: stringfrom?: string | null- href?: string | nullid?: stringpayload?: JsonreadAt?: string | null- recordId?: string | nullseenAt?: string | nulltitle: stringtopic: string@@ -23899,14 +23897,13 @@ export type Database = {createdAt?: stringdescription?: string | nulldigestedInto?: string | null+ documentId?: string | nulldocumentType?: string | nullevent?: stringfrom?: string | null- href?: string | nullid?: stringpayload?: JsonreadAt?: string | null- recordId?: string | nullseenAt?: string | nulltitle?: stringtopic?: string@@ -36235,6 +36232,90 @@ export type Database = {},]}+ searchIndex_ErvqEAYmCKsdXYKx4cufpT: {+ Row: {+ createdAt: string+ description: string | null+ entityId: string+ entityType: string+ id: number+ link: string+ metadata: Json | null+ searchVector: unknown+ tags: string[] | null+ title: string+ updatedAt: string | null+ }+ Insert: {+ createdAt?: string+ description?: string | null+ entityId: string+ entityType: string+ id?: number+ link: string+ metadata?: Json | null+ searchVector?: unknown+ tags?: string[] | null+ title: string+ updatedAt?: string | null+ }+ Update: {+ createdAt?: string+ description?: string | null+ entityId?: string+ entityType?: string+ id?: number+ link?: string+ metadata?: Json | null+ searchVector?: unknown+ tags?: string[] | null+ title?: string+ updatedAt?: string | null+ }+ Relationships: []+ }+ searchIndex_LLSW6e3aZazf2qceCYoeRa: {+ Row: {+ createdAt: string+ description: string | null+ entityId: string+ entityType: string+ id: number+ link: string+ metadata: Json | null+ searchVector: unknown+ tags: string[] | null+ title: string+ updatedAt: string | null+ }+ Insert: {+ createdAt?: string+ description?: string | null+ entityId: string+ entityType: string+ id?: number+ link: string+ metadata?: Json | null+ searchVector?: unknown+ tags?: string[] | null+ title: string+ updatedAt?: string | null+ }+ Update: {+ createdAt?: string+ description?: string | null+ entityId?: string+ entityType?: string+ id?: number+ link?: string+ metadata?: Json | null+ searchVector?: unknown+ tags?: string[] | null+ title?: string+ updatedAt?: string | null+ }+ Relationships: []+ }searchIndexRegistry: {Row: {companyId: string@@ -51468,14 +51549,14 @@ export type Database = {},{foreignKeyName: "partner_id_fkey"- columns: ["id"]+ columns: ["supplierLocationId"]isOneToOne: falsereferencedRelation: "supplierLocation"referencedColumns: ["id"]},{foreignKeyName: "partner_id_fkey"- columns: ["supplierLocationId"]+ columns: ["id"]isOneToOne: falsereferencedRelation: "supplierLocation"referencedColumns: ["id"]@@ -52959,14 +53040,14 @@ export type Database = {Relationships: [{foreignKeyName: "address_countryCode_fkey"- columns: ["customerCountryCode"]+ columns: ["supplierCountryCode"]isOneToOne: falsereferencedRelation: "country"referencedColumns: ["alpha2"]},{foreignKeyName: "address_countryCode_fkey"- columns: ["supplierCountryCode"]+ columns: ["customerCountryCode"]isOneToOne: falsereferencedRelation: "country"referencedColumns: ["alpha2"]@@ -56377,14 +56458,14 @@ export type Database = {Relationships: [{foreignKeyName: "address_countryCode_fkey"- columns: ["invoiceCountryCode"]+ columns: ["customerCountryCode"]isOneToOne: falsereferencedRelation: "country"referencedColumns: ["alpha2"]},{foreignKeyName: "address_countryCode_fkey"- columns: ["customerCountryCode"]+ columns: ["invoiceCountryCode"]isOneToOne: falsereferencedRelation: "country"referencedColumns: ["alpha2"]@@ -56949,14 +57030,14 @@ export type Database = {Relationships: [{foreignKeyName: "address_countryCode_fkey"- columns: ["paymentCountryCode"]+ columns: ["customerCountryCode"]isOneToOne: falsereferencedRelation: "country"referencedColumns: ["alpha2"]},{foreignKeyName: "address_countryCode_fkey"- columns: ["customerCountryCode"]+ columns: ["paymentCountryCode"]isOneToOne: falsereferencedRelation: "country"referencedColumns: ["alpha2"]diff --git a/packages/database/supabase/migrations/20260508120001_notifications.sql b/packages/database/supabase/migrations/20260508120001_notifications.sqlindex 1467ec2963..bb4259c0f1 100644--- a/packages/database/supabase/migrations/20260508120001_notifications.sql+++ b/packages/database/supabase/migrations/20260508120001_notifications.sql@@ -19,8 +19,7 @@ CREATE TABLE "notification" ("description" TEXT,"from" TEXT,"documentType" TEXT,- "recordId" TEXT,- "href" TEXT,+ "documentId" TEXT,"payload" JSONB NOT NULL DEFAULT '{}'::jsonb,"readAt" TIMESTAMP WITH TIME ZONE,"seenAt" TIMESTAMP WITH TIME ZONE,diff --git a/packages/documents/src/email/NotificationEmail.tsx b/packages/documents/src/email/NotificationEmail.tsxindex 32552ba9d6..8243489d76 100644--- a/packages/documents/src/email/NotificationEmail.tsx+++ b/packages/documents/src/email/NotificationEmail.tsx@@ -3,7 +3,6 @@ import {Button,Container,Heading,- Hr,Link,Preview,Section,@@ -305,7 +304,7 @@ export const NotificationEmail = ({</>)}- <Hr className={`my-[32px] nf-divider ${themeClasses.border}`} />+ {/* <Hr className={`my-[32px] nf-divider ${themeClasses.border}`} /><TextclassName={`text-[12px] leading-[18px] m-0 nf-fallback ${themeClasses.mutedText}`}@@ -313,7 +312,7 @@ export const NotificationEmail = ({You're receiving this email because you have email notificationsenabled on your Carbon account. You can manage your preferences fromyour account settings.- </Text>+ </Text> */}</Container></Body></EmailThemeProvider>diff --git a/packages/jobs/src/inngest/functions/notifications/notify.ts b/packages/jobs/src/inngest/functions/notifications/notify.tsindex 74258dd0fd..81c3a18c1b 100644--- a/packages/jobs/src/inngest/functions/notifications/notify.ts+++ b/packages/jobs/src/inngest/functions/notifications/notify.ts@@ -6,6 +6,7 @@ import {notifyTaskAssigned} from "@carbon/ee/notifications";import { companyHasPlan } from "@carbon/ee/plan.server";+import { getSlackUserIdByCarbonId } from "@carbon/ee/slack.server";import { ERP_URL } from "@carbon/env";import type { Events } from "@carbon/lib/events";import {@@ -72,6 +73,19 @@ async function getDescription(return `Quote ${quote?.data?.quoteId} assigned to you`;}+ case NotificationEvent.QuoteExpired: {+ const expiredQuote = await client+ .from("quote")+ .select("*")+ .eq("id", documentId)+ .single();+ if (expiredQuote.error) {+ console.error("Failed to get quote", expiredQuote.error);+ throw expiredQuote.error;+ }+ return `Quote ${expiredQuote?.data?.quoteId} has expired`;+ }+case NotificationEvent.SalesOrderAssignment: {const salesOrder = await client.from("salesOrder")@@ -280,6 +294,36 @@ async function getDescription(return `Training "${trainingAssignment?.data?.training?.name}" assigned to you`;}+ case NotificationEvent.PurchaseOrderAssignment: {+ const purchaseOrder = await client+ .from("purchaseOrder")+ .select("*")+ .eq("id", documentId)+ .single();++ if (purchaseOrder.error) {+ console.error("Failed to get purchaseOrder", purchaseOrder.error);+ throw purchaseOrder.error;+ }++ return `Purchase Order ${purchaseOrder?.data?.purchaseOrderId} assigned to you`;+ }++ case NotificationEvent.PurchaseInvoiceAssignment: {+ const purchaseInvoice = await client+ .from("purchaseInvoice")+ .select("*")+ .eq("id", documentId)+ .single();++ if (purchaseInvoice.error) {+ console.error("Failed to get purchaseInvoice", purchaseInvoice.error);+ throw purchaseInvoice.error;+ }++ return `Purchase Invoice ${purchaseInvoice?.data?.invoiceId} assigned to you`;+ }+case NotificationEvent.SuggestionResponse: {const suggestion = await client.from("suggestion")@@ -311,6 +355,24 @@ async function getDescription(return `Risk "${risk?.data?.title}" assigned to you`;}+ case NotificationEvent.SupplierQuoteAssignment: {+ const supplierQuoteAssignment = await client+ .from("supplierQuote")+ .select("*")+ .eq("id", documentId)+ .single();++ if (supplierQuoteAssignment.error) {+ console.error(+ "Failed to get supplier quote",+ supplierQuoteAssignment.error+ );+ throw supplierQuoteAssignment.error;+ }++ return `Supplier Quote ${supplierQuoteAssignment?.data?.supplierQuoteId} assigned to you`;+ }+case NotificationEvent.SupplierQuoteResponse: {const supplierQuote = await client.from("supplierQuote")@@ -436,38 +498,110 @@ async function getDescription(const defaultDestinations: Partial<Record<NotificationEvent, NotificationDestination[]>> = {- [NotificationEvent.ApprovalApproved]: [NotificationDestination.Email],- [NotificationEvent.ApprovalRejected]: [NotificationDestination.Email],- [NotificationEvent.ApprovalRequested]: [NotificationDestination.Email],- [NotificationEvent.DigitalQuoteResponse]: [NotificationDestination.Email],- [NotificationEvent.GaugeCalibrationExpired]: [NotificationDestination.Email],- [NotificationEvent.JobAssignment]: [NotificationDestination.Email],- [NotificationEvent.JobCompleted]: [NotificationDestination.Email],- [NotificationEvent.JobOperationAssignment]: [NotificationDestination.Email],- [NotificationEvent.JobOperationMessage]: [NotificationDestination.Email],+ [NotificationEvent.ApprovalApproved]: [+ NotificationDestination.Email,+ NotificationDestination.Slack+ ],+ [NotificationEvent.ApprovalRejected]: [+ NotificationDestination.Email,+ NotificationDestination.Slack+ ],+ [NotificationEvent.ApprovalRequested]: [+ NotificationDestination.Email,+ NotificationDestination.Slack+ ],+ [NotificationEvent.DigitalQuoteResponse]: [+ NotificationDestination.Email,+ NotificationDestination.Slack+ ],+ [NotificationEvent.GaugeCalibrationExpired]: [+ NotificationDestination.Email,+ NotificationDestination.Slack+ ],+ [NotificationEvent.JobAssignment]: [+ NotificationDestination.Email,+ NotificationDestination.Slack+ ],+ [NotificationEvent.JobCompleted]: [+ NotificationDestination.Email,+ NotificationDestination.Slack+ ],+ [NotificationEvent.JobOperationAssignment]: [+ NotificationDestination.Email,+ NotificationDestination.Slack+ ],+ [NotificationEvent.JobOperationMessage]: [+ NotificationDestination.Email,+ NotificationDestination.Slack+ ],[NotificationEvent.MaintenanceDispatchAssignment]: [- NotificationDestination.Email+ NotificationDestination.Email,+ NotificationDestination.Slack],[NotificationEvent.MaintenanceDispatchCreated]: [- NotificationDestination.Email+ NotificationDestination.Email,+ NotificationDestination.Slack+ ],+ [NotificationEvent.NonConformanceAssignment]: [+ NotificationDestination.Email,+ NotificationDestination.Slack+ ],+ [NotificationEvent.ProcedureAssignment]: [+ NotificationDestination.Email,+ NotificationDestination.Slack],- [NotificationEvent.NonConformanceAssignment]: [NotificationDestination.Email],- [NotificationEvent.ProcedureAssignment]: [NotificationDestination.Email],[NotificationEvent.PurchaseInvoiceAssignment]: [- NotificationDestination.Email+ NotificationDestination.Email,+ NotificationDestination.Slack+ ],+ [NotificationEvent.PurchaseOrderAssignment]: [+ NotificationDestination.Email,+ NotificationDestination.Slack+ ],+ [NotificationEvent.QuoteAssignment]: [+ NotificationDestination.Email,+ NotificationDestination.Slack+ ],+ [NotificationEvent.QuoteExpired]: [+ NotificationDestination.Email,+ NotificationDestination.Slack+ ],+ [NotificationEvent.RiskAssignment]: [+ NotificationDestination.Email,+ NotificationDestination.Slack+ ],+ [NotificationEvent.SalesOrderAssignment]: [+ NotificationDestination.Email,+ NotificationDestination.Slack+ ],+ [NotificationEvent.SalesRfqAssignment]: [+ NotificationDestination.Email,+ NotificationDestination.Slack+ ],+ [NotificationEvent.SalesRfqReady]: [+ NotificationDestination.Email,+ NotificationDestination.Slack+ ],+ [NotificationEvent.StockTransferAssignment]: [+ NotificationDestination.Email,+ NotificationDestination.Slack+ ],+ [NotificationEvent.SuggestionResponse]: [+ NotificationDestination.Email,+ NotificationDestination.Slack+ ],+ [NotificationEvent.SupplierQuoteAssignment]: [+ NotificationDestination.Email,+ NotificationDestination.Slack+ ],+ [NotificationEvent.SupplierQuoteResponse]: [+ NotificationDestination.Email,+ NotificationDestination.Slack],- [NotificationEvent.PurchaseOrderAssignment]: [NotificationDestination.Email],- [NotificationEvent.QuoteAssignment]: [NotificationDestination.Email],- [NotificationEvent.QuoteExpired]: [NotificationDestination.Email],- [NotificationEvent.RiskAssignment]: [NotificationDestination.Email],- [NotificationEvent.SalesOrderAssignment]: [NotificationDestination.Email],- [NotificationEvent.SalesRfqAssignment]: [NotificationDestination.Email],- [NotificationEvent.SalesRfqReady]: [NotificationDestination.Email],- [NotificationEvent.StockTransferAssignment]: [NotificationDestination.Email],- [NotificationEvent.SuggestionResponse]: [NotificationDestination.Email],- [NotificationEvent.SupplierQuoteAssignment]: [NotificationDestination.Email],- [NotificationEvent.SupplierQuoteResponse]: [NotificationDestination.Email],- [NotificationEvent.TrainingAssignment]: [NotificationDestination.Email]+ [NotificationEvent.TrainingAssignment]: [+ NotificationDestination.Email,+ NotificationDestination.Slack+ ]};export const notifyFunction = inngest.createFunction(@@ -588,10 +722,10 @@ export const notifyFunction = inngest.createFunction(description,event: payload.event,from: payload.from,- recordId: payload.documentId,+ documentId: payload.documentId,...(payload.documentType && { documentType: payload.documentType })},- recordId: payload.documentId,+ documentId: payload.documentId,title: description,topic,userId@@ -701,5 +835,63 @@ export const notifyFunction = inngest.createFunction(await step.sendEvent("fan-out-emails", emailEvents);}}++ // ---- Slack DM fan-out ----+ // Per-user DMs via the company's linked Slack workspace. Users without a+ // matching Slack account in that workspace are silently skipped.+ if (destinations.includes(NotificationDestination.Slack)) {+ const slackEvents = await step.run(+ "resolve-slack-recipients",+ async () => {+ const { data: integration, error } = await client+ .from("companyIntegration")+ .select("active, metadata")+ .eq("companyId", payload.companyId)+ .eq("id", "slack")+ .maybeSingle();++ if (error) {+ console.error("Failed to resolve Slack integration", error);+ return [];+ }+ if (!integration?.active) return [];++ const metadata = integration.metadata as {+ access_token?: string;+ } | null;+ const accessToken = metadata?.access_token;+ if (!accessToken) return [];++ const link = getNotificationLink(payload.event, payload.documentId, {+ documentType: payload.documentType+ });+ const ctaUrl = link ? `${ERP_URL}${link}` : undefined;+ const text = ctaUrl+ ? `${description}\n<${ctaUrl}|View in Carbon>`+ : description;++ const slackUserIds = await Promise.all(+ userIds.map((userId) =>+ getSlackUserIdByCarbonId(client, accessToken, userId)+ )+ );++ return slackUserIds+ .filter((id): id is string => !!id)+ .map((slackUserId) => ({+ data: {+ channel: slackUserId,+ companyId: payload.companyId,+ text+ },+ name: "carbon/send-slack" as const+ }));+ }+ );++ if (slackEvents.length > 0) {+ await step.sendEvent("fan-out-slack", slackEvents);+ }+ }});diff --git a/packages/jobs/src/inngest/functions/notifications/send-slack.ts b/packages/jobs/src/inngest/functions/notifications/send-slack.tsindex 63ca431bce..3007a0ddac 100644--- a/packages/jobs/src/inngest/functions/notifications/send-slack.ts+++ b/packages/jobs/src/inngest/functions/notifications/send-slack.ts@@ -1,3 +1,4 @@+import { getCarbonServiceRole } from "@carbon/auth/client.server";import { getSlackClient } from "@carbon/lib/slack.server";import { inngest } from "../../client";@@ -8,11 +9,26 @@ export const sendSlackFunction = inngest.createFunction(},{ event: "carbon/send-slack" },async ({ event, step }) => {- const { channel, text, blocks } = event.data;+ const { channel, text, blocks, companyId } = event.data;++ const accessToken = await step.run("resolve-slack-token", async () => {+ const client = getCarbonServiceRole();+ const { data, error } = await client+ .from("companyIntegration")+ .select("active, metadata")+ .eq("companyId", companyId)+ .eq("id", "slack")+ .maybeSingle();+ if (error || !data?.active) return null;+ const metadata = data.metadata as { access_token?: string } | null;+ return metadata?.access_token ?? null;+ });await step.run("post-message", async () => {- const slack = getSlackClient();- // Slack client is a no-op on localhost (see slack.server.ts).+ // 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);await slack.sendMessage({ blocks, channel, text });});diff --git a/packages/lib/src/slack.server.ts b/packages/lib/src/slack.server.tsindex c587907b9e..28b18457c2 100644--- a/packages/lib/src/slack.server.ts+++ b/packages/lib/src/slack.server.ts@@ -31,6 +31,8 @@ class SlackClient {}}-export function getSlackClient(): SlackClient {- return new SlackClient(process.env.SLACK_BOT_TOKEN ?? SLACK_BOT_TOKEN ?? "");+export function getSlackClient(token?: string): SlackClient {+ return new SlackClient(+ token ?? process.env.SLACK_BOT_TOKEN ?? SLACK_BOT_TOKEN ?? ""+ );}