Skip to content

Commit 774d29d

Browse files
committed
feat: fetch post faster
1 parent bd24282 commit 774d29d

File tree

4 files changed

+70
-3
lines changed

4 files changed

+70
-3
lines changed

apps/backend/src/api/routes/posts.controller.ts

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,10 @@ import { Response } from 'express';
2222
import { GetUserFromRequest } from '@gitroom/nestjs-libraries/user/user.from.request';
2323
import { ShortLinkService } from '@gitroom/nestjs-libraries/short-linking/short.link.service';
2424
import { CreateTagDto } from '@gitroom/nestjs-libraries/dtos/posts/create.tag.dto';
25-
import { AuthorizationActions, Sections } from '@gitroom/backend/services/auth/permissions/permission.exception.class';
25+
import {
26+
AuthorizationActions,
27+
Sections,
28+
} from '@gitroom/backend/services/auth/permissions/permission.exception.class';
2629

2730
@ApiTags('Posts')
2831
@Controller('/posts')
@@ -111,6 +114,11 @@ export class PostsController {
111114
return this._postsService.getOldPosts(org.id, date);
112115
}
113116

117+
@Get('/group/:group')
118+
getPostsByGroup(@GetOrgFromRequest() org: Organization, @Param('group') group: string) {
119+
return this._postsService.getPostsByGroup(org.id, group);
120+
}
121+
114122
@Get('/:id')
115123
getPost(@GetOrgFromRequest() org: Organization, @Param('id') id: string) {
116124
return this._postsService.getPost(org.id, id);

apps/frontend/src/components/launches/calendar.tsx

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -454,7 +454,7 @@ export const CalendarColumn: FC<{
454454
publishDate: loadPost.actualDate || loadPost.publishDate,
455455
};
456456

457-
const data = await (await fetch(`/posts/${post.id}`)).json();
457+
const data = await (await fetch(`/posts/group/${post.group}`)).json();
458458
const date = !isDuplicate
459459
? null
460460
: (await (await fetch('/posts/find-slot')).json()).date;

libraries/nestjs-libraries/src/database/prisma/posts/posts.repository.ts

Lines changed: 19 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -49,7 +49,7 @@ export class PostsRepository {
4949
integration: {
5050
select: {
5151
providerIdentifier: true,
52-
}
52+
},
5353
},
5454
publishDate: true,
5555
},
@@ -235,6 +235,24 @@ export class PostsRepository {
235235
});
236236
}
237237

238+
getPostsByGroup(orgId: string, group: string) {
239+
return this._post.model.post.findMany({
240+
where: {
241+
group,
242+
...(orgId ? { organizationId: orgId } : {}),
243+
deletedAt: null,
244+
},
245+
include: {
246+
integration: true,
247+
tags: {
248+
select: {
249+
tag: true,
250+
},
251+
},
252+
},
253+
});
254+
}
255+
238256
getPost(
239257
id: string,
240258
includeIntegration = false,

libraries/nestjs-libraries/src/database/prisma/posts/posts.service.ts

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -252,6 +252,47 @@ export class PostsService {
252252
}
253253
}
254254

255+
async getPostsByGroup(orgId: string, group: string) {
256+
const convertToJPEG = false;
257+
const loadAll = await this._postRepository.getPostsByGroup(orgId, group);
258+
const posts = this.arrangePostsByGroup(loadAll, undefined);
259+
260+
return {
261+
group: posts?.[0]?.group,
262+
posts: await Promise.all(
263+
(posts || []).map(async (post) => ({
264+
...post,
265+
image: await this.updateMedia(
266+
post.id,
267+
JSON.parse(post.image || '[]'),
268+
convertToJPEG
269+
),
270+
}))
271+
),
272+
integrationPicture: posts[0]?.integration?.picture,
273+
integration: posts[0].integrationId,
274+
settings: JSON.parse(posts[0].settings || '{}'),
275+
};
276+
}
277+
278+
arrangePostsByGroup(all: any, parent?: string): PostWithConditionals[] {
279+
const findAll = all
280+
.filter((p: any) =>
281+
!parent ? !p.parentPostId : p.parentPostId === parent
282+
)
283+
.map(({ integration, ...all }: any) => ({
284+
...all,
285+
...(!parent ? { integration } : {}),
286+
}));
287+
288+
return [
289+
...findAll,
290+
...(findAll.length
291+
? findAll.flatMap((p: any) => this.arrangePostsByGroup(all, p.id))
292+
: []),
293+
];
294+
}
295+
255296
async getPost(orgId: string, id: string, convertToJPEG = false) {
256297
const posts = await this.getPostsRecursively(id, true, orgId, true);
257298
const list = {

0 commit comments

Comments
 (0)