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.

🍻 create groups tables, views, and triggers

Brad Barbin4e5f4fa4e919
diff --git a/apps/carbon/app/routes/app.users.employee-types.delete.$employeeTypeId.tsx b/apps/carbon/app/routes/app.users.employee-types.delete.$employeeTypeId.tsx
index ff3db3861b..3ff110f128 100644
--- a/apps/carbon/app/routes/app.users.employee-types.delete.$employeeTypeId.tsx
+++ b/apps/carbon/app/routes/app.users.employee-types.delete.$employeeTypeId.tsx
@@ -51,6 +51,8 @@ export async function action({ request, params }: ActionArgs) {
);
}
+ // TODO - delete employeeType group
+
return redirect(
"/app/users/employee-types",
await setSessionFlash(request, {
diff --git a/apps/carbon/app/services/users/users.server.ts b/apps/carbon/app/services/users/users.server.ts
index 02d5d30f40..cffe31524d 100644
--- a/apps/carbon/app/services/users/users.server.ts
+++ b/apps/carbon/app/services/users/users.server.ts
@@ -137,12 +137,7 @@ export async function getEmployees(
);
if (args.name) {
- query = query.or(
- `lastName.ilike.${args.name}%, firstName.ilike.${args.name}%`,
- {
- foreignTable: "user",
- }
- );
+ query = query.ilike("user.fullName", `%${args.name}%`);
}
if (args.type) {
diff --git a/package.json b/package.json
index 5597a620ba..3db6a9959f 100755
--- a/package.json
+++ b/package.json
@@ -13,6 +13,7 @@
"build": "turbo run build",
"build:packages": "turbo run build --filter=./packages/*",
"clean": "turbo run clean && rm -rf node_modules && rm -rf supabase",
+ "db:rebuild": "run-s db:kill db:start setup",
"db:init": "turbo run db:init",
"db:start": "run-p db:start:database db:start:redis",
"db:start:database": "yarn workspace @carbon/database supabase start",
diff --git a/packages/carbon-database/prisma/migrations/20221123124933_custom_claims/migration.sql b/packages/carbon-database/prisma/migrations/20221121124933_claims/migration.sql
similarity index 93%
rename from packages/carbon-database/prisma/migrations/20221123124933_custom_claims/migration.sql
rename to packages/carbon-database/prisma/migrations/20221121124933_claims/migration.sql
index 49cac2f9bc..ce6b66b4e1 100644
--- a/packages/carbon-database/prisma/migrations/20221123124933_custom_claims/migration.sql
+++ b/packages/carbon-database/prisma/migrations/20221121124933_claims/migration.sql
@@ -12,10 +12,10 @@ CREATE OR REPLACE FUNCTION is_claims_admin() RETURNS "bool"
IF extract(epoch from now()) > coalesce((current_setting('request.jwt.claims', true)::jsonb)->>'exp', '0')::numeric THEN
return false; -- jwt expired
END IF;
- IF coalesce((current_setting('request.jwt.claims', true)::jsonb)->'app_metadata'->'claims_admin', 'false')::bool THEN
- return true; -- user has claims_admin set to true
+ IF coalesce((current_setting('request.jwt.claims', true)::jsonb)->'app_metadata'->'user_update', 'false')::bool THEN
+ return true; -- user has user_update set to true
ELSE
- return false; -- user does NOT have claims_admin set to true
+ return false; -- user does NOT have user_update set to true
END IF;
--------------------------------------------
-- End of block
diff --git a/packages/carbon-database/prisma/migrations/20221122043019_init/migration.sql b/packages/carbon-database/prisma/migrations/20221122043019_init/migration.sql
deleted file mode 100644
index 45fa74a0a1..0000000000
--- a/packages/carbon-database/prisma/migrations/20221122043019_init/migration.sql
+++ /dev/null
@@ -1,16 +0,0 @@
--- CreateTable
-CREATE TABLE "user" (
- "id" TEXT NOT NULL,
- "email" TEXT NOT NULL,
- "firstName" TEXT NOT NULL,
- "lastName" TEXT NOT NULL,
- "fullName" TEXT GENERATED ALWAYS AS ("firstName" || ' ' || "lastName") STORED,
- "emailVerified" TIMESTAMP(3),
- "createdAt" TIMESTAMP(3) NOT NULL DEFAULT CURRENT_TIMESTAMP,
- "updatedAt" TIMESTAMP(3),
-
- CONSTRAINT "user_pkey" PRIMARY KEY ("id")
-);
-
--- CreateIndex
-CREATE UNIQUE INDEX "user_email_key" ON "user"("email");
diff --git a/packages/carbon-database/prisma/migrations/20221122043019_user/migration.sql b/packages/carbon-database/prisma/migrations/20221122043019_user/migration.sql
new file mode 100644
index 0000000000..fb580cf608
--- /dev/null
+++ b/packages/carbon-database/prisma/migrations/20221122043019_user/migration.sql
@@ -0,0 +1,22 @@
+CREATE TABLE "user" (
+ "id" TEXT NOT NULL,
+ "email" TEXT NOT NULL,
+ "firstName" TEXT NOT NULL,
+ "lastName" TEXT NOT NULL,
+ "fullName" TEXT GENERATED ALWAYS AS ("firstName" || ' ' || "lastName") STORED,
+ "emailVerified" TIMESTAMP(3),
+ "createdAt" TIMESTAMP(3) NOT NULL DEFAULT NOW(),
+ "updatedAt" TIMESTAMP(3),
+
+ CONSTRAINT "user_pkey" PRIMARY KEY ("id")
+);
+
+CREATE UNIQUE INDEX "user_email_key" ON "user"("email");
+CREATE UNIQUE INDEX "user_fullName_key" ON "user"("fullName");
+
+ALTER TABLE "user" ENABLE ROW LEVEL SECURITY;
+
+CREATE POLICY "Anyone can view users" ON "user" FOR SELECT USING (true);
+CREATE POLICY "Only claims admin can insert users" ON "user" FOR INSERT WITH CHECK (is_claims_admin());
+CREATE POLICY "Only claims admin can update users" ON "user" FOR UPDATE WITH CHECK (is_claims_admin());
+CREATE POLICY "Only claims admin can delete users" ON "user" FOR DELETE USING (is_claims_admin());
diff --git a/packages/carbon-database/prisma/migrations/20221126132027_employee_type/migration.sql b/packages/carbon-database/prisma/migrations/20221126132027_employee_type/migration.sql
index 365d360fb9..b2864f3dd4 100644
--- a/packages/carbon-database/prisma/migrations/20221126132027_employee_type/migration.sql
+++ b/packages/carbon-database/prisma/migrations/20221126132027_employee_type/migration.sql
@@ -9,13 +9,10 @@ CREATE TABLE "employeeType" (
"createdAt" TIMESTAMP(3) NOT NULL DEFAULT CURRENT_TIMESTAMP,
"updatedAt" TIMESTAMP(3),
- CONSTRAINT "employeeType_pkey" PRIMARY KEY ("id")
+ CONSTRAINT "employeeType_pkey" PRIMARY KEY ("id"),
+ CONSTRAINT "employeeType_colorCheck" CHECK ("color" is null or "color" ~* '^#[a-f0-9]{6}$')
);
-ALTER TABLE "employeeType"
-ADD CONSTRAINT employee_type_color_check
-CHECK ("color" is null or "color" ~* '^#[a-f0-9]{6}$');
-
ALTER TABLE "employeeType" ENABLE ROW LEVEL SECURITY;
CREATE POLICY "Only claims admin can view/modify permissions for employee types" ON "employeeType" FOR ALL USING (is_claims_admin());
diff --git a/packages/carbon-database/prisma/migrations/20221126133855_permissions/migration.sql b/packages/carbon-database/prisma/migrations/20221126133855_permissions/migration.sql
index 7cd44939fc..4955cbd14f 100644
--- a/packages/carbon-database/prisma/migrations/20221126133855_permissions/migration.sql
+++ b/packages/carbon-database/prisma/migrations/20221126133855_permissions/migration.sql
@@ -1,4 +1,3 @@
--- CreateTable
CREATE TABLE "feature" (
"id" TEXT NOT NULL DEFAULT uuid_generate_v4(),
"name" TEXT NOT NULL,
@@ -8,18 +7,27 @@ CREATE TABLE "feature" (
CONSTRAINT "feature_pkey" PRIMARY KEY ("id")
);
--- CreateTable
+ALTER TABLE "feature" ENABLE ROW LEVEL SECURITY;
+
+CREATE POLICY "Only claims admin can view/modify modules" ON "feature" FOR ALL USING (is_claims_admin());
+
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,
"createdAt" TIMESTAMP(3) NOT NULL DEFAULT CURRENT_TIMESTAMP,
"updatedAt" TIMESTAMP(3),
- CONSTRAINT "employeeTypePermission_pkey" PRIMARY KEY ("employeeTypeId", "featureId")
+ CONSTRAINT "employeeTypePermission_pkey" PRIMARY KEY ("employeeTypeId", "featureId"),
+ CONSTRAINT "employeeTypePermission_employeeTypeId_fkey" FOREIGN KEY ("employeeTypeId") REFERENCES "employeeType"("id") ON DELETE CASCADE ON UPDATE CASCADE,
+ CONSTRAINT "employeeTypePermission_featureId_fkey" FOREIGN KEY ("featureId") REFERENCES "feature"("id") ON DELETE CASCADE ON UPDATE CASCADE
);
--- AddForeignKey
-ALTER TABLE "employeeTypePermission" ADD CONSTRAINT "employeeTypePermission_employeeTypeId_fkey" FOREIGN KEY ("employeeTypeId") REFERENCES "employeeType"("id") ON DELETE CASCADE ON UPDATE CASCADE;
+ALTER TABLE "employeeTypePermission" ENABLE ROW LEVEL SECURITY;
+
+CREATE POLICY "Only claims admin can view/modify permissions for employee types" ON "employeeTypePermission" FOR ALL USING (is_claims_admin());
+
--- AddForeignKey
-ALTER TABLE "employeeTypePermission" ADD CONSTRAINT "employeeTypePermission_featureId_fkey" FOREIGN KEY ("featureId") REFERENCES "feature"("id") ON DELETE CASCADE ON UPDATE CASCADE;
diff --git a/packages/carbon-database/prisma/migrations/20221127210541_permission_fields/migration.sql b/packages/carbon-database/prisma/migrations/20221127210541_permission_fields/migration.sql
deleted file mode 100644
index 0adbcc3d7e..0000000000
--- a/packages/carbon-database/prisma/migrations/20221127210541_permission_fields/migration.sql
+++ /dev/null
@@ -1,14 +0,0 @@
--- AlterTable
-ALTER TABLE "employeeTypePermission"
-ADD COLUMN "create" BOOLEAN NOT NULL DEFAULT false,
-ADD COLUMN "delete" BOOLEAN NOT NULL DEFAULT false,
-ADD COLUMN "update" BOOLEAN NOT NULL DEFAULT false,
-ADD COLUMN "view" BOOLEAN NOT NULL DEFAULT false;
-
-ALTER TABLE "feature" ENABLE ROW LEVEL SECURITY;
-
-CREATE POLICY "Only claims admin can view/modify modues" ON "feature" FOR ALL USING (is_claims_admin());
-
-ALTER TABLE "employeeTypePermission" ENABLE ROW LEVEL SECURITY;
-
-CREATE POLICY "Only claims admin can view/modify permissions for employee types" ON "employeeTypePermission" FOR ALL USING (is_claims_admin());
diff --git a/packages/carbon-database/prisma/migrations/20221202032804_employee/migration.sql b/packages/carbon-database/prisma/migrations/20221202032804_employee/migration.sql
index 571e02c3e7..ced31d6049 100644
--- a/packages/carbon-database/prisma/migrations/20221202032804_employee/migration.sql
+++ b/packages/carbon-database/prisma/migrations/20221202032804_employee/migration.sql
@@ -1,13 +1,16 @@
--- CreateTable
CREATE TABLE "employee" (
"id" TEXT NOT NULL,
"employeeTypeId" TEXT NOT NULL,
- CONSTRAINT "employee_pkey" PRIMARY KEY ("id")
+ CONSTRAINT "employee_pkey" PRIMARY KEY ("id"),
+ CONSTRAINT "employee_employeeTypeId_fkey" FOREIGN KEY ("employeeTypeId") REFERENCES "employeeType"("id") ON DELETE NO ACTION ON UPDATE NO ACTION,
+ CONSTRAINT "employee_id_fkey" FOREIGN KEY ("id") REFERENCES "user"("id") ON DELETE CASCADE ON UPDATE NO ACTION
);
--- AddForeignKey
-ALTER TABLE "employee" ADD CONSTRAINT "employee_employeeTypeId_fkey" FOREIGN KEY ("employeeTypeId") REFERENCES "employeeType"("id") ON DELETE NO ACTION ON UPDATE NO ACTION;
+ALTER TABLE "employee" ENABLE ROW LEVEL SECURITY;
+
+CREATE POLICY "Anyone can view employees" ON "employee" FOR SELECT USING (true);
+CREATE POLICY "Only claims admin can insert employees" ON "employee" FOR INSERT WITH CHECK (is_claims_admin());
+CREATE POLICY "Only claims admin can update employees" ON "employee" FOR UPDATE WITH CHECK (is_claims_admin());
+CREATE POLICY "Only claims admin can delete employees" ON "employee" FOR DELETE USING (is_claims_admin());
--- AddForeignKey
-ALTER TABLE "employee" ADD CONSTRAINT "employee_id_fkey" FOREIGN KEY ("id") REFERENCES "user"("id") ON DELETE CASCADE ON UPDATE NO ACTION;
diff --git a/packages/carbon-database/prisma/migrations/20221208201232_groups/migration.sql b/packages/carbon-database/prisma/migrations/20221208201232_groups/migration.sql
new file mode 100644
index 0000000000..33f3153c98
--- /dev/null
+++ b/packages/carbon-database/prisma/migrations/20221208201232_groups/migration.sql
@@ -0,0 +1,184 @@
+CREATE TABLE "group" (
+ "id" TEXT NOT NULL DEFAULT uuid_generate_v4(),
+ "name" TEXT NOT NULL,
+ "isIdentityGroup" BOOLEAN NOT NULL DEFAULT false,
+ "isEmployeeTypeGroup" BOOLEAN NOT NULL DEFAULT false,
+ "createdAt" TIMESTAMP(3) DEFAULT now() NOT NULL,
+ "updatedAt" TIMESTAMP(3),
+
+ CONSTRAINT "group_pkey" PRIMARY KEY ("id")
+);
+
+CREATE TABLE "membership" (
+ "id" SERIAL NOT NULL,
+ "groupId" TEXT NOT NULL,
+ "memberGroupId" TEXT,
+ "memberUserId" TEXT,
+
+ CONSTRAINT "membership_pkey" PRIMARY KEY ("id"),
+ CONSTRAINT "membership_groupId_fkey" FOREIGN KEY ("groupId") REFERENCES "group"("id") ON DELETE CASCADE ON UPDATE CASCADE,
+ CONSTRAINT "membership_memberGroupId_fkey" FOREIGN KEY ("memberGroupId") REFERENCES "group"("id") ON DELETE CASCADE ON UPDATE CASCADE,
+ CONSTRAINT "membership_memberUserId_fkey" FOREIGN KEY ("memberUserId") REFERENCES "user"("id") ON DELETE CASCADE ON UPDATE CASCADE,
+
+ CONSTRAINT uq_membership
+ UNIQUE ( "groupId", "memberGroupId", "memberUserId" ),
+
+ CONSTRAINT membership_hasPersonOrGroup
+ CHECK (
+ ("memberGroupId" IS NULL AND "memberUserId" IS NOT NULL)
+ OR
+ ("memberGroupId" IS NOT NULL AND "memberUserId" IS NULL)
+ )
+);
+
+CREATE FUNCTION public.create_employee_type_group()
+RETURNS TRIGGER AS $$
+BEGIN
+ INSERT INTO public."group" ("id", "name", "isEmployeeTypeGroup")
+ VALUES (new.id, new.name, TRUE);
+ RETURN new;
+END;
+$$ LANGUAGE plpgsql SECURITY DEFINER;
+
+CREATE FUNCTION public.update_employee_type_group()
+RETURNS TRIGGER AS $$
+BEGIN
+ UPDATE public."group" SET "name" = new.name
+ WHERE "id" = new.id AND "isEmployeeTypeGroup" = TRUE;
+ RETURN new;
+END;
+$$ LANGUAGE plpgsql SECURITY DEFINER;
+
+CREATE FUNCTION public.create_user_identity_group()
+RETURNS TRIGGER AS $$
+BEGIN
+ INSERT INTO public."group" ("id", "name", "isIdentityGroup")
+ VALUES (new.id, new."fullName", TRUE);
+
+ INSERT INTO public."membership"("groupId", "memberUserId")
+ VALUES (new.id, new.id);
+ RETURN new;
+END;
+$$ LANGUAGE plpgsql SECURITY DEFINER;
+
+CREATE FUNCTION public.update_user_identity_group()
+RETURNS TRIGGER AS $$
+BEGIN
+ update public."group" set "name" = new."fullName"
+ where "id" = new.id and "isIdentityGroup" = TRUE;
+
+ RETURN new;
+END;
+$$ LANGUAGE plpgsql SECURITY DEFINER;
+
+
+CREATE FUNCTION public.add_employee_to_employee_type_group()
+RETURNS TRIGGER AS $$
+BEGIN
+ INSERT INTO public."membership" ("groupId", "memberUserId")
+ VALUES (new."employeeTypeId", new.id);
+
+ RETURN new;
+END;
+$$ LANGUAGE plpgsql SECURITY DEFINER;
+
+CREATE FUNCTION public.update_employee_to_employee_type_group()
+RETURNS TRIGGER AS $$
+BEGIN
+ UPDATE public."membership" SET "groupId" = new."employeeTypeId"
+ WHERE "groupId" = old."employeeTypeId" AND "memberUserId" = new.id;
+
+ RETURN new;
+END;
+$$ LANGUAGE plpgsql SECURITY DEFINER;
+
+
+CREATE TRIGGER on_employee_type_created
+ AFTER INSERT on public."employeeType"
+ FOR EACH ROW EXECUTE PROCEDURE public.create_employee_type_group();
+
+CREATE TRIGGER on_user_created
+ AFTER INSERT on public.user
+ FOR EACH ROW EXECUTE PROCEDURE public.create_user_identity_group();
+
+CREATE TRIGGER on_employee_created
+ AFTER INSERT on public.employee
+ FOR EACH ROW EXECUTE PROCEDURE public.add_employee_to_employee_type_group();
+
+CREATE TRIGGER on_user_updated
+ AFTER UPDATE on public.user
+ FOR EACH ROW EXECUTE PROCEDURE public.update_user_identity_group();
+
+CREATE TRIGGER on_employee_updated
+ AFTER UPDATE on public.employee
+ FOR EACH ROW EXECUTE PROCEDURE public.update_employee_to_employee_type_group();
+
+CREATE TRIGGER on_employee_type_updated
+ AFTER UPDATE on public."employeeType"
+ FOR EACH ROW EXECUTE PROCEDURE public.update_employee_type_group();
+
+
+CREATE VIEW "groupWithMember" AS
+SELECT
+ g.name,
+ g."isIdentityGroup",
+ g."isEmployeeTypeGroup",
+ gm."groupId",
+ gm."memberGroupId",
+ gm."memberUserId",
+ to_jsonb(u) as member
+FROM
+ "membership" gm
+ INNER JOIN "group" g ON g.id = gm."groupId"
+ LEFT OUTER JOIN "user" u ON u.id = gm."memberUserId";
+
+CREATE VIEW "groupsWithMembers" AS
+SELECT
+ root_g."groupId",
+ root_g."name",
+ root_g."memberGroupId",
+ COALESCE(json_agg(members.member) FILTER (WHERE members.member IS NOT NULL), '[]') as users,
+ count(members.member)
+FROM
+ "groupWithMember" root_g
+LEFT JOIN "groupWithMember" members
+ ON members."groupId" = root_g."memberUserId"
+WHERE root_g."isIdentityGroup" = false
+GROUP BY
+ root_g."groupId",
+ root_g."name",
+ root_g."memberGroupId";
+
+CREATE OR REPLACE FUNCTION groups_for_user(uid text) RETURNS "jsonb"
+ LANGUAGE "plpgsql" SECURITY DEFINER SET search_path = public
+ AS $$
+ DECLARE retval jsonb;
+ BEGIN
+ WITH RECURSIVE "groupsForUser" AS (
+ SELECT "groupId", "memberGroupId", "memberUserId" FROM "membership"
+ WHERE "memberUserId" = uid::text
+ UNION
+ SELECT g1."groupId", g1."memberGroupId", g1."memberUserId" FROM "membership" g1
+ INNER JOIN "groupsForUser" g2 ON g2."groupId" = g1."memberGroupId"
+ ) SELECT coalesce(json_agg("groupId"), '[]') INTO retval AS groups FROM "groupsForUser";
+ RETURN retval;
+ END;
+$$;
+
+CREATE OR REPLACE FUNCTION users_for_groups(groups text[]) RETURNS "jsonb"
+ LANGUAGE "plpgsql" SECURITY DEFINER SET search_path = public
+ AS $$
+ DECLARE retval jsonb;
+ BEGIN
+ WITH RECURSIVE "usersForGroups" AS (
+ SELECT "groupId", "memberGroupId", "memberUserId" FROM "membership"
+ WHERE "groupId" = ANY(groups)
+ UNION
+ SELECT g1."groupId", g1."memberGroupId", g1."memberUserId" FROM "membership" g1
+ INNER JOIN "usersForGroups" g2 ON g2."memberGroupId" = g1."groupId"
+ ) SELECT coalesce(json_agg("memberUserId"), '[]') AS groups INTO retval FROM "usersForGroups" WHERE "memberUserId" IS NOT NULL;
+ RETURN retval;
+ END;
+$$;
+
+NOTIFY pgrst, 'reload schema';
diff --git a/packages/carbon-database/prisma/schema.prisma b/packages/carbon-database/prisma/schema.prisma
index 07f752e17a..30c3f468e7 100644
--- a/packages/carbon-database/prisma/schema.prisma
+++ b/packages/carbon-database/prisma/schema.prisma
@@ -7,54 +7,79 @@ datasource db {
url = env("DB_URL")
}
-model User {
- id String @id @default(cuid())
- email String @unique
- firstName String
- lastName String
- emailVerified DateTime?
- createdAt DateTime @default(now())
- updatedAt DateTime? @updatedAt
- Employee Employee?
-}
-
-model EmployeeType {
- id String @id @default(dbgenerated("uuid_generate_v4()"))
- name String
- color String? @default("#000000")
- protected Boolean @default(false)
- createdAt DateTime @default(now())
- updatedAt DateTime? @updatedAt
- Employee Employee[]
- employeeTypePermission EmployeeTypePermission[]
+model employee {
+ id String @id
+ employeeTypeId String
+ employeeType employeeType @relation(fields: [employeeTypeId], references: [id], onDelete: NoAction, onUpdate: NoAction)
+ user user @relation(fields: [id], references: [id], onDelete: Cascade, onUpdate: NoAction)
}
-model Feature {
+model employeeType {
id String @id @default(dbgenerated("uuid_generate_v4()"))
name String
+ color String? @default("#000000")
+ protected Boolean @default(false)
createdAt DateTime @default(now())
- updatedAt DateTime? @updatedAt
- employeeTypePermission EmployeeTypePermission[]
+ updatedAt DateTime?
+ employee employee[]
+ employeeTypePermission employeeTypePermission[]
}
-model EmployeeTypePermission {
+model employeeTypePermission {
employeeTypeId String
- moduleId String
- createdAt DateTime @default(now())
- updatedAt DateTime? @updatedAt
+ featureId String
create Boolean @default(false)
delete Boolean @default(false)
update Boolean @default(false)
view Boolean @default(false)
- employeeType EmployeeType @relation(fields: [employeeTypeId], references: [id], onDelete: Cascade)
- module Feature @relation(fields: [moduleId], references: [id], onDelete: Cascade)
+ createdAt DateTime @default(now())
+ updatedAt DateTime?
+ employeeType employeeType @relation(fields: [employeeTypeId], references: [id], onDelete: Cascade)
+ feature feature @relation(fields: [featureId], references: [id], onDelete: Cascade)
- @@id([employeeTypeId, moduleId])
+ @@id([employeeTypeId, featureId])
}
-model Employee {
- id String @id
- employeeTypeId String
- EmployeeType EmployeeType @relation(fields: [employeeTypeId], references: [id], onDelete: NoAction, onUpdate: NoAction)
- User User @relation(fields: [id], references: [id], onDelete: Cascade, onUpdate: NoAction)
+model feature {
+ id String @id @default(dbgenerated("uuid_generate_v4()"))
+ name String
+ createdAt DateTime @default(now())
+ updatedAt DateTime?
+ employeeTypePermission employeeTypePermission[]
+}
+
+model user {
+ id String @id
+ email String @unique
+ firstName String
+ lastName String
+ fullName String? @default(dbgenerated("((\"firstName\" || ' '::text) || \"lastName\")"))
+ emailVerified DateTime?
+ createdAt DateTime @default(now())
+ updatedAt DateTime?
+ employee employee?
+ groupMembership groupMembership[]
+}
+
+model group {
+ id String @id @default(dbgenerated("uuid_generate_v4()"))
+ name String
+ isIdentityGroup Boolean @default(false)
+ isEmployeeTypeGroup Boolean @default(false)
+ createdAt DateTime @default(now())
+ updatedAt DateTime?
+ groupMembership_groupTogroupMembership_groupId groupMembership[] @relation("groupTogroupMembership_groupId")
+ groupMembership_groupTogroupMembership_memberGroupId groupMembership[] @relation("groupTogroupMembership_memberGroupId")
+}
+
+model groupMembership {
+ id Int @id @default(autoincrement())
+ groupId String
+ memberGroupId String?
+ memberUserId String?
+ group_groupTogroupMembership_groupId group @relation("groupTogroupMembership_groupId", fields: [groupId], references: [id], onDelete: Cascade)
+ group_groupTogroupMembership_memberGroupId group? @relation("groupTogroupMembership_memberGroupId", fields: [memberGroupId], references: [id], onDelete: Cascade)
+ user user? @relation(fields: [memberUserId], references: [id], onDelete: Cascade)
+
+ @@unique([groupId, memberGroupId, memberUserId], map: "uq_groupmembership")
}
diff --git a/packages/carbon-database/src/seed/admin.ts b/packages/carbon-database/src/seed/admin.ts
index 929c443296..cbccc1409c 100644
--- a/packages/carbon-database/src/seed/admin.ts
+++ b/packages/carbon-database/src/seed/admin.ts
@@ -7,9 +7,7 @@ const admin = {
lastName: "Admin",
};
-const claims: Record<string, boolean> = {
- claims_admin: true,
-};
+const claims: Record<string, boolean> = {};
possibleFeatures.forEach((name) => {
const moduleName = name.toLowerCase();
diff --git a/packages/carbon-database/src/types.ts b/packages/carbon-database/src/types.ts
index 529aa69ce6..555bcb251b 100644
--- a/packages/carbon-database/src/types.ts
+++ b/packages/carbon-database/src/types.ts
@@ -57,80 +57,126 @@ export interface Database {
}
employeeType: {
Row: {
- name: string
- updatedAt: string | null
id: string
+ name: string
color: string | null
protected: boolean
createdAt: string
+ updatedAt: string | null
}
Insert: {
- name: string
- updatedAt?: string | null
id?: string
+ name: string
color?: string | null
protected?: boolean
createdAt?: string
+ updatedAt?: string | null
}
Update: {
- name?: string
- updatedAt?: string | null
id?: string
+ name?: string
color?: string | null
protected?: boolean
createdAt?: string
+ updatedAt?: string | null
}
}
employeeTypePermission: {
Row: {
employeeTypeId: string
featureId: string
- updatedAt: string | null
- createdAt: string
create: boolean
delete: boolean
update: boolean
view: boolean
+ createdAt: string
+ updatedAt: string | null
}
Insert: {
employeeTypeId: string
featureId: string
- updatedAt?: string | null
- createdAt?: string
create?: boolean
delete?: boolean
update?: boolean
view?: boolean
+ createdAt?: string
+ updatedAt?: string | null
}
Update: {
employeeTypeId?: string
featureId?: string
- updatedAt?: string | null
- createdAt?: string
create?: boolean
delete?: boolean
update?: boolean
view?: boolean
+ createdAt?: string
+ updatedAt?: string | null
}
}
feature: {
Row: {
- name: string
- updatedAt: string | null
id: string
+ name: string
createdAt: string
+ updatedAt: string | null
}
Insert: {
- name: string
- updatedAt?: string | null
id?: string
+ name: string
createdAt?: string
+ updatedAt?: string | null
}
Update: {
+ id?: string
name?: string
+ createdAt?: string
+ updatedAt?: string | null
+ }
+ }
+ group: {
+ Row: {
+ id: string
+ name: string
+ isIdentityGroup: boolean
+ isEmployeeTypeGroup: boolean
+ createdAt: string
+ updatedAt: string | null
+ }
+ Insert: {
+ id?: string
+ name: string
+ isIdentityGroup?: boolean
+ isEmployeeTypeGroup?: boolean
+ createdAt?: string
updatedAt?: string | null
+ }
+ Update: {
id?: string
+ name?: string
+ isIdentityGroup?: boolean
+ isEmployeeTypeGroup?: boolean
createdAt?: string
+ updatedAt?: string | null
+ }
+ }
+ membership: {
+ Row: {
+ id: number
+ groupId: string
+ memberGroupId: string | null
+ memberUserId: string | null
+ }
+ Insert: {
+ id?: number
+ groupId: string
+ memberGroupId?: string | null
+ memberUserId?: string | null
+ }
+ Update: {
+ id?: number
+ groupId?: string
+ memberGroupId?: string | null
+ memberUserId?: string | null
}
}
user: {
@@ -139,35 +185,46 @@ export interface Database {
email: string
firstName: string
lastName: string
- emailVerified: string | null
- updatedAt: string | null
fullName: string | null
+ emailVerified: string | null
createdAt: string
+ updatedAt: string | null
}
Insert: {
id: string
email: string
firstName: string
lastName: string
- emailVerified?: string | null
- updatedAt?: string | null
fullName?: string | null
+ emailVerified?: string | null
createdAt?: string
+ updatedAt?: string | null
}
Update: {
id?: string
email?: string
firstName?: string
lastName?: string
- emailVerified?: string | null
- updatedAt?: string | null
fullName?: string | null
+ emailVerified?: string | null
createdAt?: string
+ updatedAt?: string | null
}
}
}
Views: {
- [_ in never]: never
+ groupWithMember: {
+ Row: {
+ id: string | null
+ name: string | null
+ isIdentityGroup: boolean | null
+ isEmployeeTypeGroup: boolean | null
+ groupId: string | null
+ memberGroupId: string | null
+ memberUserId: string | null
+ member: Json | null
+ }
+ }
}
Functions: {
delete_claim: {