2020-12-17 17:36:51 +00:00
import csv from 'csvtojson' ;
2020-12-18 19:07:05 +00:00
import { Authorized , Body , ContentType , Controller , Param , Post , QueryParam , Req , UseBefore } from 'routing-controllers' ;
2020-12-17 18:17:35 +00:00
import { OpenAPI , ResponseSchema } from 'routing-controllers-openapi' ;
2020-12-17 16:14:08 +00:00
import { RunnerGroupNeededError } from '../errors/RunnerErrors' ;
2020-12-17 18:17:35 +00:00
import { RunnerGroupNotFoundError } from '../errors/RunnerGroupErrors' ;
2020-12-16 18:03:27 +00:00
import RawBodyMiddleware from '../middlewares/RawBody' ;
2020-12-17 15:32:29 +00:00
import { ImportRunner } from '../models/actions/ImportRunner' ;
2020-12-17 15:26:33 +00:00
import { ResponseRunner } from '../models/responses/ResponseRunner' ;
import { RunnerController } from './RunnerController' ;
2020-12-16 17:20:31 +00:00
2020-12-16 18:03:27 +00:00
@Controller ( )
2020-12-18 19:07:05 +00:00
@Authorized ( [ "RUNNER:IMPORT" , "TEAM:IMPORT" ] )
2020-12-22 18:13:20 +00:00
@OpenAPI ( { security : [ { "AuthToken" : [ ] } , { "RefreshTokenCookie" : [ ] } ] } )
2020-12-16 17:20:31 +00:00
export class ImportController {
2020-12-17 15:26:33 +00:00
private runnerController : RunnerController ;
2020-12-16 17:20:31 +00:00
/ * *
* Gets the repository of this controller ' s model / entity .
* /
constructor ( ) {
2020-12-17 15:26:33 +00:00
this . runnerController = new RunnerController ( ) ;
2020-12-16 17:20:31 +00:00
}
2020-12-16 18:03:27 +00:00
@Post ( '/runners/import' )
2020-12-17 15:26:33 +00:00
@ContentType ( "application/json" )
2020-12-17 19:54:01 +00:00
@ResponseSchema ( ResponseRunner , { isArray : true , statusCode : 200 } )
@ResponseSchema ( RunnerGroupNotFoundError , { statusCode : 404 } )
@ResponseSchema ( RunnerGroupNeededError , { statusCode : 406 } )
2020-12-22 18:45:09 +00:00
@OpenAPI ( { description : "Create new runners from json and insert them into the provided group. <br> If teams/classes are provided alongside the runner's name they'll automaticly be created under the provided org and the runners will be inserted into the teams instead." } )
2020-12-17 16:25:17 +00:00
async postJSON ( @Body ( { validate : true , type : ImportRunner } ) importRunners : ImportRunner [ ] , @QueryParam ( "group" ) groupID : number ) {
2020-12-17 16:14:08 +00:00
if ( ! groupID ) { throw new RunnerGroupNeededError ( ) ; }
2020-12-17 15:26:33 +00:00
let responseRunners : ResponseRunner [ ] = new Array < ResponseRunner > ( ) ;
for await ( let runner of importRunners ) {
2020-12-17 16:14:08 +00:00
responseRunners . push ( await this . runnerController . post ( await runner . toCreateRunner ( groupID ) ) ) ;
2020-12-17 15:26:33 +00:00
}
return responseRunners ;
2020-12-16 18:03:27 +00:00
}
2021-01-24 17:34:15 +00:00
@Post ( '/organizations/:id/import' )
2020-12-17 15:46:00 +00:00
@ContentType ( "application/json" )
2020-12-17 19:54:01 +00:00
@ResponseSchema ( ResponseRunner , { isArray : true , statusCode : 200 } )
@ResponseSchema ( RunnerGroupNotFoundError , { statusCode : 404 } )
@ResponseSchema ( RunnerGroupNeededError , { statusCode : 406 } )
2020-12-22 18:45:09 +00:00
@OpenAPI ( { description : "Create new runners from json and insert them into the provided org. <br> If teams/classes are provided alongside the runner's name they'll automaticly be created under the provided org and the runners will be inserted into the teams instead." } )
2020-12-17 16:25:17 +00:00
async postOrgsJSON ( @Body ( { validate : true , type : ImportRunner } ) importRunners : ImportRunner [ ] , @Param ( 'id' ) id : number ) {
2020-12-17 15:46:00 +00:00
return await this . postJSON ( importRunners , id )
}
2020-12-17 16:14:08 +00:00
@Post ( '/teams/:id/import' )
@ContentType ( "application/json" )
2020-12-17 19:54:01 +00:00
@ResponseSchema ( ResponseRunner , { isArray : true , statusCode : 200 } )
@ResponseSchema ( RunnerGroupNotFoundError , { statusCode : 404 } )
@ResponseSchema ( RunnerGroupNeededError , { statusCode : 406 } )
2020-12-17 18:15:11 +00:00
@OpenAPI ( { description : "Create new runners from json and insert them into the provided team" } )
2020-12-17 16:25:17 +00:00
async postTeamsJSON ( @Body ( { validate : true , type : ImportRunner } ) importRunners : ImportRunner [ ] , @Param ( 'id' ) id : number ) {
2020-12-17 16:14:08 +00:00
return await this . postJSON ( importRunners , id )
}
2020-12-17 18:27:38 +00:00
@Post ( '/runners/import/csv' )
2020-12-17 18:17:35 +00:00
@ContentType ( "application/json" )
2020-12-16 18:03:27 +00:00
@UseBefore ( RawBodyMiddleware )
2020-12-17 19:54:01 +00:00
@ResponseSchema ( ResponseRunner , { isArray : true , statusCode : 200 } )
@ResponseSchema ( RunnerGroupNotFoundError , { statusCode : 404 } )
@ResponseSchema ( RunnerGroupNeededError , { statusCode : 406 } )
2020-12-22 18:45:09 +00:00
@OpenAPI ( { description : "Create new runners from csv and insert them into the provided group. <br> If teams/classes are provided alongside the runner's name they'll automaticly be created under the provided org and the runners will be inserted into the teams instead." } )
2020-12-17 17:36:51 +00:00
async postCSV ( @Req ( ) request : any , @QueryParam ( "group" ) groupID : number ) {
let csvParse = await csv ( { delimiter : [ "," , ";" ] , trim : true } ) . fromString ( request . rawBody . toString ( ) ) ;
let importRunners : ImportRunner [ ] = new Array < ImportRunner > ( ) ;
for await ( let runner of csvParse ) {
let newImportRunner = new ImportRunner ( ) ;
newImportRunner . firstname = runner . firstname ;
newImportRunner . middlename = runner . middlename ;
newImportRunner . lastname = runner . lastname ;
if ( runner . class === undefined ) { newImportRunner . team = runner . team ; }
else { newImportRunner . class = runner . class ; }
importRunners . push ( newImportRunner ) ;
}
return await this . postJSON ( importRunners , groupID ) ;
}
2021-01-24 17:34:15 +00:00
@Post ( '/organizations/:id/import/csv' )
2020-12-17 18:17:35 +00:00
@ContentType ( "application/json" )
2020-12-17 17:36:51 +00:00
@UseBefore ( RawBodyMiddleware )
2020-12-17 19:54:01 +00:00
@ResponseSchema ( ResponseRunner , { isArray : true , statusCode : 200 } )
@ResponseSchema ( RunnerGroupNotFoundError , { statusCode : 404 } )
@ResponseSchema ( RunnerGroupNeededError , { statusCode : 406 } )
2020-12-22 18:45:09 +00:00
@OpenAPI ( { description : "Create new runners from csv and insert them into the provided org. <br> If teams/classes are provided alongside the runner's name they'll automaticly be created under the provided org and the runners will be inserted into the teams instead." } )
2020-12-17 17:36:51 +00:00
async postOrgsCSV ( @Req ( ) request : any , @Param ( "id" ) id : number ) {
return await this . postCSV ( request , id ) ;
}
@Post ( '/teams/:id/import/csv' )
2020-12-17 18:17:35 +00:00
@ContentType ( "application/json" )
2020-12-17 17:36:51 +00:00
@UseBefore ( RawBodyMiddleware )
2020-12-17 19:54:01 +00:00
@ResponseSchema ( ResponseRunner , { isArray : true , statusCode : 200 } )
@ResponseSchema ( RunnerGroupNotFoundError , { statusCode : 404 } )
@ResponseSchema ( RunnerGroupNeededError , { statusCode : 406 } )
2020-12-17 18:15:11 +00:00
@OpenAPI ( { description : "Create new runners from csv and insert them into the provided team" } )
2020-12-17 17:36:51 +00:00
async postTeamsCSV ( @Req ( ) request : any , @Param ( "id" ) id : number ) {
return await this . postCSV ( request , id ) ;
2020-12-16 17:20:31 +00:00
}
}