Skip to content

Commit 4b06d17

Browse files
feat(tg): permissions
1 parent 87525a5 commit 4b06d17

File tree

4 files changed

+132
-3
lines changed

4 files changed

+132
-3
lines changed

backend/src/db/schema/tg/index.ts

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
1-
import * as groups from "./groups"
2-
import * as test from "./test"
1+
import * as groups from "./groups";
2+
import * as test from "./test";
3+
import * as permissions from "./permissions";
34

4-
export const schema = {...groups, ...test}
5+
export const schema = { ...groups, ...test, ...permissions };
Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
import { timeColumns } from "@/db/columns";
2+
import { createTable } from "../create-table";
3+
import {
4+
bigint,
5+
index,
6+
primaryKey,
7+
varchar,
8+
} from "drizzle-orm/pg-core";
9+
10+
export const USER_ROLE = {
11+
ADMIN: "admin",
12+
HR: "hr",
13+
DIRETTIVO: "direttivo",
14+
OWNER: "owner",
15+
} as const;
16+
17+
export const ARRAY_USER_ROLE = [USER_ROLE.ADMIN, USER_ROLE.HR, USER_ROLE.DIRETTIVO, USER_ROLE.OWNER] as const
18+
export type TUserRole = (typeof USER_ROLE)[keyof typeof USER_ROLE];
19+
20+
export const permissions = createTable.tg(
21+
"permissions",
22+
{
23+
userId: bigint("user_id", { mode: "number" }).primaryKey(),
24+
role: varchar("role", { length: 128 })
25+
.$type<TUserRole>()
26+
.default(USER_ROLE.ADMIN)
27+
.notNull(),
28+
addedBy: bigint("added_by_id", { mode: "number" }).notNull(),
29+
modifiedBy: bigint("modified_by_id", { mode: "number" }),
30+
31+
...timeColumns,
32+
},
33+
);
34+
35+
export const groupAdmins = createTable.tg(
36+
"group_admins",
37+
{
38+
userId: bigint("user_id", { mode: "number" }).notNull(),
39+
groupId: bigint("group_id", { mode: "number" }).notNull(),
40+
addedBy: bigint("added_by_id", { mode: "number" }).notNull(),
41+
42+
...timeColumns,
43+
},
44+
(t) => [
45+
primaryKey({ columns: [t.userId, t.groupId] }),
46+
index("user_id_idx").on(t.userId),
47+
],
48+
);

backend/src/routers/tg/index.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
import { createTRPCRouter } from "@/trpc";
22
import groups from "./groups";
3+
import permissions from "./permissions";
34

45
export const tgRouter = createTRPCRouter({
56
groups,
7+
permissions
68
});
Lines changed: 78 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,78 @@
1+
import { DB, SCHEMA } from "@/db";
2+
import { ARRAY_USER_ROLE } from "@/db/schema/tg/permissions";
3+
import { createTRPCRouter, publicProcedure } from "@/trpc";
4+
import { and, eq } from "drizzle-orm";
5+
import { z } from "zod";
6+
7+
const s = SCHEMA.TG;
8+
9+
export default createTRPCRouter({
10+
getRole: publicProcedure
11+
.input(z.object({ userId: z.number() }))
12+
.query(async ({ input }) => {
13+
const [res] = await DB.select({
14+
role: s.permissions.role,
15+
})
16+
.from(s.permissions)
17+
.where(eq(s.permissions.userId, input.userId))
18+
.limit(1);
19+
20+
return {
21+
userId: input.userId,
22+
role: res ? res.role : "user",
23+
};
24+
}),
25+
26+
setRole: publicProcedure
27+
.input(
28+
z.object({
29+
userId: z.number(),
30+
role: z.enum(ARRAY_USER_ROLE),
31+
adderId: z.number(),
32+
}),
33+
)
34+
.query(async ({ input }) => {
35+
await DB.insert(s.permissions)
36+
.values({
37+
userId: input.userId,
38+
role: input.role,
39+
addedBy: input.adderId,
40+
})
41+
.onConflictDoUpdate({
42+
target: s.permissions.userId,
43+
set: { role: input.role, modifiedBy: input.adderId },
44+
});
45+
}),
46+
47+
checkGroup: publicProcedure
48+
.input(z.object({ userId: z.number(), groupId: z.number() }))
49+
.query(async ({ input }) => {
50+
const res = await DB.$count(
51+
s.groupAdmins,
52+
and(
53+
eq(s.groupAdmins.userId, input.userId),
54+
eq(s.groupAdmins.groupId, input.groupId),
55+
),
56+
);
57+
58+
return res != 0;
59+
}),
60+
61+
addGroup: publicProcedure
62+
.input(
63+
z.object({
64+
userId: z.number(),
65+
adderId: z.number(),
66+
groupId: z.number(),
67+
}),
68+
)
69+
.query(async ({ input }) => {
70+
await DB.insert(s.groupAdmins)
71+
.values({
72+
userId: input.userId,
73+
groupId: input.groupId,
74+
addedBy: input.adderId,
75+
})
76+
.onConflictDoNothing();
77+
}),
78+
});

0 commit comments

Comments
 (0)