games_app/backend/dist/controllers/supportController.js
2026-07-07 15:03:17 +02:00

306 lines
11 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;
};
})();
var __importDefault = (this && this.__importDefault) || function (mod) {
return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.getOrCreateConversation = getOrCreateConversation;
exports.listMyConversations = listMyConversations;
exports.getMessages = getMessages;
exports.postMessage = postMessage;
exports.postTyping = postTyping;
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 fs_1 = __importDefault(require("fs"));
const optionalAuth_1 = require("../middleware/optionalAuth");
const supportUpload_1 = require("../middleware/supportUpload");
const fileMagic_1 = require("../utils/fileMagic");
const uploadValidation_1 = require("../utils/uploadValidation");
const supportService = __importStar(require("../services/supportService"));
const fileIntegrityService_1 = require("../services/fileIntegrityService");
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') {
res.status(404).json({ error: msg });
return;
}
res.status(400).json({ error: msg });
}
async function getOrCreateConversation(req, res) {
try {
const conv = await supportService.getOrCreateConversation(buildCtx(req));
res.json(conv);
}
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 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 uploadAttachment(req, res) {
try {
if (!req.file) {
res.status(400).json({ error: 'No file uploaded' });
return;
}
const filePath = supportService.resolveUploadPath(req.file.filename);
try {
await (0, uploadValidation_1.validateUploadedFile)(filePath, supportUpload_1.SUPPORT_ALLOWED_MIMES);
}
catch (e) {
(0, fileMagic_1.removeUploadedFile)(filePath);
const msg = e instanceof Error ? e.message : 'File validation failed';
res.status(msg.includes('virus') ? 422 : 400).json({ error: msg });
return;
}
await supportService.assertConversationAccess(req.params.id, buildCtx(req));
const url = supportService.publicAttachmentUrl(req.file.filename);
await (0, fileIntegrityService_1.recordUploadedFile)(fileIntegrityService_1.FILE_INTEGRITY_ROOTS.support, req.file.filename);
res.status(201).json({
attachmentUrl: url,
attachmentName: req.file.originalname,
attachmentMime: req.file.mimetype,
});
}
catch (e) {
handleError(res, e);
}
}
async function getAttachment(req, res) {
try {
const filePath = supportService.resolveUploadPath(req.params.filename);
if (!fs_1.default.existsSync(filePath)) {
res.status(404).json({ error: 'File not found' });
return;
}
res.sendFile(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 filePath = supportService.resolveUploadPath(req.file.filename);
try {
await (0, uploadValidation_1.validateUploadedFile)(filePath, supportUpload_1.SUPPORT_ALLOWED_MIMES);
}
catch (e) {
(0, fileMagic_1.removeUploadedFile)(filePath);
const msg = e instanceof Error ? e.message : 'File validation failed';
res.status(msg.includes('virus') ? 422 : 400).json({ error: msg });
return;
}
const url = supportService.publicAttachmentUrl(req.file.filename);
await (0, fileIntegrityService_1.recordUploadedFile)(fileIntegrityService_1.FILE_INTEGRITY_ROOTS.support, req.file.filename);
res.status(201).json({
attachmentUrl: url,
attachmentName: req.file.originalname,
attachmentMime: req.file.mimetype,
});
}
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