games_app/backend/dist-test-faq/services/gameService.js

167 lines
6.4 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.listGames = listGames;
exports.listAllGamesAdmin = listAllGamesAdmin;
exports.getGame = getGame;
exports.listRegisteredImplementations = listRegisteredImplementations;
exports.parseGameInput = parseGameInput;
exports.createGame = createGame;
exports.updateGame = updateGame;
exports.deleteGame = deleteGame;
exports.listImplementationsAdmin = listImplementationsAdmin;
const registry_1 = require("../games/registry");
const gameRepository = __importStar(require("../repositories/gameRepository"));
function toGameDto(row) {
return {
id: row.id,
slug: row.slug,
title: row.title,
description: row.description,
category: row.category,
thumbnailUrl: row.thumbnail_url,
accentColor: row.accent_color ?? '#D6B25E',
status: row.status,
sortOrder: row.sort_order,
isFeatured: row.is_featured,
implementationKey: row.implementation_key,
createdAt: row.created_at.toISOString(),
updatedAt: row.updated_at.toISOString(),
};
}
async function listGames(options) {
const rows = await gameRepository.listActiveGames(options?.featured === true);
return rows.map(toGameDto);
}
async function listAllGamesAdmin() {
const rows = await gameRepository.listAllGames();
return rows.map(toGameDto);
}
async function getGame(slug) {
const row = await gameRepository.getGameBySlug(slug);
return row ? toGameDto(row) : null;
}
const VALID_STATUS = ['active', 'coming_soon', 'maintenance'];
function listRegisteredImplementations() {
return (0, registry_1.listRegisteredModules)().map((m) => ({
key: m.implementationKey,
defaultSlug: m.slug,
label: m.displayName,
}));
}
function assertValidImplementationKey(key) {
if (key == null || key === '')
return;
if (!(0, registry_1.isRegisteredImplementationKey)(key)) {
throw new Error(`Unknown implementation key: ${key}`);
}
}
function parseGameInput(body, partial = false) {
const slug = typeof body.slug === 'string' ? body.slug.trim() : '';
const title = typeof body.title === 'string' ? body.title.trim() : '';
if (!partial && (!slug || !title)) {
throw new Error('slug and title are required');
}
if (partial && !slug && !title && body.slug === undefined && body.title === undefined) {
// ok for partial updates with other fields
}
const status = typeof body.status === 'string' && VALID_STATUS.includes(body.status)
? body.status
: undefined;
return {
slug: slug || undefined,
title: title || undefined,
description: typeof body.description === 'string' ? body.description : body.description === null ? null : undefined,
category: typeof body.category === 'string' ? body.category.trim() : undefined,
thumbnailUrl: typeof body.thumbnailUrl === 'string'
? body.thumbnailUrl
: body.thumbnailUrl === null
? null
: undefined,
accentColor: typeof body.accentColor === 'string' ? body.accentColor : undefined,
status,
sortOrder: typeof body.sortOrder === 'number' ? body.sortOrder : undefined,
isFeatured: typeof body.isFeatured === 'boolean' ? body.isFeatured : undefined,
implementationKey: typeof body.implementationKey === 'string'
? body.implementationKey.trim() || null
: body.implementationKey === null
? null
: undefined,
};
}
async function createGame(body) {
const input = parseGameInput(body);
if (!input.slug || !input.title)
throw new Error('slug and title are required');
assertValidImplementationKey(input.implementationKey);
const existing = await gameRepository.getGameBySlug(input.slug);
if (existing)
throw new Error('A game with this slug already exists');
const row = await gameRepository.createGame({
slug: input.slug,
title: input.title,
description: input.description,
category: input.category,
thumbnailUrl: input.thumbnailUrl,
accentColor: input.accentColor,
status: input.status,
sortOrder: input.sortOrder,
isFeatured: input.isFeatured,
implementationKey: input.implementationKey,
});
return toGameDto(row);
}
async function updateGame(id, body) {
const input = parseGameInput(body, true);
assertValidImplementationKey(input.implementationKey);
if (input.slug) {
const clash = await gameRepository.getGameBySlug(input.slug);
if (clash && clash.id !== id)
throw new Error('A game with this slug already exists');
}
const row = await gameRepository.updateGame(id, input);
if (!row)
throw new Error('Game not found');
return toGameDto(row);
}
async function deleteGame(id) {
const ok = await gameRepository.deleteGame(id);
if (!ok)
throw new Error('Game not found');
}
async function listImplementationsAdmin() {
return listRegisteredImplementations();
}
//# sourceMappingURL=gameService.js.map