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.

Add notification for approval rules with lower amount

Gaurav Bhatt255f50d615c9
diff --git a/apps/erp/app/modules/shared/shared.service.ts b/apps/erp/app/modules/shared/shared.service.ts
index fd0c81b86e..8d67409555 100644
--- a/apps/erp/app/modules/shared/shared.service.ts
+++ b/apps/erp/app/modules/shared/shared.service.ts
@@ -464,6 +464,44 @@ export async function getApproverUserIdsForRule(
return combined;
}
+/**
+ * "Notified of spend" cascade resolver. When a tiered approval lands at
+ * a high tier, approvers of every enabled rule with a strictly lower
+ * `lowerBoundAmount` get pinged — visibility into spend that bypassed
+ * their tier. Returns deduped user IDs.
+ */
+export async function getLowerTierApproverUserIds(
+ client: SupabaseClient<Database>,
+ documentType: (typeof approvalDocumentType)[number],
+ companyId: string,
+ amount: number | null | undefined
+): Promise<string[]> {
+ if (amount == null) return [];
+
+ const matched = await getApprovalRuleByAmount(
+ client,
+ documentType,
+ companyId,
+ amount
+ );
+ if (!matched.data) return [];
+
+ const lowerRules = await client
+ .from("approvalRule")
+ .select("approverGroupIds, defaultApproverId")
+ .eq("documentType", documentType)
+ .eq("companyId", companyId)
+ .eq("enabled", true)
+ .lt("lowerBoundAmount", matched.data.lowerBoundAmount ?? 0);
+
+ if (lowerRules.error || !lowerRules.data?.length) return [];
+
+ const expanded = await Promise.all(
+ lowerRules.data.map((rule) => getApproverUserIdsForRule(client, rule))
+ );
+ return [...new Set(expanded.flat())];
+}
+
export async function getApprovalRuleById(
client: SupabaseClient<Database>,
id: string,
diff --git a/apps/erp/app/routes/x+/purchase-order+/$orderId.tsx b/apps/erp/app/routes/x+/purchase-order+/$orderId.tsx
index 3c9496f09a..76ff7e04c0 100644
--- a/apps/erp/app/routes/x+/purchase-order+/$orderId.tsx
+++ b/apps/erp/app/routes/x+/purchase-order+/$orderId.tsx
@@ -37,6 +37,7 @@ import {
canApproveRequest,
canCancelRequest,
getLatestApprovalRequestForDocument,
+ getLowerTierApproverUserIds,
rejectRequest
} from "~/modules/shared";
import { getUser } from "~/modules/users/users.server";
@@ -153,6 +154,41 @@ export async function action(args: ActionFunctionArgs) {
}
}
+ // Notified-of-spend cascade. Purchase orders use tiered amount rules,
+ // so when a decision is made at a high tier we ping the approvers of
+ // every enabled rule with a strictly lower threshold — they get
+ // visibility into spend that bypassed their tier. Filtered to exclude
+ // the requester (already notified above) and the current approver.
+ try {
+ const lowerTierIds = await getLowerTierApproverUserIds(
+ serviceRole,
+ "purchaseOrder",
+ companyId,
+ approvalRequest.data.amount ?? undefined
+ );
+ const recipients = lowerTierIds.filter(
+ (id) => id !== requestedBy && id !== userId
+ );
+ if (recipients.length > 0) {
+ await trigger("notify", {
+ event:
+ decision === "Approved"
+ ? NotificationEvent.ApprovalApproved
+ : NotificationEvent.ApprovalRejected,
+ companyId,
+ documentId: orderId,
+ documentType: "purchaseOrder",
+ recipient: { type: "users", userIds: recipients },
+ from: userId
+ });
+ }
+ } catch (e) {
+ console.error(
+ "Failed to trigger lower-tier spend notification for purchase order",
+ e
+ );
+ }
+
// If approved, handle post-approval tasks: PDF generation, document creation, email, price updates
if (decision === "Approved") {
const purchaseOrder = await getPurchaseOrder(serviceRole, orderId);
diff --git a/apps/erp/app/routes/x+/settings+/approval-rules.tsx b/apps/erp/app/routes/x+/settings+/approval-rules.tsx
index 0106630772..c4fecfe9f6 100644
--- a/apps/erp/app/routes/x+/settings+/approval-rules.tsx
+++ b/apps/erp/app/routes/x+/settings+/approval-rules.tsx
@@ -30,7 +30,9 @@ export async function loader({ request }: LoaderFunctionArgs) {
.select("id, name")
.eq("companyId", companyId)
.eq("isCustomerOrgGroup", false)
+ .eq("isCustomerTypeGroup", false)
.eq("isSupplierOrgGroup", false)
+ .eq("isSupplierTypeGroup", false)
]);
if (rules.error) {