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: add companyId to permissions model

Brad Barbin6bac45c72262
diff --git a/apps/carbon/app/modules/users/types.ts b/apps/carbon/app/modules/users/types.ts
index d1dee6069c..97639f3973 100644
--- a/apps/carbon/app/modules/users/types.ts
+++ b/apps/carbon/app/modules/users/types.ts
@@ -34,6 +34,7 @@ export type Feature = NonNullable<
export type Group = {
data: {
id: string;
+ companyId: number;
isEmployeeTypeGroup: boolean;
isCustomerOrgGroup: boolean;
isCustomerTypeGroup: boolean;
@@ -46,10 +47,10 @@ export type Group = {
};
export type Permission = {
- view: boolean;
- create: boolean;
- update: boolean;
- delete: boolean;
+ view: number[];
+ create: number[];
+ update: number[];
+ delete: number[];
};
export type Supplier = NonNullable<
diff --git a/apps/carbon/app/modules/users/users.server.ts b/apps/carbon/app/modules/users/users.server.ts
index 1444010f3a..586addd3ed 100644
--- a/apps/carbon/app/modules/users/users.server.ts
+++ b/apps/carbon/app/modules/users/users.server.ts
@@ -336,7 +336,6 @@ export async function getUserClaims(request: Request) {
if (!claims) {
// TODO: remove service role from here, and move it up a level
const rawClaims = await getClaims(getSupabaseServiceRole(), userId);
-
if (rawClaims.error || rawClaims.data === null) {
throw redirect(
path.to.authenticatedRoot,
@@ -425,14 +424,14 @@ function makeClaimsFromEmployeeType({
data,
}: {
data: {
- view: boolean;
- create: boolean;
- update: boolean;
- delete: boolean;
+ view: number[];
+ create: number[];
+ update: number[];
+ delete: number[];
feature: ListItem | ListItem[] | null;
}[];
}) {
- const claims: Record<string, boolean> = {};
+ const claims: Record<string, number[]> = {};
data.forEach((permission) => {
if (permission.feature === null || Array.isArray(permission.feature)) {
@@ -461,7 +460,7 @@ function isClaimPermission(key: string, value: unknown) {
return (
action !== undefined &&
["view", "create", "update", "delete"].includes(action) &&
- typeof value === "boolean"
+ Array.isArray(value)
);
}
@@ -483,10 +482,10 @@ export function makeEmptyPermissionsFromFeatures(data: Feature[]) {
acc[module.name] = {
id: module.id,
permission: {
- view: false,
- create: false,
- update: false,
- delete: false,
+ view: [],
+ create: [],
+ update: [],
+ delete: [],
},
};
return acc;
@@ -505,25 +504,25 @@ export function makePermissionsFromClaims(claims: Json[] | null) {
const [module, action] = key.split("_");
if (!(module in permissions)) {
permissions[module] = {
- view: false,
- create: false,
- update: false,
- delete: false,
+ view: [],
+ create: [],
+ update: [],
+ delete: [],
};
}
switch (action) {
case "view":
- permissions[module]["view"] = value as boolean;
+ permissions[module]["view"] = value as number[];
break;
case "create":
- permissions[module]["create"] = value as boolean;
+ permissions[module]["create"] = value as number[];
break;
case "update":
- permissions[module]["update"] = value as boolean;
+ permissions[module]["update"] = value as number[];
break;
case "delete":
- permissions[module]["delete"] = value as boolean;
+ permissions[module]["delete"] = value as number[];
break;
}
}
@@ -603,7 +602,7 @@ export async function resetPassword(userId: string, password: string) {
async function setUserClaims(
userId: string,
- claims: Record<string, boolean | string>
+ claims: Record<string, number[] | string>
) {
return getSupabaseServiceRole().auth.admin.updateUserById(userId, {
app_metadata: claims,
@@ -652,7 +651,7 @@ export async function updatePermissions(
? {}
: claims.data;
- const newClaims: Record<string, boolean> = {};
+ const newClaims: Record<string, number[]> = {};
Object.entries(permissions).forEach(([name, permission]) => {
const module = name.toLowerCase();
if (!addOnly || permission.view)
@@ -666,8 +665,8 @@ export async function updatePermissions(
});
const claimsUpdate = await setUserClaims(id, {
- ...(currentClaims as Record<string, boolean>),
- ...(newClaims as Record<string, boolean>),
+ ...(currentClaims as Record<string, number[]>),
+ ...(newClaims as Record<string, number[]>),
});
if (claimsUpdate.error)
return error(claimsUpdate.error, "Failed to update claims");
diff --git a/apps/carbon/app/services/auth/auth.server.ts b/apps/carbon/app/services/auth/auth.server.ts
index 9b42e3ef5b..56b4344034 100644
--- a/apps/carbon/app/services/auth/auth.server.ts
+++ b/apps/carbon/app/services/auth/auth.server.ts
@@ -103,14 +103,18 @@ export async function requirePermissions(
return myClaims.role === permission;
}
if (!(permission in myClaims.permissions)) return false;
- return myClaims.permissions[permission][
- action as "view" | "create" | "update" | "delete"
- ];
- } else if (Array.isArray(permission)) {
- return permission.every((p) => {
- return myClaims.permissions[p][
+ const permissionForCompany =
+ myClaims.permissions[permission][
action as "view" | "create" | "update" | "delete"
];
+ return permissionForCompany.includes(0); // TODO: check if the user has access to the company
+ } else if (Array.isArray(permission)) {
+ return permission.every((p) => {
+ const permissionForCompany =
+ myClaims.permissions[p][
+ action as "view" | "create" | "update" | "delete"
+ ];
+ return permissionForCompany.includes(0); // TODO: check if the user has access to the company
});
} else {
return false;
diff --git a/packages/database/src/seed.ts b/packages/database/src/seed.ts
index 9ab3094048..fb2369d612 100644
--- a/packages/database/src/seed.ts
+++ b/packages/database/src/seed.ts
@@ -114,10 +114,10 @@ async function seed() {
const employeeTypePermissions = [] as {
employeeTypeId: string;
featureId: string;
- create: boolean;
- update: boolean;
- delete: boolean;
- view: boolean;
+ create: number[];
+ update: number[];
+ delete: number[];
+ view: number[];
}[];
Object.entries(employeeTypePermissionsDefinitions).forEach(
diff --git a/packages/database/src/seed/admin.ts b/packages/database/src/seed/admin.ts
index 57061e0b3f..d2676050c8 100644
--- a/packages/database/src/seed/admin.ts
+++ b/packages/database/src/seed/admin.ts
@@ -1,3 +1,4 @@
+import { ALL_TEAMS_ID } from "./employee-types";
import { possibleFeatures } from "./features";
const admin = {
@@ -7,16 +8,16 @@ const admin = {
lastName: "Admin",
};
-const claims: Record<string, boolean | string> = {
+const claims: Record<string, string | number[]> = {
role: "employee",
};
possibleFeatures.forEach((name) => {
const moduleName = name.toLowerCase();
- claims[`${moduleName}_view`] = true;
- claims[`${moduleName}_create`] = true;
- claims[`${moduleName}_update`] = true;
- claims[`${moduleName}_delete`] = true;
+ claims[`${moduleName}_view`] = [ALL_TEAMS_ID];
+ claims[`${moduleName}_create`] = [ALL_TEAMS_ID];
+ claims[`${moduleName}_update`] = [ALL_TEAMS_ID];
+ claims[`${moduleName}_delete`] = [ALL_TEAMS_ID];
});
export { admin, claims };
diff --git a/packages/database/src/seed/employee-types.ts b/packages/database/src/seed/employee-types.ts
index 971f616ce3..1e1e10c1ae 100644
--- a/packages/database/src/seed/employee-types.ts
+++ b/packages/database/src/seed/employee-types.ts
@@ -1,16 +1,16 @@
import { v4 as uuidv4 } from "uuid";
import type { Feature } from "./features";
-export const possibleEmployees = ["Admin", "Project Manager", "Sales"] as const;
+export const initialEmployeeTypes = ["Admin"] as const;
-export type EmployeeType = (typeof possibleEmployees)[number];
+export type EmployeeType = (typeof initialEmployeeTypes)[number];
const employeeTypes = {} as Record<
EmployeeType,
{ id: string; name: string; protected?: boolean }
>;
-possibleEmployees.forEach((type) => {
+initialEmployeeTypes.forEach((type) => {
employeeTypes[type] = {
id: uuidv4(),
name: type,
@@ -18,270 +18,100 @@ possibleEmployees.forEach((type) => {
};
});
+export const ALL_TEAMS_ID = 0;
+
export { employeeTypes };
export const employeeTypePermissionsDefinitions: Record<
EmployeeType,
Record<
Feature,
- { create: boolean; update: boolean; delete: boolean; view: boolean }
+ { create: number[]; update: number[]; delete: number[]; view: number[] }
>
> = {
Admin: {
Accounting: {
- create: true,
- update: true,
- delete: true,
- view: true,
- },
- Invoicing: {
- create: true,
- update: true,
- delete: true,
- view: true,
- },
- Parts: {
- create: true,
- update: true,
- delete: true,
- view: true,
- },
- // Jobs: {
- // create: true,
- // update: true,
- // delete: true,
- // view: true,
- // },
- Inventory: {
- create: true,
- update: true,
- delete: true,
- view: true,
- },
- // Scheduling: {
- // create: true,
- // update: true,
- // delete: true,
- // view: true,
- // },
- Sales: {
- create: true,
- update: true,
- delete: true,
- view: true,
- },
- Purchasing: {
- create: true,
- update: true,
- delete: true,
- view: true,
- },
- Documents: {
- create: true,
- update: true,
- delete: true,
- view: true,
- },
- // Messaging: {
- // create: true,
- // update: true,
- // delete: true,
- // view: true,
- // },
- // Timecards: {
- // create: true,
- // update: true,
- // delete: true,
- // view: true,
- // },
- Resources: {
- create: true,
- update: true,
- delete: true,
- view: true,
- },
- Users: {
- create: true,
- update: true,
- delete: true,
- view: true,
- },
- Settings: {
- create: true,
- update: true,
- delete: true,
- view: true,
- },
- },
- "Project Manager": {
- Accounting: {
- create: false,
- update: false,
- delete: false,
- view: false,
- },
- Invoicing: {
- create: false,
- update: false,
- delete: false,
- view: false,
- },
- Parts: {
- create: true,
- update: true,
- delete: true,
- view: true,
- },
- // Jobs: {
- // create: true,
- // update: true,
- // delete: true,
- // view: true,
- // },
- Inventory: {
- create: false,
- update: false,
- delete: false,
- view: true,
- },
- // Scheduling: {
- // create: true,
- // update: true,
- // delete: true,
- // view: true,
- // },
- Sales: {
- create: false,
- update: false,
- delete: false,
- view: false,
- },
- Purchasing: {
- create: false,
- update: false,
- delete: false,
- view: false,
- },
- Documents: {
- create: true,
- update: true,
- delete: true,
- view: true,
- },
- // Messaging: {
- // create: true,
- // update: true,
- // delete: true,
- // view: true,
- // },
- // Timecards: {
- // create: false,
- // update: false,
- // delete: false,
- // view: true,
- // },
- Resources: {
- create: true,
- update: true,
- delete: true,
- view: true,
- },
- Users: {
- create: false,
- update: false,
- delete: false,
- view: false,
- },
- Settings: {
- create: false,
- update: false,
- delete: false,
- view: false,
- },
- },
- Sales: {
- Accounting: {
- create: false,
- update: false,
- delete: false,
- view: false,
+ create: [ALL_TEAMS_ID],
+ update: [ALL_TEAMS_ID],
+ delete: [ALL_TEAMS_ID],
+ view: [ALL_TEAMS_ID],
},
Invoicing: {
- create: false,
- update: false,
- delete: false,
- view: false,
+ create: [ALL_TEAMS_ID],
+ update: [ALL_TEAMS_ID],
+ delete: [ALL_TEAMS_ID],
+ view: [ALL_TEAMS_ID],
},
Parts: {
- create: false,
- update: false,
- delete: false,
- view: true,
+ create: [ALL_TEAMS_ID],
+ update: [ALL_TEAMS_ID],
+ delete: [ALL_TEAMS_ID],
+ view: [ALL_TEAMS_ID],
},
// Jobs: {
- // create: false,
- // update: false,
- // delete: false,
- // view: true,
+ // create: [ALL_TEAMS_ID],
+ // update: [ALL_TEAMS_ID],
+ // delete: [ALL_TEAMS_ID],
+ // view: [ALL_TEAMS_ID],
// },
Inventory: {
- create: false,
- update: false,
- delete: false,
- view: true,
+ create: [ALL_TEAMS_ID],
+ update: [ALL_TEAMS_ID],
+ delete: [ALL_TEAMS_ID],
+ view: [ALL_TEAMS_ID],
},
// Scheduling: {
- // create: false,
- // update: false,
- // delete: false,
- // view: true,
+ // create: [ALL_TEAMS_ID],
+ // update: [ALL_TEAMS_ID],
+ // delete: [ALL_TEAMS_ID],
+ // view: [ALL_TEAMS_ID],
// },
Sales: {
- create: true,
- update: true,
- delete: true,
- view: true,
+ create: [ALL_TEAMS_ID],
+ update: [ALL_TEAMS_ID],
+ delete: [ALL_TEAMS_ID],
+ view: [ALL_TEAMS_ID],
},
Purchasing: {
- create: false,
- update: false,
- delete: false,
- view: false,
+ create: [ALL_TEAMS_ID],
+ update: [ALL_TEAMS_ID],
+ delete: [ALL_TEAMS_ID],
+ view: [ALL_TEAMS_ID],
},
Documents: {
- create: true,
- update: true,
- delete: true,
- view: true,
+ create: [ALL_TEAMS_ID],
+ update: [ALL_TEAMS_ID],
+ delete: [ALL_TEAMS_ID],
+ view: [ALL_TEAMS_ID],
},
// Messaging: {
- // create: true,
- // update: true,
- // delete: true,
- // view: true,
+ // create: [ALL_TEAMS_ID],
+ // update: [ALL_TEAMS_ID],
+ // delete: [ALL_TEAMS_ID],
+ // view: [ALL_TEAMS_ID],
// },
// Timecards: {
- // create: false,
- // update: false,
- // delete: false,
- // view: false,
+ // create: [ALL_TEAMS_ID],
+ // update: [ALL_TEAMS_ID],
+ // delete: [ALL_TEAMS_ID],
+ // view: [ALL_TEAMS_ID],
// },
Resources: {
- create: false,
- update: false,
- delete: false,
- view: false,
+ create: [ALL_TEAMS_ID],
+ update: [ALL_TEAMS_ID],
+ delete: [ALL_TEAMS_ID],
+ view: [ALL_TEAMS_ID],
},
Users: {
- create: false,
- update: false,
- delete: false,
- view: false,
+ create: [ALL_TEAMS_ID],
+ update: [ALL_TEAMS_ID],
+ delete: [ALL_TEAMS_ID],
+ view: [ALL_TEAMS_ID],
},
Settings: {
- create: false,
- update: false,
- delete: false,
- view: false,
+ create: [ALL_TEAMS_ID],
+ update: [ALL_TEAMS_ID],
+ delete: [ALL_TEAMS_ID],
+ view: [ALL_TEAMS_ID],
},
},
};
diff --git a/packages/database/src/types.ts b/packages/database/src/types.ts
index dafba481bf..429c8b9175 100644
--- a/packages/database/src/types.ts
+++ b/packages/database/src/types.ts
@@ -2680,6 +2680,7 @@ export type Database = {
};
customerType: {
Row: {
+ companyId: number | null;
createdAt: string;
createdBy: string;
customFields: Json | null;
@@ -2690,6 +2691,7 @@ export type Database = {
updatedBy: string | null;
};
Insert: {
+ companyId?: number | null;
createdAt?: string;
createdBy: string;
customFields?: Json | null;
@@ -2700,6 +2702,7 @@ export type Database = {
updatedBy?: string | null;
};
Update: {
+ companyId?: number | null;
createdAt?: string;
createdBy?: string;
customFields?: Json | null;
@@ -3619,6 +3622,7 @@ export type Database = {
};
employeeType: {
Row: {
+ companyId: number | null;
createdAt: string;
id: string;
name: string;
@@ -3626,6 +3630,7 @@ export type Database = {
updatedAt: string | null;
};
Insert: {
+ companyId?: number | null;
createdAt?: string;
id?: string;
name: string;
@@ -3633,44 +3638,53 @@ export type Database = {
updatedAt?: string | null;
};
Update: {
+ companyId?: number | null;
createdAt?: string;
id?: string;
name?: string;
protected?: boolean;
updatedAt?: string | null;
};
- Relationships: [];
+ Relationships: [
+ {
+ foreignKeyName: "employeeType_companyId_fkey";
+ columns: ["companyId"];
+ isOneToOne: false;
+ referencedRelation: "company";
+ referencedColumns: ["id"];
+ }
+ ];
};
employeeTypePermission: {
Row: {
- create: boolean;
+ create: number[];
createdAt: string;
- delete: boolean;
+ delete: number[];
employeeTypeId: string;
featureId: string;
- update: boolean;
+ update: number[];
updatedAt: string | null;
- view: boolean;
+ view: number[];
};
Insert: {
- create?: boolean;
+ create?: number[];
createdAt?: string;
- delete?: boolean;
+ delete?: number[];
employeeTypeId: string;
featureId: string;
- update?: boolean;
+ update?: number[];
updatedAt?: string | null;
- view?: boolean;
+ view?: number[];
};
Update: {
- create?: boolean;
+ create?: number[];
createdAt?: string;
- delete?: boolean;
+ delete?: number[];
employeeTypeId?: string;
featureId?: string;
- update?: boolean;
+ update?: number[];
updatedAt?: string | null;
- view?: boolean;
+ view?: number[];
};
Relationships: [
{
@@ -4021,6 +4035,7 @@ export type Database = {
};
group: {
Row: {
+ companyId: number | null;
createdAt: string;
id: string;
isCustomerOrgGroup: boolean;
@@ -4033,6 +4048,7 @@ export type Database = {
updatedAt: string | null;
};
Insert: {
+ companyId?: number | null;
createdAt?: string;
id?: string;
isCustomerOrgGroup?: boolean;
@@ -4045,6 +4061,7 @@ export type Database = {
updatedAt?: string | null;
};
Update: {
+ companyId?: number | null;
createdAt?: string;
id?: string;
isCustomerOrgGroup?: boolean;
@@ -4056,7 +4073,15 @@ export type Database = {
name?: string;
updatedAt?: string | null;
};
- Relationships: [];
+ Relationships: [
+ {
+ foreignKeyName: "group_companyId_fkey";
+ columns: ["companyId"];
+ isOneToOne: false;
+ referencedRelation: "company";
+ referencedColumns: ["id"];
+ }
+ ];
};
holiday: {
Row: {
@@ -13471,6 +13496,7 @@ export type Database = {
};
supplierType: {
Row: {
+ companyId: number | null;
createdAt: string;
createdBy: string;
customFields: Json | null;
@@ -13481,6 +13507,7 @@ export type Database = {
updatedBy: string | null;
};
Insert: {
+ companyId?: number | null;
createdAt?: string;
createdBy: string;
customFields?: Json | null;
@@ -13491,6 +13518,7 @@ export type Database = {
updatedBy?: string | null;
};
Update: {
+ companyId?: number | null;
createdAt?: string;
createdBy?: string;
customFields?: Json | null;
@@ -13501,6 +13529,13 @@ export type Database = {
updatedBy?: string | null;
};
Relationships: [
+ {
+ foreignKeyName: "supplierType_companyId_fkey";
+ columns: ["companyId"];
+ isOneToOne: false;
+ referencedRelation: "company";
+ referencedColumns: ["id"];
+ },
{
foreignKeyName: "supplierType_createdBy_fkey";
columns: ["createdBy"];
@@ -14118,6 +14153,57 @@ export type Database = {
}
];
};
+ userToCompany: {
+ Row: {
+ companyId: number;
+ userId: string;
+ };
+ Insert: {
+ companyId: number;
+ userId: string;
+ };
+ Update: {
+ companyId?: number;
+ userId?: string;
+ };
+ Relationships: [
+ {
+ foreignKeyName: "userToCompany_companyId_fkey";
+ columns: ["companyId"];
+ isOneToOne: false;
+ referencedRelation: "company";
+ referencedColumns: ["id"];
+ },
+ {
+ foreignKeyName: "userToCompany_userId_fkey";
+ columns: ["userId"];
+ isOneToOne: false;
+ referencedRelation: "employees";
+ referencedColumns: ["id"];
+ },
+ {
+ foreignKeyName: "userToCompany_userId_fkey";
+ columns: ["userId"];
+ isOneToOne: false;
+ referencedRelation: "employeeSummary";
+ referencedColumns: ["id"];
+ },
+ {
+ foreignKeyName: "userToCompany_userId_fkey";
+ columns: ["userId"];
+ isOneToOne: false;
+ referencedRelation: "user";
+ referencedColumns: ["id"];
+ },
+ {
+ foreignKeyName: "userToCompany_userId_fkey";
+ columns: ["userId"];
+ isOneToOne: false;
+ referencedRelation: "userDefaults";
+ referencedColumns: ["userId"];
+ }
+ ];
+ };
warehouse: {
Row: {
active: boolean;
@@ -14725,6 +14811,7 @@ export type Database = {
Row: {
accountManagerId: string | null;
assignee: string | null;
+ companyId: number | null;
createdAt: string | null;
createdBy: string | null;
customerStatusId: string | null;
@@ -14733,7 +14820,6 @@ export type Database = {
id: string | null;
logo: string | null;
name: string | null;
- orderCount: number | null;
status: string | null;
taxId: string | null;
type: string | null;
@@ -14797,6 +14883,13 @@ export type Database = {
referencedRelation: "userDefaults";
referencedColumns: ["userId"];
},
+ {
+ foreignKeyName: "customer_companyId_fkey";
+ columns: ["companyId"];
+ isOneToOne: false;
+ referencedRelation: "company";
+ referencedColumns: ["id"];
+ },
{
foreignKeyName: "customer_createdBy_fkey";
columns: ["createdBy"];
@@ -15027,6 +15120,7 @@ export type Database = {
};
groupMembers: {
Row: {
+ companyId: number | null;
groupId: string | null;
id: number | null;
isCustomerOrgGroup: boolean | null;
@@ -15041,6 +15135,13 @@ export type Database = {
user: Json | null;
};
Relationships: [
+ {
+ foreignKeyName: "group_companyId_fkey";
+ columns: ["companyId"];
+ isOneToOne: false;
+ referencedRelation: "company";
+ referencedColumns: ["id"];
+ },
{
foreignKeyName: "membership_groupId_fkey";
columns: ["groupId"];
@@ -15087,6 +15188,7 @@ export type Database = {
};
groups: {
Row: {
+ companyId: number | null;
id: string | null;
isCustomerOrgGroup: boolean | null;
isCustomerTypeGroup: boolean | null;
@@ -15101,6 +15203,7 @@ export type Database = {
};
groups_recursive: {
Row: {
+ companyId: number | null;
groupId: string | null;
isCustomerOrgGroup: boolean | null;
isCustomerTypeGroup: boolean | null;
@@ -17401,6 +17504,7 @@ export type Database = {
Row: {
accountManagerId: string | null;
assignee: string | null;
+ companyId: number | null;
createdAt: string | null;
createdBy: string | null;
customFields: Json | null;
@@ -17474,6 +17578,13 @@ export type Database = {
referencedRelation: "userDefaults";
referencedColumns: ["userId"];
},
+ {
+ foreignKeyName: "supplier_companyId_fkey";
+ columns: ["companyId"];
+ isOneToOne: false;
+ referencedRelation: "company";
+ referencedColumns: ["id"];
+ },
{
foreignKeyName: "supplier_createdBy_fkey";
columns: ["createdBy"];
@@ -17638,6 +17749,7 @@ export type Database = {
Returns: {
id: string;
name: string;
+ companyId: number;
parentId: string;
isEmployeeTypeGroup: boolean;
isCustomerOrgGroup: boolean;
diff --git a/packages/database/supabase/functions/lib/types.ts b/packages/database/supabase/functions/lib/types.ts
index 0cf3b2d8de..dfdad3ee53 100644
--- a/packages/database/supabase/functions/lib/types.ts
+++ b/packages/database/supabase/functions/lib/types.ts
@@ -1363,7 +1363,7 @@ export type Database = {
countryCode: string | null
email: string | null
fax: string | null
- id: string
+ id: number
logo: string | null
name: string
phone: string | null
@@ -1380,7 +1380,7 @@ export type Database = {
countryCode?: string | null
email?: string | null
fax?: string | null
- id?: string
+ id?: number
logo?: string | null
name: string
phone?: string | null
@@ -1397,7 +1397,7 @@ export type Database = {
countryCode?: string | null
email?: string | null
fax?: string | null
- id?: string
+ id?: number
logo?: string | null
name?: string
phone?: string | null
@@ -1957,7 +1957,7 @@ export type Database = {
Row: {
accountManagerId: string | null
assignee: string | null
- companyId: string | null
+ companyId: number | null
createdAt: string
createdBy: string | null
customerStatusId: string | null
@@ -1973,7 +1973,7 @@ export type Database = {
Insert: {
accountManagerId?: string | null
assignee?: string | null
- companyId?: string | null
+ companyId?: number | null
createdAt?: string
createdBy?: string | null
customerStatusId?: string | null
@@ -1989,7 +1989,7 @@ export type Database = {
Update: {
accountManagerId?: string | null
assignee?: string | null
- companyId?: string | null
+ companyId?: number | null
createdAt?: string
createdBy?: string | null
customerStatusId?: string | null
@@ -2690,6 +2690,7 @@ export type Database = {
}
customerType: {
Row: {
+ companyId: number | null
createdAt: string
createdBy: string
customFields: Json | null
@@ -2700,6 +2701,7 @@ export type Database = {
updatedBy: string | null
}
Insert: {
+ companyId?: number | null
createdAt?: string
createdBy: string
customFields?: Json | null
@@ -2710,6 +2712,7 @@ export type Database = {
updatedBy?: string | null
}
Update: {
+ companyId?: number | null
createdAt?: string
createdBy?: string
customFields?: Json | null
@@ -3629,6 +3632,7 @@ export type Database = {
}
employeeType: {
Row: {
+ companyId: number | null
createdAt: string
id: string
name: string
@@ -3636,6 +3640,7 @@ export type Database = {
updatedAt: string | null
}
Insert: {
+ companyId?: number | null
createdAt?: string
id?: string
name: string
@@ -3643,44 +3648,53 @@ export type Database = {
updatedAt?: string | null
}
Update: {
+ companyId?: number | null
createdAt?: string
id?: string
name?: string
protected?: boolean
updatedAt?: string | null
}
- Relationships: []
+ Relationships: [
+ {
+ foreignKeyName: "employeeType_companyId_fkey"
+ columns: ["companyId"]
+ isOneToOne: false
+ referencedRelation: "company"
+ referencedColumns: ["id"]
+ },
+ ]
}
employeeTypePermission: {
Row: {
- create: boolean
+ create: number[]
createdAt: string
- delete: boolean
+ delete: number[]
employeeTypeId: string
featureId: string
- update: boolean
+ update: number[]
updatedAt: string | null
- view: boolean
+ view: number[]
}
Insert: {
- create?: boolean
+ create?: number[]
createdAt?: string
- delete?: boolean
+ delete?: number[]
employeeTypeId: string
featureId: string
- update?: boolean
+ update?: number[]
updatedAt?: string | null
- view?: boolean
+ view?: number[]
}
Update: {
- create?: boolean
+ create?: number[]
createdAt?: string
- delete?: boolean
+ delete?: number[]
employeeTypeId?: string
featureId?: string
- update?: boolean
+ update?: number[]
updatedAt?: string | null
- view?: boolean
+ view?: number[]
}
Relationships: [
{
@@ -4031,6 +4045,7 @@ export type Database = {
}
group: {
Row: {
+ companyId: number | null
createdAt: string
id: string
isCustomerOrgGroup: boolean
@@ -4043,6 +4058,7 @@ export type Database = {
updatedAt: string | null
}
Insert: {
+ companyId?: number | null
createdAt?: string
id?: string
isCustomerOrgGroup?: boolean
@@ -4055,6 +4071,7 @@ export type Database = {
updatedAt?: string | null
}
Update: {
+ companyId?: number | null
createdAt?: string
id?: string
isCustomerOrgGroup?: boolean
@@ -4066,7 +4083,15 @@ export type Database = {
name?: string
updatedAt?: string | null
}
- Relationships: []
+ Relationships: [
+ {
+ foreignKeyName: "group_companyId_fkey"
+ columns: ["companyId"]
+ isOneToOne: false
+ referencedRelation: "company"
+ referencedColumns: ["id"]
+ },
+ ]
}
holiday: {
Row: {
@@ -4526,7 +4551,6 @@ export type Database = {
approvedBy: string | null
assignee: string | null
blocked: boolean
- companyId: string | null
createdAt: string
createdBy: string
customFields: Json | null
@@ -4549,7 +4573,6 @@ export type Database = {
approvedBy?: string | null
assignee?: string | null
blocked?: boolean
- companyId?: string | null
createdAt?: string
createdBy: string
customFields?: Json | null
@@ -4572,7 +4595,6 @@ export type Database = {
approvedBy?: string | null
assignee?: string | null
blocked?: boolean
- companyId?: string | null
createdAt?: string
createdBy?: string
customFields?: Json | null
@@ -4646,13 +4668,6 @@ export type Database = {
referencedRelation: "userDefaults"
referencedColumns: ["userId"]
},
- {
- foreignKeyName: "part_companyId_fkey"
- columns: ["companyId"]
- isOneToOne: false
- referencedRelation: "company"
- referencedColumns: ["id"]
- },
{
foreignKeyName: "part_createdBy_fkey"
columns: ["createdBy"]
@@ -6713,7 +6728,6 @@ export type Database = {
Row: {
assignee: string | null
balance: number
- companyId: string | null
createdAt: string
createdBy: string
currencyCode: string
@@ -6742,7 +6756,6 @@ export type Database = {
Insert: {
assignee?: string | null
balance?: number
- companyId?: string | null
createdAt?: string
createdBy: string
currencyCode: string
@@ -6771,7 +6784,6 @@ export type Database = {
Update: {
assignee?: string | null
balance?: number
- companyId?: string | null
createdAt?: string
createdBy?: string
currencyCode?: string
@@ -6826,13 +6838,6 @@ export type Database = {
referencedRelation: "userDefaults"
referencedColumns: ["userId"]
},
- {
- foreignKeyName: "purchaseInvoice_companyId_fkey"
- columns: ["companyId"]
- isOneToOne: false
- referencedRelation: "company"
- referencedColumns: ["id"]
- },
{
foreignKeyName: "purchaseInvoice_createdBy_fkey"
columns: ["createdBy"]
@@ -7448,7 +7453,6 @@ export type Database = {
assignee: string | null
closedAt: string | null
closedBy: string | null
- companyId: string | null
createdAt: string
createdBy: string
customFields: Json | null
@@ -7470,7 +7474,6 @@ export type Database = {
assignee?: string | null
closedAt?: string | null
closedBy?: string | null
- companyId?: string | null
createdAt?: string
createdBy: string
customFields?: Json | null
@@ -7492,7 +7495,6 @@ export type Database = {
assignee?: string | null
closedAt?: string | null
closedBy?: string | null
- companyId?: string | null
createdAt?: string
createdBy?: string
customFields?: Json | null
@@ -7567,13 +7569,6 @@ export type Database = {
referencedRelation: "userDefaults"
referencedColumns: ["userId"]
},
- {
- foreignKeyName: "purchaseOrder_companyId_fkey"
- columns: ["companyId"]
- isOneToOne: false
- referencedRelation: "company"
- referencedColumns: ["id"]
- },
{
foreignKeyName: "purchaseOrder_createdBy_fkey"
columns: ["createdBy"]
@@ -8580,7 +8575,6 @@ export type Database = {
quote: {
Row: {
assignee: string | null
- companyId: string | null
createdAt: string
createdBy: string
customerContactId: string | null
@@ -8602,7 +8596,6 @@ export type Database = {
}
Insert: {
assignee?: string | null
- companyId?: string | null
createdAt?: string
createdBy: string
customerContactId?: string | null
@@ -8624,7 +8617,6 @@ export type Database = {
}
Update: {
assignee?: string | null
- companyId?: string | null
createdAt?: string
createdBy?: string
customerContactId?: string | null
@@ -8673,13 +8665,6 @@ export type Database = {
referencedRelation: "userDefaults"
referencedColumns: ["userId"]
},
- {
- foreignKeyName: "quote_companyId_fkey"
- columns: ["companyId"]
- isOneToOne: false
- referencedRelation: "company"
- referencedColumns: ["id"]
- },
{
foreignKeyName: "quote_createdBy_fkey"
columns: ["createdBy"]
@@ -9653,7 +9638,6 @@ export type Database = {
receipt: {
Row: {
assignee: string | null
- companyId: string | null
createdAt: string
createdBy: string
customFields: Json | null
@@ -9675,7 +9659,6 @@ export type Database = {
}
Insert: {
assignee?: string | null
- companyId?: string | null
createdAt?: string
createdBy: string
customFields?: Json | null
@@ -9697,7 +9680,6 @@ export type Database = {
}
Update: {
assignee?: string | null
- companyId?: string | null
createdAt?: string
createdBy?: string
customFields?: Json | null
@@ -9746,13 +9728,6 @@ export type Database = {
referencedRelation: "userDefaults"
referencedColumns: ["userId"]
},
- {
- foreignKeyName: "receipt_companyId_fkey"
- columns: ["companyId"]
- isOneToOne: false
- referencedRelation: "company"
- referencedColumns: ["id"]
- },
{
foreignKeyName: "receipt_createdBy_fkey"
columns: ["createdBy"]
@@ -10068,7 +10043,6 @@ export type Database = {
requestForQuote: {
Row: {
assignee: string | null
- companyId: string | null
createdAt: string | null
createdBy: string
customFields: Json | null
@@ -10086,7 +10060,6 @@ export type Database = {
}
Insert: {
assignee?: string | null
- companyId?: string | null
createdAt?: string | null
createdBy: string
customFields?: Json | null
@@ -10104,7 +10077,6 @@ export type Database = {
}
Update: {
assignee?: string | null
- companyId?: string | null
createdAt?: string | null
createdBy?: string
customFields?: Json | null
@@ -10149,13 +10121,6 @@ export type Database = {
referencedRelation: "userDefaults"
referencedColumns: ["userId"]
},
- {
- foreignKeyName: "requestForQuote_companyId_fkey"
- columns: ["companyId"]
- isOneToOne: false
- referencedRelation: "company"
- referencedColumns: ["id"]
- },
{
foreignKeyName: "requestForQuote_createdBy_fkey"
columns: ["createdBy"]
@@ -11838,7 +11803,6 @@ export type Database = {
approvedBy: string | null
assignee: string | null
blocked: boolean
- companyId: string | null
createdAt: string
createdBy: string
customFields: Json | null
@@ -11858,7 +11822,6 @@ export type Database = {
approvedBy?: string | null
assignee?: string | null
blocked?: boolean
- companyId?: string | null
createdAt?: string
createdBy: string
customFields?: Json | null
@@ -11878,7 +11841,6 @@ export type Database = {
approvedBy?: string | null
assignee?: string | null
blocked?: boolean
- companyId?: string | null
createdAt?: string
createdBy?: string
customFields?: Json | null
@@ -11949,13 +11911,6 @@ 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"]
@@ -12646,7 +12601,7 @@ export type Database = {
Row: {
accountManagerId: string | null
assignee: string | null
- companyId: string | null
+ companyId: number | null
createdAt: string
createdBy: string | null
customFields: Json | null
@@ -12662,7 +12617,7 @@ export type Database = {
Insert: {
accountManagerId?: string | null
assignee?: string | null
- companyId?: string | null
+ companyId?: number | null
createdAt?: string
createdBy?: string | null
customFields?: Json | null
@@ -12678,7 +12633,7 @@ export type Database = {
Update: {
accountManagerId?: string | null
assignee?: string | null
- companyId?: string | null
+ companyId?: number | null
createdAt?: string
createdBy?: string | null
customFields?: Json | null
@@ -13561,6 +13516,7 @@ export type Database = {
}
supplierType: {
Row: {
+ companyId: number | null
createdAt: string
createdBy: string
customFields: Json | null
@@ -13571,6 +13527,7 @@ export type Database = {
updatedBy: string | null
}
Insert: {
+ companyId?: number | null
createdAt?: string
createdBy: string
customFields?: Json | null
@@ -13581,6 +13538,7 @@ export type Database = {
updatedBy?: string | null
}
Update: {
+ companyId?: number | null
createdAt?: string
createdBy?: string
customFields?: Json | null
@@ -13591,6 +13549,13 @@ export type Database = {
updatedBy?: string | null
}
Relationships: [
+ {
+ foreignKeyName: "supplierType_companyId_fkey"
+ columns: ["companyId"]
+ isOneToOne: false
+ referencedRelation: "company"
+ referencedColumns: ["id"]
+ },
{
foreignKeyName: "supplierType_createdBy_fkey"
columns: ["createdBy"]
@@ -14208,6 +14173,57 @@ export type Database = {
},
]
}
+ userToCompany: {
+ Row: {
+ companyId: number
+ userId: string
+ }
+ Insert: {
+ companyId: number
+ userId: string
+ }
+ Update: {
+ companyId?: number
+ userId?: string
+ }
+ Relationships: [
+ {
+ foreignKeyName: "userToCompany_companyId_fkey"
+ columns: ["companyId"]
+ isOneToOne: false
+ referencedRelation: "company"
+ referencedColumns: ["id"]
+ },
+ {
+ foreignKeyName: "userToCompany_userId_fkey"
+ columns: ["userId"]
+ isOneToOne: false
+ referencedRelation: "employees"
+ referencedColumns: ["id"]
+ },
+ {
+ foreignKeyName: "userToCompany_userId_fkey"
+ columns: ["userId"]
+ isOneToOne: false
+ referencedRelation: "employeeSummary"
+ referencedColumns: ["id"]
+ },
+ {
+ foreignKeyName: "userToCompany_userId_fkey"
+ columns: ["userId"]
+ isOneToOne: false
+ referencedRelation: "user"
+ referencedColumns: ["id"]
+ },
+ {
+ foreignKeyName: "userToCompany_userId_fkey"
+ columns: ["userId"]
+ isOneToOne: false
+ referencedRelation: "userDefaults"
+ referencedColumns: ["userId"]
+ },
+ ]
+ }
warehouse: {
Row: {
active: boolean
@@ -14815,6 +14831,7 @@ export type Database = {
Row: {
accountManagerId: string | null
assignee: string | null
+ companyId: number | null
createdAt: string | null
createdBy: string | null
customerStatusId: string | null
@@ -14887,6 +14904,13 @@ export type Database = {
referencedRelation: "userDefaults"
referencedColumns: ["userId"]
},
+ {
+ foreignKeyName: "customer_companyId_fkey"
+ columns: ["companyId"]
+ isOneToOne: false
+ referencedRelation: "company"
+ referencedColumns: ["id"]
+ },
{
foreignKeyName: "customer_createdBy_fkey"
columns: ["createdBy"]
@@ -15117,6 +15141,7 @@ export type Database = {
}
groupMembers: {
Row: {
+ companyId: number | null
groupId: string | null
id: number | null
isCustomerOrgGroup: boolean | null
@@ -15131,6 +15156,13 @@ export type Database = {
user: Json | null
}
Relationships: [
+ {
+ foreignKeyName: "group_companyId_fkey"
+ columns: ["companyId"]
+ isOneToOne: false
+ referencedRelation: "company"
+ referencedColumns: ["id"]
+ },
{
foreignKeyName: "membership_groupId_fkey"
columns: ["groupId"]
@@ -15177,6 +15209,7 @@ export type Database = {
}
groups: {
Row: {
+ companyId: number | null
id: string | null
isCustomerOrgGroup: boolean | null
isCustomerTypeGroup: boolean | null
@@ -15191,6 +15224,7 @@ export type Database = {
}
groups_recursive: {
Row: {
+ companyId: number | null
groupId: string | null
isCustomerOrgGroup: boolean | null
isCustomerTypeGroup: boolean | null
@@ -17491,6 +17525,7 @@ export type Database = {
Row: {
accountManagerId: string | null
assignee: string | null
+ companyId: number | null
createdAt: string | null
createdBy: string | null
customFields: Json | null
@@ -17564,6 +17599,13 @@ export type Database = {
referencedRelation: "userDefaults"
referencedColumns: ["userId"]
},
+ {
+ foreignKeyName: "supplier_companyId_fkey"
+ columns: ["companyId"]
+ isOneToOne: false
+ referencedRelation: "company"
+ referencedColumns: ["id"]
+ },
{
foreignKeyName: "supplier_createdBy_fkey"
columns: ["createdBy"]
@@ -17728,6 +17770,7 @@ export type Database = {
Returns: {
id: string
name: string
+ companyId: number
parentId: string
isEmployeeTypeGroup: boolean
isCustomerOrgGroup: boolean
diff --git a/packages/database/supabase/migrations/20231219110852_company.sql b/packages/database/supabase/migrations/20230123004317_companies.sql
similarity index 79%
rename from packages/database/supabase/migrations/20231219110852_company.sql
rename to packages/database/supabase/migrations/20230123004317_companies.sql
index 64b761fca7..f111aa81f6 100644
--- a/packages/database/supabase/migrations/20231219110852_company.sql
+++ b/packages/database/supabase/migrations/20230123004317_companies.sql
@@ -1,5 +1,5 @@
CREATE TABLE "company" (
- "id" BOOLEAN NOT NULL DEFAULT TRUE,
+ "id" SERIAL PRIMARY KEY,
"name" TEXT NOT NULL,
"taxId" TEXT,
"logo" TEXT,
@@ -15,10 +15,6 @@ CREATE TABLE "company" (
"website" TEXT,
"updatedBy" TEXT,
- CONSTRAINT "company_pkey" PRIMARY KEY ("id"),
- -- this is a hack to make sure that this table only ever has one row
- CONSTRAINT "accountDefault_id_check" CHECK ("id" = TRUE),
- CONSTRAINT "accountDefault_id_unique" UNIQUE ("id"),
CONSTRAINT "accountDefault_updatedBy_fkey" FOREIGN KEY ("updatedBy") REFERENCES "user"("id")
);
@@ -42,4 +38,9 @@ CREATE POLICY "Employees with settings_update can update company" ON "company"
USING (
coalesce(get_my_claim('settings_update')::boolean, false) = true
AND (get_my_claim('role'::text)) = '"employee"'::jsonb
- );
\ No newline at end of file
+ );
+
+CREATE TABLE "userToCompany" (
+ "userId" TEXT NOT NULL REFERENCES "user" ("id") ON UPDATE CASCADE ON DELETE CASCADE,
+ "companyId" INTEGER NOT NULL REFERENCES "company" ("id") ON UPDATE CASCADE ON DELETE CASCADE
+);
\ No newline at end of file
diff --git a/packages/database/supabase/migrations/20230123004339_employee-types.sql b/packages/database/supabase/migrations/20230123004339_employee-types.sql
index b0269da423..2f913babc6 100644
--- a/packages/database/supabase/migrations/20230123004339_employee-types.sql
+++ b/packages/database/supabase/migrations/20230123004339_employee-types.sql
@@ -4,11 +4,13 @@ CREATE EXTENSION IF NOT EXISTS "uuid-ossp";
CREATE TABLE "employeeType" (
"id" TEXT NOT NULL DEFAULT uuid_generate_v4(),
"name" TEXT NOT NULL,
+ "companyId" INTEGER,
"protected" BOOLEAN NOT NULL DEFAULT false,
"createdAt" TIMESTAMP WITH TIME ZONE NOT NULL DEFAULT NOW(),
"updatedAt" TIMESTAMP WITH TIME ZONE,
-
- CONSTRAINT "employeeType_pkey" PRIMARY KEY ("id")
+
+ CONSTRAINT "employeeType_pkey" PRIMARY KEY ("id"),
+ CONSTRAINT "employeeType_companyId_fkey" FOREIGN KEY ("companyId") REFERENCES "company"("id") ON DELETE CASCADE ON UPDATE CASCADE
);
ALTER TABLE "employeeType" ENABLE ROW LEVEL SECURITY;
diff --git a/packages/database/supabase/migrations/20230123004409_permissions.sql b/packages/database/supabase/migrations/20230123004409_permissions.sql
index 7a25c96dff..cee8d88022 100644
--- a/packages/database/supabase/migrations/20230123004409_permissions.sql
+++ b/packages/database/supabase/migrations/20230123004409_permissions.sql
@@ -14,10 +14,10 @@ CREATE POLICY "Only claims admin can view/modify modules" ON "feature" FOR ALL U
CREATE TABLE "employeeTypePermission" (
"employeeTypeId" TEXT NOT NULL,
"featureId" TEXT NOT NULL,
- "create" BOOLEAN NOT NULL DEFAULT false,
- "delete" BOOLEAN NOT NULL DEFAULT false,
- "update" BOOLEAN NOT NULL DEFAULT false,
- "view" BOOLEAN NOT NULL DEFAULT false,
+ "create" INTEGER[] NOT NULL DEFAULT '{}',
+ "delete" INTEGER[] NOT NULL DEFAULT '{}',
+ "update" INTEGER[] NOT NULL DEFAULT '{}',
+ "view" INTEGER[] NOT NULL DEFAULT '{}',
"createdAt" TIMESTAMP WITH TIME ZONE NOT NULL DEFAULT NOW(),
"updatedAt" TIMESTAMP WITH TIME ZONE,
diff --git a/packages/database/supabase/migrations/20230123004612_suppliers-and-customers.sql b/packages/database/supabase/migrations/20230123004612_suppliers-and-customers.sql
index c83201ff58..4225cf7bef 100644
--- a/packages/database/supabase/migrations/20230123004612_suppliers-and-customers.sql
+++ b/packages/database/supabase/migrations/20230123004612_suppliers-and-customers.sql
@@ -63,6 +63,7 @@ CREATE TABLE "supplierType" (
"id" TEXT NOT NULL DEFAULT uuid_generate_v4(),
"name" TEXT NOT NULL,
"protected" BOOLEAN NOT NULL DEFAULT false,
+ "companyId" INTEGER,
"createdAt" TIMESTAMP WITH TIME ZONE NOT NULL DEFAULT NOW(),
"createdBy" TEXT NOT NULL,
"updatedBy" TEXT,
@@ -70,7 +71,8 @@ CREATE TABLE "supplierType" (
"customFields" JSONB,
CONSTRAINT "supplierType_pkey" PRIMARY KEY ("id"),
- CONSTRAINT "supplierType_name_unique" UNIQUE ("name"),
+ CONSTRAINT "supplierType_name_unique" UNIQUE ("name", "companyId"),
+ CONSTRAINT "supplierType_companyId_fkey" FOREIGN KEY ("companyId") REFERENCES "company"("id") ON UPDATE CASCADE ON DELETE SET NULL,
CONSTRAINT "supplierType_createdBy_fkey" FOREIGN KEY ("createdBy") REFERENCES "user"("id") ON UPDATE CASCADE ON DELETE RESTRICT,
CONSTRAINT "supplierType_updatedBy_fkey" FOREIGN KEY ("updatedBy") REFERENCES "user"("id") ON UPDATE CASCADE ON DELETE SET NULL
);
@@ -83,6 +85,7 @@ CREATE TABLE "supplier" (
"taxId" TEXT,
"accountManagerId" TEXT,
"logo" TEXT,
+ "companyId" INTEGER,
"createdAt" TIMESTAMP WITH TIME ZONE DEFAULT NOW() NOT NULL,
"createdBy" TEXT,
"updatedAt" TIMESTAMP WITH TIME ZONE,
@@ -93,6 +96,7 @@ CREATE TABLE "supplier" (
CONSTRAINT "supplier_supplierTypeId_fkey" FOREIGN KEY ("supplierTypeId") REFERENCES "supplierType"("id") ON UPDATE CASCADE ON DELETE SET NULL,
CONSTRAINT "supplier_supplierStatusId_fkey" FOREIGN KEY ("supplierStatusId") REFERENCES "supplierStatus"("id") ON UPDATE CASCADE ON DELETE SET NULL,
CONSTRAINT "supplier_accountManagerId_fkey" FOREIGN KEY ("accountManagerId") REFERENCES "user"("id") ON UPDATE CASCADE ON DELETE SET NULL,
+ CONSTRAINT "supplier_companyId_fkey" FOREIGN KEY ("companyId") REFERENCES "company"("id") ON UPDATE CASCADE ON DELETE SET NULL,
CONSTRAINT "supplier_createdBy_fkey" FOREIGN KEY ("createdBy") REFERENCES "user"("id") ON UPDATE CASCADE,
CONSTRAINT "supplier_updatedBy_fkey" FOREIGN KEY ("updatedBy") REFERENCES "user"("id") ON UPDATE CASCADE,
CONSTRAINT "supplier_name_unique" UNIQUE ("name")
@@ -158,6 +162,7 @@ CREATE TABLE "customerType" (
"id" TEXT NOT NULL DEFAULT uuid_generate_v4(),
"name" TEXT NOT NULL,
"protected" BOOLEAN NOT NULL DEFAULT false,
+ "companyId" INTEGER,
"createdAt" TIMESTAMP WITH TIME ZONE NOT NULL DEFAULT NOW(),
"createdBy" TEXT NOT NULL,
"updatedBy" TEXT,
@@ -165,7 +170,7 @@ CREATE TABLE "customerType" (
"customFields" JSONB,
CONSTRAINT "customerType_pkey" PRIMARY KEY ("id"),
- CONSTRAINT "customerType_name_unique" UNIQUE ("name"),
+ CONSTRAINT "customerType_name_unique" UNIQUE ("name", "companyId"),
CONSTRAINT "customerType_createdBy_fkey" FOREIGN KEY ("createdBy") REFERENCES "user"("id") ON UPDATE CASCADE ON DELETE RESTRICT,
CONSTRAINT "customerType_updatedBy_fkey" FOREIGN KEY ("updatedBy") REFERENCES "user"("id") ON UPDATE CASCADE ON DELETE SET NULL
);
@@ -178,6 +183,7 @@ CREATE TABLE "customer" (
"taxId" TEXT,
"accountManagerId" TEXT,
"logo" TEXT,
+ "companyId" INTEGER,
"createdAt" TIMESTAMP WITH TIME ZONE DEFAULT NOW() NOT NULL,
"createdBy" TEXT,
"updatedAt" TIMESTAMP WITH TIME ZONE,
@@ -188,6 +194,7 @@ CREATE TABLE "customer" (
CONSTRAINT "customer_customerTypeId_fkey" FOREIGN KEY ("customerTypeId") REFERENCES "customerType"("id") ON UPDATE CASCADE ON DELETE SET NULL,
CONSTRAINT "customer_customerStatusId_fkey" FOREIGN KEY ("customerStatusId") REFERENCES "customerStatus"("id") ON UPDATE CASCADE ON DELETE SET NULL,
CONSTRAINT "customer_accountManagerId_fkey" FOREIGN KEY ("accountManagerId") REFERENCES "user"("id") ON UPDATE CASCADE ON DELETE SET NULL,
+ CONSTRAINT "customer_companyId_fkey" FOREIGN KEY ("companyId") REFERENCES "company"("id") ON UPDATE CASCADE ON DELETE SET NULL,
CONSTRAINT "customer_createdBy_fkey" FOREIGN KEY ("createdBy") REFERENCES "user"("id") ON UPDATE CASCADE ON DELETE SET NULL,
CONSTRAINT "customer_updatedBy_fkey" FOREIGN KEY ("updatedBy") REFERENCES "user"("id") ON UPDATE CASCADE ON DELETE SET NULL,
CONSTRAINT "customer_name_unique" UNIQUE ("name")
diff --git a/packages/database/supabase/migrations/20230123004632_groups.sql b/packages/database/supabase/migrations/20230123004632_groups.sql
index 5a1e1129de..2261713fc8 100644
--- a/packages/database/supabase/migrations/20230123004632_groups.sql
+++ b/packages/database/supabase/migrations/20230123004632_groups.sql
@@ -1,6 +1,7 @@
CREATE TABLE "group" (
"id" TEXT NOT NULL DEFAULT uuid_generate_v4(),
"name" TEXT NOT NULL,
+ "companyId" INTEGER,
"isIdentityGroup" BOOLEAN NOT NULL DEFAULT false,
"isEmployeeTypeGroup" BOOLEAN NOT NULL DEFAULT false,
"isCustomerOrgGroup" BOOLEAN NOT NULL DEFAULT false,
@@ -10,7 +11,8 @@ CREATE TABLE "group" (
"createdAt" TIMESTAMP WITH TIME ZONE NOT NULL DEFAULT NOW(),
"updatedAt" TIMESTAMP WITH TIME ZONE,
- CONSTRAINT "group_pkey" PRIMARY KEY ("id")
+ CONSTRAINT "group_pkey" PRIMARY KEY ("id"),
+ CONSTRAINT "group_companyId_fkey" FOREIGN KEY ("companyId") REFERENCES "company"("id") ON DELETE CASCADE ON UPDATE CASCADE
);
CREATE TABLE "membership" (
@@ -42,8 +44,8 @@ CREATE INDEX index_membership_memberUserId ON "membership" ("memberUserId");
CREATE FUNCTION public.create_employee_type_group()
RETURNS TRIGGER AS $$
BEGIN
- INSERT INTO public."group" ("id", "name", "isEmployeeTypeGroup")
- VALUES (new.id, new.name, TRUE);
+ INSERT INTO public."group" ("id", "name", "isEmployeeTypeGroup", "companyId")
+ VALUES (new.id, new.name, TRUE, new."companyId");
INSERT INTO public."membership"("groupId", "memberGroupId")
VALUES ('00000000-0000-0000-0000-000000000000', new.id);
@@ -131,6 +133,7 @@ CREATE OR REPLACE VIEW "groupMembers" AS
SELECT
gm.id,
g.name,
+ g."companyId",
g."isIdentityGroup",
g."isEmployeeTypeGroup",
g."isCustomerOrgGroup",
@@ -153,6 +156,7 @@ CREATE RECURSIVE VIEW groups_recursive
(
"groupId",
"name",
+ "companyId",
"parentId",
"isIdentityGroup",
"isEmployeeTypeGroup",
@@ -164,7 +168,8 @@ CREATE RECURSIVE VIEW groups_recursive
) AS
SELECT
"groupId",
- "name",
+ "name",
+ "companyId",
NULL AS "parentId",
"isIdentityGroup",
"isEmployeeTypeGroup",
@@ -178,6 +183,7 @@ CREATE RECURSIVE VIEW groups_recursive
SELECT
g2."groupId",
g2.name,
+ g2."companyId",
g1."groupId" AS "parentId",
g1."isIdentityGroup",
g2."isEmployeeTypeGroup",
@@ -198,6 +204,7 @@ CREATE OR REPLACE VIEW "groups" AS
"isSupplierOrgGroup",
"isSupplierTypeGroup",
"name",
+ "companyId",
"parentId",
coalesce(jsonb_agg("user") filter (where "user" is not null), '[]') as users
FROM groups_recursive
@@ -205,6 +212,7 @@ CREATE OR REPLACE VIEW "groups" AS
GROUP BY
"groupId",
"name",
+ "companyId",
"parentId",
"isEmployeeTypeGroup",
"isCustomerOrgGroup",
@@ -221,6 +229,7 @@ CREATE OR REPLACE FUNCTION groups_query(
RETURNS TABLE (
"id" TEXT,
"name" TEXT,
+ "companyId" INTEGER,
"parentId" TEXT,
"isEmployeeTypeGroup" BOOLEAN,
"isCustomerOrgGroup" BOOLEAN,
@@ -241,6 +250,7 @@ AS $$
SELECT
g."id",
g."name",
+ g."companyId",
g."parentId",
g."isEmployeeTypeGroup",
g."isCustomerOrgGroup",
@@ -289,8 +299,8 @@ $$;
CREATE FUNCTION public.create_customer_type_group()
RETURNS TRIGGER AS $$
BEGIN
- INSERT INTO public."group" ("id", "name", "isCustomerTypeGroup")
- VALUES (new.id, new.name, TRUE);
+ INSERT INTO public."group" ("id", "name", "isCustomerTypeGroup", "companyId")
+ VALUES (new.id, new.name, TRUE, new."companyId");
INSERT INTO public."membership"("groupId", "memberGroupId")
VALUES ('11111111-1111-1111-1111-111111111111', new.id);
@@ -301,8 +311,8 @@ $$ LANGUAGE plpgsql SECURITY DEFINER;
CREATE FUNCTION public.create_supplier_type_group()
RETURNS TRIGGER AS $$
BEGIN
- INSERT INTO public."group" ("id", "name", "isSupplierTypeGroup")
- VALUES (new.id, new.name, TRUE);
+ INSERT INTO public."group" ("id", "name", "isSupplierTypeGroup", "companyId")
+ VALUES (new.id, new.name, TRUE, new."companyId");
INSERT INTO public."membership"("groupId", "memberGroupId")
VALUES ('22222222-2222-2222-2222-222222222222', new.id);
@@ -314,8 +324,8 @@ $$ LANGUAGE plpgsql SECURITY DEFINER;
CREATE FUNCTION public.create_customer_org_group()
RETURNS TRIGGER AS $$
BEGIN
- INSERT INTO public."group" ("id", "name", "isCustomerOrgGroup")
- VALUES (new.id, new.name, TRUE);
+ INSERT INTO public."group" ("id", "name", "isCustomerOrgGroup", "companyId")
+ VALUES (new.id, new.name, TRUE, new."companyId");
IF new."customerTypeId" IS NOT NULL THEN
INSERT INTO public."membership"("groupId", "memberGroupId")
@@ -329,8 +339,8 @@ $$ LANGUAGE plpgsql SECURITY DEFINER;
CREATE FUNCTION public.create_supplier_org_group()
RETURNS TRIGGER AS $$
BEGIN
- INSERT INTO public."group" ("id", "name", "isSupplierOrgGroup")
- VALUES (new.id, new.name, TRUE);
+ INSERT INTO public."group" ("id", "name", "isSupplierOrgGroup", "companyId")
+ VALUES (new.id, new.name, TRUE, new."companyId");
IF new."supplierTypeId" IS NOT NULL THEN
INSERT INTO public."membership"("groupId", "memberGroupId")
@@ -345,7 +355,7 @@ CREATE FUNCTION public.add_customer_account_to_customer_group()
RETURNS TRIGGER AS $$
BEGIN
INSERT INTO public."membership" ("groupId", "memberUserId")
- VALUES (new."customerId", new.id);
+ VALUES (new."companyId", new.id);
RETURN new;
END;
$$ LANGUAGE plpgsql SECURITY DEFINER;
diff --git a/packages/database/supabase/migrations/20240412173041_companies.sql b/packages/database/supabase/migrations/20240412173041_companies.sql
deleted file mode 100644
index 7965f023e8..0000000000
--- a/packages/database/supabase/migrations/20240412173041_companies.sql
+++ /dev/null
@@ -1,21 +0,0 @@
--- Update the company table to have a UUID id primary key instead of boolean primary key
--- This will allow us to have multiple companies in the future
-
-ALTER TABLE "company" DROP CONSTRAINT IF EXISTS "company_pkey";
-ALTER TABLE "company" DROP CONSTRAINT IF EXISTS "accountDefault_id_check";
-ALTER TABLE "company" DROP CONSTRAINT IF EXISTS "accountDefault_id_unique";
-ALTER TABLE "company" ADD COLUMN "tmpId" TEXT NOT NULL DEFAULT xid();
-UPDATE "company" SET "tmpId" = xid();
-ALTER TABLE "company" DROP COLUMN IF EXISTS "id";
-ALTER TABLE "company" RENAME COLUMN "tmpId" TO "id";
-ALTER TABLE "company" ADD CONSTRAINT "company_pkey" PRIMARY KEY ("id");
-
-ALTER TABLE "customer" ADD COLUMN "companyId" TEXT REFERENCES "company" ("id") ON DELETE SET NULL;
-ALTER TABLE "part" ADD COLUMN "companyId" TEXT REFERENCES "company" ("id") ON DELETE SET NULL;
-ALTER TABLE "purchaseOrder" ADD COLUMN "companyId" TEXT REFERENCES "company" ("id") ON DELETE SET NULL;
-ALTER TABLE "purchaseInvoice" ADD COLUMN "companyId" TEXT REFERENCES "company" ("id") ON DELETE SET NULL;
-ALTER TABLE "quote" ADD COLUMN "companyId" TEXT REFERENCES "company" ("id") ON DELETE SET NULL;
-ALTER TABLE "receipt" ADD COLUMN "companyId" TEXT REFERENCES "company" ("id") ON DELETE SET NULL;
-ALTER TABLE "requestForQuote" ADD COLUMN "companyId" TEXT REFERENCES "company" ("id") ON DELETE SET NULL;
-ALTER TABLE "service" ADD COLUMN "companyId" TEXT REFERENCES "company" ("id") ON DELETE SET NULL;
-ALTER TABLE "supplier" ADD COLUMN "companyId" TEXT REFERENCES "company" ("id") ON DELETE SET NULL;
\ No newline at end of file