lapeh 1.0.1 → 1.0.2

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -1,353 +1,353 @@
1
- import { Request, Response } from "express";
2
- import { prisma } from "../prisma";
3
- import { sendSuccess, sendError } from "../utils/response";
4
-
5
- export async function createRole(req: Request, res: Response) {
6
- const { name, slug, description } = req.body || {};
7
- if (!name || !slug) {
8
- sendError(res, 422, "Validation error", {
9
- name: !name ? ["Nama role wajib diisi"] : undefined,
10
- slug: !slug ? ["Slug role wajib diisi"] : undefined,
11
- });
12
- return;
13
- }
14
- const exists = await prisma.roles.findUnique({ where: { slug } });
15
- if (exists) {
16
- sendError(res, 409, "Role already exists", {
17
- slug: ["Slug role sudah digunakan"],
18
- });
19
- return;
20
- }
21
- const role = await prisma.roles.create({
22
- data: {
23
- name,
24
- slug,
25
- description: description || null,
26
- created_at: new Date(),
27
- updated_at: new Date(),
28
- },
29
- });
30
- sendSuccess(res, 201, "Role created", role);
31
- }
32
-
33
- export async function listRoles(_req: Request, res: Response) {
34
- const roles = await prisma.roles.findMany({
35
- orderBy: { id: "asc" },
36
- });
37
- sendSuccess(res, 200, "Roles list", roles);
38
- }
39
-
40
- export async function updateRole(req: Request, res: Response) {
41
- const { id } = req.params;
42
- const roleId = BigInt(id);
43
- const { name, slug, description } = req.body || {};
44
- const role = await prisma.roles.findUnique({ where: { id: roleId } });
45
- if (!role) {
46
- sendError(res, 404, "Role not found");
47
- return;
48
- }
49
- if (slug) {
50
- const exists = await prisma.roles.findFirst({
51
- where: {
52
- slug,
53
- NOT: { id: roleId },
54
- },
55
- });
56
- if (exists) {
57
- sendError(res, 409, "Role already exists", {
58
- slug: ["Slug role sudah digunakan"],
59
- });
60
- return;
61
- }
62
- }
63
- const updated = await prisma.roles.update({
64
- where: { id: roleId },
65
- data: {
66
- name: name ?? role.name,
67
- slug: slug ?? role.slug,
68
- description: description ?? role.description,
69
- updated_at: new Date(),
70
- },
71
- });
72
- sendSuccess(res, 200, "Role updated", updated);
73
- }
74
-
75
- export async function deleteRole(req: Request, res: Response) {
76
- const { id } = req.params;
77
- const roleId = BigInt(id);
78
- const role = await prisma.roles.findUnique({ where: { id: roleId } });
79
- if (!role) {
80
- sendError(res, 404, "Role not found");
81
- return;
82
- }
83
- await prisma.role_permissions.deleteMany({ where: { role_id: roleId } });
84
- await prisma.user_roles.deleteMany({ where: { role_id: roleId } });
85
- await prisma.roles.delete({ where: { id: roleId } });
86
- sendSuccess(res, 200, "Role deleted", null);
87
- }
88
-
89
- export async function createPermission(req: Request, res: Response) {
90
- const { name, slug, description } = req.body || {};
91
- if (!name || !slug) {
92
- sendError(res, 422, "Validation error", {
93
- name: !name ? ["Nama permission wajib diisi"] : undefined,
94
- slug: !slug ? ["Slug permission wajib diisi"] : undefined,
95
- });
96
- return;
97
- }
98
- const exists = await prisma.permissions.findUnique({ where: { slug } });
99
- if (exists) {
100
- sendError(res, 409, "Permission already exists", {
101
- slug: ["Slug permission sudah digunakan"],
102
- });
103
- return;
104
- }
105
- const permission = await prisma.permissions.create({
106
- data: {
107
- name,
108
- slug,
109
- description: description || null,
110
- created_at: new Date(),
111
- updated_at: new Date(),
112
- },
113
- });
114
- sendSuccess(res, 201, "Permission created", permission);
115
- }
116
-
117
- export async function listPermissions(_req: Request, res: Response) {
118
- const permissions = await prisma.permissions.findMany({
119
- orderBy: { id: "asc" },
120
- });
121
- sendSuccess(res, 200, "Permissions list", permissions);
122
- }
123
-
124
- export async function updatePermission(req: Request, res: Response) {
125
- const { id } = req.params;
126
- const permissionId = BigInt(id);
127
- const { name, slug, description } = req.body || {};
128
- const permission = await prisma.permissions.findUnique({
129
- where: { id: permissionId },
130
- });
131
- if (!permission) {
132
- sendError(res, 404, "Permission not found");
133
- return;
134
- }
135
- if (slug) {
136
- const exists = await prisma.permissions.findFirst({
137
- where: {
138
- slug,
139
- NOT: { id: permissionId },
140
- },
141
- });
142
- if (exists) {
143
- sendError(res, 409, "Permission already exists", {
144
- slug: ["Slug permission sudah digunakan"],
145
- });
146
- return;
147
- }
148
- }
149
- const updated = await prisma.permissions.update({
150
- where: { id: permissionId },
151
- data: {
152
- name: name ?? permission.name,
153
- slug: slug ?? permission.slug,
154
- description: description ?? permission.description,
155
- updated_at: new Date(),
156
- },
157
- });
158
- sendSuccess(res, 200, "Permission updated", updated);
159
- }
160
-
161
- export async function deletePermission(req: Request, res: Response) {
162
- const { id } = req.params;
163
- const permissionId = BigInt(id);
164
- const permission = await prisma.permissions.findUnique({
165
- where: { id: permissionId },
166
- });
167
- if (!permission) {
168
- sendError(res, 404, "Permission not found");
169
- return;
170
- }
171
- await prisma.role_permissions.deleteMany({
172
- where: { permission_id: permissionId },
173
- });
174
- await prisma.user_permissions.deleteMany({
175
- where: { permission_id: permissionId },
176
- });
177
- await prisma.permissions.delete({ where: { id: permissionId } });
178
- sendSuccess(res, 200, "Permission deleted", null);
179
- }
180
-
181
- export async function assignRoleToUser(req: Request, res: Response) {
182
- const { userId, roleId } = req.body || {};
183
- if (!userId || !roleId) {
184
- sendError(res, 422, "Validation error", {
185
- userId: !userId ? ["userId wajib diisi"] : undefined,
186
- roleId: !roleId ? ["roleId wajib diisi"] : undefined,
187
- });
188
- return;
189
- }
190
- const user = await prisma.users.findUnique({
191
- where: { id: BigInt(userId) },
192
- });
193
- if (!user) {
194
- sendError(res, 404, "User not found");
195
- return;
196
- }
197
- const role = await prisma.roles.findUnique({
198
- where: { id: BigInt(roleId) },
199
- });
200
- if (!role) {
201
- sendError(res, 404, "Role not found");
202
- return;
203
- }
204
- await prisma.user_roles.upsert({
205
- where: {
206
- user_id_role_id: {
207
- user_id: BigInt(userId),
208
- role_id: BigInt(roleId),
209
- },
210
- },
211
- create: {
212
- user_id: BigInt(userId),
213
- role_id: BigInt(roleId),
214
- created_at: new Date(),
215
- },
216
- update: {},
217
- });
218
- sendSuccess(res, 200, "Role assigned to user", null);
219
- }
220
-
221
- export async function removeRoleFromUser(req: Request, res: Response) {
222
- const { userId, roleId } = req.body || {};
223
- if (!userId || !roleId) {
224
- sendError(res, 422, "Validation error", {
225
- userId: !userId ? ["userId wajib diisi"] : undefined,
226
- roleId: !roleId ? ["roleId wajib diisi"] : undefined,
227
- });
228
- return;
229
- }
230
- await prisma.user_roles.deleteMany({
231
- where: {
232
- user_id: BigInt(userId),
233
- role_id: BigInt(roleId),
234
- },
235
- });
236
- sendSuccess(res, 200, "Role removed from user", null);
237
- }
238
-
239
- export async function assignPermissionToRole(req: Request, res: Response) {
240
- const { roleId, permissionId } = req.body || {};
241
- if (!roleId || !permissionId) {
242
- sendError(res, 422, "Validation error", {
243
- roleId: !roleId ? ["roleId wajib diisi"] : undefined,
244
- permissionId: !permissionId ? ["permissionId wajib diisi"] : undefined,
245
- });
246
- return;
247
- }
248
- const role = await prisma.roles.findUnique({
249
- where: { id: BigInt(roleId) },
250
- });
251
- if (!role) {
252
- sendError(res, 404, "Role not found");
253
- return;
254
- }
255
- const permission = await prisma.permissions.findUnique({
256
- where: { id: BigInt(permissionId) },
257
- });
258
- if (!permission) {
259
- sendError(res, 404, "Permission not found");
260
- return;
261
- }
262
- await prisma.role_permissions.upsert({
263
- where: {
264
- role_id_permission_id: {
265
- role_id: BigInt(roleId),
266
- permission_id: BigInt(permissionId),
267
- },
268
- },
269
- create: {
270
- role_id: BigInt(roleId),
271
- permission_id: BigInt(permissionId),
272
- created_at: new Date(),
273
- },
274
- update: {},
275
- });
276
- sendSuccess(res, 200, "Permission assigned to role", null);
277
- }
278
-
279
- export async function removePermissionFromRole(req: Request, res: Response) {
280
- const { roleId, permissionId } = req.body || {};
281
- if (!roleId || !permissionId) {
282
- sendError(res, 422, "Validation error", {
283
- roleId: !roleId ? ["roleId wajib diisi"] : undefined,
284
- permissionId: !permissionId ? ["permissionId wajib diisi"] : undefined,
285
- });
286
- return;
287
- }
288
- await prisma.role_permissions.deleteMany({
289
- where: {
290
- role_id: BigInt(roleId),
291
- permission_id: BigInt(permissionId),
292
- },
293
- });
294
- sendSuccess(res, 200, "Permission removed from role", null);
295
- }
296
-
297
- export async function assignPermissionToUser(req: Request, res: Response) {
298
- const { userId, permissionId } = req.body || {};
299
- if (!userId || !permissionId) {
300
- sendError(res, 422, "Validation error", {
301
- userId: !userId ? ["userId wajib diisi"] : undefined,
302
- permissionId: !permissionId ? ["permissionId wajib diisi"] : undefined,
303
- });
304
- return;
305
- }
306
- const user = await prisma.users.findUnique({
307
- where: { id: BigInt(userId) },
308
- });
309
- if (!user) {
310
- sendError(res, 404, "User not found");
311
- return;
312
- }
313
- const permission = await prisma.permissions.findUnique({
314
- where: { id: BigInt(permissionId) },
315
- });
316
- if (!permission) {
317
- sendError(res, 404, "Permission not found");
318
- return;
319
- }
320
- await prisma.user_permissions.upsert({
321
- where: {
322
- user_id_permission_id: {
323
- user_id: BigInt(userId),
324
- permission_id: BigInt(permissionId),
325
- },
326
- },
327
- create: {
328
- user_id: BigInt(userId),
329
- permission_id: BigInt(permissionId),
330
- created_at: new Date(),
331
- },
332
- update: {},
333
- });
334
- sendSuccess(res, 200, "Permission assigned to user", null);
335
- }
336
-
337
- export async function removePermissionFromUser(req: Request, res: Response) {
338
- const { userId, permissionId } = req.body || {};
339
- if (!userId || !permissionId) {
340
- sendError(res, 422, "Validation error", {
341
- userId: !userId ? ["userId wajib diisi"] : undefined,
342
- permissionId: !permissionId ? ["permissionId wajib diisi"] : undefined,
343
- });
344
- return;
345
- }
346
- await prisma.user_permissions.deleteMany({
347
- where: {
348
- user_id: BigInt(userId),
349
- permission_id: BigInt(permissionId),
350
- },
351
- });
352
- sendSuccess(res, 200, "Permission removed from user", null);
353
- }
1
+ import { Request, Response } from "express";
2
+ import { prisma } from "../prisma";
3
+ import { sendSuccess, sendError } from "../utils/response";
4
+
5
+ export async function createRole(req: Request, res: Response) {
6
+ const { name, slug, description } = req.body || {};
7
+ if (!name || !slug) {
8
+ sendError(res, 422, "Validation error", {
9
+ name: !name ? ["Nama role wajib diisi"] : undefined,
10
+ slug: !slug ? ["Slug role wajib diisi"] : undefined,
11
+ });
12
+ return;
13
+ }
14
+ const exists = await prisma.roles.findUnique({ where: { slug } });
15
+ if (exists) {
16
+ sendError(res, 409, "Role already exists", {
17
+ slug: ["Slug role sudah digunakan"],
18
+ });
19
+ return;
20
+ }
21
+ const role = await prisma.roles.create({
22
+ data: {
23
+ name,
24
+ slug,
25
+ description: description || null,
26
+ created_at: new Date(),
27
+ updated_at: new Date(),
28
+ },
29
+ });
30
+ sendSuccess(res, 201, "Role created", role);
31
+ }
32
+
33
+ export async function listRoles(_req: Request, res: Response) {
34
+ const roles = await prisma.roles.findMany({
35
+ orderBy: { id: "asc" },
36
+ });
37
+ sendSuccess(res, 200, "Roles list", roles);
38
+ }
39
+
40
+ export async function updateRole(req: Request, res: Response) {
41
+ const { id } = req.params;
42
+ const roleId = BigInt(id);
43
+ const { name, slug, description } = req.body || {};
44
+ const role = await prisma.roles.findUnique({ where: { id: roleId } });
45
+ if (!role) {
46
+ sendError(res, 404, "Role not found");
47
+ return;
48
+ }
49
+ if (slug) {
50
+ const exists = await prisma.roles.findFirst({
51
+ where: {
52
+ slug,
53
+ NOT: { id: roleId },
54
+ },
55
+ });
56
+ if (exists) {
57
+ sendError(res, 409, "Role already exists", {
58
+ slug: ["Slug role sudah digunakan"],
59
+ });
60
+ return;
61
+ }
62
+ }
63
+ const updated = await prisma.roles.update({
64
+ where: { id: roleId },
65
+ data: {
66
+ name: name ?? role.name,
67
+ slug: slug ?? role.slug,
68
+ description: description ?? role.description,
69
+ updated_at: new Date(),
70
+ },
71
+ });
72
+ sendSuccess(res, 200, "Role updated", updated);
73
+ }
74
+
75
+ export async function deleteRole(req: Request, res: Response) {
76
+ const { id } = req.params;
77
+ const roleId = BigInt(id);
78
+ const role = await prisma.roles.findUnique({ where: { id: roleId } });
79
+ if (!role) {
80
+ sendError(res, 404, "Role not found");
81
+ return;
82
+ }
83
+ await prisma.role_permissions.deleteMany({ where: { role_id: roleId } });
84
+ await prisma.user_roles.deleteMany({ where: { role_id: roleId } });
85
+ await prisma.roles.delete({ where: { id: roleId } });
86
+ sendSuccess(res, 200, "Role deleted", null);
87
+ }
88
+
89
+ export async function createPermission(req: Request, res: Response) {
90
+ const { name, slug, description } = req.body || {};
91
+ if (!name || !slug) {
92
+ sendError(res, 422, "Validation error", {
93
+ name: !name ? ["Nama permission wajib diisi"] : undefined,
94
+ slug: !slug ? ["Slug permission wajib diisi"] : undefined,
95
+ });
96
+ return;
97
+ }
98
+ const exists = await prisma.permissions.findUnique({ where: { slug } });
99
+ if (exists) {
100
+ sendError(res, 409, "Permission already exists", {
101
+ slug: ["Slug permission sudah digunakan"],
102
+ });
103
+ return;
104
+ }
105
+ const permission = await prisma.permissions.create({
106
+ data: {
107
+ name,
108
+ slug,
109
+ description: description || null,
110
+ created_at: new Date(),
111
+ updated_at: new Date(),
112
+ },
113
+ });
114
+ sendSuccess(res, 201, "Permission created", permission);
115
+ }
116
+
117
+ export async function listPermissions(_req: Request, res: Response) {
118
+ const permissions = await prisma.permissions.findMany({
119
+ orderBy: { id: "asc" },
120
+ });
121
+ sendSuccess(res, 200, "Permissions list", permissions);
122
+ }
123
+
124
+ export async function updatePermission(req: Request, res: Response) {
125
+ const { id } = req.params;
126
+ const permissionId = BigInt(id);
127
+ const { name, slug, description } = req.body || {};
128
+ const permission = await prisma.permissions.findUnique({
129
+ where: { id: permissionId },
130
+ });
131
+ if (!permission) {
132
+ sendError(res, 404, "Permission not found");
133
+ return;
134
+ }
135
+ if (slug) {
136
+ const exists = await prisma.permissions.findFirst({
137
+ where: {
138
+ slug,
139
+ NOT: { id: permissionId },
140
+ },
141
+ });
142
+ if (exists) {
143
+ sendError(res, 409, "Permission already exists", {
144
+ slug: ["Slug permission sudah digunakan"],
145
+ });
146
+ return;
147
+ }
148
+ }
149
+ const updated = await prisma.permissions.update({
150
+ where: { id: permissionId },
151
+ data: {
152
+ name: name ?? permission.name,
153
+ slug: slug ?? permission.slug,
154
+ description: description ?? permission.description,
155
+ updated_at: new Date(),
156
+ },
157
+ });
158
+ sendSuccess(res, 200, "Permission updated", updated);
159
+ }
160
+
161
+ export async function deletePermission(req: Request, res: Response) {
162
+ const { id } = req.params;
163
+ const permissionId = BigInt(id);
164
+ const permission = await prisma.permissions.findUnique({
165
+ where: { id: permissionId },
166
+ });
167
+ if (!permission) {
168
+ sendError(res, 404, "Permission not found");
169
+ return;
170
+ }
171
+ await prisma.role_permissions.deleteMany({
172
+ where: { permission_id: permissionId },
173
+ });
174
+ await prisma.user_permissions.deleteMany({
175
+ where: { permission_id: permissionId },
176
+ });
177
+ await prisma.permissions.delete({ where: { id: permissionId } });
178
+ sendSuccess(res, 200, "Permission deleted", null);
179
+ }
180
+
181
+ export async function assignRoleToUser(req: Request, res: Response) {
182
+ const { userId, roleId } = req.body || {};
183
+ if (!userId || !roleId) {
184
+ sendError(res, 422, "Validation error", {
185
+ userId: !userId ? ["userId wajib diisi"] : undefined,
186
+ roleId: !roleId ? ["roleId wajib diisi"] : undefined,
187
+ });
188
+ return;
189
+ }
190
+ const user = await prisma.users.findUnique({
191
+ where: { id: BigInt(userId) },
192
+ });
193
+ if (!user) {
194
+ sendError(res, 404, "User not found");
195
+ return;
196
+ }
197
+ const role = await prisma.roles.findUnique({
198
+ where: { id: BigInt(roleId) },
199
+ });
200
+ if (!role) {
201
+ sendError(res, 404, "Role not found");
202
+ return;
203
+ }
204
+ await prisma.user_roles.upsert({
205
+ where: {
206
+ user_id_role_id: {
207
+ user_id: BigInt(userId),
208
+ role_id: BigInt(roleId),
209
+ },
210
+ },
211
+ create: {
212
+ user_id: BigInt(userId),
213
+ role_id: BigInt(roleId),
214
+ created_at: new Date(),
215
+ },
216
+ update: {},
217
+ });
218
+ sendSuccess(res, 200, "Role assigned to user", null);
219
+ }
220
+
221
+ export async function removeRoleFromUser(req: Request, res: Response) {
222
+ const { userId, roleId } = req.body || {};
223
+ if (!userId || !roleId) {
224
+ sendError(res, 422, "Validation error", {
225
+ userId: !userId ? ["userId wajib diisi"] : undefined,
226
+ roleId: !roleId ? ["roleId wajib diisi"] : undefined,
227
+ });
228
+ return;
229
+ }
230
+ await prisma.user_roles.deleteMany({
231
+ where: {
232
+ user_id: BigInt(userId),
233
+ role_id: BigInt(roleId),
234
+ },
235
+ });
236
+ sendSuccess(res, 200, "Role removed from user", null);
237
+ }
238
+
239
+ export async function assignPermissionToRole(req: Request, res: Response) {
240
+ const { roleId, permissionId } = req.body || {};
241
+ if (!roleId || !permissionId) {
242
+ sendError(res, 422, "Validation error", {
243
+ roleId: !roleId ? ["roleId wajib diisi"] : undefined,
244
+ permissionId: !permissionId ? ["permissionId wajib diisi"] : undefined,
245
+ });
246
+ return;
247
+ }
248
+ const role = await prisma.roles.findUnique({
249
+ where: { id: BigInt(roleId) },
250
+ });
251
+ if (!role) {
252
+ sendError(res, 404, "Role not found");
253
+ return;
254
+ }
255
+ const permission = await prisma.permissions.findUnique({
256
+ where: { id: BigInt(permissionId) },
257
+ });
258
+ if (!permission) {
259
+ sendError(res, 404, "Permission not found");
260
+ return;
261
+ }
262
+ await prisma.role_permissions.upsert({
263
+ where: {
264
+ role_id_permission_id: {
265
+ role_id: BigInt(roleId),
266
+ permission_id: BigInt(permissionId),
267
+ },
268
+ },
269
+ create: {
270
+ role_id: BigInt(roleId),
271
+ permission_id: BigInt(permissionId),
272
+ created_at: new Date(),
273
+ },
274
+ update: {},
275
+ });
276
+ sendSuccess(res, 200, "Permission assigned to role", null);
277
+ }
278
+
279
+ export async function removePermissionFromRole(req: Request, res: Response) {
280
+ const { roleId, permissionId } = req.body || {};
281
+ if (!roleId || !permissionId) {
282
+ sendError(res, 422, "Validation error", {
283
+ roleId: !roleId ? ["roleId wajib diisi"] : undefined,
284
+ permissionId: !permissionId ? ["permissionId wajib diisi"] : undefined,
285
+ });
286
+ return;
287
+ }
288
+ await prisma.role_permissions.deleteMany({
289
+ where: {
290
+ role_id: BigInt(roleId),
291
+ permission_id: BigInt(permissionId),
292
+ },
293
+ });
294
+ sendSuccess(res, 200, "Permission removed from role", null);
295
+ }
296
+
297
+ export async function assignPermissionToUser(req: Request, res: Response) {
298
+ const { userId, permissionId } = req.body || {};
299
+ if (!userId || !permissionId) {
300
+ sendError(res, 422, "Validation error", {
301
+ userId: !userId ? ["userId wajib diisi"] : undefined,
302
+ permissionId: !permissionId ? ["permissionId wajib diisi"] : undefined,
303
+ });
304
+ return;
305
+ }
306
+ const user = await prisma.users.findUnique({
307
+ where: { id: BigInt(userId) },
308
+ });
309
+ if (!user) {
310
+ sendError(res, 404, "User not found");
311
+ return;
312
+ }
313
+ const permission = await prisma.permissions.findUnique({
314
+ where: { id: BigInt(permissionId) },
315
+ });
316
+ if (!permission) {
317
+ sendError(res, 404, "Permission not found");
318
+ return;
319
+ }
320
+ await prisma.user_permissions.upsert({
321
+ where: {
322
+ user_id_permission_id: {
323
+ user_id: BigInt(userId),
324
+ permission_id: BigInt(permissionId),
325
+ },
326
+ },
327
+ create: {
328
+ user_id: BigInt(userId),
329
+ permission_id: BigInt(permissionId),
330
+ created_at: new Date(),
331
+ },
332
+ update: {},
333
+ });
334
+ sendSuccess(res, 200, "Permission assigned to user", null);
335
+ }
336
+
337
+ export async function removePermissionFromUser(req: Request, res: Response) {
338
+ const { userId, permissionId } = req.body || {};
339
+ if (!userId || !permissionId) {
340
+ sendError(res, 422, "Validation error", {
341
+ userId: !userId ? ["userId wajib diisi"] : undefined,
342
+ permissionId: !permissionId ? ["permissionId wajib diisi"] : undefined,
343
+ });
344
+ return;
345
+ }
346
+ await prisma.user_permissions.deleteMany({
347
+ where: {
348
+ user_id: BigInt(userId),
349
+ permission_id: BigInt(permissionId),
350
+ },
351
+ });
352
+ sendSuccess(res, 200, "Permission removed from user", null);
353
+ }