|
1 | 1 | import { Hono } from "hono"; |
2 | | -import { z } from "zod"; |
3 | | -import { auth } from "@/lib/auth"; |
| 2 | +import { zValidator } from "@hono/zod-validator"; |
| 3 | +import { requireAdmin, requireMembership } from "@/lib/authUtils"; |
4 | 4 | import { ShiftService } from "@/lib/services/ShiftService"; |
5 | | -import { MembersService } from "@/lib/services/MemberService"; |
6 | | -import { EventService } from "@/lib/services/EventService"; |
| 5 | +import { |
| 6 | + createShiftSchema, |
| 7 | + updateShiftSchema, |
| 8 | + shiftParamSchema, |
| 9 | +} from "@/lib/shifts"; |
7 | 10 | import shiftRsvpApp from "@/api/shifts/rsvps"; |
8 | | - |
9 | | -const createShiftSchema = z.object({ |
10 | | - eventId: z.string().min(1, "eventId is required"), |
11 | | - name: z.string().optional(), |
12 | | - description: z.string().optional(), |
13 | | - startTimestamp: z.string().optional(), |
14 | | - duration: z.string().optional(), |
15 | | - rsvpLimit: z.number().optional(), |
16 | | -}); |
17 | | - |
18 | | -const updateShiftSchema = z.object({ |
19 | | - name: z.string().optional(), |
20 | | - description: z.string().optional(), |
21 | | - startTimestamp: z.string().optional(), |
22 | | - duration: z.string().optional(), |
23 | | - rsvpLimit: z.number().optional(), |
24 | | - eventId: z.string().optional(), |
25 | | -}); |
| 11 | +import { shiftErrorResponse } from "@/api/shifts/errors"; |
26 | 12 |
|
27 | 13 | const app = new Hono() |
28 | | - .post("/", async (c) => { |
29 | | - const session = await auth.api.getSession({ |
30 | | - headers: c.req.header(), |
31 | | - }); |
32 | | - |
33 | | - if (!session?.user) { |
34 | | - return c.json({ error: "Unauthorized" }, { status: 401 }); |
35 | | - } |
36 | | - |
37 | | - const organizationId = session.session.activeOrganizationId; |
38 | | - if (!organizationId) { |
39 | | - return c.json({ error: "No active organization" }, { status: 400 }); |
40 | | - } |
41 | | - |
42 | | - const membership = await MembersService.findByUserAndOrganization( |
43 | | - session.user.id, |
44 | | - organizationId, |
45 | | - ); |
46 | | - |
47 | | - if (!MembersService.isAdminOrOwner(membership?.role)) { |
48 | | - return c.json({ error: "Forbidden" }, { status: 403 }); |
49 | | - } |
50 | | - |
51 | | - let body: unknown = {}; |
| 14 | + .post("/", zValidator("json", createShiftSchema), async (c) => { |
52 | 15 | try { |
53 | | - body = await c.req.json(); |
54 | | - } catch { |
55 | | - body = {}; |
56 | | - } |
57 | | - |
58 | | - const parsed = createShiftSchema.safeParse(body); |
59 | | - if (!parsed.success) { |
60 | | - return c.json({ error: "Invalid request body" }, { status: 400 }); |
61 | | - } |
62 | | - |
63 | | - const data = parsed.data; |
64 | | - const event = await EventService.findById(data.eventId); |
65 | | - if (!event || event.organizationId !== organizationId) { |
66 | | - return c.json({ error: "Event not found" }, { status: 404 }); |
| 16 | + const { session, user } = await requireAdmin(c); |
| 17 | + const shift = await ShiftService.createShift( |
| 18 | + session.activeOrganizationId!, |
| 19 | + user.id, |
| 20 | + c.req.valid("json"), |
| 21 | + ); |
| 22 | + return c.json({ status: 200, shift }); |
| 23 | + } catch (error) { |
| 24 | + return shiftErrorResponse(c, error); |
67 | 25 | } |
68 | | - |
69 | | - const startTimestamp = |
70 | | - data.startTimestamp && !isNaN(new Date(data.startTimestamp).getTime()) |
71 | | - ? new Date(data.startTimestamp) |
72 | | - : new Date(); |
73 | | - |
74 | | - const shift = await ShiftService.createShift({ |
75 | | - organizationId, |
76 | | - eventId: data.eventId, |
77 | | - name: data.name || "", |
78 | | - description: data.description || "", |
79 | | - startTimestamp, |
80 | | - duration: data.duration || "", |
81 | | - rsvpLimit: data.rsvpLimit || 0, |
82 | | - }); |
83 | | - |
84 | | - return c.json({ status: 200, shift }); |
85 | 26 | }) |
86 | | - .patch("/:shiftId", async (c) => { |
87 | | - const session = await auth.api.getSession({ |
88 | | - headers: c.req.header(), |
89 | | - }); |
90 | | - |
91 | | - if (!session?.user) { |
92 | | - return c.json({ error: "Unauthorized" }, { status: 401 }); |
93 | | - } |
94 | | - |
95 | | - const organizationId = session.session.activeOrganizationId; |
96 | | - if (!organizationId) { |
97 | | - return c.json({ error: "No active organization" }, { status: 400 }); |
98 | | - } |
99 | | - |
100 | | - const membership = await MembersService.findByUserAndOrganization( |
101 | | - session.user.id, |
102 | | - organizationId, |
103 | | - ); |
104 | | - |
105 | | - if (!MembersService.isAdminOrOwner(membership?.role)) { |
106 | | - return c.json({ error: "Forbidden" }, { status: 403 }); |
107 | | - } |
108 | | - |
109 | | - const { shiftId } = c.req.param(); |
110 | | - let body: unknown = {}; |
111 | | - try { |
112 | | - body = await c.req.json(); |
113 | | - } catch { |
114 | | - body = {}; |
115 | | - } |
116 | | - |
117 | | - const parsed = updateShiftSchema.safeParse(body); |
118 | | - if (!parsed.success) { |
119 | | - return c.json({ error: "Invalid request body" }, { status: 400 }); |
120 | | - } |
121 | | - |
122 | | - const data = parsed.data; |
123 | | - |
124 | | - if (Object.keys(data).length === 0) { |
125 | | - return c.json({ error: "No fields to update" }, { status: 400 }); |
126 | | - } |
127 | | - |
128 | | - if (data.eventId) { |
129 | | - const event = await EventService.findById(data.eventId); |
130 | | - if (!event || event.organizationId !== organizationId) { |
131 | | - return c.json({ error: "Event not found" }, { status: 404 }); |
| 27 | + .patch( |
| 28 | + "/:shiftId", |
| 29 | + zValidator("param", shiftParamSchema), |
| 30 | + zValidator("json", updateShiftSchema), |
| 31 | + async (c) => { |
| 32 | + try { |
| 33 | + const { session, user } = await requireAdmin(c); |
| 34 | + const shift = await ShiftService.updateShift( |
| 35 | + c.req.valid("param").shiftId, |
| 36 | + session.activeOrganizationId!, |
| 37 | + user.id, |
| 38 | + c.req.valid("json"), |
| 39 | + ); |
| 40 | + return c.json({ success: true, shift }); |
| 41 | + } catch (error) { |
| 42 | + return shiftErrorResponse(c, error); |
132 | 43 | } |
133 | | - } |
134 | | - |
135 | | - const updates = Object.fromEntries( |
136 | | - Object.entries({ |
137 | | - ...data, |
138 | | - startTimestamp: |
139 | | - data.startTimestamp !== undefined |
140 | | - ? new Date(data.startTimestamp) |
141 | | - : undefined, |
142 | | - }).filter(([, value]) => value !== undefined), |
143 | | - ) as { |
144 | | - name?: string; |
145 | | - description?: string; |
146 | | - startTimestamp?: Date; |
147 | | - duration?: string; |
148 | | - rsvpLimit?: number; |
149 | | - eventId?: string; |
150 | | - }; |
151 | | - |
152 | | - await ShiftService.updateShift(shiftId, updates); |
153 | | - const shift = await ShiftService.findById(shiftId); |
154 | | - |
155 | | - return c.json({ success: true, shift }); |
156 | | - }) |
157 | | - .get("/:shiftId", async (c) => { |
158 | | - const session = await auth.api.getSession({ |
159 | | - headers: c.req.header(), |
160 | | - }); |
161 | | - |
162 | | - if (!session?.user) { |
163 | | - return c.json({ error: "Unauthorized" }, { status: 401 }); |
164 | | - } |
165 | | - |
166 | | - const organizationId = session.session.activeOrganizationId; |
167 | | - if (!organizationId) { |
168 | | - return c.json({ error: "No active organization" }, { status: 400 }); |
169 | | - } |
170 | | - |
171 | | - const { shiftId } = c.req.param(); |
172 | | - const shift = await ShiftService.findById(shiftId); |
173 | | - |
174 | | - if (!shift || shift.organizationId !== organizationId) { |
175 | | - return c.json( |
176 | | - { error: "Shift not found or unauthorized" }, |
177 | | - { status: 404 }, |
| 44 | + }, |
| 45 | + ) |
| 46 | + .get("/:shiftId", zValidator("param", shiftParamSchema), async (c) => { |
| 47 | + try { |
| 48 | + const { session, user } = await requireMembership(c); |
| 49 | + const shift = await ShiftService.findById( |
| 50 | + c.req.valid("param").shiftId, |
| 51 | + session.activeOrganizationId!, |
| 52 | + user.id, |
178 | 53 | ); |
| 54 | + if (!shift) return c.json({ error: "Shift not found" }, 404); |
| 55 | + return c.json({ status: 200, shift }); |
| 56 | + } catch (error) { |
| 57 | + return shiftErrorResponse(c, error); |
179 | 58 | } |
180 | | - |
181 | | - return c.json({ status: 200, shift }); |
182 | 59 | }) |
183 | | - .delete("/:shiftId", async (c) => { |
184 | | - const session = await auth.api.getSession({ |
185 | | - headers: c.req.header(), |
186 | | - }); |
187 | | - |
188 | | - if (!session?.user) { |
189 | | - return c.json({ error: "Unauthorized" }, { status: 401 }); |
190 | | - } |
191 | | - |
192 | | - const organizationId = session.session.activeOrganizationId; |
193 | | - if (!organizationId) { |
194 | | - return c.json({ error: "No active organization" }, { status: 400 }); |
195 | | - } |
196 | | - |
197 | | - const membership = await MembersService.findByUserAndOrganization( |
198 | | - session.user.id, |
199 | | - organizationId, |
200 | | - ); |
201 | | - |
202 | | - if (!MembersService.isAdminOrOwner(membership?.role)) { |
203 | | - return c.json({ error: "Forbidden" }, { status: 403 }); |
204 | | - } |
205 | | - |
206 | | - const { shiftId } = c.req.param(); |
207 | | - const shift = await ShiftService.deleteById(shiftId); |
208 | | - |
209 | | - if (!shift) { |
210 | | - return c.json({ error: "Shift not found" }, { status: 404 }); |
| 60 | + .delete("/:shiftId", zValidator("param", shiftParamSchema), async (c) => { |
| 61 | + try { |
| 62 | + const { session, user } = await requireAdmin(c); |
| 63 | + const shift = await ShiftService.deleteById( |
| 64 | + c.req.valid("param").shiftId, |
| 65 | + session.activeOrganizationId!, |
| 66 | + user.id, |
| 67 | + ); |
| 68 | + return c.json({ success: true, shift }); |
| 69 | + } catch (error) { |
| 70 | + return shiftErrorResponse(c, error); |
211 | 71 | } |
212 | | - |
213 | | - return c.json({ success: true, shift }); |
214 | 72 | }) |
215 | 73 | .route("", shiftRsvpApp); |
216 | 74 |
|
|
0 commit comments