From c7c9f39e88bfe9bee31594d32b0768033813d5e7 Mon Sep 17 00:00:00 2001 From: Arvin Xu Date: Tue, 23 Jul 2024 12:49:28 +0800 Subject: [PATCH] =?UTF-8?q?=E2=99=BB=EF=B8=8F=20refactor:=20move=20server?= =?UTF-8?q?=20modules=20(#3291)?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../server/models/__tests__/user.test.ts | 2 +- src/database/server/models/user.ts | 2 +- .../KeyVaultsEncrypt}/index.test.ts | 0 .../KeyVaultsEncrypt}/index.ts | 0 .../{files/s3.ts => modules/S3/index.ts} | 0 src/server/routers/edge/upload.ts | 2 +- src/server/routers/lambda/importer.ts | 25 +++++++++++-------- .../__tests__/fixtures/messages.json | 0 .../dataImporter}/__tests__/index.test.ts | 6 ++--- .../dataImporter}/index.ts | 2 +- vitest.config.ts | 2 +- 11 files changed, 23 insertions(+), 18 deletions(-) rename src/server/{keyVaultsEncrypt => modules/KeyVaultsEncrypt}/index.test.ts (100%) rename src/server/{keyVaultsEncrypt => modules/KeyVaultsEncrypt}/index.ts (100%) rename src/server/{files/s3.ts => modules/S3/index.ts} (100%) rename src/server/{modules/DataImporter => services/dataImporter}/__tests__/fixtures/messages.json (100%) rename src/server/{modules/DataImporter => services/dataImporter}/__tests__/index.test.ts (99%) rename src/server/{modules/DataImporter => services/dataImporter}/index.ts (99%) diff --git a/src/database/server/models/__tests__/user.test.ts b/src/database/server/models/__tests__/user.test.ts index 04ba9d09d88d..fb10afd637d0 100644 --- a/src/database/server/models/__tests__/user.test.ts +++ b/src/database/server/models/__tests__/user.test.ts @@ -3,7 +3,7 @@ import { afterEach, beforeEach, describe, expect, it, vi } from 'vitest'; import { INBOX_SESSION_ID } from '@/const/session'; import { getTestDBInstance } from '@/database/server/core/dbForTest'; -import { KeyVaultsGateKeeper } from '@/server/keyVaultsEncrypt'; +import { KeyVaultsGateKeeper } from '@/server/modules/KeyVaultsEncrypt'; import { UserPreference } from '@/types/user'; import { UserSettings } from '@/types/user/settings'; diff --git a/src/database/server/models/user.ts b/src/database/server/models/user.ts index 6178aabafbe4..14b5054a9e7a 100644 --- a/src/database/server/models/user.ts +++ b/src/database/server/models/user.ts @@ -3,7 +3,7 @@ import { eq } from 'drizzle-orm'; import { DeepPartial } from 'utility-types'; import { serverDB } from '@/database/server/core/db'; -import { KeyVaultsGateKeeper } from '@/server/keyVaultsEncrypt'; +import { KeyVaultsGateKeeper } from '@/server/modules/KeyVaultsEncrypt'; import { UserPreference } from '@/types/user'; import { UserSettings } from '@/types/user/settings'; import { merge } from '@/utils/merge'; diff --git a/src/server/keyVaultsEncrypt/index.test.ts b/src/server/modules/KeyVaultsEncrypt/index.test.ts similarity index 100% rename from src/server/keyVaultsEncrypt/index.test.ts rename to src/server/modules/KeyVaultsEncrypt/index.test.ts diff --git a/src/server/keyVaultsEncrypt/index.ts b/src/server/modules/KeyVaultsEncrypt/index.ts similarity index 100% rename from src/server/keyVaultsEncrypt/index.ts rename to src/server/modules/KeyVaultsEncrypt/index.ts diff --git a/src/server/files/s3.ts b/src/server/modules/S3/index.ts similarity index 100% rename from src/server/files/s3.ts rename to src/server/modules/S3/index.ts diff --git a/src/server/routers/edge/upload.ts b/src/server/routers/edge/upload.ts index ebaf30fcc457..f981b6375ada 100644 --- a/src/server/routers/edge/upload.ts +++ b/src/server/routers/edge/upload.ts @@ -1,7 +1,7 @@ import { z } from 'zod'; import { passwordProcedure, router } from '@/libs/trpc'; -import { S3 } from '@/server/files/s3'; +import { S3 } from '@/server/modules/S3'; export const uploadRouter = router({ createS3PreSignedUrl: passwordProcedure diff --git a/src/server/routers/lambda/importer.ts b/src/server/routers/lambda/importer.ts index 36abc16330c7..3f65d0ca30df 100644 --- a/src/server/routers/lambda/importer.ts +++ b/src/server/routers/lambda/importer.ts @@ -2,12 +2,21 @@ import { TRPCError } from '@trpc/server'; import { z } from 'zod'; import { authedProcedure, router } from '@/libs/trpc'; -import { S3 } from '@/server/files/s3'; -import { DataImporter } from '@/server/modules/DataImporter'; +import { S3 } from '@/server/modules/S3'; +import { DataImporterService } from '@/server/services/dataImporter'; import { ImportResults, ImporterEntryData } from '@/types/importer'; +const importProcedure = authedProcedure.use(async (opts) => { + const { ctx } = opts; + const dataImporterService = new DataImporterService(ctx.userId); + + return opts.next({ + ctx: { dataImporterService }, + }); +}); + export const importerRouter = router({ - importByFile: authedProcedure + importByFile: importProcedure .input(z.object({ pathname: z.string() })) .mutation(async ({ input, ctx }): Promise => { let data: ImporterEntryData | undefined; @@ -27,12 +36,10 @@ export const importerRouter = router({ }); } - const dataImporter = new DataImporter(ctx.userId); - - return dataImporter.importData(data); + return ctx.dataImporterService.importData(data); }), - importByPost: authedProcedure + importByPost: importProcedure .input( z.object({ data: z.object({ @@ -45,8 +52,6 @@ export const importerRouter = router({ }), ) .mutation(async ({ input, ctx }): Promise => { - const dataImporter = new DataImporter(ctx.userId); - - return dataImporter.importData(input.data); + return ctx.dataImporterService.importData(input.data); }), }); diff --git a/src/server/modules/DataImporter/__tests__/fixtures/messages.json b/src/server/services/dataImporter/__tests__/fixtures/messages.json similarity index 100% rename from src/server/modules/DataImporter/__tests__/fixtures/messages.json rename to src/server/services/dataImporter/__tests__/fixtures/messages.json diff --git a/src/server/modules/DataImporter/__tests__/index.test.ts b/src/server/services/dataImporter/__tests__/index.test.ts similarity index 99% rename from src/server/modules/DataImporter/__tests__/index.test.ts rename to src/server/services/dataImporter/__tests__/index.test.ts index 55f4570c7448..989402c57670 100644 --- a/src/server/modules/DataImporter/__tests__/index.test.ts +++ b/src/server/services/dataImporter/__tests__/index.test.ts @@ -16,7 +16,7 @@ import { CURRENT_CONFIG_VERSION } from '@/migrations'; import { ImportResult } from '@/services/config'; import { ImporterEntryData } from '@/types/importer'; -import { DataImporter } from '../index'; +import { DataImporterService } from '../index'; import mockImportData from './fixtures/messages.json'; let serverDB = await getTestDBInstance(); @@ -28,7 +28,7 @@ vi.mock('@/database/server/core/db', async () => ({ })); const userId = 'test-user-id'; -let importer: DataImporter; +let importer: DataImporterService; beforeEach(async () => { await serverDB.delete(users); @@ -38,7 +38,7 @@ beforeEach(async () => { await tx.insert(users).values({ id: userId }); }); - importer = new DataImporter(userId); + importer = new DataImporterService(userId); }); describe('DataImporter', () => { diff --git a/src/server/modules/DataImporter/index.ts b/src/server/services/dataImporter/index.ts similarity index 99% rename from src/server/modules/DataImporter/index.ts rename to src/server/services/dataImporter/index.ts index f02727679022..e89f8a3b0745 100644 --- a/src/server/modules/DataImporter/index.ts +++ b/src/server/services/dataImporter/index.ts @@ -15,7 +15,7 @@ import { import { ImportResult } from '@/services/config'; import { ImporterEntryData } from '@/types/importer'; -export class DataImporter { +export class DataImporterService { private userId: string; /** diff --git a/vitest.config.ts b/vitest.config.ts index 20666906aa83..f115e79c68ba 100644 --- a/vitest.config.ts +++ b/vitest.config.ts @@ -30,7 +30,7 @@ export default defineConfig({ '**/dist/**', '**/build/**', 'src/database/server/**/**', - 'src/server/modules/**/**', + 'src/server/services/**/**', ], globals: true, server: {