From 774d29d798477c960271bc7ea4e6c7304da14cae Mon Sep 17 00:00:00 2001 From: Nevo David Date: Wed, 7 Jan 2026 18:38:08 +0700 Subject: [PATCH] feat: fetch post faster --- .../src/api/routes/posts.controller.ts | 10 ++++- .../src/components/launches/calendar.tsx | 2 +- .../database/prisma/posts/posts.repository.ts | 20 ++++++++- .../database/prisma/posts/posts.service.ts | 41 +++++++++++++++++++ 4 files changed, 70 insertions(+), 3 deletions(-) diff --git a/apps/backend/src/api/routes/posts.controller.ts b/apps/backend/src/api/routes/posts.controller.ts index c51852ee..ce03df00 100644 --- a/apps/backend/src/api/routes/posts.controller.ts +++ b/apps/backend/src/api/routes/posts.controller.ts @@ -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); diff --git a/apps/frontend/src/components/launches/calendar.tsx b/apps/frontend/src/components/launches/calendar.tsx index 997218f8..f0dbb4f6 100644 --- a/apps/frontend/src/components/launches/calendar.tsx +++ b/apps/frontend/src/components/launches/calendar.tsx @@ -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; diff --git a/libraries/nestjs-libraries/src/database/prisma/posts/posts.repository.ts b/libraries/nestjs-libraries/src/database/prisma/posts/posts.repository.ts index cb2a647a..56efa445 100644 --- a/libraries/nestjs-libraries/src/database/prisma/posts/posts.repository.ts +++ b/libraries/nestjs-libraries/src/database/prisma/posts/posts.repository.ts @@ -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, diff --git a/libraries/nestjs-libraries/src/database/prisma/posts/posts.service.ts b/libraries/nestjs-libraries/src/database/prisma/posts/posts.service.ts index d8dfd75d..98cc144b 100644 --- a/libraries/nestjs-libraries/src/database/prisma/posts/posts.service.ts +++ b/libraries/nestjs-libraries/src/database/prisma/posts/posts.service.ts @@ -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 = {