games_app/backend/dist/controllers/supportController.js
2026-11-09 18:14:42 +02:00

415 lines
15 KiB
JavaScript

"use strict";
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.getOrCreateConversation = getOrCreateConversation;
exports.postCurrentMessage = postCurrentMessage;
exports.userSendCurrentWithAttachment = userSendCurrentWithAttachment;
exports.listMyConversations = listMyConversations;
exports.getMessages = getMessages;
exports.patchMessage = patchMessage;
exports.removeMessage = removeMessage;
exports.postMessage = postMessage;
exports.postTyping = postTyping;
exports.uploadCurrentAttachment = uploadCurrentAttachment;
exports.uploadAttachment = uploadAttachment;
exports.getAttachment = getAttachment;
exports.adminListConversations = adminListConversations;
exports.adminGetConversation = adminGetConversation;
exports.adminUpdateConversation = adminUpdateConversation;
exports.adminPostMessage = adminPostMessage;
exports.adminGetMessages = adminGetMessages;
exports.adminUnreadCount = adminUnreadCount;
exports.adminUploadAttachment = adminUploadAttachment;
exports.adminSendWithAttachment = adminSendWithAttachment;
exports.userSendWithAttachment = userSendWithAttachment;
const optionalAuth_1 = require("../middleware/optionalAuth");
const fileMagic_1 = require("../utils/fileMagic");
const authService_1 = require("../services/authService");
const supportService = __importStar(require("../services/supportService"));
function buildCtx(req) {
return {
userId: req.user?.userId,
role: req.user?.role,
visitorToken: (0, optionalAuth_1.readVisitorToken)(req),
};
}
function handleError(res, e) {
const msg = e instanceof Error ? e.message : 'Request failed';
if (msg === 'Forbidden') {
res.status(403).json({ error: msg });
return;
}
if (msg === 'Conversation not found' || msg === 'Not found' || msg === 'Message not found') {
res.status(404).json({ error: msg });
return;
}
if (msg === 'Edit window expired' ||
msg === 'Delete window expired' ||
msg === 'Message already deleted') {
res.status(409).json({ error: msg });
return;
}
res.status(400).json({ error: msg });
}
async function getOrCreateConversation(req, res) {
try {
const conv = await supportService.findCurrentConversation(buildCtx(req));
res.json(conv);
}
catch (e) {
handleError(res, e);
}
}
async function postCurrentMessage(req, res) {
try {
const body = typeof req.body?.body === 'string' ? req.body.body : undefined;
const msg = await supportService.sendToCurrentConversation(buildCtx(req), { body });
res.status(201).json(msg);
}
catch (e) {
handleError(res, e);
}
}
async function userSendCurrentWithAttachment(req, res) {
try {
const body = typeof req.body?.body === 'string' ? req.body.body : undefined;
const attachmentUrl = typeof req.body?.attachmentUrl === 'string' ? req.body.attachmentUrl : undefined;
const attachmentName = typeof req.body?.attachmentName === 'string' ? req.body.attachmentName : undefined;
const attachmentMime = typeof req.body?.attachmentMime === 'string' ? req.body.attachmentMime : undefined;
const msg = await supportService.sendToCurrentConversation(buildCtx(req), {
body,
attachmentUrl,
attachmentName,
attachmentMime,
});
res.status(201).json(msg);
}
catch (e) {
handleError(res, e);
}
}
async function listMyConversations(req, res) {
try {
const items = await supportService.listUserConversations(buildCtx(req));
res.json({ items });
}
catch (e) {
handleError(res, e);
}
}
async function getMessages(req, res) {
try {
const after = typeof req.query.after === 'string' ? req.query.after : undefined;
const data = await supportService.getConversationMessages(req.params.id, buildCtx(req), after);
res.json(data);
}
catch (e) {
handleError(res, e);
}
}
async function patchMessage(req, res) {
try {
const body = typeof req.body?.body === 'string' ? req.body.body : '';
const msg = await supportService.editMessage(req.params.id, req.params.messageId, buildCtx(req), body);
res.json(msg);
}
catch (e) {
handleError(res, e);
}
}
async function removeMessage(req, res) {
try {
const msg = await supportService.deleteMessage(req.params.id, req.params.messageId, buildCtx(req));
res.json(msg);
}
catch (e) {
handleError(res, e);
}
}
async function postMessage(req, res) {
try {
const body = typeof req.body?.body === 'string' ? req.body.body : undefined;
const msg = await supportService.sendMessage(req.params.id, buildCtx(req), { body });
res.status(201).json(msg);
}
catch (e) {
handleError(res, e);
}
}
async function postTyping(req, res) {
try {
const active = req.body?.active !== false;
const result = await supportService.setTyping(req.params.id, buildCtx(req), active);
res.json(result);
}
catch (e) {
handleError(res, e);
}
}
async function uploadCurrentAttachment(req, res) {
try {
if (!req.file) {
res.status(400).json({ error: 'No file uploaded' });
return;
}
const ctx = buildCtx(req);
try {
const saved = await supportService.finalizeSupportUpload({
tempFilename: req.file.filename,
originalName: req.file.originalname,
ctx,
});
res.status(201).json({
attachmentUrl: saved.url,
attachmentName: saved.originalName,
attachmentMime: saved.mime,
});
}
catch (e) {
(0, fileMagic_1.removeUploadedFile)(supportService.resolveUploadPath(req.file.filename));
const msg = e instanceof Error ? e.message : 'File validation failed';
res.status(msg.toLowerCase().includes('virus') ? 422 : 400).json({ error: msg });
}
}
catch (e) {
handleError(res, e);
}
}
async function uploadAttachment(req, res) {
try {
if (!req.file) {
res.status(400).json({ error: 'No file uploaded' });
return;
}
const ctx = buildCtx(req);
try {
await supportService.assertConversationAccess(req.params.id, ctx);
const saved = await supportService.finalizeSupportUpload({
tempFilename: req.file.filename,
originalName: req.file.originalname,
conversationId: req.params.id,
ctx,
});
res.status(201).json({
attachmentUrl: saved.url,
attachmentName: saved.originalName,
attachmentMime: saved.mime,
});
}
catch (e) {
(0, fileMagic_1.removeUploadedFile)(supportService.resolveUploadPath(req.file.filename));
const msg = e instanceof Error ? e.message : 'File validation failed';
if (msg === 'Forbidden' || msg === 'Conversation not found') {
handleError(res, e);
return;
}
res.status(msg.toLowerCase().includes('virus') ? 422 : 400).json({ error: msg });
}
}
catch (e) {
handleError(res, e);
}
}
async function getAttachment(req, res) {
try {
// <img src> cannot send Authorization — accept accessToken query as fallback.
if (!req.user) {
const q = req.query.accessToken;
if (typeof q === 'string' && q.trim()) {
try {
req.user = await (0, authService_1.verifyToken)(q.trim());
}
catch {
res.status(401).json({ error: 'Invalid or expired token' });
return;
}
}
}
const ctx = buildCtx(req);
if (!ctx.userId && !ctx.visitorToken && !supportService.isStaffRole(ctx.role)) {
res.status(401).json({ error: 'Authentication required' });
return;
}
const access = await supportService.assertSupportFileDownloadAccess(req.params.filename, ctx);
const downloadName = access.originalName || req.params.filename;
res.setHeader('Content-Type', access.mime);
res.setHeader('Content-Disposition', supportService.attachmentContentDisposition(downloadName));
res.setHeader('X-Content-Type-Options', 'nosniff');
// Helmet defaults to CORP same-origin; the web app is on another port/origin.
res.setHeader('Cross-Origin-Resource-Policy', 'cross-origin');
res.sendFile(access.filePath);
}
catch (e) {
handleError(res, e);
}
}
async function adminListConversations(req, res) {
try {
const assignedTo = req.query.assignedTo === 'unassigned'
? null
: typeof req.query.assignedTo === 'string'
? req.query.assignedTo
: undefined;
const data = await supportService.listAdminConversations({
status: typeof req.query.status === 'string' ? req.query.status : undefined,
assignedTo,
search: typeof req.query.search === 'string' ? req.query.search : undefined,
page: req.query.page ? Number(req.query.page) : 1,
pageSize: req.query.pageSize ? Number(req.query.pageSize) : 30,
});
res.json(data);
}
catch (e) {
handleError(res, e);
}
}
async function adminGetConversation(req, res) {
try {
const data = await supportService.getAdminConversationDetail(req.params.id);
res.json(data);
}
catch (e) {
handleError(res, e);
}
}
async function adminUpdateConversation(req, res) {
try {
const body = req.body;
const updated = await supportService.updateAdminConversation(req.params.id, req.user.userId, {
status: typeof body.status === 'string' ? body.status : undefined,
assignedTo: body.assignedTo === null
? null
: typeof body.assignedTo === 'string'
? body.assignedTo
: undefined,
subject: typeof body.subject === 'string' ? body.subject : undefined,
});
res.json(updated);
}
catch (e) {
handleError(res, e);
}
}
async function adminPostMessage(req, res) {
try {
const body = typeof req.body?.body === 'string' ? req.body.body : undefined;
const msg = await supportService.sendMessage(req.params.id, { userId: req.user.userId, role: req.user.role }, { body });
res.status(201).json(msg);
}
catch (e) {
handleError(res, e);
}
}
async function adminGetMessages(req, res) {
try {
const after = typeof req.query.after === 'string' ? req.query.after : undefined;
const data = await supportService.getConversationMessages(req.params.id, { userId: req.user.userId, role: req.user.role }, after);
res.json(data);
}
catch (e) {
handleError(res, e);
}
}
async function adminUnreadCount(_req, res) {
try {
const count = await supportService.getStaffUnreadCount();
res.json({ unreadCount: count });
}
catch (e) {
handleError(res, e);
}
}
async function adminUploadAttachment(req, res) {
try {
if (!req.file) {
res.status(400).json({ error: 'No file uploaded' });
return;
}
const ctx = buildCtx(req);
try {
const saved = await supportService.finalizeSupportUpload({
tempFilename: req.file.filename,
originalName: req.file.originalname,
conversationId: req.params.id,
ctx,
});
res.status(201).json({
attachmentUrl: saved.url,
attachmentName: saved.originalName,
attachmentMime: saved.mime,
});
}
catch (e) {
(0, fileMagic_1.removeUploadedFile)(supportService.resolveUploadPath(req.file.filename));
const msg = e instanceof Error ? e.message : 'File validation failed';
res.status(msg.toLowerCase().includes('virus') ? 422 : 400).json({ error: msg });
}
}
catch (e) {
handleError(res, e);
}
}
async function adminSendWithAttachment(req, res) {
try {
const body = typeof req.body?.body === 'string' ? req.body.body : undefined;
const attachmentUrl = typeof req.body?.attachmentUrl === 'string' ? req.body.attachmentUrl : undefined;
const attachmentName = typeof req.body?.attachmentName === 'string' ? req.body.attachmentName : undefined;
const attachmentMime = typeof req.body?.attachmentMime === 'string' ? req.body.attachmentMime : undefined;
const msg = await supportService.sendMessage(req.params.id, { userId: req.user.userId, role: req.user.role }, { body, attachmentUrl, attachmentName, attachmentMime });
res.status(201).json(msg);
}
catch (e) {
handleError(res, e);
}
}
async function userSendWithAttachment(req, res) {
try {
const body = typeof req.body?.body === 'string' ? req.body.body : undefined;
const attachmentUrl = typeof req.body?.attachmentUrl === 'string' ? req.body.attachmentUrl : undefined;
const attachmentName = typeof req.body?.attachmentName === 'string' ? req.body.attachmentName : undefined;
const attachmentMime = typeof req.body?.attachmentMime === 'string' ? req.body.attachmentMime : undefined;
const msg = await supportService.sendMessage(req.params.id, buildCtx(req), {
body,
attachmentUrl,
attachmentName,
attachmentMime,
});
res.status(201).json(msg);
}
catch (e) {
handleError(res, e);
}
}
//# sourceMappingURL=supportController.js.map