@@ -6,34 +6,23 @@ import {
66 Param ,
77 Patch ,
88 Post ,
9- Req ,
10- HttpStatus ,
119} from '@nestjs/common' ;
12- import { AppException } from 'omniboxd/common/exceptions/app.exception' ;
13- import { I18n , I18nContext } from 'nestjs-i18n' ;
1410import { GroupsService } from './groups.service' ;
1511import { CreateGroupDto } from './dto/create-group.dto' ;
1612import { plainToInstance } from 'class-transformer' ;
1713import { GroupDto } from './dto/group.dto' ;
1814import { UpdateGroupDto } from './dto/update-group.dto' ;
1915import { GroupUserDto } from './dto/group-user.dto' ;
2016import { AddGroupUserDto } from './dto/add-group-user.dto' ;
21- import { NamespacesService } from 'omniboxd/namespaces/namespaces.service ' ;
17+ import { NamespaceOwner } from 'omniboxd/namespaces/decorators/namespace-owner.decorator ' ;
2218
2319@Controller ( 'api/v1/namespaces/:namespaceId/groups' )
2420export class GroupsController {
25- constructor (
26- private readonly groupsService : GroupsService ,
27- private readonly namespacesService : NamespacesService ,
28- ) { }
21+ constructor ( private readonly groupsService : GroupsService ) { }
2922
23+ @NamespaceOwner ( )
3024 @Get ( )
31- async list ( @Req ( ) req , @Param ( 'namespaceId' ) namespaceId : string , @I18n ( ) i18n : I18nContext ) {
32- if ( ! ( await this . namespacesService . userIsOwner ( namespaceId , req . user . id ) ) ) {
33- const message = i18n . t ( 'namespace.errors.userNotOwner' ) ;
34- throw new AppException ( message , 'USER_NOT_OWNER' , HttpStatus . FORBIDDEN ) ;
35- }
36-
25+ async list ( @Param ( 'namespaceId' ) namespaceId : string ) {
3726 const groups = await this . groupsService . listGroups ( namespaceId ) ;
3827 const invitations =
3928 await this . groupsService . listGroupInvitations ( namespaceId ) ;
@@ -50,36 +39,26 @@ export class GroupsController {
5039 } ) ;
5140 }
5241
42+ @NamespaceOwner ( )
5343 @Post ( )
5444 async create (
55- @Req ( ) req ,
5645 @Param ( 'namespaceId' ) namespaceId : string ,
5746 @Body ( ) createGroupDto : CreateGroupDto ,
58- @I18n ( ) i18n : I18nContext ,
5947 ) {
60- if ( ! ( await this . namespacesService . userIsOwner ( namespaceId , req . user . id ) ) ) {
61- const message = i18n . t ( 'namespace.errors.userNotOwner' ) ;
62- throw new AppException ( message , 'USER_NOT_OWNER' , HttpStatus . FORBIDDEN ) ;
63- }
6448 const group = await this . groupsService . createGroup (
6549 namespaceId ,
6650 createGroupDto ,
6751 ) ;
6852 return plainToInstance ( GroupDto , group ) ;
6953 }
7054
55+ @NamespaceOwner ( )
7156 @Patch ( ':groupId' )
7257 async update (
73- @Req ( ) req ,
7458 @Param ( 'namespaceId' ) namespaceId : string ,
7559 @Param ( 'groupId' ) groupId : string ,
7660 @Body ( ) updateGroupDto : UpdateGroupDto ,
77- @I18n ( ) i18n : I18nContext ,
7861 ) {
79- if ( ! ( await this . namespacesService . userIsOwner ( namespaceId , req . user . id ) ) ) {
80- const message = i18n . t ( 'namespace.errors.userNotOwner' ) ;
81- throw new AppException ( message , 'USER_NOT_OWNER' , HttpStatus . FORBIDDEN ) ;
82- }
8362 const group = await this . groupsService . updateGroup (
8463 namespaceId ,
8564 groupId ,
@@ -88,49 +67,34 @@ export class GroupsController {
8867 return plainToInstance ( GroupDto , group ) ;
8968 }
9069
70+ @NamespaceOwner ( )
9171 @Delete ( ':groupId' )
9272 async delete (
93- @Req ( ) req ,
9473 @Param ( 'namespaceId' ) namespaceId : string ,
9574 @Param ( 'groupId' ) groupId : string ,
96- @I18n ( ) i18n : I18nContext ,
9775 ) {
98- if ( ! ( await this . namespacesService . userIsOwner ( namespaceId , req . user . id ) ) ) {
99- const message = i18n . t ( 'namespace.errors.userNotOwner' ) ;
100- throw new AppException ( message , 'USER_NOT_OWNER' , HttpStatus . FORBIDDEN ) ;
101- }
10276 await this . groupsService . deleteGroup ( namespaceId , groupId ) ;
10377 }
10478
79+ @NamespaceOwner ( )
10580 @Get ( ':groupId/users' )
10681 async listGroupUsers (
107- @Req ( ) req ,
10882 @Param ( 'namespaceId' ) namespaceId : string ,
10983 @Param ( 'groupId' ) groupId : string ,
110- @I18n ( ) i18n : I18nContext ,
11184 ) {
112- if ( ! ( await this . namespacesService . userIsOwner ( namespaceId , req . user . id ) ) ) {
113- const message = i18n . t ( 'namespace.errors.userNotOwner' ) ;
114- throw new AppException ( message , 'USER_NOT_OWNER' , HttpStatus . FORBIDDEN ) ;
115- }
11685 const users = await this . groupsService . listGroupUsers ( namespaceId , groupId ) ;
11786 return plainToInstance ( GroupUserDto , users , {
11887 excludeExtraneousValues : true ,
11988 } ) ;
12089 }
12190
91+ @NamespaceOwner ( )
12292 @Post ( ':groupId/users' )
12393 async addGroupUser (
124- @Req ( ) req ,
12594 @Param ( 'namespaceId' ) namespaceId : string ,
12695 @Param ( 'groupId' ) groupId : string ,
12796 @Body ( ) addGroupUserDto : AddGroupUserDto ,
128- @I18n ( ) i18n : I18nContext ,
12997 ) {
130- if ( ! ( await this . namespacesService . userIsOwner ( namespaceId , req . user . id ) ) ) {
131- const message = i18n . t ( 'namespace.errors.userNotOwner' ) ;
132- throw new AppException ( message , 'USER_NOT_OWNER' , HttpStatus . FORBIDDEN ) ;
133- }
13498 const actions : Array < Promise < any > > = [ ] ;
13599 addGroupUserDto . userIds . forEach ( ( userId ) => {
136100 if ( userId ) {
@@ -142,18 +106,13 @@ export class GroupsController {
142106 await Promise . all ( actions ) ;
143107 }
144108
109+ @NamespaceOwner ( )
145110 @Delete ( ':groupId/users/:userId' )
146111 async deleteGroupUser (
147- @Req ( ) req ,
148112 @Param ( 'namespaceId' ) namespaceId : string ,
149113 @Param ( 'groupId' ) groupId : string ,
150114 @Param ( 'userId' ) userId : string ,
151- @I18n ( ) i18n : I18nContext ,
152115 ) {
153- if ( ! ( await this . namespacesService . userIsOwner ( namespaceId , req . user . id ) ) ) {
154- const message = i18n . t ( 'namespace.errors.userNotOwner' ) ;
155- throw new AppException ( message , 'USER_NOT_OWNER' , HttpStatus . FORBIDDEN ) ;
156- }
157116 await this . groupsService . deleteGroupUser ( namespaceId , groupId , userId ) ;
158117 }
159118}
0 commit comments