@@ -40,7 +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' ;
43+ import { ChildrenMetaDto } from './dto/list-children-resp.dto' ;
4444
4545const TASK_PRIORITY = 5 ;
4646
@@ -128,28 +128,6 @@ export class NamespaceResourcesService {
128128 return resources . map ( ( resource ) => resource . id ) ;
129129 }
130130
131- private async hasChildren (
132- namespaceId : string ,
133- parents : ResourceMetaDto [ ] ,
134- userId : string ,
135- ) : Promise < boolean > {
136- const children = await this . resourcesService . getSubResources (
137- namespaceId ,
138- parents [ 0 ] . id ,
139- ) ;
140- for ( const child of children ) {
141- const permission = await this . permissionsService . getCurrentPermission (
142- namespaceId ,
143- [ child , ...parents ] ,
144- userId ,
145- ) ;
146- if ( permission !== ResourcePermission . NO_ACCESS ) {
147- return true ;
148- }
149- }
150- return false ;
151- }
152-
153131 async findByIds ( namespaceId : string , ids : Array < string > ) {
154132 if ( ids . length <= 0 ) {
155133 return [ ] ;
@@ -186,6 +164,12 @@ export class NamespaceResourcesService {
186164 data : CreateResourceDto ,
187165 manager ?: EntityManager ,
188166 ) {
167+ if ( ! manager ) {
168+ return await this . dataSource . transaction ( async ( manager ) => {
169+ return await this . create ( userId , data , manager ) ;
170+ } ) ;
171+ }
172+
189173 const ok = await this . permissionsService . userHasPermission (
190174 data . namespaceId ,
191175 data . parentId ,
@@ -198,49 +182,14 @@ export class NamespaceResourcesService {
198182 throw new ForbiddenException ( 'Not authorized to create resource.' ) ;
199183 }
200184
201- const transaction = async ( manager : EntityManager ) => {
202- const repo = manager . getRepository ( Resource ) ;
203- const parentResource = await repo . findOne ( {
204- where : {
205- id : data . parentId ,
206- namespaceId : data . namespaceId ,
207- } ,
208- } ) ;
209- if ( ! parentResource ) {
210- throw new BadRequestException ( 'Parent resource not exists.' ) ;
211- }
212- if ( data . namespaceId && parentResource . namespaceId !== data . namespaceId ) {
213- throw new BadRequestException (
214- "Parent resource's namespace & space must match the resource's." ,
215- ) ;
216- }
217-
218- // Use provided tag_ids directly
219- const tagIds = data . tag_ids || [ ] ;
220-
221- const resource = repo . create ( {
185+ return await this . resourcesService . createResource (
186+ {
222187 ...data ,
223188 userId,
224- namespaceId : data . namespaceId ,
225- parentId : parentResource . id ,
226- tagIds : tagIds . length > 0 ? tagIds : undefined ,
227- } ) ;
228- const savedResource = await repo . save ( resource ) ;
229- await this . wizardTaskService . createIndexTask (
230- TASK_PRIORITY ,
231- userId ,
232- savedResource ,
233- manager . getRepository ( Task ) ,
234- ) ;
235- return savedResource ;
236- } ;
237-
238- if ( manager ) {
239- return await transaction ( manager ) ;
240- }
241- return await this . dataSource . transaction ( async ( manager ) => {
242- return await transaction ( manager ) ;
243- } ) ;
189+ tagIds : data . tag_ids ,
190+ } ,
191+ manager ,
192+ ) ;
244193 }
245194
246195 async duplicate ( userId : string , resourceId : string ) {
@@ -370,33 +319,26 @@ export class NamespaceResourcesService {
370319 : resourcesWithTags ;
371320 }
372321
373- async move ( { namespaceId, resourceId, targetId, userId } ) {
374- const resourceHasPermission =
375- await this . permissionsService . userHasPermission (
376- namespaceId ,
377- resourceId ,
378- userId ,
379- ) ;
380- if ( ! resourceHasPermission ) {
322+ async move (
323+ namespaceId : string ,
324+ resourceId : string ,
325+ userId : string ,
326+ targetId : string ,
327+ ) {
328+ const ok = await this . permissionsService . userHasPermission (
329+ namespaceId ,
330+ resourceId ,
331+ userId ,
332+ ) ;
333+ if ( ! ok ) {
381334 throw new ForbiddenException ( 'Not authorized' ) ;
382335 }
383- const resource = await this . resourceRepository . findOneByOrFail ( {
384- id : resourceId ,
385- } ) ;
386-
387- // Validate that the target resource exists
388- const targetResource = await this . resourceRepository . findOne ( {
389- where : { namespaceId, id : targetId } ,
390- } ) ;
391- if ( ! targetResource ) {
392- throw new NotFoundException ( 'Target resource not found' ) ;
393- }
394-
395- const newResource = this . resourceRepository . create ( {
396- ...resource ,
397- parentId : targetId ,
398- } ) ;
399- await this . resourceRepository . save ( newResource ) ;
336+ await this . resourcesService . updateResource (
337+ namespaceId ,
338+ resourceId ,
339+ userId ,
340+ { parentId : targetId } ,
341+ ) ;
400342 }
401343
402344 async search ( { namespaceId, excludeResourceId, name, userId } ) {
@@ -470,10 +412,9 @@ export class NamespaceResourcesService {
470412 if ( permission === ResourcePermission . NO_ACCESS ) {
471413 return [ ] ;
472414 }
473- const children = await this . resourcesService . getSubResources (
474- namespaceId ,
415+ const children = await this . resourcesService . getSubResources ( namespaceId , [
475416 parents [ 0 ] . id ,
476- ) ;
417+ ] ) ;
477418 const filteredChildren : ResourceMetaDto [ ] = [ ] ;
478419 for ( const child of children ) {
479420 const permission = await this . permissionsService . getCurrentPermission (
@@ -492,26 +433,47 @@ export class NamespaceResourcesService {
492433 namespaceId : string ,
493434 resourceId : string ,
494435 userId : string ,
495- ) : Promise < ListChildrenRespDto [ ] > {
436+ ) : Promise < ChildrenMetaDto [ ] > {
496437 const parents = await this . resourcesService . getParentResourcesOrFail (
497438 namespaceId ,
498439 resourceId ,
499440 ) ;
500- const children = await this . getSubResourcesByParents (
441+ const children = await this . resourcesService . getSubResources ( namespaceId , [
442+ resourceId ,
443+ ] ) ;
444+ const subChildren = await this . resourcesService . getSubResources (
445+ namespaceId ,
446+ children . map ( ( child ) => child . id ) ,
447+ ) ;
448+ const resources = [ ...parents , ...children , ...subChildren ] ;
449+ const permissionMap = await this . permissionsService . getCurrentPermissions (
501450 namespaceId ,
502- parents ,
503451 userId ,
452+ resources ,
504453 ) ;
505- const resps : ListChildrenRespDto [ ] = [ ] ;
454+
455+ const hasChildrenMap = new Map < string , boolean > ( ) ;
456+ for ( const subChild of subChildren ) {
457+ if ( ! subChild . parentId ) {
458+ continue ;
459+ }
460+ const permission = permissionMap . get ( subChild . id ) ;
461+ if ( ! permission || permission === ResourcePermission . NO_ACCESS ) {
462+ continue ;
463+ }
464+ hasChildrenMap . set ( subChild . parentId , true ) ;
465+ }
466+
467+ const childrenDtos : ChildrenMetaDto [ ] = [ ] ;
506468 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 ) ) ;
469+ const permission = permissionMap . get ( child . id ) ;
470+ if ( ! permission || permission === ResourcePermission . NO_ACCESS ) {
471+ continue ;
472+ }
473+ const hasChildren = hasChildrenMap . get ( child . id ) || false ;
474+ childrenDtos . push ( new ChildrenMetaDto ( child , hasChildren ) ) ;
513475 }
514- return resps ;
476+ return childrenDtos ;
515477 }
516478
517479 async getSpaceType (
@@ -586,12 +548,17 @@ export class NamespaceResourcesService {
586548 return resource ;
587549 }
588550
589- async update ( userId : string , id : string , data : UpdateResourceDto ) {
551+ async update ( userId : string , resourceId : string , data : UpdateResourceDto ) {
590552 await this . resourcesService . updateResource (
591553 data . namespaceId ,
592- id ,
554+ resourceId ,
593555 userId ,
594- data . toUpdateReq ( ) ,
556+ {
557+ name : data . name ,
558+ tagIds : data . tag_ids ,
559+ content : data . content ,
560+ attrs : data . attrs ,
561+ } ,
595562 ) ;
596563 }
597564
@@ -792,22 +759,6 @@ export class NamespaceResourcesService {
792759 return { fileStream, resource } ;
793760 }
794761
795- async createFolder (
796- namespaceId : string ,
797- parentId : string | null ,
798- userId : string | null ,
799- manager : EntityManager ,
800- ) {
801- return await manager . save (
802- manager . create ( Resource , {
803- resourceType : ResourceType . FOLDER ,
804- namespaceId,
805- parentId,
806- userId,
807- } ) ,
808- ) ;
809- }
810-
811762 async listAllUserAccessibleResources (
812763 namespaceId : string ,
813764 userId : string ,
0 commit comments