Files
drop/server/api/v1/admin/library/sources/index.patch.ts
Paco dfa30c8a65 Admin home page #128 (#259)
* First iteration on the new PieChart component

* #128 Adds new admin home page

* Fixes code after merging conflicts

* Removes empty file

* Uses real data for admin home page, and improves style

* Reverts debugging code

* Defines missing variable

* Caches user stats data for admin home page

* Typo

* Styles improvements

* Invalidates cache on signup/signin

* Implements top 5 biggest games

* Improves styling

* Improves style

* Using generateManifest to get the proper size

* Reading data from cache

* Removes unnecessary import

* Improves caching mechanism for game sizes

* Removes lint errors

* Replaces piechart tooltip with colors in legend

* Fixes caching

* Fixes caching and slight improvement on pie chart colours

* Fixes a few bugs related to caching

* Fixes bug where app signin didn't refresh cache

* feat: style improvements

* fix: lint

---------

Co-authored-by: DecDuck <declanahofmeyr@gmail.com>
2025-11-08 09:14:45 +11:00

69 lines
1.9 KiB
TypeScript

import { type } from "arktype";
import { readDropValidatedBody, throwingArktype } from "~/server/arktype";
import aclManager from "~/server/internal/acls";
import prisma from "~/server/internal/db/database";
import libraryManager from "~/server/internal/library";
import type { WorkingLibrarySource } from "~/server/api/v1/admin/library/sources/index.get";
import { libraryConstructors } from "~/server/plugins/05.library-init";
const UpdateLibrarySource = type({
id: "string",
name: "string",
options: "object",
}).configure(throwingArktype);
export default defineEventHandler<{ body: typeof UpdateLibrarySource.infer }>(
async (h3) => {
const allowed = await aclManager.allowSystemACL(h3, [
"library:sources:update",
"setup",
]);
if (!allowed) throw createError({ statusCode: 403 });
const body = await readDropValidatedBody(h3, UpdateLibrarySource);
const source = await prisma.library.findUnique({ where: { id: body.id } });
if (!source)
throw createError({
statusCode: 400,
statusMessage: "Library source not found",
});
const constructor = libraryConstructors[source.backend];
try {
const newLibrary = constructor(body.options, source.id);
// Test we can actually use it
if ((await newLibrary.listGames()) === undefined) {
throw "Library failed to fetch games.";
}
const updatedSource = await prisma.library.update({
where: {
id: source.id,
},
data: {
name: body.name,
options: body.options,
},
});
libraryManager.removeLibrary(source.id);
libraryManager.addLibrary(newLibrary);
const workingSource: WorkingLibrarySource = {
...updatedSource,
working: true,
};
return workingSource;
} catch (e) {
throw createError({
statusCode: 400,
statusMessage: `Failed to create source: ${e}`,
});
}
},
);