fixed web socket noti by token
All checks were successful
Build & Deploy on Dev / build (push) Successful in 1m1s

This commit is contained in:
Warunee Tamkoo 2026-05-15 14:33:00 +07:00
parent 74d03176cd
commit b103e15788

View file

@ -36,13 +36,26 @@ export class SocketController extends Controller {
},
@Request() req: RequestWithUser,
) {
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 };
sendWebSocket(
"socket-notification",
{ success: !payload.error, message: payload.message },
{
roles: payload.roles || req.user.role || [],
userId: payload.targetUserId || req.user.sub || [],
},
recipients,
);
}
}