Skip to content

Commit 2518b89

Browse files
authored
Merge pull request #170 from import-ai/refactor/search
fix(search): fix non-existing resources
2 parents 9d3e7ee + d6c36af commit 2518b89

File tree

7 files changed

+39
-12
lines changed

7 files changed

+39
-12
lines changed

src/namespace-resources/namespace-resources.service.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -440,7 +440,7 @@ export class NamespaceResourcesService {
440440
resourceId: string,
441441
userId: string,
442442
): Promise<ResourceMetaDto[]> {
443-
const parents = await this.resourcesService.getParentResources(
443+
const parents = await this.resourcesService.getParentResourcesOrFail(
444444
namespaceId,
445445
resourceId,
446446
);
@@ -491,7 +491,7 @@ export class NamespaceResourcesService {
491491
resourceId: string,
492492
userId: string,
493493
): Promise<ListChildrenRespDto[]> {
494-
const parents = await this.resourcesService.getParentResources(
494+
const parents = await this.resourcesService.getParentResourcesOrFail(
495495
namespaceId,
496496
resourceId,
497497
);
@@ -538,7 +538,7 @@ export class NamespaceResourcesService {
538538
if (resource.namespaceId !== namespaceId) {
539539
throw new NotFoundException('Not found');
540540
}
541-
const parentResources = await this.resourcesService.getParentResources(
541+
const parentResources = await this.resourcesService.getParentResourcesOrFail(
542542
namespaceId,
543543
resource.parentId,
544544
);

src/permissions/permissions.service.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -233,7 +233,7 @@ export class PermissionsService {
233233
userId: string,
234234
): Promise<ListRespDto> {
235235
// Get resources
236-
const resources = await this.resourcesService.getParentResources(
236+
const resources = await this.resourcesService.getParentResourcesOrFail(
237237
namespaceId,
238238
resourceId,
239239
);
@@ -320,7 +320,7 @@ export class PermissionsService {
320320
return false;
321321
}
322322
if (!resources) {
323-
resources = await this.resourcesService.getParentResources(
323+
resources = await this.resourcesService.getParentResourcesOrFail(
324324
namespaceId,
325325
resourceId,
326326
);

src/resources/resources.service.ts

Lines changed: 14 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -16,15 +16,26 @@ export class ResourcesService {
1616
private readonly wizardTaskService: WizardTaskService,
1717
) {}
1818

19-
async getParentResources(
19+
async getParentResourcesOrFail(
2020
namespaceId: string,
2121
resourceId: string | null,
2222
): Promise<ResourceMetaDto[]> {
2323
if (!resourceId) {
2424
return [];
2525
}
26+
const resources = await this.getParentResources(namespaceId, resourceId);
27+
if (resources.length === 0) {
28+
throw new NotFoundException('Resource not found');
29+
}
30+
return resources;
31+
}
32+
33+
async getParentResources(
34+
namespaceId: string,
35+
resourceId: string | null,
36+
): Promise<ResourceMetaDto[]> {
2637
const resources: Resource[] = [];
27-
while (true) {
38+
while (resourceId) {
2839
const resource = await this.resourceRepository.findOne({
2940
select: [
3041
'id',
@@ -38,12 +49,9 @@ export class ResourcesService {
3849
where: { namespaceId, id: resourceId },
3950
});
4051
if (!resource) {
41-
throw new NotFoundException('Resource not found');
52+
return [];
4253
}
4354
resources.push(resource);
44-
if (!resource.parentId) {
45-
break;
46-
}
4755
resourceId = resource.parentId;
4856
}
4957
return resources.map((r) => ResourceMetaDto.fromEntity(r));

src/search/search.e2e-spec.ts

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ import { ConfigService } from '@nestjs/config';
1717
import { getRepositoryToken } from '@nestjs/typeorm';
1818
import { WizardTaskService } from 'omniboxd/tasks/wizard-task.service';
1919
import { Task } from 'omniboxd/tasks/tasks.entity';
20+
import { ResourcesService } from 'omniboxd/resources/resources.service';
2021

2122
// Set environment variable to avoid the error in SearchService constructor
2223
process.env.OBB_WIZARD_BASE_URL = 'http://localhost:8000';
@@ -111,6 +112,12 @@ describe('SearchController (e2e)', () => {
111112
listAllResources: jest.fn().mockResolvedValue([]),
112113
},
113114
},
115+
{
116+
provide: ResourcesService,
117+
useValue: {
118+
getParentResources: jest.fn().mockResolvedValue([]),
119+
},
120+
},
114121
{
115122
provide: MessagesService,
116123
useValue: {

src/search/search.module.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ import { ConversationsModule } from 'omniboxd/conversations/conversations.module
1111
import { TypeOrmModule } from '@nestjs/typeorm';
1212
import { Task } from 'omniboxd/tasks/tasks.entity';
1313
import { TasksModule } from 'omniboxd/tasks/tasks.module';
14+
import { ResourcesModule } from 'omniboxd/resources/resources.module';
1415

1516
@Module({
1617
exports: [SearchService],
@@ -19,6 +20,7 @@ import { TasksModule } from 'omniboxd/tasks/tasks.module';
1920
imports: [
2021
PermissionsModule,
2122
NamespaceResourcesModule,
23+
ResourcesModule,
2224
MessagesModule,
2325
ConversationsModule,
2426
TasksModule,

src/search/search.service.ts

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ import { InjectRepository } from '@nestjs/typeorm';
1818
import { WizardTaskService } from 'omniboxd/tasks/wizard-task.service';
1919
import { MessagesService } from 'omniboxd/messages/messages.service';
2020
import { ConversationsService } from 'omniboxd/conversations/conversations.service';
21+
import { ResourcesService } from 'omniboxd/resources/resources.service';
2122

2223
const TASK_PRIORITY = 4;
2324

@@ -28,6 +29,7 @@ export class SearchService {
2829
constructor(
2930
private readonly permissionsService: PermissionsService,
3031
private readonly namespaceResourcesService: NamespaceResourcesService,
32+
private readonly resourcesService: ResourcesService,
3133
private readonly messagesService: MessagesService,
3234
private readonly conversationsService: ConversationsService,
3335
private readonly configService: ConfigService,
@@ -70,12 +72,20 @@ export class SearchService {
7072
continue;
7173
}
7274
seenResourceIds.add(chunk.resourceId);
75+
const parentResources = await this.resourcesService.getParentResources(
76+
namespaceId,
77+
chunk.resourceId,
78+
);
79+
if (!parentResources) {
80+
continue;
81+
}
7382
if (userId) {
7483
const hasPermission = await this.permissionsService.userHasPermission(
7584
namespaceId,
7685
chunk.resourceId,
7786
userId,
7887
ResourcePermission.CAN_VIEW,
88+
parentResources,
7989
);
8090
if (!hasPermission) {
8191
continue;

src/shared-resources/shared-resources.service.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -61,7 +61,7 @@ export class SharedResourcesService {
6161
throw new NotFoundException('Resource not found');
6262
}
6363
if (resource.id !== share.resourceId) {
64-
const parents = await this.resourcesService.getParentResources(
64+
const parents = await this.resourcesService.getParentResourcesOrFail(
6565
share.namespaceId,
6666
resource.parentId,
6767
);

0 commit comments

Comments
 (0)