@@ -40,6 +40,7 @@ import { TagDto } from 'omniboxd/tag/dto/tag.dto';
4040import { ResourceAttachmentsService } from 'omniboxd/resource-attachments/resource-attachments.service' ;
4141import { ResourcesService } from 'omniboxd/resources/resources.service' ;
4242import { ResourceMetaDto } from 'omniboxd/resources/dto/resource-meta.dto' ;
43+ import { ListChildrenRespDto } from './dto/list-children-resp.dto' ;
4344
4445const TASK_PRIORITY = 5 ;
4546
@@ -129,6 +130,28 @@ export class NamespaceResourcesService {
129130 return resources . map ( ( resource ) => resource . id ) ;
130131 }
131132
133+ private async hasChildren (
134+ namespaceId : string ,
135+ parents : ResourceMetaDto [ ] ,
136+ userId : string ,
137+ ) : Promise < boolean > {
138+ const children = await this . resourcesService . getSubResources (
139+ namespaceId ,
140+ parents [ 0 ] . id ,
141+ ) ;
142+ for ( const child of children ) {
143+ const permission = await this . permissionsService . getCurrentPermission (
144+ namespaceId ,
145+ [ child , ...parents ] ,
146+ userId ,
147+ ) ;
148+ if ( permission !== ResourcePermission . NO_ACCESS ) {
149+ return true ;
150+ }
151+ }
152+ return false ;
153+ }
154+
132155 async findByIds ( namespaceId : string , ids : Array < string > ) {
133156 if ( ids . length <= 0 ) {
134157 return [ ] ;
@@ -385,7 +408,7 @@ export class NamespaceResourcesService {
385408 } ;
386409 // Self and child exclusions
387410 if ( excludeResourceId ) {
388- const resourceChildren = await this . getAndFilterSubResources (
411+ const resourceChildren = await this . getSubResourcesByUser (
389412 namespaceId ,
390413 excludeResourceId ,
391414 userId ,
@@ -410,7 +433,7 @@ export class NamespaceResourcesService {
410433 return filteredResources . map ( ( res ) => ResourceMetaDto . fromEntity ( res ) ) ;
411434 }
412435
413- async getAndFilterSubResources (
436+ async getSubResourcesByUser (
414437 namespaceId : string ,
415438 resourceId : string ,
416439 userId : string ,
@@ -420,6 +443,25 @@ export class NamespaceResourcesService {
420443 namespaceId ,
421444 resourceId ,
422445 ) ;
446+ const children = await this . getSubResourcesByParents (
447+ namespaceId ,
448+ parents ,
449+ userId ,
450+ ) ;
451+ if ( includeParent ) {
452+ return [ parents [ 0 ] , ...children ] ;
453+ }
454+ return children ;
455+ }
456+
457+ async getSubResourcesByParents (
458+ namespaceId : string ,
459+ parents : ResourceMetaDto [ ] ,
460+ userId : string ,
461+ ) : Promise < ResourceMetaDto [ ] > {
462+ if ( ! parents ) {
463+ return [ ] ;
464+ }
423465 const permission = await this . permissionsService . getCurrentPermission (
424466 namespaceId ,
425467 parents ,
@@ -430,7 +472,7 @@ export class NamespaceResourcesService {
430472 }
431473 const children = await this . resourcesService . getSubResources (
432474 namespaceId ,
433- resourceId ,
475+ parents [ 0 ] . id ,
434476 ) ;
435477 const filteredChildren : ResourceMetaDto [ ] = [ ] ;
436478 for ( const child of children ) {
@@ -443,12 +485,35 @@ export class NamespaceResourcesService {
443485 filteredChildren . push ( child ) ;
444486 }
445487 }
446- if ( includeParent ) {
447- return [ parents [ 0 ] , ...filteredChildren ] ;
448- }
449488 return filteredChildren ;
450489 }
451490
491+ async listChildren (
492+ namespaceId : string ,
493+ resourceId : string ,
494+ userId : string ,
495+ ) : Promise < ListChildrenRespDto [ ] > {
496+ const parents = await this . resourcesService . getParentResources (
497+ namespaceId ,
498+ resourceId ,
499+ ) ;
500+ const children = await this . getSubResourcesByParents (
501+ namespaceId ,
502+ parents ,
503+ userId ,
504+ ) ;
505+ const resps : ListChildrenRespDto [ ] = [ ] ;
506+ for ( const child of children ) {
507+ const hasChildren = await this . hasChildren (
508+ namespaceId ,
509+ [ child , ...parents ] ,
510+ userId ,
511+ ) ;
512+ resps . push ( new ListChildrenRespDto ( child , hasChildren ) ) ;
513+ }
514+ return resps ;
515+ }
516+
452517 async getSpaceType (
453518 namespaceId : string ,
454519 rootResourceId : string ,
0 commit comments