diff --git a/src/presentation/interfaces/middleware/project-validation.ts b/src/presentation/interfaces/middleware/project-validation.ts index 7865607..5b7e359 100644 --- a/src/presentation/interfaces/middleware/project-validation.ts +++ b/src/presentation/interfaces/middleware/project-validation.ts @@ -8,6 +8,7 @@ export interface IMiddlewareProjectValidation { rulesProjectRequestCreationModel: (ValidationChain | ((req: Request, res: Response, next: NextFunction) => Response | undefined))[] rulesProjectUpdateModel: ((Middleware & ContextRunner) | ((req: Request, res: Response, next: NextFunction) => Response | undefined))[] rulesProjectBackup: (ValidationChain | ((req: Request, res: Response, next: NextFunction) => Response | undefined))[] + rulesProjectBackupFromImport: (ValidationChain | ((req: Request, res: Response, next: NextFunction) => Response | undefined))[] } diff --git a/src/presentation/middleware/project-validation.ts b/src/presentation/middleware/project-validation.ts index 21c7b49..5996acf 100644 --- a/src/presentation/middleware/project-validation.ts +++ b/src/presentation/middleware/project-validation.ts @@ -303,4 +303,23 @@ export class MiddlewareProjectValidation implements IMiddlewareProjectValidation next(); }, ] + + rulesProjectBackupFromImport = [ + check("backup_project").default(false) + .isIn([true, false]).withMessage('Backup project must be a boolean true or false value.'), + check("backup_project_skip_already_imported").default(true) + .isIn([true, false]).withMessage('Backup project, skip already imported must be a boolean true or false value.'), + check("samples") + .exists().withMessage('Samples are required.') + .isArray().withMessage('Samples must be an array.'), + // Error Handling Middleware + (req: Request, res: Response, next: NextFunction) => { + const errors = validationResult(req); + if (!errors.isEmpty()) { + // Centralized error handling for validation errors + return res.status(422).json({ errors: errors.array() }); + } + next(); + }, + ] } diff --git a/src/presentation/routers/project-router.ts b/src/presentation/routers/project-router.ts index a0c5fb5..0a8e1db 100644 --- a/src/presentation/routers/project-router.ts +++ b/src/presentation/routers/project-router.ts @@ -179,10 +179,15 @@ export default function ProjectRouter( } }) - router.post('/:project_id/samples/import', middlewareAuth.auth,/*middlewareSampleValidation.rulesImport,*/ async (req: Request, res: Response) => { + router.post('/:project_id/samples/import', middlewareAuth.auth, middlewareProjectValidation.rulesProjectBackupFromImport, async (req: Request, res: Response) => { try { - const tasks = await importSamplesUseCase.execute((req as CustomRequest).token, req.params.project_id as any, { ...req.body }.samples); - res.status(200).send(tasks) + const task_import_samples = await importSamplesUseCase.execute((req as CustomRequest).token, req.params.project_id as any, { ...req.body }.samples); + if (req.body.backup_project === true) { + const task_backup_project = await backupProjectUseCase.execute((req as CustomRequest).token, req.params.project_id as any, req.body.backup_project_skip_already_imported); + res.status(200).send([task_import_samples, task_backup_project]) + } else { + res.status(200).send(task_import_samples) + } } catch (err) { console.log(err) // if (err.message === "User cannot be used") res.status(403).send({ errors: [err.message] })