diff --git a/package.json b/package.json index ceaec78..58591d3 100644 --- a/package.json +++ b/package.json @@ -58,6 +58,7 @@ "express": "^4.18.2", "framer-motion": "^7.6.1", "mantine-datatable": "^1.7.9", + "nanoid": "v3", "next": "12.3.1", "node-telegram-bot-api": "^0.59.0", "pino": "^8.6.1", diff --git a/pnpm-lock.yaml b/pnpm-lock.yaml index 7fbb340..4b31de2 100644 --- a/pnpm-lock.yaml +++ b/pnpm-lock.yaml @@ -54,6 +54,7 @@ specifiers: husky: ^8.0.1 lint-staged: ^13.0.3 mantine-datatable: ^1.7.9 + nanoid: v3 next: 12.3.1 node-telegram-bot-api: ^0.59.0 pino: ^8.6.1 @@ -104,6 +105,7 @@ dependencies: express: 4.18.2 framer-motion: 7.6.1_biqbaboplfbrettd7655fr4n2y mantine-datatable: 1.7.11_74ukrfujzqikl2eukc5thaxmea + nanoid: 3.3.4 next: 12.3.1_biqbaboplfbrettd7655fr4n2y node-telegram-bot-api: 0.59.0 pino: 8.7.0 diff --git a/prisma/migrations/20221103223653_add_settings_schema/migration.sql b/prisma/migrations/20221103223653_add_settings_schema/migration.sql new file mode 100644 index 0000000..02f5e8c --- /dev/null +++ b/prisma/migrations/20221103223653_add_settings_schema/migration.sql @@ -0,0 +1,47 @@ +-- CreateTable +CREATE TABLE "Settings" ( + "id" SERIAL NOT NULL, + "telegramEnabled" BOOLEAN NOT NULL DEFAULT false, + "telegramToken" TEXT, + "telegramChatId" TEXT, + "telegramSendSilently" BOOLEAN NOT NULL DEFAULT false, + "appriseEnabled" BOOLEAN NOT NULL DEFAULT false, + "appriseHost" TEXT, + "appriseUrls" TEXT [] DEFAULT ARRAY [] :: TEXT [], + "komgaEnabled" BOOLEAN NOT NULL DEFAULT false, + "komgaHost" TEXT, + "komgaUser" TEXT, + "komgaPassword" TEXT, + CONSTRAINT "Settings_pkey" PRIMARY KEY ("id") +); + +INSERT INTO + "Settings" ( + id, + "telegramEnabled", + "telegramToken", + "telegramChatId", + "telegramSendSilently", + "appriseEnabled", + "appriseHost", + "appriseUrls", + "komgaEnabled", + "komgaHost", + "komgaUser", + "komgaPassword" + ) +VALUES + ( + 1, + false, + null, + null, + false, + false, + null, + '{}', + false, + null, + null, + null + ); \ No newline at end of file diff --git a/prisma/schema.prisma b/prisma/schema.prisma index ae8b84b..ab292e6 100644 --- a/prisma/schema.prisma +++ b/prisma/schema.prisma @@ -57,3 +57,18 @@ model Metadata { synonyms String[] @default([]) urls String[] @default([]) } + +model Settings { + id Int @id @default(autoincrement()) + telegramEnabled Boolean @default(false) + telegramToken String? + telegramChatId String? + telegramSendSilently Boolean @default(false) + appriseEnabled Boolean @default(false) + appriseHost String? + appriseUrls String[] @default([]) + komgaEnabled Boolean @default(false) + komgaHost String? + komgaUser String? + komgaPassword String? +} diff --git a/src/server/trpc/router/index.ts b/src/server/trpc/router/index.ts index 94cd3d2..3a1e90d 100644 --- a/src/server/trpc/router/index.ts +++ b/src/server/trpc/router/index.ts @@ -2,10 +2,12 @@ import { t } from '../trpc'; import { libraryRouter } from './library'; import { mangaRouter } from './manga'; +import { settingsRouter } from './settings'; export const appRouter = t.router({ library: libraryRouter, manga: mangaRouter, + settings: settingsRouter, }); // export type definition of API diff --git a/src/server/trpc/router/settings.ts b/src/server/trpc/router/settings.ts new file mode 100644 index 0000000..26caa9c --- /dev/null +++ b/src/server/trpc/router/settings.ts @@ -0,0 +1,38 @@ +import { z } from 'zod'; +import { getMangalConfig, setMangalConfig } from '../../utils/mangal'; +import { t } from '../trpc'; + +export const settingsRouter = t.router({ + query: t.procedure.query(async ({ ctx }) => { + const mangalConfig = (await getMangalConfig()).sort((a, b) => a.key.localeCompare(b.key)); + const appConfig = await ctx.prisma.settings.findFirstOrThrow(); + return { + mangalConfig, + appConfig, + }; + }), + update: t.procedure + .input( + z.object({ + key: z.string().min(1), + value: z.string().or(z.boolean()).or(z.number()).or(z.string().array()), + updateType: z.literal('mangal').or(z.literal('app')), + }), + ) + .mutation(async ({ ctx, input }) => { + const { key, value, updateType } = input; + if (updateType === 'mangal') { + await setMangalConfig(key, value); + } else if (updateType === 'app') { + const appConfig = await ctx.prisma.settings.findFirstOrThrow(); + await ctx.prisma.settings.update({ + where: { + id: appConfig.id, + }, + data: { + [key]: value, + }, + }); + } + }), +});