feat: fetch post faster

This commit is contained in:
Nevo David 2026-01-07 18:38:08 +07:00
parent bd24282d2b
commit 774d29d798
4 changed files with 70 additions and 3 deletions

View File

@ -22,7 +22,10 @@ import { Response } from 'express';
import { GetUserFromRequest } from '@gitroom/nestjs-libraries/user/user.from.request';
import { ShortLinkService } from '@gitroom/nestjs-libraries/short-linking/short.link.service';
import { CreateTagDto } from '@gitroom/nestjs-libraries/dtos/posts/create.tag.dto';
import { AuthorizationActions, Sections } from '@gitroom/backend/services/auth/permissions/permission.exception.class';
import {
AuthorizationActions,
Sections,
} from '@gitroom/backend/services/auth/permissions/permission.exception.class';
@ApiTags('Posts')
@Controller('/posts')
@ -111,6 +114,11 @@ export class PostsController {
return this._postsService.getOldPosts(org.id, date);
}
@Get('/group/:group')
getPostsByGroup(@GetOrgFromRequest() org: Organization, @Param('group') group: string) {
return this._postsService.getPostsByGroup(org.id, group);
}
@Get('/:id')
getPost(@GetOrgFromRequest() org: Organization, @Param('id') id: string) {
return this._postsService.getPost(org.id, id);

View File

@ -454,7 +454,7 @@ export const CalendarColumn: FC<{
publishDate: loadPost.actualDate || loadPost.publishDate,
};
const data = await (await fetch(`/posts/${post.id}`)).json();
const data = await (await fetch(`/posts/group/${post.group}`)).json();
const date = !isDuplicate
? null
: (await (await fetch('/posts/find-slot')).json()).date;

View File

@ -49,7 +49,7 @@ export class PostsRepository {
integration: {
select: {
providerIdentifier: true,
}
},
},
publishDate: true,
},
@ -235,6 +235,24 @@ export class PostsRepository {
});
}
getPostsByGroup(orgId: string, group: string) {
return this._post.model.post.findMany({
where: {
group,
...(orgId ? { organizationId: orgId } : {}),
deletedAt: null,
},
include: {
integration: true,
tags: {
select: {
tag: true,
},
},
},
});
}
getPost(
id: string,
includeIntegration = false,

View File

@ -252,6 +252,47 @@ export class PostsService {
}
}
async getPostsByGroup(orgId: string, group: string) {
const convertToJPEG = false;
const loadAll = await this._postRepository.getPostsByGroup(orgId, group);
const posts = this.arrangePostsByGroup(loadAll, undefined);
return {
group: posts?.[0]?.group,
posts: await Promise.all(
(posts || []).map(async (post) => ({
...post,
image: await this.updateMedia(
post.id,
JSON.parse(post.image || '[]'),
convertToJPEG
),
}))
),
integrationPicture: posts[0]?.integration?.picture,
integration: posts[0].integrationId,
settings: JSON.parse(posts[0].settings || '{}'),
};
}
arrangePostsByGroup(all: any, parent?: string): PostWithConditionals[] {
const findAll = all
.filter((p: any) =>
!parent ? !p.parentPostId : p.parentPostId === parent
)
.map(({ integration, ...all }: any) => ({
...all,
...(!parent ? { integration } : {}),
}));
return [
...findAll,
...(findAll.length
? findAll.flatMap((p: any) => this.arrangePostsByGroup(all, p.id))
: []),
];
}
async getPost(orgId: string, id: string, convertToJPEG = false) {
const posts = await this.getPostsRecursively(id, true, orgId, true);
const list = {