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: migrations for multi-tenant - batch 12
Brad Barbinae0367511d40
diff --git a/apps/carbon/app/modules/parts/parts.service.ts b/apps/carbon/app/modules/parts/parts.service.tsindex 4c66764c34..10038188d6 100644--- a/apps/carbon/app/modules/parts/parts.service.ts+++ b/apps/carbon/app/modules/parts/parts.service.ts@@ -426,6 +426,7 @@ export async function upsertPartPlanning(client: SupabaseClient<Database>,partPlanning:| {+ companyId: number;partId: string;locationId: string;createdBy: string;@@ -436,7 +437,7 @@ export async function upsertPartPlanning(})) {if ("createdBy" in partPlanning) {- return client.from("partPlanning").insert(partPlanning);+ return client.from("partPlanning").insert({ ...partPlanning, companyId });}return client.from("partPlanning")diff --git a/apps/carbon/app/routes/x+/part+/$partId.planning.tsx b/apps/carbon/app/routes/x+/part+/$partId.planning.tsxindex 2314bacf53..c07e702950 100644--- a/apps/carbon/app/routes/x+/part+/$partId.planning.tsx+++ b/apps/carbon/app/routes/x+/part+/$partId.planning.tsx@@ -20,7 +20,7 @@ import { path } from "~/utils/path";import { error, success } from "~/utils/result";export async function loader({ request, params }: LoaderFunctionArgs) {- const { client, userId } = await requirePermissions(request, {+ const { client, companyId, userId } = await requirePermissions(request, {view: "parts",});@@ -65,6 +65,7 @@ export async function loader({ request, params }: LoaderFunctionArgs) {if (partPlanning.error || !partPlanning.data) {const insertPartPlanning = await upsertPartPlanning(client, {partId,+ companyId,locationId,createdBy: userId,});diff --git a/packages/database/supabase/functions/lib/types.ts b/packages/database/supabase/functions/lib/types.tsindex c9ddc0e3c7..034ccc353e 100644--- a/packages/database/supabase/functions/lib/types.ts+++ b/packages/database/supabase/functions/lib/types.ts@@ -3172,6 +3172,7 @@ export type Database = {document: {Row: {active: boolean+ companyId: numbercreatedAt: stringcreatedBy: stringdescription: string | null@@ -3192,6 +3193,7 @@ export type Database = {}Insert: {active?: boolean+ companyId: numbercreatedAt?: stringcreatedBy: stringdescription?: string | null@@ -3212,6 +3214,7 @@ export type Database = {}Update: {active?: boolean+ companyId?: numbercreatedAt?: stringcreatedBy?: stringdescription?: string | null@@ -5251,6 +5254,7 @@ export type Database = {}partInventory: {Row: {+ companyId: numbercreatedAt: stringcreatedBy: stringcustomFields: Json | null@@ -5261,6 +5265,7 @@ export type Database = {updatedBy: string | null}Insert: {+ companyId: numbercreatedAt?: stringcreatedBy: stringcustomFields?: Json | null@@ -5271,6 +5276,7 @@ export type Database = {updatedBy?: string | null}Update: {+ companyId?: numbercreatedAt?: stringcreatedBy?: stringcustomFields?: Json | null@@ -5281,6 +5287,13 @@ export type Database = {updatedBy?: string | null}Relationships: [+ {+ foreignKeyName: "partInventory_companyId_fkey"+ columns: ["companyId"]+ isOneToOne: false+ referencedRelation: "company"+ referencedColumns: ["id"]+ },{foreignKeyName: "partInventory_createdBy_fkey"columns: ["createdBy"]@@ -5632,6 +5645,7 @@ export type Database = {}partPlanning: {Row: {+ companyId: numbercreatedAt: stringcreatedBy: stringcritical: boolean@@ -5654,6 +5668,7 @@ export type Database = {updatedBy: string | null}Insert: {+ companyId: numbercreatedAt?: stringcreatedBy: stringcritical?: boolean@@ -5676,6 +5691,7 @@ export type Database = {updatedBy?: string | null}Update: {+ companyId?: numbercreatedAt?: stringcreatedBy?: stringcritical?: boolean@@ -5698,6 +5714,13 @@ export type Database = {updatedBy?: string | null}Relationships: [+ {+ foreignKeyName: "partPlanning_companyId_fkey"+ columns: ["companyId"]+ isOneToOne: false+ referencedRelation: "company"+ referencedColumns: ["id"]+ },{foreignKeyName: "partPlanning_createdBy_fkey"columns: ["createdBy"]@@ -12298,6 +12321,7 @@ export type Database = {approvedBy: string | nullassignee: string | nullblocked: boolean+ companyId: numbercreatedAt: stringcreatedBy: stringcustomFields: Json | null@@ -12317,6 +12341,7 @@ export type Database = {approvedBy?: string | nullassignee?: string | nullblocked?: boolean+ companyId: numbercreatedAt?: stringcreatedBy: stringcustomFields?: Json | null@@ -12336,6 +12361,7 @@ export type Database = {approvedBy?: string | nullassignee?: string | nullblocked?: boolean+ companyId?: numbercreatedAt?: stringcreatedBy?: stringcustomFields?: Json | null@@ -12406,6 +12432,13 @@ export type Database = {referencedRelation: "userDefaults"referencedColumns: ["userId"]},+ {+ foreignKeyName: "service_companyId_fkey"+ columns: ["companyId"]+ isOneToOne: false+ referencedRelation: "company"+ referencedColumns: ["id"]+ },{foreignKeyName: "service_createdBy_fkey"columns: ["createdBy"]@@ -12474,6 +12507,7 @@ export type Database = {serviceSupplier: {Row: {active: boolean+ companyId: numbercreatedAt: stringcreatedBy: stringcustomFields: Json | null@@ -12486,6 +12520,7 @@ export type Database = {}Insert: {active?: boolean+ companyId: numbercreatedAt?: stringcreatedBy: stringcustomFields?: Json | null@@ -12498,6 +12533,7 @@ export type Database = {}Update: {active?: boolean+ companyId?: numbercreatedAt?: stringcreatedBy?: stringcustomFields?: Json | null@@ -12509,6 +12545,13 @@ export type Database = {updatedBy?: string | null}Relationships: [+ {+ foreignKeyName: "serviceSupplier_companyId_fkey"+ columns: ["companyId"]+ isOneToOne: false+ referencedRelation: "company"+ referencedColumns: ["id"]+ },{foreignKeyName: "serviceSupplier_createdBy_fkey"columns: ["createdBy"]@@ -13038,6 +13081,7 @@ export type Database = {shippingTerm: {Row: {active: boolean+ companyId: numbercreatedAt: stringcreatedBy: stringid: string@@ -13047,6 +13091,7 @@ export type Database = {}Insert: {active?: boolean+ companyId: numbercreatedAt?: stringcreatedBy: stringid?: string@@ -13056,6 +13101,7 @@ export type Database = {}Update: {active?: boolean+ companyId?: numbercreatedAt?: stringcreatedBy?: stringid?: string@@ -13064,6 +13110,13 @@ export type Database = {updatedBy?: string | null}Relationships: [+ {+ foreignKeyName: "shippingTerm_companyId_fkey"+ columns: ["companyId"]+ isOneToOne: false+ referencedRelation: "company"+ referencedColumns: ["id"]+ },{foreignKeyName: "shippingTerm_createdBy_fkey"columns: ["createdBy"]@@ -15727,6 +15780,7 @@ export type Database = {documents: {Row: {active: boolean | null+ companyId: number | nullcreatedAt: string | nullcreatedBy: string | nulldescription: string | null@@ -18009,6 +18063,7 @@ export type Database = {approvedBy: string | nullassignee: string | nullblocked: boolean | null+ companyId: number | nullcreatedAt: string | nullcreatedBy: string | nullcustomFields: Json | null@@ -18081,6 +18136,13 @@ export type Database = {referencedRelation: "userDefaults"referencedColumns: ["userId"]},+ {+ foreignKeyName: "service_companyId_fkey"+ columns: ["companyId"]+ isOneToOne: false+ referencedRelation: "company"+ referencedColumns: ["id"]+ },{foreignKeyName: "service_createdBy_fkey"columns: ["createdBy"]@@ -18537,7 +18599,6 @@ export type Database = {has_any_company_permission: {Args: {claim: string- company: string}Returns: boolean}diff --git a/packages/database/supabase/migrations/20230123004206_claims.sql b/packages/database/supabase/migrations/20230123004206_claims.sqlindex e6ee805563..747c9c5b1b 100644--- a/packages/database/supabase/migrations/20230123004206_claims.sql+++ b/packages/database/supabase/migrations/20230123004206_claims.sql@@ -123,7 +123,7 @@ CREATE OR REPLACE FUNCTION has_company_permission(claim text, company integer) REND;$$;-CREATE OR REPLACE FUNCTION has_any_company_permission(claim text, company text) RETURNS "bool"+CREATE OR REPLACE FUNCTION has_any_company_permission(claim text) RETURNS "bool"LANGUAGE "plpgsql" SECURITY DEFINER SET search_path = publicAS $$DECLAREdiff --git a/packages/database/supabase/migrations/20230330024716_parts.sql b/packages/database/supabase/migrations/20230330024716_parts.sqlindex dd990e5b42..f605cb4ed5 100644--- a/packages/database/supabase/migrations/20230330024716_parts.sql+++ b/packages/database/supabase/migrations/20230330024716_parts.sql@@ -678,8 +678,6 @@ CREATE POLICY "Employees with parts_delete can delete shelves" ON "shelf"has_company_permission('parts_delete', "companyId"));--- TODO: resume migrations here-CREATE TABLE "partPlanning" ("partId" TEXT NOT NULL,"locationId" TEXT NOT NULL,@@ -696,6 +694,7 @@ CREATE TABLE "partPlanning" ("minimumOrderQuantity" INTEGER NOT NULL DEFAULT 0,"maximumOrderQuantity" INTEGER NOT NULL DEFAULT 0,"orderMultiple" INTEGER NOT NULL DEFAULT 1,+ "companyId" INTEGER NOT NULL,"createdBy" TEXT NOT NULL,"createdAt" TIMESTAMP WITH TIME ZONE NOT NULL DEFAULT NOW(),"updatedBy" TEXT,@@ -706,6 +705,7 @@ CREATE TABLE "partPlanning" (CONSTRAINT "partPlanning_partId_locationId_key" UNIQUE ("partId", "locationId"),CONSTRAINT "partPlanning_partId_fkey" FOREIGN KEY ("partId") REFERENCES "part"("id") ON DELETE CASCADE ON UPDATE CASCADE,CONSTRAINT "partPlanning_locationId_fkey" FOREIGN KEY ("locationId") REFERENCES "location"("id") ON DELETE CASCADE ON UPDATE CASCADE,+ CONSTRAINT "partPlanning_companyId_fkey" FOREIGN KEY ("companyId") REFERENCES "company"("id") ON DELETE CASCADE ON UPDATE CASCADE,CONSTRAINT "partPlanning_createdBy_fkey" FOREIGN KEY ("createdBy") REFERENCES "user"("id"),CONSTRAINT "partPlanning_updatedBy_fkey" FOREIGN KEY ("updatedBy") REFERENCES "user"("id"));@@ -716,23 +716,23 @@ ALTER TABLE "partPlanning" ENABLE ROW LEVEL SECURITY;CREATE POLICY "Employees with parts can view part planning" ON "partPlanning"FOR SELECTUSING (- coalesce(get_my_claim('parts_view')::boolean,false)- AND (get_my_claim('role'::text)) = '"employee"'::jsonb+ has_role('employee') AND+ has_company_permission('parts_view', "companyId"));-- these are records are created lazily when a user attempts to view themCREATE POLICY "Employees with parts can insert part planning" ON "partPlanning"FOR INSERTWITH CHECK (- coalesce(get_my_claim('parts_view')::boolean,false)- AND (get_my_claim('role'::text)) = '"employee"'::jsonb+ has_role('employee') AND+ has_company_permission('parts_view', "companyId"));CREATE POLICY "Employees with parts_update can update part planning" ON "partPlanning"FOR UPDATEUSING (- coalesce(get_my_claim('parts_update')::boolean, false) = true- AND (get_my_claim('role'::text)) = '"employee"'::jsonb+ has_role('employee') AND+ has_company_permission('parts_update', "companyId"));@@ -740,6 +740,7 @@ CREATE TABLE "partInventory" ("partId" TEXT NOT NULL,"locationId" TEXT NOT NULL,"defaultShelfId" TEXT,+ "companyId" INTEGER NOT NULL,"createdBy" TEXT NOT NULL,"createdAt" TIMESTAMP WITH TIME ZONE NOT NULL DEFAULT NOW(),"updatedBy" TEXT,@@ -750,6 +751,7 @@ CREATE TABLE "partInventory" (CONSTRAINT "partInventory_partId_fkey" FOREIGN KEY ("partId") REFERENCES "part"("id") ON DELETE CASCADE ON UPDATE CASCADE,CONSTRAINT "partInventory_locationId_fkey" FOREIGN KEY ("locationId") REFERENCES "location"("id") ON DELETE CASCADE ON UPDATE CASCADE,CONSTRAINT "partInventory_shelfId_fkey" FOREIGN KEY ("defaultShelfId", "locationId") REFERENCES "shelf"("id", "locationId") ON DELETE SET NULL,+ CONSTRAINT "partInventory_companyId_fkey" FOREIGN KEY ("companyId") REFERENCES "company"("id") ON DELETE CASCADE ON UPDATE CASCADE,CONSTRAINT "partInventory_createdBy_fkey" FOREIGN KEY ("createdBy") REFERENCES "user"("id"),CONSTRAINT "partInventory_updatedBy_fkey" FOREIGN KEY ("updatedBy") REFERENCES "user"("id"));@@ -762,23 +764,23 @@ ALTER TABLE "partInventory" ENABLE ROW LEVEL SECURITY;CREATE POLICY "Employees with part_view can view part planning" ON "partInventory"FOR SELECTUSING (- coalesce(get_my_claim('parts_view')::boolean,false)- AND (get_my_claim('role'::text)) = '"employee"'::jsonb+ has_role('employee') AND+ has_company_permission('parts_view', "companyId"));-- these are records are created lazily when a user attempts to view themCREATE POLICY "Employees with part_view can insert part planning" ON "partInventory"FOR INSERTWITH CHECK (- coalesce(get_my_claim('parts_view')::boolean,false)- AND (get_my_claim('role'::text)) = '"employee"'::jsonb+ has_role('employee') AND+ has_company_permission('parts_view', "companyId"));CREATE POLICY "Employees with parts_update can update part planning" ON "partInventory"FOR UPDATEUSING (- coalesce(get_my_claim('parts_update')::boolean, false) = true- AND (get_my_claim('role'::text)) = '"employee"'::jsonb+ has_role('employee') AND+ has_company_permission('parts_update', "companyId"));diff --git a/packages/database/supabase/migrations/20230412120726_services.sql b/packages/database/supabase/migrations/20230412120726_services.sqlindex abf4975a84..2a6e1cd3c4 100644--- a/packages/database/supabase/migrations/20230412120726_services.sql+++ b/packages/database/supabase/migrations/20230412120726_services.sql@@ -16,6 +16,7 @@ CREATE TABLE "service" ("fromDate" DATE,"toDate" DATE,"customFields" JSONB,+ "companyId" INTEGER NOT NULL,"createdBy" TEXT NOT NULL,"createdAt" TIMESTAMP WITH TIME ZONE NOT NULL DEFAULT NOW(),"updatedBy" TEXT,@@ -23,36 +24,42 @@ CREATE TABLE "service" (CONSTRAINT "service_pkey" PRIMARY KEY ("id"),CONSTRAINT "service_partGroupId_fkey" FOREIGN KEY ("partGroupId") REFERENCES "partGroup"("id"),+ CONSTRAINT "service_companyId_fkey" FOREIGN KEY ("companyId") REFERENCES "company"("id"),CONSTRAINT "service_approvedBy_fkey" FOREIGN KEY ("approvedBy") REFERENCES "user"("id"),CONSTRAINT "service_createdBy_fkey" FOREIGN KEY ("createdBy") REFERENCES "user"("id"),CONSTRAINT "service_updatedBy_fkey" FOREIGN KEY ("updatedBy") REFERENCES "user"("id"));+CREATE INDEX "service_companyId_idx" ON "service"("companyId");+CREATE POLICY "Employees can view services" ON "service"FOR SELECTUSING (- (get_my_claim('role'::text)) = '"employee"'::jsonb+ has_role('employee')+ AND "companyId" = ANY(+ select "companyId" from "userToCompany" where "userId" = auth.uid()::text+ ));CREATE POLICY "Employees with parts_create can insert services" ON "service"FOR INSERTWITH CHECK (- coalesce(get_my_claim('parts_create')::boolean,false)- AND (get_my_claim('role'::text)) = '"employee"'::jsonb+ has_role('employee') AND+ has_company_permission('parts_create', "companyId"));CREATE POLICY "Employees with parts_update can update services" ON "service"FOR UPDATEUSING (- coalesce(get_my_claim('parts_update')::boolean, false) = true- AND (get_my_claim('role'::text)) = '"employee"'::jsonb+ has_role('employee') AND+ has_company_permission('parts_update', "companyId"));CREATE POLICY "Employees with parts_delete can delete services" ON "service"FOR DELETEUSING (- coalesce(get_my_claim('parts_delete')::boolean, false) = true- AND (get_my_claim('role'::text)) = '"employee"'::jsonb+ has_role('employee') AND+ has_company_permission('parts_delete', "companyId"));@@ -62,6 +69,7 @@ CREATE TABLE "serviceSupplier" ("supplierId" TEXT NOT NULL,"supplierServiceId" TEXT,"active" BOOLEAN NOT NULL DEFAULT true,+ "companyId" INTEGER NOT NULL,"createdBy" TEXT NOT NULL,"createdAt" TIMESTAMP WITH TIME ZONE NOT NULL DEFAULT NOW(),"updatedBy" TEXT,@@ -72,6 +80,7 @@ CREATE TABLE "serviceSupplier" (CONSTRAINT "serviceSupplier_serviceId_fkey" FOREIGN KEY ("serviceId") REFERENCES "service"("id") ON DELETE CASCADE ON UPDATE CASCADE,CONSTRAINT "serviceSupplier_supplierId_fkey" FOREIGN KEY ("supplierId") REFERENCES "supplier"("id") ON DELETE CASCADE,CONSTRAINT "serviceSupplier_service_supplier_unique" UNIQUE ("serviceId", "supplierId"),+ CONSTRAINT "serviceSupplier_companyId_fkey" FOREIGN KEY ("companyId") REFERENCES "company"("id"),CONSTRAINT "serviceSupplier_createdBy_fkey" FOREIGN KEY ("createdBy") REFERENCES "user"("id"),CONSTRAINT "serviceSupplier_updatedBy_fkey" FOREIGN KEY ("updatedBy") REFERENCES "user"("id"));@@ -83,39 +92,39 @@ ALTER TABLE "serviceSupplier" ENABLE ROW LEVEL SECURITY;CREATE POLICY "Employees with part/purchasing_view can view service suppliers" ON "serviceSupplier"FOR SELECTUSING (+ has_role('employee') AND(- coalesce(get_my_claim('parts_view')::boolean,false) OR- coalesce(get_my_claim('purchasing_view')::boolean,false)+ has_company_permission('parts_create', "companyId") OR+ has_company_permission('purchase_view', "companyId"))- AND (get_my_claim('role'::text)) = '"employee"'::jsonb);CREATE POLICY "Employees with parts_update can update service suppliers" ON "serviceSupplier"FOR UPDATEUSING (- coalesce(get_my_claim('parts_update')::boolean, false) = true- AND (get_my_claim('role'::text)) = '"employee"'::jsonb+ has_role('employee') AND+ has_company_permission('parts_update', "companyId"));CREATE POLICY "Employees with parts_create can create service suppliers" ON "serviceSupplier"FOR INSERTWITH CHECK (- coalesce(get_my_claim('parts_create')::boolean, false) = true- AND (get_my_claim('role'::text)) = '"employee"'::jsonb+ has_role('employee') AND+ has_company_permission('parts_create', "companyId"));CREATE POLICY "Employees with parts_delete can delete service suppliers" ON "serviceSupplier"FOR DELETEUSING (- coalesce(get_my_claim('parts_delete')::boolean, false) = true- AND (get_my_claim('role'::text)) = '"employee"'::jsonb+ has_role('employee') AND+ has_company_permission('parts_delete', "companyId"));CREATE POLICY "Suppliers with parts_view can view their own part suppliers" ON "serviceSupplier"FOR SELECTUSING (- coalesce(get_my_claim('parts_view')::boolean,false)- AND (get_my_claim('role'::text)) = '"supplier"'::jsonb+ has_role('supplier') AND+ has_company_permission('parts_view', "companyId")AND "supplierId" IN (SELECT "supplierId" FROM "supplierAccount" WHERE id::uuid = auth.uid())@@ -124,8 +133,8 @@ CREATE POLICY "Suppliers with parts_view can view their own part suppliers" ON "CREATE POLICY "Suppliers with parts_update can update their own part suppliers" ON "serviceSupplier"FOR UPDATEUSING (- coalesce(get_my_claim('parts_update')::boolean,false)- AND (get_my_claim('role'::text)) = '"supplier"'::jsonb+ has_role('supplier') AND+ has_company_permission('parts_update', "companyId")AND "supplierId" IN (SELECT "supplierId" FROM "supplierAccount" WHERE id::uuid = auth.uid())@@ -145,6 +154,7 @@ CREATE OR REPLACE VIEW "services" WITH(SECURITY_INVOKER=true) ASs."fromDate",s."toDate",s."customFields",+ s."companyId",pg.name AS "partGroup",array_agg(ss."supplierId") AS "supplierIds"FROM "service" s@@ -163,4 +173,5 @@ CREATE OR REPLACE VIEW "services" WITH(SECURITY_INVOKER=true) ASs."fromDate",s."toDate",s."customFields",+ s."companyId",pg.name;\ No newline at end of filediff --git a/packages/database/supabase/migrations/20230423023136_documents.sql b/packages/database/supabase/migrations/20230423023136_documents.sqlindex b494912197..86cd84b65b 100644--- a/packages/database/supabase/migrations/20230423023136_documents.sql+++ b/packages/database/supabase/migrations/20230423023136_documents.sql@@ -22,6 +22,7 @@ CREATE TABLE "document" ("readGroups" TEXT[],"writeGroups" TEXT[],"active" BOOLEAN NOT NULL DEFAULT TRUE,+ "companyId" INTEGER NOT NULL,"createdBy" TEXT NOT NULL,"createdAt" TIMESTAMP WITH TIME ZONE NOT NULL DEFAULT NOW(),"updatedBy" TEXT,@@ -39,33 +40,32 @@ ALTER TABLE "document" ENABLE ROW LEVEL SECURITY;CREATE POLICY "Users with documents can view documents where they are in the readGroups" ON "document"FOR SELECT USING (- coalesce(get_my_claim('documents_view')::boolean, false) = true- AND (groups_for_user(auth.uid()::text) && "readGroups") = true+ has_company_permission('documents_view', "companyId") AND+ (groups_for_user(auth.uid()::text) && "readGroups") = true);CREATE POLICY "Users with documents_create can create documents where they are in the writeGroups" ON "document"FOR INSERT WITH CHECK (- coalesce(get_my_claim('documents_create')::boolean, false) = true- AND (groups_for_user(auth.uid()::text) && "writeGroups") = true+ has_company_permission('documents_create', "companyId") AND+ (groups_for_user(auth.uid()::text) && "writeGroups") = true);CREATE POLICY "Users with documents_update can update documents where they are in the writeGroups" ON "document"FOR UPDATE USING (- coalesce(get_my_claim('documents_update')::boolean, false) = true- AND (groups_for_user(auth.uid()::text) && "writeGroups") = true+ has_company_permission('documents_update', "companyId") AND+ (groups_for_user(auth.uid()::text) && "writeGroups") = true);CREATE POLICY "Users with documents_delete can delete documents where they are in the writeGroups" ON "document"FOR DELETE USING (- coalesce(get_my_claim('documents_delete')::boolean, false) = true- AND (groups_for_user(auth.uid()::text) && "writeGroups") = true+ has_company_permission('documents_delete', "companyId") AND+ (groups_for_user(auth.uid()::text) && "writeGroups") = true);CREATE POLICY "Private buckets view requires ownership or document.readGroups" ON storage.objectsFOR SELECT USING (bucket_id = 'private'AND (auth.role() = 'authenticated')- AND coalesce(get_my_claim('documents_view')::boolean, false) = trueAND ((storage.foldername(name))[1] = auth.uid()::textOR "name" IN (@@ -78,7 +78,7 @@ CREATE POLICY "Private buckets insert requires ownership or document.writeGroupsFOR INSERT WITH CHECK (bucket_id = 'private'AND (auth.role() = 'authenticated')- AND coalesce(get_my_claim('documents_create')::boolean, false) = true+ AND has_any_company_permission('documents_create')AND ((storage.foldername(name))[1] = auth.uid()::textOR "name" IN (@@ -91,7 +91,7 @@ CREATE POLICY "Private buckets update requires ownership or document.writeGroupsFOR UPDATE USING (bucket_id = 'private'AND (auth.role() = 'authenticated')- AND coalesce(get_my_claim('documents_update')::boolean, false) = true+ AND has_any_company_permission('documents_update')AND ((storage.foldername(name))[1] = auth.uid()::textOR "name" IN (@@ -104,7 +104,7 @@ CREATE POLICY "Private buckets delete requires ownership or document.writeGroupsFOR DELETE USING (bucket_id = 'private'AND (auth.role() = 'authenticated')- AND coalesce(get_my_claim('documents_delete')::boolean, false) = true+ AND has_any_company_permission('documents_delete')AND ((storage.foldername(name))[1] = auth.uid()::textOR "name" IN (@@ -137,6 +137,18 @@ CREATE TABLE "documentTransaction" (CREATE INDEX "documentTransaction_documentId_idx" ON "documentTransaction" ("documentId");CREATE INDEX "documentTransaction_userId_idx" ON "documentTransaction" ("userId");+ALTER TABLE "documentTransaction" ENABLE ROW LEVEL SECURITY;++CREATE POLICY "Users with documents_view can see document transactions" ON "documentTransaction"+ FOR SELECT USING (+ has_company_permission('documents_view', (SELECT "companyId" FROM "document" WHERE "id" = "documentId"))+ );++CREATE POLICY "Users can insert document transactions" ON "documentTransaction"+ FOR INSERT WITH CHECK (+ auth.uid()::text = "userId"+ );+CREATE TABLE "documentFavorite" ("documentId" TEXT NOT NULL,"userId" TEXT NOT NULL,diff --git a/packages/database/supabase/migrations/20230510035345_purchasing.sql b/packages/database/supabase/migrations/20230510035345_purchasing.sqlindex 425e6793f9..c9317cae2d 100644--- a/packages/database/supabase/migrations/20230510035345_purchasing.sql+++ b/packages/database/supabase/migrations/20230510035345_purchasing.sql@@ -40,7 +40,7 @@ CREATE POLICY "Certain employees can view payment terms" ON "paymentTerm"has_company_permission('sales_view', "companyId") = true ORhas_company_permission('purchasing_view', "companyId") = true)- AND (get_my_claim('role'::text)) = '"employee"'::jsonb+ AND has_role('employee'));@@ -48,21 +48,21 @@ CREATE POLICY "Employees with accounting_create can insert payment terms" ON "paFOR INSERTWITH CHECK (has_company_permission('accounting_create', "companyId")- AND (get_my_claim('role'::text)) = '"employee"'::jsonb+ AND has_role('employee'));CREATE POLICY "Employees with accounting_update can update payment terms" ON "paymentTerm"FOR UPDATEUSING (has_company_permission('accounting_update', "companyId") = true- AND (get_my_claim('role'::text)) = '"employee"'::jsonb+ AND has_role('employee'));CREATE POLICY "Employees with accounting_delete can delete payment terms" ON "paymentTerm"FOR DELETEUSING (has_company_permission('accounting_delete', "companyId") = true- AND (get_my_claim('role'::text)) = '"employee"'::jsonb+ AND has_role('employee'));@@ -103,41 +103,42 @@ CREATE POLICY "Certain employees can view shipping methods" ON "shippingMethod"FOR SELECTUSING ((- coalesce(get_my_claim('accounting_view')::boolean, false) = true OR- coalesce(get_my_claim('inventory_view')::boolean, false) = true OR- coalesce(get_my_claim('parts_view')::boolean, false) = true OR- coalesce(get_my_claim('purchasing_view')::boolean, false) = true OR- coalesce(get_my_claim('sales_view')::boolean, false) = true+ has_company_permission('accounting_view', "companyId") OR+ has_company_permission('inventory_view', "companyId") OR+ has_company_permission('parts_view', "companyId") OR+ has_company_permission('purchasing_view', "companyId") OR+ has_company_permission('sales_view', "companyId"))- AND (get_my_claim('role'::text)) = '"employee"'::jsonb+ AND has_role('employee'));CREATE POLICY "Employees with inventory_create can insert shipping methods" ON "shippingMethod"FOR INSERTWITH CHECK (- coalesce(get_my_claim('inventory_create')::boolean,false)- AND (get_my_claim('role'::text)) = '"employee"'::jsonb+ has_company_permission('inventory_create', "companyId")+ AND has_role('employee'));CREATE POLICY "Employees with inventory_update can update shipping methods" ON "shippingMethod"FOR UPDATEUSING (- coalesce(get_my_claim('inventory_update')::boolean, false) = true- AND (get_my_claim('role'::text)) = '"employee"'::jsonb+ has_company_permission('inventory_update', "companyId")+ AND has_role('employee'));CREATE POLICY "Employees with inventory_delete can delete shipping methods" ON "shippingMethod"FOR DELETEUSING (- coalesce(get_my_claim('inventory_delete')::boolean, false) = true- AND (get_my_claim('role'::text)) = '"employee"'::jsonb+ has_company_permission('inventory_delete', "companyId")+ AND has_role('employee'));CREATE TABLE "shippingTerm" ("id" TEXT NOT NULL DEFAULT xid(),"name" TEXT NOT NULL,"active" BOOLEAN NOT NULL DEFAULT TRUE,+ "companyId" INTEGER NOT NULL,"createdAt" TIMESTAMP WITH TIME ZONE NOT NULL DEFAULT NOW(),"createdBy" TEXT NOT NULL,"updatedAt" TIMESTAMP WITH TIME ZONE,@@ -145,10 +146,47 @@ CREATE TABLE "shippingTerm" (CONSTRAINT "shippingTerm_pkey" PRIMARY KEY ("id"),CONSTRAINT "shippingTerm_name_key" UNIQUE ("name"),+ CONSTRAINT "shippingTerm_companyId_fkey" FOREIGN KEY ("companyId") REFERENCES "company" ("id") ON DELETE CASCADE ON UPDATE CASCADE,CONSTRAINT "shippingTerm_createdBy_fkey" FOREIGN KEY ("createdBy") REFERENCES "user" ("id") ON DELETE RESTRICT,CONSTRAINT "shippingTerm_updatedBy_fkey" FOREIGN KEY ("updatedBy") REFERENCES "user" ("id") ON DELETE RESTRICT);+ALTER TABLE "shippingTerm" ENABLE ROW LEVEL SECURITY;++CREATE POLICY "Certain employees can view shipping terms" ON "shippingTerm"+ FOR SELECT+ USING (+ (+ has_company_permission('accounting_view', "companyId") OR+ has_company_permission('inventory_view', "companyId") OR+ has_company_permission('parts_view', "companyId") OR+ has_company_permission('purchasing_view', "companyId") OR+ has_company_permission('sales_view', "companyId")+ )+ AND has_role('employee')+ );++CREATE POLICY "Employees with inventory_create can insert shipping terms" ON "shippingTerm"+ FOR INSERT+ WITH CHECK (+ has_company_permission('inventory_create', "companyId")+ AND has_role('employee')+);++CREATE POLICY "Employees with inventory_update can update shipping terms" ON "shippingTerm"+ FOR UPDATE+ USING (+ has_company_permission('inventory_update', "companyId")+ AND has_role('employee')+ );++CREATE POLICY "Employees with inventory_delete can delete shipping terms" ON "shippingTerm"+ FOR DELETE+ USING (+ has_company_permission('inventory_delete', "companyId")+ AND has_role('employee')+ );+CREATE TYPE "purchaseOrderType" AS ENUM ('Purchase',@@ -224,6 +262,20 @@ CREATE TABLE "purchaseOrderStatusHistory" (CONSTRAINT "purchaseOrderStatusHistory_createdBy_fkey" FOREIGN KEY ("createdBy") REFERENCES "user" ("id") ON DELETE RESTRICT);+ALTER TABLE "purchaseOrderStatusHistory" ENABLE ROW LEVEL SECURITY;++CREATE POLICY "Users can view purchase order status history" ON "purchaseOrderStatusHistory"+ FOR SELECT USING (+ has_company_permission('purchasing_view', (SELECT "companyId" FROM "purchaseOrder" WHERE "id" = "purchaseOrderId"))+ );++CREATE POLICY "Users can insert purchase order status history" ON "purchaseOrderStatusHistory"+ FOR INSERT WITH CHECK (+ has_company_permission('purchasing_update', (SELECT "companyId" FROM "purchaseOrder" WHERE "id" = "purchaseOrderId"))+ );+++CREATE TABLE "purchaseOrderLine" ("id" TEXT NOT NULL DEFAULT xid(),@@ -392,6 +444,19 @@ CREATE TABLE "purchaseOrderTransaction" (CREATE INDEX "purchaseOrderTransaction_purchaseOrderId_idx" ON "purchaseOrderTransaction" ("purchaseOrderId");CREATE INDEX "purchaseOrderTransaction_userId_idx" ON "purchaseOrderTransaction" ("userId");+ALTER TABLE "purchaseOrderTransaction" ENABLE ROW LEVEL SECURITY;++CREATE POLICY "Employees with purchasing_view can view purchase order transactions" ON "purchaseOrderTransaction"+ FOR SELECT USING (+ has_role('employee') AND+ has_company_permission('purchasing_view', (SELECT "companyId" FROM "purchaseOrder" WHERE "id" = "purchaseOrderId"))+ );++CREATE POLICY "User with purchasing_update can insert purchase order transactions" ON "purchaseOrderTransaction"+ FOR INSERT WITH CHECK (+ has_company_permission('purchasing_update', (SELECT "companyId" FROM "purchaseOrder" WHERE "id" = "purchaseOrderId"))+ );+CREATE TABLE "purchaseOrderFavorite" ("purchaseOrderId" TEXT NOT NULL,"userId" TEXT NOT NULL,