Skip to content

Commit 71094a5

Browse files
feat(permissions): creator role, check if user can add bot to chat
1 parent af46bea commit 71094a5

File tree

4 files changed

+44
-4
lines changed

4 files changed

+44
-4
lines changed

backend/package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "backend",
3-
"version": "0.6.1",
3+
"version": "0.7.0",
44
"description": "PoliNetwork backend server",
55
"private": true,
66
"keywords": [],

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

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,10 +11,11 @@ export const USER_ROLE = {
1111
ADMIN: "admin",
1212
HR: "hr",
1313
DIRETTIVO: "direttivo",
14+
CREATOR: "creator",
1415
OWNER: "owner",
1516
} as const;
1617

17-
export const ARRAY_USER_ROLE = [USER_ROLE.ADMIN, USER_ROLE.HR, USER_ROLE.DIRETTIVO, USER_ROLE.OWNER] as const
18+
export const ARRAY_USER_ROLE = [USER_ROLE.ADMIN, USER_ROLE.HR, USER_ROLE.DIRETTIVO, USER_ROLE.OWNER, USER_ROLE.CREATOR] as const
1819
export type TUserRole = (typeof USER_ROLE)[keyof typeof USER_ROLE];
1920

2021
export const permissions = createTable.tg(

backend/src/routers/tg/permissions.ts

Lines changed: 40 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,9 @@
11
import { DB, SCHEMA } from "@/db";
2-
import { ARRAY_USER_ROLE } from "@/db/schema/tg/permissions";
2+
import {
3+
ARRAY_USER_ROLE,
4+
TUserRole,
5+
USER_ROLE,
6+
} from "@/db/schema/tg/permissions";
37
import { createTRPCRouter, publicProcedure } from "@/trpc";
48
import { and, eq } from "drizzle-orm";
59
import { z } from "zod";
@@ -75,4 +79,39 @@ export default createTRPCRouter({
7579
})
7680
.onConflictDoNothing();
7781
}),
82+
83+
canAddBot: publicProcedure
84+
.input(
85+
z.object({
86+
userId: z.number(),
87+
}),
88+
)
89+
.output(
90+
z.union([
91+
z.object({ allowed: z.boolean(), error: z.null() }),
92+
z.object({
93+
allowed: z.literal(false),
94+
error: z.enum(["NOT_FOUND"]),
95+
}),
96+
]),
97+
)
98+
.query(async ({ input }) => {
99+
const res = await DB.select({ role: s.permissions.role })
100+
.from(s.permissions)
101+
.where(eq(s.permissions.userId, input.userId))
102+
.limit(1);
103+
104+
if (!res[0]) return { error: "NOT_FOUND", allowed: false };
105+
const { role } = res[0];
106+
const allowed = (
107+
[
108+
USER_ROLE.HR,
109+
USER_ROLE.OWNER,
110+
USER_ROLE.CREATOR,
111+
USER_ROLE.DIRETTIVO,
112+
] as TUserRole[]
113+
).includes(role);
114+
115+
return { error: null, allowed };
116+
}),
78117
});

package/package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "@polinetwork/backend",
3-
"version": "0.6.1",
3+
"version": "0.7.0",
44
"description": "Utils to interact with the backend.",
55
"repository": {
66
"type": "git",

0 commit comments

Comments
 (0)