Skip to content

Commit

Permalink
DEV : add backup project option to import samples
Browse files Browse the repository at this point in the history
  • Loading branch information
juliecoust committed Nov 27, 2024
1 parent 69aa0ca commit ace5cf8
Show file tree
Hide file tree
Showing 3 changed files with 28 additions and 3 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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))[]
}


19 changes: 19 additions & 0 deletions src/presentation/middleware/project-validation.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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();
},
]
}
11 changes: 8 additions & 3 deletions src/presentation/routers/project-router.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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] })
Expand Down

0 comments on commit ace5cf8

Please sign in to comment.