-
Notifications
You must be signed in to change notification settings - Fork 1
/
group-categories.ts
63 lines (50 loc) · 2 KB
/
group-categories.ts
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
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
import type { Request, Response } from 'express';
import * as schema from '../db/schema';
import { and, eq } from 'drizzle-orm';
import { canViewGroupsInGroupCategory } from '../services/group-categories';
import { NodePgDatabase } from 'drizzle-orm/node-postgres';
export function getGroupCategoriesHandler(dbPool: NodePgDatabase<typeof schema>) {
return async function (req: Request, res: Response) {
const groupCategories = await dbPool.query.groupCategories.findMany();
return res.json({ data: groupCategories });
};
}
export function getGroupCategoryHandler(dbPool: NodePgDatabase<typeof schema>) {
return async function (req: Request, res: Response) {
const groupCategoryId = req.params.id;
if (!groupCategoryId) {
return res.status(400).json({ error: 'Group Category ID is required' });
}
const groupCategory = await dbPool.query.groupCategories.findFirst({
where: and(eq(schema.groupCategories.id, groupCategoryId)),
});
return res.json({ data: groupCategory });
};
}
export function getGroupCategoriesGroupsHandler(dbPool: NodePgDatabase<typeof schema>) {
return async function (req: Request, res: Response) {
const groupCategoryId = req.params.id;
if (!groupCategoryId) {
return res.status(400).json({ errors: ['expected group category id'] });
}
const groupCategory = await dbPool.query.groupCategories.findFirst({
where: eq(schema.groupCategories.id, groupCategoryId),
});
if (!groupCategory) {
return res.status(404).json({ error: 'Group Category not found' });
}
const canView = await canViewGroupsInGroupCategory(dbPool, groupCategory.id);
if (!canView) {
return res
.status(403)
.json({ error: 'You do not have permission to view this group category' });
}
const groups = await dbPool.query.groups.findMany({
where: eq(schema.groups.groupCategoryId, groupCategory.id),
columns: {
secret: false,
},
});
return res.json({ data: groups });
};
}