11import {
22 Authorized ,
3+ BadRequestError ,
34 Body ,
45 CurrentUser ,
56 Get ,
67 HttpCode ,
78 JsonController ,
89 Param ,
9- Patch ,
1010 Post ,
11+ Put ,
1112 QueryParams
1213} from 'routing-controllers' ;
1314import { ResponseSchema } from 'routing-controllers-openapi' ;
1415
1516import { BaseFilter , Cooperation , CooperationListChunk , dataSource , User } from '../../model' ;
16- import { searchConditionOf } from '../../utility' ;
1717import { ActivityLogController } from '../User' ;
1818import { ActivityController } from './Activity' ;
1919
2020const cooperationStore = dataSource . getRepository ( Cooperation ) ;
2121
22- @JsonController ( '/activity' )
22+ @JsonController ( '/activity/:aid/cooperation ' )
2323export class CooperationController {
24- @Post ( '/:aid/cooperation' )
24+ @Post ( )
2525 @Authorized ( )
2626 @HttpCode ( 201 )
2727 @ResponseSchema ( Cooperation )
@@ -32,27 +32,36 @@ export class CooperationController {
3232 ) {
3333 const activity = await ActivityController . assertAdmin ( aid , createdBy ) ;
3434
35- const cooperation = await cooperationStore . save ( { ...body , activity, createdBy } ) ;
35+ if ( ! activity . cooperationLevels ?. find ( ( { id } ) => id === body . level . id ) )
36+ throw new BadRequestError (
37+ `Cooperation Level with ID "${ body . level . id } " isn't included in "${ activity . title } " activity`
38+ ) ;
39+ if ( body . partner . id === activity . organization . id )
40+ throw new BadRequestError ( "Can't cooperate with yourself" ) ;
3641
42+ const cooperation = await cooperationStore . save ( {
43+ ...body ,
44+ activity,
45+ organization : activity . organization ,
46+ createdBy
47+ } ) ;
3748 await ActivityLogController . logCreate ( createdBy , 'Cooperation' , cooperation . id ) ;
3849
3950 return cooperation ;
4051 }
4152
42- @Get ( '/:aid/cooperation' )
53+ @Get ( )
4354 @ResponseSchema ( CooperationListChunk )
44- async getList ( @QueryParams ( ) { keywords, pageSize = 10 , pageIndex = 1 } : BaseFilter ) {
45- const where = searchConditionOf < Cooperation > ( [ 'title' ] , keywords ) ;
46-
55+ async getList ( @QueryParams ( ) { pageSize = 10 , pageIndex = 1 } : BaseFilter ) {
4756 const [ list , count ] = await cooperationStore . findAndCount ( {
48- where,
4957 skip : pageSize * ( pageIndex - 1 ) ,
50- take : pageSize
58+ take : pageSize ,
59+ relations : [ 'level' , 'partner' ]
5160 } ) ;
5261 return { list, count } ;
5362 }
5463
55- @Patch ( ':aid/cooperation /:id')
64+ @Put ( ' /:id')
5665 @Authorized ( )
5766 @ResponseSchema ( Cooperation )
5867 async edit (
0 commit comments