hrms-api-org/src/controllers/SocketController.ts

62 lines
1.7 KiB
TypeScript
Raw Normal View History

2026-05-14 12:33:21 +07:00
import { Body, Controller, Post, Request, Route, Security } from "tsoa";
import { sendWebSocket } from "../services/webSocket";
2026-05-14 12:33:21 +07:00
import { RequestWithUser } from "../middlewares/user";
2025-09-02 16:18:48 +07:00
@Route("/api/v1/org/through-socket")
export class SocketController extends Controller {
@Post("notify")
async notify(
@Body()
payload: {
message: string;
userId?: string | string[];
roles?: string | string[];
error?: boolean;
},
) {
sendWebSocket(
"socket-notification",
{ success: !payload.error, message: payload.message },
{
roles: payload.roles || [],
userId: payload.userId || [],
},
);
}
2026-05-14 12:33:21 +07:00
@Post("notify-from-token")
@Security("bearerAuth")
async notifyFromToken(
@Body()
payload: {
message: string;
targetUserId?: string | string[];
roles?: string | string[];
error?: boolean;
},
@Request() req: RequestWithUser,
) {
2026-05-15 14:33:00 +07:00
const toArray = (value?: string | string[]) => {
if (Array.isArray(value)) return value.filter(Boolean);
if (typeof value === "string" && value.trim()) return [value];
return [] as string[];
};
const targetUserIds = toArray(payload.targetUserId);
const targetRoles = toArray(payload.roles);
// If caller provides explicit user targets, do not combine with role targeting.
// This prevents accidental broad notifications when roles include common roles.
const recipients =
targetUserIds.length > 0
? { userId: targetUserIds, roles: [] as string[] }
: { userId: [req.user.sub], roles: targetRoles };
2026-05-14 12:33:21 +07:00
sendWebSocket(
"socket-notification",
{ success: !payload.error, message: payload.message },
2026-05-15 14:33:00 +07:00
recipients,
2026-05-14 12:33:21 +07:00
);
}
}