diff --git a/server/src/internal-api/import/import.ts b/server/src/internal-api/import/import.ts index 658141f0e..fee859993 100644 --- a/server/src/internal-api/import/import.ts +++ b/server/src/internal-api/import/import.ts @@ -2,13 +2,13 @@ import { Router, NextFunction, Request, Response } from "express"; import { FileUploadImportTypes } from "kamaitachi-common"; import multer, { MulterError } from "multer"; import Prudence from "prudence"; +import { Logger } from "winston"; import { GetUserWithID } from "../../core/user-core"; -import db from "../../db"; import CreateLogCtx from "../../logger"; import prValidate from "../../middleware/prudence-validate"; import { RequireLoggedIn } from "../../middleware/require-logged-in"; import ScoreImportFatalError from "../../score-import/framework/core/score-import-error"; -import { ResolveFileUploadData } from "../../score-import/framework/parsing/file-upload"; +import { ResolveFileUploadData } from "../../score-import/framework/input-parsing/file-upload"; import ScoreImportMain from "../../score-import/framework/score-import-main"; import serverConfig from "../../server-config"; @@ -61,23 +61,13 @@ router.post( try { let importType = req.body.importType as FileUploadImportTypes; - let { iterable, idStrings, context, converter } = await ResolveFileUploadData( - importType, - req.file, - req.body, - logger - ); + + const inputParser = async (logger: Logger) => + await ResolveFileUploadData(importType, req.file, req.body, logger); const userDoc = await GetUserWithID(req.session.ktchi!.userID); - let importDocument = await ScoreImportMain( - userDoc, - importType, - idStrings, - iterable, - converter, - context - ); + let importDocument = await ScoreImportMain(userDoc, importType, inputParser); } catch (err) { if (err instanceof ScoreImportFatalError) { return res.status(err.statusCode).json(err.data); diff --git a/server/src/score-import/framework/parsing/file-upload.ts b/server/src/score-import/framework/input-parsing/file-upload.ts similarity index 100% rename from server/src/score-import/framework/parsing/file-upload.ts rename to server/src/score-import/framework/input-parsing/file-upload.ts diff --git a/server/src/score-import/framework/score-import-main.ts b/server/src/score-import/framework/score-import-main.ts index eaa26cb20..1336e915e 100644 --- a/server/src/score-import/framework/score-import-main.ts +++ b/server/src/score-import/framework/score-import-main.ts @@ -4,7 +4,7 @@ import { KTBlackImportDocument, PublicUserDocument, } from "kamaitachi-common"; -import { ConverterFunction } from "../../types"; +import { ImportInputParser } from "../../types"; import { InsertQueue } from "./core/insert-score"; import { ImportAllIterableData } from "./importing/score-importing"; import { CreateImportLoggerAndID } from "./core/import-logger"; @@ -12,20 +12,19 @@ import { CreateImportLoggerAndID } from "./core/import-logger"; export default async function ScoreImportMain( user: PublicUserDocument, importType: ImportTypes, - idStrings: [IDStrings] & IDStrings[], - iterableData: Iterable | AsyncIterable, - ConverterFunction: ConverterFunction, - context: C + InputParser: ImportInputParser ) { const timeStarted = Date.now(); const { importID, logger } = CreateImportLoggerAndID(user, importType); + const { iterable, ConverterFunction, context, idStrings } = await InputParser(logger); + // @todo: scope logger properly logger.verbose("Received import request."); let importInfo = await ImportAllIterableData( user.id, - iterableData, + iterable, ConverterFunction, context, logger @@ -49,6 +48,7 @@ export default async function ScoreImportMain( importID, timeFinished: Date.now(), timeStarted, + createdSessions: [], // @todo, see session ctor userID: user.id, }; diff --git a/server/src/score-import/import-types/iidx-eamusement-csv/parser.ts b/server/src/score-import/import-types/iidx-eamusement-csv/parser.ts index 02be0fe44..2af2a2f0f 100644 --- a/server/src/score-import/import-types/iidx-eamusement-csv/parser.ts +++ b/server/src/score-import/import-types/iidx-eamusement-csv/parser.ts @@ -224,7 +224,7 @@ function ParseEamusementCSV( return { iterable: data, context, - converter: ConverterFn, + ConverterFunction: ConverterFn, idStrings: [`iidx:${context.playtype}` as "iidx:SP" | "iidx:DP"], }; } diff --git a/server/src/types.ts b/server/src/types.ts index 29e790a9d..04995fd23 100644 --- a/server/src/types.ts +++ b/server/src/types.ts @@ -52,11 +52,15 @@ export interface ConverterFunction { (data: D, processContext: C, logger: Logger): Promise; } +export interface ImportInputParser { + (logger: Logger): ParserFunctionReturns | Promise>; +} + export interface ParserFunctionReturnsSync { iterable: Iterable; idStrings: [IDStrings] & IDStrings[]; context: C; - converter: ConverterFunction; + ConverterFunction: ConverterFunction; } export type ParserFunctionReturns = @@ -67,7 +71,7 @@ export interface ParserFunctionReturnsAsync { iterable: AsyncIterable; idStrings: [IDStrings] & IDStrings[]; context: C; - converter: ConverterFunction; + ConverterFunction: ConverterFunction; } /** @@ -87,7 +91,7 @@ export type DryScore< export interface ScoreConverterInformation { data: D[]; - converter: ConverterFunction; + ConverterFunction: ConverterFunction; } export interface OrphanedScore {