games_app/backend/dist/controllers/userController.js
2026-07-25 22:48:54 +02:00

143 lines
5.7 KiB
JavaScript

"use strict";
/**
* User controller — HTTP handlers for the authenticated user's profile (read and update).
*/
var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
if (k2 === undefined) k2 = k;
var desc = Object.getOwnPropertyDescriptor(m, k);
if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
desc = { enumerable: true, get: function() { return m[k]; } };
}
Object.defineProperty(o, k2, desc);
}) : (function(o, m, k, k2) {
if (k2 === undefined) k2 = k;
o[k2] = m[k];
}));
var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) {
Object.defineProperty(o, "default", { enumerable: true, value: v });
}) : function(o, v) {
o["default"] = v;
});
var __importStar = (this && this.__importStar) || (function () {
var ownKeys = function(o) {
ownKeys = Object.getOwnPropertyNames || function (o) {
var ar = [];
for (var k in o) if (Object.prototype.hasOwnProperty.call(o, k)) ar[ar.length] = k;
return ar;
};
return ownKeys(o);
};
return function (mod) {
if (mod && mod.__esModule) return mod;
var result = {};
if (mod != null) for (var k = ownKeys(mod), i = 0; i < k.length; i++) if (k[i] !== "default") __createBinding(result, mod, k[i]);
__setModuleDefault(result, mod);
return result;
};
})();
Object.defineProperty(exports, "__esModule", { value: true });
exports.getMe = getMe;
exports.updateMe = updateMe;
exports.listSecurityNotifications = listSecurityNotifications;
exports.markSecurityNotificationRead = markSecurityNotificationRead;
exports.markAllSecurityNotificationsRead = markAllSecurityNotificationsRead;
const authService = __importStar(require("../services/authService"));
const userSecurityNotificationRepository = __importStar(require("../repositories/userSecurityNotificationRepository"));
/**
* Returns the authenticated user's profile.
* @param req - Authenticated request; user ID is taken from the JWT.
* @param res - JSON profile object on success, 400 on failure.
*/
async function getMe(req, res) {
try {
const userId = req.user.userId;
const profile = await authService.getProfile(userId);
res.json(profile);
}
catch (e) {
const msg = e instanceof Error ? e.message : 'Failed to load profile';
res.status(400).json({ error: msg });
}
}
/**
* Updates the authenticated user's profile fields (name, date of birth, email, phone).
* @param req - Authenticated request. Body: optional `firstName`, `middleName`, `lastName`, `dateOfBirth`, `email`, `phone`.
* @param res - JSON `{ message: 'Profile updated' }` on success, 400 on validation or update failure.
*/
async function updateMe(req, res) {
try {
const userId = req.user.userId;
const { firstName, middleName, lastName, dateOfBirth, email, phone, countryOfResidence, nationality, gender } = req.body;
await authService.updateProfile(userId, {
...(firstName !== undefined ? { firstName } : {}),
...(middleName !== undefined ? { middleName } : {}),
...(lastName !== undefined ? { lastName } : {}),
...(dateOfBirth !== undefined ? { dateOfBirth } : {}),
...(email !== undefined ? { email } : {}),
...(phone !== undefined ? { phone } : {}),
...(countryOfResidence !== undefined ? { countryOfResidence } : {}),
...(nationality !== undefined ? { nationality } : {}),
...(gender !== undefined ? { gender } : {}),
});
res.json({ message: 'Profile updated' });
}
catch (e) {
const msg = e instanceof Error ? e.message : 'Update failed';
res.status(400).json({ error: msg });
}
}
async function listSecurityNotifications(req, res) {
try {
const userId = req.user.userId;
const limit = parseInt(req.query.limit || '30', 10);
const offset = parseInt(req.query.offset || '0', 10);
const unreadOnly = req.query.unreadOnly === 'true';
const result = await userSecurityNotificationRepository.listForUser(userId, {
limit,
offset,
unreadOnly,
});
res.json({
items: result.items.map((row) => ({
id: row.id,
eventType: row.event_type,
title: row.title,
body: row.body,
readAt: row.read_at,
metadata: row.metadata,
createdAt: row.created_at,
})),
total: result.total,
unreadCount: result.unreadCount,
});
}
catch (e) {
const msg = e instanceof Error ? e.message : 'Failed to load notifications';
res.status(500).json({ error: msg });
}
}
async function markSecurityNotificationRead(req, res) {
try {
const ok = await userSecurityNotificationRepository.markRead(req.user.userId, req.params.notificationId);
if (!ok) {
res.status(404).json({ error: 'Notification not found' });
return;
}
res.json({ message: 'Marked as read' });
}
catch (e) {
const msg = e instanceof Error ? e.message : 'Failed to update notification';
res.status(400).json({ error: msg });
}
}
async function markAllSecurityNotificationsRead(req, res) {
try {
const count = await userSecurityNotificationRepository.markAllRead(req.user.userId);
res.json({ marked: count });
}
catch (e) {
const msg = e instanceof Error ? e.message : 'Failed to update notifications';
res.status(400).json({ error: msg });
}
}
//# sourceMappingURL=userController.js.map