Skip to content

Commit

Permalink
feat(cms): Most common page types are now directly fetched from cms (#…
Browse files Browse the repository at this point in the history
…16320)

* Most common page types are now directly fetched from cms

* Revert frontpage change

* Fetch manuals from contentful

* Remove cache invalidation service

* Fetch events and service web page from cms

* Remove todos

* Fetch keywords as well

---------

Co-authored-by: kodiakhq[bot] <49736102+kodiakhq[bot]@users.noreply.github.com>
  • Loading branch information
RunarVestmann and kodiakhq[bot] authored Nov 5, 2024
1 parent 68d57e5 commit f9931f3
Show file tree
Hide file tree
Showing 5 changed files with 111 additions and 280 deletions.
117 changes: 97 additions & 20 deletions libs/cms/src/lib/cms.contentful.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,6 @@ import { ContentfulRepository, localeMap } from './contentful.repository'
import { GetAlertBannerInput } from './dto/getAlertBanner.input'
import { AlertBanner, mapAlertBanner } from './models/alertBanner.model'
import { mapUrl, Url } from './models/url.model'
import { mapTellUsAStory, TellUsAStory } from './models/tellUsAStory.model'
import { GetSubpageHeaderInput } from './dto/getSubpageHeader.input'
import { mapSubpageHeader, SubpageHeader } from './models/subpageHeader.model'
import {
Expand Down Expand Up @@ -82,6 +81,9 @@ import { GetGenericTagBySlugInput } from './dto/getGenericTagBySlug.input'
import { GenericTag, mapGenericTag } from './models/genericTag.model'
import { GetEmailSignupInput } from './dto/getEmailSignup.input'
import { LifeEventPage, mapLifeEventPage } from './models/lifeEventPage.model'
import { mapManual } from './models/manual.model'
import { mapServiceWebPage } from './models/serviceWebPage.model'
import { mapEvent } from './models/event.model'

const errorHandler = (name: string) => {
return (error: Error) => {
Expand All @@ -90,20 +92,42 @@ const errorHandler = (name: string) => {
}
}

const ArticleFields = [
// we want to exclude relatedArticles because it's a self-referencing
// relation and selecting related articles to a depth of 10 would make the
// response huge
'sys',
'fields.slug',
'fields.title',
'fields.shortTitle',
'fields.content',
'fields.subgroup',
'fields.group',
'fields.category',
'fields.subArticles',
].join(',')
const ArticleFields = (
[
// we want to exclude relatedArticles because it's a self-referencing
// relation and selecting related articles to a depth of 10 would make the
// response huge
'sys',
'fields.activeTranslations',
'fields.alertBanner',
'fields.category',
'fields.content',
'fields.contentStatus',
'fields.featuredImage',
'fields.group',
'fields.importance',
'fields.intro',
'fields.organization',
'fields.otherCategories',
'fields.otherGroups',
'fields.otherSubgroups',
'fields.processEntry',
'fields.processEntryButtonText',
'fields.relatedContent',
'fields.relatedOrganization',
'fields.responsibleParty',
'fields.shortTitle',
'fields.showTableOfContents',
'fields.signLanguageVideo',
'fields.slug',
'fields.stepper',
'fields.subArticles',
'fields.subgroup',
'fields.title',
'fields.userStories',
'fields.keywords',
] as ('sys' | `fields.${keyof types.IArticle['fields']}`)[]
).join(',')

@Injectable()
export class CmsContentfulService {
Expand Down Expand Up @@ -378,8 +402,9 @@ export class CmsContentfulService {
): Promise<OrganizationPage> {
const params = {
['content_type']: 'organizationPage',
include: 10,
include: 5,
'fields.slug': slug,
limit: 1,
}

const result = await this.contentfulRepository
Expand All @@ -399,10 +424,11 @@ export class CmsContentfulService {
): Promise<OrganizationSubpage> {
const params = {
['content_type']: 'organizationSubpage',
include: 10,
include: 5,
'fields.slug': slug,
'fields.organizationPage.sys.contentType.sys.id': 'organizationPage',
'fields.organizationPage.fields.slug': organizationSlug,
limit: 1,
}
const result = await this.contentfulRepository
.getLocalizedEntries<types.IOrganizationSubpageFields>(lang, params)
Expand All @@ -415,6 +441,23 @@ export class CmsContentfulService {
)
}

async getServiceWebPage(slug: string, lang: string) {
const params = {
['content_type']: 'serviceWebPage',
include: 5,
'fields.slug': slug,
limit: 1,
}
const result = await this.contentfulRepository
.getLocalizedEntries<types.IServiceWebPageFields>(lang, params)
.catch(errorHandler('getServiceWebPage'))

return (
(result.items as types.IServiceWebPage[]).map(mapServiceWebPage)[0] ??
null
)
}

async getAuctions(
lang: string,
organization?: string,
Expand Down Expand Up @@ -467,6 +510,7 @@ export class CmsContentfulService {
const params = {
['content_type']: 'projectPage',
'fields.slug': slug,
limit: 1,
}

const result = await this.contentfulRepository
Expand All @@ -483,7 +527,8 @@ export class CmsContentfulService {
['content_type']: 'article',
'fields.slug': slug,
select: ArticleFields,
include: 10,
include: 5,
limit: 1,
}

const result = await this.contentfulRepository
Expand Down Expand Up @@ -542,8 +587,9 @@ export class CmsContentfulService {
async getNews(lang: string, slug: string): Promise<News | null> {
const params = {
['content_type']: 'news',
include: 10,
include: 5,
'fields.slug': slug,
limit: 1,
}

const result = await this.contentfulRepository
Expand All @@ -553,6 +599,36 @@ export class CmsContentfulService {
return (result.items as types.INews[]).map(mapNews)[0] ?? null
}

async getSingleEvent(lang: string, slug: string) {
const params = {
['content_type']: 'event',
include: 5,
'fields.slug': slug,
limit: 1,
}

const result = await this.contentfulRepository
.getLocalizedEntries<types.IEventFields>(lang, params)
.catch(errorHandler('getSingleEvent'))

return (result.items as types.IEvent[]).map(mapEvent)[0] ?? null
}

async getSingleManual(lang: string, slug: string) {
const params = {
['content_type']: 'manual',
include: 5,
'fields.slug': slug,
limit: 1,
}

const result = await this.contentfulRepository
.getLocalizedEntries<types.IManualFields>(lang, params)
.catch(errorHandler('getSingleManual'))

return (result.items as types.IManual[]).map(mapManual)[0] ?? null
}

async getContentSlug({
id,
}: GetContentSlugInput): Promise<ContentSlug | null> {
Expand Down Expand Up @@ -828,8 +904,9 @@ export class CmsContentfulService {
const params = {
['content_type']: 'frontpage',
'fields.pageIdentifier': pageIdentifier,
include: 10,
include: 5,
order: '-sys.createdAt',
limit: 1,
}

const result = await this.contentfulRepository
Expand Down
41 changes: 13 additions & 28 deletions libs/cms/src/lib/cms.resolver.ts
Original file line number Diff line number Diff line change
Expand Up @@ -246,20 +246,18 @@ export class CmsResolver {
async getOrganizationPage(
@Args('input') input: GetOrganizationPageInput,
): Promise<OrganizationPage | null> {
return this.cmsElasticsearchService.getSingleDocumentTypeBySlug(
getElasticsearchIndex(input.lang),
{ type: 'webOrganizationPage', slug: input.slug },
)
return this.cmsContentfulService.getOrganizationPage(input.slug, input.lang)
}

@CacheControl(defaultCache)
@Query(() => OrganizationSubpage, { nullable: true })
async getOrganizationSubpage(
@Args('input') input: GetOrganizationSubpageInput,
): Promise<OrganizationSubpage | null> {
return this.cmsElasticsearchService.getSingleOrganizationSubpage(
getElasticsearchIndex(input.lang),
{ ...input },
return this.cmsContentfulService.getOrganizationSubpage(
input.organizationSlug,
input.slug,
input.lang,
)
}

Expand All @@ -268,10 +266,7 @@ export class CmsResolver {
async getServiceWebPage(
@Args('input') input: GetServiceWebPageInput,
): Promise<ServiceWebPage | null> {
return this.cmsElasticsearchService.getSingleDocumentTypeBySlug(
getElasticsearchIndex(input.lang),
{ type: 'webServiceWebPage', slug: input.slug },
)
return this.cmsContentfulService.getServiceWebPage(input.slug, input.lang)
}

@CacheControl(defaultCache)
Expand Down Expand Up @@ -422,11 +417,10 @@ export class CmsResolver {
async getSingleArticle(
@Args('input') { lang, slug }: GetSingleArticleInput,
): Promise<(Partial<Article> & { lang: Locale }) | null> {
const article: Article | null =
await this.cmsElasticsearchService.getSingleDocumentTypeBySlug<Article>(
getElasticsearchIndex(lang),
{ type: 'webArticle', slug },
)
const article: Article | null = await this.cmsContentfulService.getArticle(
slug,
lang,
)

if (!article) return null

Expand All @@ -452,21 +446,15 @@ export class CmsResolver {
getSingleNews(
@Args('input') { lang, slug }: GetSingleNewsInput,
): Promise<News | null> {
return this.cmsElasticsearchService.getSingleDocumentTypeBySlug<News>(
getElasticsearchIndex(lang),
{ type: 'webNews', slug },
)
return this.cmsContentfulService.getNews(lang, slug)
}

@CacheControl(defaultCache)
@Query(() => EventModel, { nullable: true })
getSingleEvent(
@Args('input') { lang, slug }: GetSingleEventInput,
): Promise<EventModel | null> {
return this.cmsElasticsearchService.getSingleDocumentTypeBySlug<EventModel>(
getElasticsearchIndex(lang),
{ type: 'webEvent', slug },
)
return this.cmsContentfulService.getSingleEvent(lang, slug)
}

@CacheControl(defaultCache)
Expand Down Expand Up @@ -620,10 +608,7 @@ export class CmsResolver {
getSingleManual(
@Args('input') input: GetSingleManualInput,
): Promise<Manual | null> {
return this.cmsElasticsearchService.getSingleDocumentTypeBySlug(
getElasticsearchIndex(input.lang),
{ type: 'webManual', slug: input.slug },
)
return this.cmsContentfulService.getSingleManual(input.lang, input.slug)
}

@CacheControl(defaultCache)
Expand Down
Loading

0 comments on commit f9931f3

Please sign in to comment.