fix: instrument bare catch blocks — logger.error sur stream/admin/user
This commit is contained in:
@@ -3,6 +3,7 @@ import { AppDataSource } from "../config/data-source";
|
||||
import { User } from "../entities/User";
|
||||
import { UserSubscription } from "../entities/UserSubscription";
|
||||
import { requireAuth, AuthenticatedRequest } from "../middleware/auth.middleware";
|
||||
import logger from "../utils/logger";
|
||||
|
||||
const router = Router();
|
||||
|
||||
@@ -27,45 +28,50 @@ async function getActiveSub(userId: string) {
|
||||
router.get("/me/profile", requireAuth, async (req: Request, res: Response): Promise<void> => {
|
||||
const { user } = req as AuthenticatedRequest;
|
||||
|
||||
const localUser = await AppDataSource.getRepository(User).findOne({
|
||||
where: { superOAuthId: user.id },
|
||||
relations: ["userRoles", "userRoles.role"],
|
||||
});
|
||||
try {
|
||||
const localUser = await AppDataSource.getRepository(User).findOne({
|
||||
where: { superOAuthId: user.id },
|
||||
relations: ["userRoles", "userRoles.role"],
|
||||
});
|
||||
|
||||
if (!localUser) {
|
||||
res.status(404).json({ success: false, error: "USER_NOT_FOUND" });
|
||||
return;
|
||||
if (!localUser) {
|
||||
res.status(404).json({ success: false, error: "USER_NOT_FOUND" });
|
||||
return;
|
||||
}
|
||||
|
||||
const roles = localUser.userRoles.map((ur) => ur.role.slug);
|
||||
const activeSub = await getActiveSub(localUser.id);
|
||||
|
||||
const plan = activeSub
|
||||
? { slug: activeSub.plan.slug, name: activeSub.plan.name, level: activeSub.plan.level }
|
||||
: null;
|
||||
|
||||
const subscription = activeSub
|
||||
? {
|
||||
status: activeSub.status,
|
||||
startsAt: activeSub.startsAt.toISOString(),
|
||||
endsAt: activeSub.endsAt ? activeSub.endsAt.toISOString() : null,
|
||||
}
|
||||
: null;
|
||||
|
||||
res.json({
|
||||
success: true,
|
||||
data: {
|
||||
id: localUser.id,
|
||||
superOAuthId: localUser.superOAuthId,
|
||||
email: localUser.email,
|
||||
nickname: localUser.nickname,
|
||||
avatar: localUser.avatar,
|
||||
roles,
|
||||
plan,
|
||||
subscription,
|
||||
createdAt: localUser.createdAt.toISOString(),
|
||||
},
|
||||
});
|
||||
} catch (err) {
|
||||
logger.error("GET /users/me/profile — DB error", { err });
|
||||
res.status(500).json({ success: false, error: "INTERNAL_ERROR" });
|
||||
}
|
||||
|
||||
const roles = localUser.userRoles.map((ur) => ur.role.slug);
|
||||
const activeSub = await getActiveSub(localUser.id);
|
||||
|
||||
const plan = activeSub
|
||||
? { slug: activeSub.plan.slug, name: activeSub.plan.name, level: activeSub.plan.level }
|
||||
: null;
|
||||
|
||||
const subscription = activeSub
|
||||
? {
|
||||
status: activeSub.status,
|
||||
startsAt: activeSub.startsAt.toISOString(),
|
||||
endsAt: activeSub.endsAt ? activeSub.endsAt.toISOString() : null,
|
||||
}
|
||||
: null;
|
||||
|
||||
res.json({
|
||||
success: true,
|
||||
data: {
|
||||
id: localUser.id,
|
||||
superOAuthId: localUser.superOAuthId,
|
||||
email: localUser.email,
|
||||
nickname: localUser.nickname,
|
||||
avatar: localUser.avatar,
|
||||
roles,
|
||||
plan,
|
||||
subscription,
|
||||
createdAt: localUser.createdAt.toISOString(),
|
||||
},
|
||||
});
|
||||
});
|
||||
|
||||
/**
|
||||
@@ -95,33 +101,38 @@ router.patch("/me", requireAuth, async (req: Request, res: Response): Promise<vo
|
||||
if (!["http:", "https:"].includes(parsed.protocol)) {
|
||||
throw new Error("invalid protocol");
|
||||
}
|
||||
} catch {
|
||||
} catch (_err) {
|
||||
res.status(400).json({ success: false, error: "INVALID_AVATAR", message: "avatar must be a valid http/https URL or null" });
|
||||
return;
|
||||
}
|
||||
}
|
||||
|
||||
const userRepo = AppDataSource.getRepository(User);
|
||||
const localUser = await userRepo.findOne({ where: { superOAuthId: user.id } });
|
||||
try {
|
||||
const userRepo = AppDataSource.getRepository(User);
|
||||
const localUser = await userRepo.findOne({ where: { superOAuthId: user.id } });
|
||||
|
||||
if (!localUser) {
|
||||
res.status(404).json({ success: false, error: "USER_NOT_FOUND" });
|
||||
return;
|
||||
if (!localUser) {
|
||||
res.status(404).json({ success: false, error: "USER_NOT_FOUND" });
|
||||
return;
|
||||
}
|
||||
|
||||
if (nickname !== undefined) localUser.nickname = (nickname as string).trim();
|
||||
if (avatar !== undefined) localUser.avatar = avatar as string | null;
|
||||
|
||||
await userRepo.save(localUser);
|
||||
|
||||
res.json({
|
||||
success: true,
|
||||
data: {
|
||||
id: localUser.id,
|
||||
nickname: localUser.nickname,
|
||||
avatar: localUser.avatar,
|
||||
},
|
||||
});
|
||||
} catch (err) {
|
||||
logger.error("PATCH /users/me — DB error", { err });
|
||||
res.status(500).json({ success: false, error: "INTERNAL_ERROR" });
|
||||
}
|
||||
|
||||
if (nickname !== undefined) localUser.nickname = (nickname as string).trim();
|
||||
if (avatar !== undefined) localUser.avatar = avatar as string | null;
|
||||
|
||||
await userRepo.save(localUser);
|
||||
|
||||
res.json({
|
||||
success: true,
|
||||
data: {
|
||||
id: localUser.id,
|
||||
nickname: localUser.nickname,
|
||||
avatar: localUser.avatar,
|
||||
},
|
||||
});
|
||||
});
|
||||
|
||||
export default router;
|
||||
|
||||
Reference in New Issue
Block a user