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: don't create duplicate userToCompany entries

Brad Barbin07593efcaa72
diff --git a/apps/carbon/app/routes/onboarding+/company.tsx b/apps/carbon/app/routes/onboarding+/company.tsx
index 7f8d5a2599..c0a0eb6822 100644
--- a/apps/carbon/app/routes/onboarding+/company.tsx
+++ b/apps/carbon/app/routes/onboarding+/company.tsx
@@ -35,10 +35,7 @@ import {
seedCompany,
updateCompany,
} from "~/modules/settings";
-import {
- addUserToCompany,
- getPermissionCacheKey,
-} from "~/modules/users/users.server";
+import { getPermissionCacheKey } from "~/modules/users/users.server";
import { requirePermissions } from "~/services/auth/auth.server";
import {
destroyAuthSession,
@@ -150,11 +147,7 @@ export async function action({ request }: ActionFunctionArgs) {
throw new Error("Fatal: failed to get location ID");
}
- const [userToCompany, job] = await Promise.all([
- addUserToCompany(supabaseClient, {
- userId,
- companyId,
- }),
+ const [job] = await Promise.all([
insertEmployeeJob(supabaseClient, {
id: userId,
companyId,
@@ -163,11 +156,6 @@ export async function action({ request }: ActionFunctionArgs) {
redis.del(getPermissionCacheKey(userId)),
]);
- if (userToCompany.error) {
- logger.error(userToCompany.error);
- throw new Error("Fatal: failed to add user to company");
- }
-
if (job.error) {
logger.error(job.error);
throw new Error("Fatal: failed to insert job");
diff --git a/packages/database/supabase/migrations/20230123004317_companies.sql b/packages/database/supabase/migrations/20230123004317_companies.sql
index 8b5b857d90..8bf0c5a7e1 100644
--- a/packages/database/supabase/migrations/20230123004317_companies.sql
+++ b/packages/database/supabase/migrations/20230123004317_companies.sql
@@ -21,12 +21,6 @@ CREATE TABLE "company" (
ALTER TABLE "company" ENABLE ROW LEVEL SECURITY;
-CREATE POLICY "Authenticated users can view company" ON "company"
- FOR SELECT
- USING (
- auth.role() = 'authenticated'
- );
-
CREATE POLICY "Employees with settings_create can create company" ON "company"
FOR INSERT
WITH CHECK (
@@ -48,15 +42,25 @@ CREATE POLICY "Employees with settings_delete can delete company" ON "company"
CREATE TABLE "userToCompany" (
"userId" TEXT NOT NULL REFERENCES "user" ("id") ON UPDATE CASCADE ON DELETE CASCADE,
- "companyId" TEXT NOT NULL REFERENCES "company" ("id") ON UPDATE CASCADE ON DELETE CASCADE
+ "companyId" TEXT NOT NULL REFERENCES "company" ("id") ON UPDATE CASCADE ON DELETE CASCADE,
+
+ CONSTRAINT "userToCompany_pkey" PRIMARY KEY ("userId", "companyId")
);
+CREATE POLICY "Authenticated users can view company" ON "company"
+ FOR SELECT
+ USING (
+ auth.role() = 'authenticated' AND "id" IN (
+ SELECT "companyId" FROM "userToCompany" WHERE "userId" = auth.uid()::text
+ )
+ );
+
ALTER TABLE "userToCompany" ENABLE ROW LEVEL SECURITY;
CREATE POLICY "Authenticated users can view userToCompany" ON "userToCompany"
FOR SELECT
USING (
- auth.role() = 'authenticated'
+ auth.role() = 'authenticated'
);
CREATE POLICY "Employees with users_create can create userToCompany" ON "userToCompany"