refactor /auth/me into /me
This commit is contained in:
1 parent
4b92b2a8df
commit
1a2d11e6de
14 files changed
+226
-110
No files matched your search
@@ -0,0 +1,62 @@
|
||||
import { Request, Response, NextFunction } from "express";
|
||||
import type { IMeService } from "@/services/business/MeService.js";
|
||||
import ApiError from "@/utils/ApiError.js";
|
||||
|
||||
export interface IMeController {
|
||||
me: (req: Request, res: Response, next: NextFunction) => Promise<void>;
|
||||
getEntitlements: (
|
||||
req: Request,
|
||||
res: Response,
|
||||
next: NextFunction
|
||||
) => Promise<void>;
|
||||
getPermissions: (
|
||||
req: Request,
|
||||
res: Response,
|
||||
next: NextFunction
|
||||
) => Promise<void>;
|
||||
}
|
||||
|
||||
class MeController implements IMeController {
|
||||
private meService: IMeService;
|
||||
|
||||
constructor(meService: IMeService) {
|
||||
this.meService = meService;
|
||||
}
|
||||
|
||||
me = async (req: Request, res: Response, next: NextFunction) => {
|
||||
try {
|
||||
const user = req.user;
|
||||
if (!user) {
|
||||
throw new ApiError("Unauthorized", 401);
|
||||
}
|
||||
const returnableUser = await this.meService.me(user.sub);
|
||||
res
|
||||
.status(200)
|
||||
.json({ message: "User retrieved successfully", data: returnableUser });
|
||||
} catch (error) {
|
||||
next(error);
|
||||
}
|
||||
};
|
||||
|
||||
getEntitlements = async (req: Request, res: Response, next: NextFunction) => {
|
||||
try {
|
||||
const orgId = req.user?.orgId;
|
||||
if (!orgId) throw new ApiError("Missing org context", 400);
|
||||
const entitlements = await this.meService.getEntitlements(orgId);
|
||||
res.status(200).json({ message: "OK", data: entitlements });
|
||||
} catch (error) {
|
||||
next(error);
|
||||
}
|
||||
};
|
||||
|
||||
getPermissions = async (req: Request, res: Response, next: NextFunction) => {
|
||||
try {
|
||||
const data = await this.meService.getPermissions((req as any).user);
|
||||
res.status(200).json({ message: "OK", data });
|
||||
} catch (error) {
|
||||
next(error);
|
||||
}
|
||||
};
|
||||
}
|
||||
|
||||
export default MeController;
|
||||
@@ -16,3 +16,5 @@ export { default as IncidentsController } from "./IncidentsController.js";
|
||||
export { default as SettingsController } from "./SettingsController.js";
|
||||
export { default as BillingController } from "./BillingController.js";
|
||||
export { default as StripeController } from "./StripeController.js";
|
||||
export { default as MeController } from "./MeController.js";
|
||||
export type { IMeController } from "./MeController.js";
|
||||
Reference in new issue
Block a user