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: people in groups with higher amounts should be able to approve requests

Brad Barbin1538d366e090
diff --git a/apps/erp/app/modules/approvals/approvals.service.ts b/apps/erp/app/modules/approvals/approvals.service.ts
index a581b04c1b..c7edbb347f 100644
--- a/apps/erp/app/modules/approvals/approvals.service.ts
+++ b/apps/erp/app/modules/approvals/approvals.service.ts
@@ -38,6 +38,44 @@ export async function canApproveRequest(
client: SupabaseClient<Database>,
approvalRequest: ApprovalRequestForApproveCheck,
userId: string
+): Promise<boolean> {
+ const rules = await getApprovalRulesForApprover(
+ client,
+ approvalRequest.documentType,
+ approvalRequest.companyId
+ );
+
+ if (!rules.data || rules.data.length === 0) {
+ return false;
+ }
+
+ const userGroups = await client.rpc("groups_for_user", { uid: userId });
+ const userGroupIds = userGroups.data || [];
+
+ // Check if user can approve via any rule (higher amount approvers can approve lower amounts)
+ return rules.data.some((rule) => {
+ if (rule.defaultApproverId === userId) {
+ return true;
+ }
+
+ const approverGroupIds = rule.approverGroupIds;
+ if (!approverGroupIds || approverGroupIds.length === 0) {
+ return false;
+ }
+
+ return approverGroupIds.some((groupId) => userGroupIds.includes(groupId));
+ });
+}
+
+/**
+ * Checks if a user can approve a request based on the specific rule matching the amount.
+ * This is the original approval check logic - user must be on the rule that matches the amount.
+ * Used for "Assigned to Me" lists.
+ */
+export async function canApproveRequestInWindow(
+ client: SupabaseClient<Database>,
+ approvalRequest: ApprovalRequestForApproveCheck,
+ userId: string
): Promise<boolean> {
const rule = await getApprovalRuleByAmount(
client,
@@ -514,8 +552,9 @@ export async function getPendingApprovalsForApprover(
})
);
+ // Use canApproveRequestInWindow to only show requests within user's specific approval window
const canApprovePromises = pendingWithReadableFields.map(async (approval) => {
- const canApprove = await canApproveRequest(
+ const canApprove = await canApproveRequestInWindow(
client,
{
amount: approval.amount,
@@ -625,6 +664,20 @@ export async function getApprovalRuleByAmount(
.maybeSingle();
}
+export async function getApprovalRulesForApprover(
+ client: SupabaseClient<Database>,
+ documentType: (typeof approvalDocumentType)[number],
+ companyId: string
+) {
+ return client
+ .from("approvalRule")
+ .select("*")
+ .eq("documentType", documentType)
+ .eq("companyId", companyId)
+ .eq("enabled", true)
+ .order("lowerBoundAmount", { ascending: false });
+}
+
export async function isApprovalRequired(
client: SupabaseClient<Database>,
documentType: (typeof approvalDocumentType)[number],
diff --git a/apps/erp/app/modules/approvals/ui/ApprovalRuleCard.tsx b/apps/erp/app/modules/approvals/ui/ApprovalRuleCard.tsx
index 33f65b270a..abc850c171 100644
--- a/apps/erp/app/modules/approvals/ui/ApprovalRuleCard.tsx
+++ b/apps/erp/app/modules/approvals/ui/ApprovalRuleCard.tsx
@@ -3,7 +3,6 @@ import {
AccordionContent,
AccordionItem,
AccordionTrigger,
- Badge,
Card,
DropdownMenu,
DropdownMenuContent,
@@ -13,6 +12,7 @@ import {
Heading,
HStack,
IconButton,
+ Status,
useDisclosure
} from "@carbon/react";
import { memo, useCallback } from "react";
@@ -78,12 +78,12 @@ const ApprovalRuleCard = memo(
) &&
` over ${currencyFormatter.format(rule.lowerBoundAmount ?? 0)}`}
</Heading>
- <Badge
- variant={rule.enabled ? "green" : "gray"}
+ <Status
+ color={rule.enabled ? "green" : "gray"}
className="text-xs font-medium"
>
{rule.enabled ? "Enabled" : "Disabled"}
- </Badge>
+ </Status>
</HStack>
</AccordionTrigger>
<div className="absolute right-12 top-1/2 -translate-y-1/2 z-10">