-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathfetch-user-access.ts
More file actions
41 lines (34 loc) · 976 Bytes
/
Copy pathfetch-user-access.ts
File metadata and controls
41 lines (34 loc) · 976 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
import type { Session } from "next-auth";
import { getAuthSession } from "./auth";
import { db } from "./db";
export async function getUserAccessData(session?: Session): Promise<number[]> {
let userData;
if (session) {
userData = session.user;
} else {
const session = await getAuthSession();
if (!session?.user) return [];
userData = session.user;
}
// if you are customer, you have access only to your company cases/orders/proposals
if (userData.permission === "client") {
const data = await db.userAccess.findMany({
where: {
userId: userData.id,
},
select: {
companyId: true,
},
});
const parsedData = data.map(({ companyId }) => companyId);
return parsedData;
}
// if you are anything else, you have access to everything
const data = await db.company.findMany({
select: {
id: true,
},
});
const parsedData = data.map(({ id }) => id);
return parsedData;
}