forked from JumboCode/TheLegacyProject
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
208a778
commit 18e65e3
Showing
13 changed files
with
135 additions
and
269 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
7 changes: 2 additions & 5 deletions
7
.../api/user/[uid]/edit-role/route.schema.ts → src/app/api/admin/edit-role/route.schema.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,52 @@ | ||
import { withRole, withSession } from "@server/decorator"; | ||
import { EditRoleRequest, EditRoleResponse } from "./route.schema"; | ||
import { NextResponse } from "next/server"; | ||
import { prisma } from "@server/db/client"; | ||
import { Role } from "@prisma/client"; | ||
|
||
/** | ||
* Update multiple non-admin user's role. | ||
*/ | ||
export const PATCH = withSession( | ||
withRole(["ADMIN"], async ({ req, session }) => { | ||
const maybeBody = EditRoleRequest.safeParse(await req.json()); | ||
|
||
if (!maybeBody.success || session.user.role !== "ADMIN") { | ||
return NextResponse.json( | ||
EditRoleResponse.parse({ | ||
code: "INVALID_REQUEST", | ||
message: "Invalid request", | ||
}), | ||
{ status: 400 } | ||
); | ||
} | ||
|
||
const updateManyStudents = (ids: string[], role: Role) => { | ||
if (ids.length > 0) { | ||
return prisma.user.updateMany({ | ||
where: { | ||
id: { | ||
in: ids, | ||
}, | ||
}, | ||
data: { | ||
role: role, | ||
}, | ||
}); | ||
} | ||
return Promise.resolve(); | ||
}; | ||
|
||
await Promise.allSettled([ | ||
updateManyStudents(maybeBody.data.chapterLeaders, "CHAPTER_LEADER"), | ||
updateManyStudents(maybeBody.data.users, "USER"), | ||
]); | ||
|
||
return NextResponse.json( | ||
EditRoleResponse.parse({ | ||
code: "SUCCESS", | ||
message: "Updated user role", | ||
}) | ||
); | ||
}) | ||
); |
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.