-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(user): add permissions while creating role (#622)
- Loading branch information
1 parent
f8d5fb7
commit 5023a51
Showing
9 changed files
with
238 additions
and
18 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
interface CustomApiErrorType { | ||
message: string; | ||
name: string; | ||
statusCode: number; | ||
} | ||
|
||
class CustomApiError extends Error { | ||
public statusCode: number; | ||
|
||
constructor({ message, name, statusCode }: CustomApiErrorType) { | ||
super(message); | ||
this.message = message; | ||
this.name = name; | ||
this.statusCode = statusCode; | ||
} | ||
} | ||
|
||
export default CustomApiError; | ||
|
||
export type { CustomApiErrorType }; |
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
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,61 @@ | ||
import CustomApiError from "../../../customApiError"; | ||
import RoleService from "../service"; | ||
|
||
import type { FastifyReply } from "fastify"; | ||
import type { SessionRequest } from "supertokens-node/framework/fastify"; | ||
|
||
const deleteRole = async (request: SessionRequest, reply: FastifyReply) => { | ||
const { log, query } = request; | ||
|
||
try { | ||
let { role } = query as { role?: string }; | ||
|
||
if (role) { | ||
try { | ||
role = JSON.parse(role) as string; | ||
} catch { | ||
/* empty */ | ||
} | ||
|
||
if (typeof role != "string") { | ||
throw new CustomApiError({ | ||
name: "UNKNOWN_ROLE_ERROR", | ||
message: `Invalid role`, | ||
statusCode: 422, | ||
}); | ||
} | ||
|
||
const service = new RoleService(); | ||
|
||
const deleteResponse = await service.deleteRole(role); | ||
|
||
return reply.send(deleteResponse); | ||
} | ||
|
||
throw new CustomApiError({ | ||
name: "UNKNOWN_ROLE_ERROR", | ||
message: `Invalid role`, | ||
statusCode: 422, | ||
}); | ||
} catch (error) { | ||
if (error instanceof CustomApiError) { | ||
reply.status(error.statusCode); | ||
|
||
return reply.send({ | ||
message: error.message, | ||
name: error.name, | ||
statusCode: error.statusCode, | ||
}); | ||
} | ||
|
||
log.error(error); | ||
reply.status(500); | ||
|
||
return reply.send({ | ||
status: "ERROR", | ||
message: "Oops! Something went wrong", | ||
}); | ||
} | ||
}; | ||
|
||
export default deleteRole; |
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
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
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