Skip to content

Commit 78d142c

Browse files
committed
fix(post): surface hasInsightsInLocale on detail response
Post detail endpoint never returned the flag, so the frontend insights chip was always hidden. Mirror note.controller: import AiModule, inject AiInsightsService, fold hasInsightsInLang into the existing Promise.all, and expose hasInsightsInLocale on the response.
1 parent 89d56c0 commit 78d142c

4 files changed

Lines changed: 41 additions & 15 deletions

File tree

apps/core/src/modules/post/post.controller.ts

Lines changed: 23 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,8 @@ import { MongoIdDto } from '~/shared/dto/id.dto'
2828
import { addYearCondition } from '~/transformers/db-query.transformer'
2929
import { applyContentPreference } from '~/utils/content.util'
3030

31+
import { AiInsightsService } from '../ai/ai-insights/ai-insights.service'
32+
import { parseLanguageCode } from '../ai/ai-language.util'
3133
import type { CategoryModel } from '../category/category.model'
3234
import { PostModel } from './post.model'
3335
import {
@@ -46,6 +48,7 @@ export class PostController {
4648
private readonly postService: PostService,
4749
private readonly countingService: CountingService,
4850
private readonly translationService: TranslationService,
51+
private readonly aiInsightsService: AiInsightsService,
4952
) {}
5053

5154
@Get('/')
@@ -338,20 +341,25 @@ export class PostController {
338341
.map((item) => item?._id?.toString?.() ?? item?.id)
339342
.filter((id): id is string => Boolean(id))
340343

341-
const [translationResult, relatedTitleMap] = await Promise.all([
342-
this.translationService.translateArticle({
343-
articleId: postDocument.id,
344-
targetLang: lang,
345-
allowHidden: Boolean(isAuthenticated),
346-
originalData: {
347-
title: baseData.title,
348-
text: baseData.text,
349-
summary: baseData.summary,
350-
tags: baseData.tags,
351-
},
352-
}),
353-
this.translationService.getCachedTitles(relatedIds, lang),
354-
])
344+
const insightsLang = parseLanguageCode(lang)
345+
const [translationResult, relatedTitleMap, hasInsightsInLocale] =
346+
await Promise.all([
347+
this.translationService.translateArticle({
348+
articleId: postDocument.id,
349+
targetLang: lang,
350+
allowHidden: Boolean(isAuthenticated),
351+
originalData: {
352+
title: baseData.title,
353+
text: baseData.text,
354+
summary: baseData.summary,
355+
tags: baseData.tags,
356+
},
357+
}),
358+
this.translationService.getCachedTitles(relatedIds, lang),
359+
this.aiInsightsService
360+
.hasInsightsInLang(postDocument.id, insightsLang)
361+
.catch(() => false),
362+
])
355363

356364
const translatedRelated = relatedTitleMap.size
357365
? relatedList.map((item) => {
@@ -377,6 +385,7 @@ export class PostController {
377385
sourceLang: translationResult.sourceLang,
378386
translationMeta: translationResult.translationMeta,
379387
availableTranslations: translationResult.availableTranslations,
388+
hasInsightsInLocale,
380389
liked,
381390
},
382391
query.prefer,

apps/core/src/modules/post/post.module.ts

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,16 @@
11
import { Global, Module } from '@nestjs/common'
2+
23
import { POST_SERVICE_TOKEN } from '~/constants/injection.constant'
4+
5+
import { AiModule } from '../ai/ai.module'
36
import { DraftModule } from '../draft/draft.module'
47
import { SlugTrackerModule } from '../slug-tracker/slug-tracker.module'
58
import { PostController } from './post.controller'
69
import { PostService } from './post.service'
710

811
@Global()
912
@Module({
10-
imports: [SlugTrackerModule, DraftModule],
13+
imports: [SlugTrackerModule, DraftModule, AiModule],
1114
controllers: [PostController],
1215
providers: [
1316
PostService,

apps/core/test/src/modules/post/post-content-format.spec.ts

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@ import {
2121
DRAFT_SERVICE_TOKEN,
2222
POST_SERVICE_TOKEN,
2323
} from '~/constants/injection.constant'
24+
import { AiInsightsService } from '~/modules/ai/ai-insights/ai-insights.service'
2425
import { CategoryModel } from '~/modules/category/category.model'
2526
import { CategoryService } from '~/modules/category/category.service'
2627
import { CommentModel } from '~/modules/comment/comment.model'
@@ -76,6 +77,12 @@ describe('Post ContentFormat (e2e)', async () => {
7677
},
7778
fileReferenceProvider,
7879
translationProvider,
80+
{
81+
provide: AiInsightsService,
82+
useValue: {
83+
hasInsightsInLang: vi.fn().mockResolvedValue(false),
84+
},
85+
},
7986
],
8087
imports: [],
8188
models: [

apps/core/test/src/modules/post/post.controller.e2e-spec.ts

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@ import {
2121
DRAFT_SERVICE_TOKEN,
2222
POST_SERVICE_TOKEN,
2323
} from '~/constants/injection.constant'
24+
import { AiInsightsService } from '~/modules/ai/ai-insights/ai-insights.service'
2425
import { CategoryModel } from '~/modules/category/category.model'
2526
import { CategoryService } from '~/modules/category/category.service'
2627
import { CommentModel } from '~/modules/comment/comment.model'
@@ -83,6 +84,12 @@ describe('PostController (e2e)', async () => {
8384
},
8485
fileReferenceProvider,
8586
translationProvider,
87+
{
88+
provide: AiInsightsService,
89+
useValue: {
90+
hasInsightsInLang: vi.fn().mockResolvedValue(false),
91+
},
92+
},
8693
],
8794
imports: [],
8895
models: [

0 commit comments

Comments
 (0)