@@ -13,10 +13,8 @@ import {
1313 Patch ,
1414 Post ,
1515 Query ,
16- Req ,
1716} from '@nestjs/common' ;
1817import { UserId } from 'omniboxd/decorators/user-id.decorator' ;
19- import { Request } from 'express' ;
2018import { ResourceMetaDto } from 'omniboxd/resources/dto/resource-meta.dto' ;
2119import { ChildrenMetaDto } from './dto/list-children-resp.dto' ;
2220
@@ -29,7 +27,7 @@ export class NamespaceResourcesController {
2927
3028 @Get ( )
3129 async findById (
32- @Req ( ) req : Request ,
30+ @UserId ( ) userId : string ,
3331 @Param ( 'namespaceId' ) namespaceId : string ,
3432 @Query ( 'id' ) id : string ,
3533 ) {
@@ -40,11 +38,23 @@ export class NamespaceResourcesController {
4038 if ( ids . length <= 0 ) {
4139 return [ ] ;
4240 }
43- return await this . namespaceResourcesService . findByIds (
41+ const resources = await this . namespaceResourcesService . findByIds (
4442 namespaceId ,
45- req . user ! . id ,
43+ userId ,
4644 ids ,
4745 ) ;
46+ return Promise . all (
47+ resources . map ( ( resource ) =>
48+ this . namespaceResourcesService
49+ . hasChildren ( userId , namespaceId , resource . id )
50+ . then ( ( hasChildren ) =>
51+ Promise . resolve ( {
52+ ...resource ,
53+ hasChildren,
54+ } ) ,
55+ ) ,
56+ ) ,
57+ ) ;
4858 }
4959
5060 @Post ( )
@@ -62,33 +72,33 @@ export class NamespaceResourcesController {
6272
6373 @Post ( ':resourceId/duplicate' )
6474 async duplicate (
65- @Req ( ) req : Request ,
75+ @UserId ( ) userId : string ,
6676 @Param ( 'namespaceId' ) namespaceId : string ,
6777 @Param ( 'resourceId' ) resourceId : string ,
6878 ) {
6979 const newResource = await this . namespaceResourcesService . duplicate (
70- req . user ! . id ,
80+ userId ,
7181 namespaceId ,
7282 resourceId ,
7383 ) ;
7484 return await this . namespaceResourcesService . getResource ( {
7585 namespaceId,
76- userId : req . user ! . id ,
86+ userId,
7787 resourceId : newResource . id ,
7888 } ) ;
7989 }
8090
8191 @Get ( 'query' )
8292 async query (
83- @Req ( ) req : Request ,
93+ @UserId ( ) userId : string ,
8494 @Param ( 'namespaceId' ) namespaceId : string ,
8595 @Query ( 'parentId' ) parentId : string ,
8696 @Query ( 'tags' ) tags : string ,
8797 ) {
8898 return await this . namespaceResourcesService . query (
8999 namespaceId ,
90100 parentId ,
91- req . user ! . id ,
101+ userId ,
92102 tags ,
93103 ) ;
94104 }
@@ -108,15 +118,15 @@ export class NamespaceResourcesController {
108118
109119 @Post ( ':resourceId/move/:targetId' )
110120 async move (
111- @Req ( ) req : Request ,
121+ @UserId ( ) userId : string ,
112122 @Param ( 'namespaceId' ) namespaceId : string ,
113123 @Param ( 'resourceId' ) resourceId : string ,
114124 @Param ( 'targetId' ) targetId : string ,
115125 ) {
116126 return await this . namespaceResourcesService . move (
117127 namespaceId ,
118128 resourceId ,
119- req . user ! . id ,
129+ userId ,
120130 targetId ,
121131 ) ;
122132 }
@@ -138,74 +148,83 @@ export class NamespaceResourcesController {
138148
139149 @Get ( ':resourceId' )
140150 async get (
141- @Req ( ) req : Request ,
151+ @UserId ( ) userId : string ,
142152 @Param ( 'namespaceId' ) namespaceId : string ,
143153 @Param ( 'resourceId' ) resourceId : string ,
144154 ) {
145155 return await this . namespaceResourcesService . getResource ( {
146156 namespaceId,
147157 resourceId,
148- userId : req . user ! . id ,
158+ userId,
149159 } ) ;
150160 }
151161
152162 @Patch ( ':resourceId' )
153163 async update (
154- @Req ( ) req : Request ,
164+ @UserId ( ) userId : string ,
155165 @Param ( 'namespaceId' ) namespaceId : string ,
156166 @Param ( 'resourceId' ) resourceId : string ,
157167 @Body ( ) data : UpdateResourceDto ,
158168 ) {
159169 const hasPermission = await this . permissionsService . userHasPermission (
160170 namespaceId ,
161171 resourceId ,
162- req . user ! . id ,
172+ userId ,
163173 ResourcePermission . CAN_EDIT ,
164174 ) ;
165175 if ( ! hasPermission ) {
166176 throw new ForbiddenException ( 'Not authorized' ) ;
167177 }
168- await this . namespaceResourcesService . update ( req . user ! . id , resourceId , data ) ;
178+ await this . namespaceResourcesService . update ( userId , resourceId , data ) ;
169179 return await this . namespaceResourcesService . getResource ( {
170180 namespaceId,
171181 resourceId,
172- userId : req . user ! . id ,
182+ userId,
173183 } ) ;
174184 }
175185
176186 @Delete ( ':resourceId' )
177187 async delete (
178- @Req ( ) req : Request ,
188+ @UserId ( ) userId : string ,
179189 @Param ( 'namespaceId' ) namespaceId : string ,
180190 @Param ( 'resourceId' ) resourceId : string ,
181191 ) {
182192 const hasPermission = await this . permissionsService . userHasPermission (
183193 namespaceId ,
184194 resourceId ,
185- req . user ! . id ,
195+ userId ,
186196 ResourcePermission . CAN_EDIT ,
187197 ) ;
188198 if ( ! hasPermission ) {
189199 throw new ForbiddenException ( 'Not authorized' ) ;
190200 }
191201 return await this . namespaceResourcesService . delete (
192- req . user ! . id ,
202+ userId ,
193203 namespaceId ,
194204 resourceId ,
195205 ) ;
196206 }
197207
198208 @Post ( ':resourceId/restore' )
199209 async restore (
200- @Req ( ) req : Request ,
210+ @UserId ( ) userId : string ,
201211 @Param ( 'namespaceId' ) namespaceId : string ,
202212 @Param ( 'resourceId' ) resourceId : string ,
203213 ) {
204- await this . namespaceResourcesService . restore ( req . user ! . id , resourceId ) ;
205- return await this . namespaceResourcesService . getResource ( {
214+ await this . namespaceResourcesService . restore ( userId , resourceId ) ;
215+ const resource = await this . namespaceResourcesService . getResource ( {
206216 namespaceId,
207217 resourceId,
208- userId : req . user ! . id ,
218+ userId,
209219 } ) ;
220+ const hasChildren = await this . namespaceResourcesService . hasChildren (
221+ userId ,
222+ namespaceId ,
223+ resourceId ,
224+ ) ;
225+ return {
226+ ...resource ,
227+ hasChildren,
228+ } ;
210229 }
211230}
0 commit comments