Skip to content

Commit 5ef720c

Browse files
authored
Merge pull request #211 from import-ai/feat/recent_api
feat(resources): add recent endpoint/service
2 parents ed046ea + 0953dbf commit 5ef720c

File tree

3 files changed

+35
-1
lines changed

3 files changed

+35
-1
lines changed

src/namespace-resources/namespace-resources.controller.ts

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -146,6 +146,20 @@ export class NamespaceResourcesController {
146146
});
147147
}
148148

149+
@Get('recent')
150+
async recent(
151+
@UserId() userId: string,
152+
@Param('namespaceId') namespaceId: string,
153+
@Query('limit') limit?: string,
154+
): Promise<ResourceMetaDto[]> {
155+
const take = Number.isFinite(Number(limit)) ? Number(limit) : 10;
156+
return await this.namespaceResourcesService.recent(
157+
namespaceId,
158+
userId,
159+
take,
160+
);
161+
}
162+
149163
@Get(':resourceId')
150164
async get(
151165
@UserId() userId: string,

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

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -395,6 +395,27 @@ export class NamespaceResourcesService {
395395
return filteredResources.map((res) => ResourceMetaDto.fromEntity(res));
396396
}
397397

398+
async recent(
399+
namespaceId: string,
400+
userId: string,
401+
limit: number = 10,
402+
): Promise<ResourceMetaDto[]> {
403+
const allVisible = await this.getUserVisibleResources(userId, namespaceId);
404+
const sorted = allVisible
405+
.filter((r) => r.parentId !== null)
406+
.sort((a, b) => b.updatedAt.getTime() - a.updatedAt.getTime());
407+
const take = Math.max(1, Math.min(100, limit));
408+
return sorted.slice(0, take);
409+
}
410+
411+
// Alias for clarity and reuse across modules
412+
async getUserVisibleResources(
413+
userId: string,
414+
namespaceId: string,
415+
): Promise<ResourceMetaDto[]> {
416+
return await this.getAllResourcesByUser(userId, namespaceId);
417+
}
418+
398419
async getSubResourcesByUser(
399420
userId: string,
400421
namespaceId: string,

src/namespaces/namespaces.service.ts

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
import { InjectRepository } from '@nestjs/typeorm';
22
import { Namespace } from './entities/namespace.entity';
3-
import { Resource } from 'omniboxd/resources/entities/resource.entity';
43
import { UpdateNamespaceDto } from './dto/update-namespace.dto';
54
import { NamespaceMemberDto } from './dto/namespace-member.dto';
65
import { GroupUser } from 'omniboxd/groups/entities/group-user.entity';

0 commit comments

Comments
 (0)