Skip to content

Commit

Permalink
feat(cms): Be able to query organization by national id (#17122)
Browse files Browse the repository at this point in the history
* Be able to query organization by national id

* Refactor

* Be able to conditionally require field value to match code before refactor

---------

Co-authored-by: kodiakhq[bot] <49736102+kodiakhq[bot]@users.noreply.github.com>
  • Loading branch information
baering and kodiakhq[bot] authored Dec 4, 2024
1 parent 4dec76c commit 4a229a4
Showing 1 changed file with 23 additions and 31 deletions.
54 changes: 23 additions & 31 deletions libs/cms/src/lib/cms.contentful.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -348,18 +348,20 @@ export class CmsContentfulService {
)
}

async getOrganization(
slug: string,
private async getOrganizationBy(
fieldName: string,
fieldValue: string,
lang: string,
requireFieldValue = false,
): Promise<Organization | null> {
if (!slug) {
if (requireFieldValue && !fieldValue) {
return null
}

const params = {
['content_type']: 'organization',
include: 10,
'fields.slug': slug,
[`fields.${fieldName}`]: fieldValue,
limit: 1,
}

Expand All @@ -372,42 +374,32 @@ export class CmsContentfulService {
)
}

async getOrganization(
slug: string,
lang: string,
): Promise<Organization | null> {
return this.getOrganizationBy('slug', slug, lang, true)
}

async getOrganizationByTitle(
title: string,
lang: string,
): Promise<Organization> {
const params = {
['content_type']: 'organization',
include: 10,
'fields.title[match]': title,
}

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

return (
(result.items as types.IOrganization[]).map(mapOrganization)[0] ?? null
)
): Promise<Organization | null> {
return this.getOrganizationBy('title[match]', title, lang)
}

async getOrganizationByReferenceId(
referenceId: string,
lang: string,
): Promise<Organization> {
const params = {
['content_type']: 'organization',
include: 10,
'fields.referenceIdentifier': referenceId,
}

const result = await this.contentfulRepository
.getLocalizedEntries<types.IOrganizationFields>(lang, params)
.catch(errorHandler('getOrganization'))
): Promise<Organization | null> {
return this.getOrganizationBy('referenceIdentifier', referenceId, lang)
}

return (
(result.items as types.IOrganization[]).map(mapOrganization)[0] ?? null
)
async getOrganizationByNationalId(
nationalId: string,
lang: string,
): Promise<Organization | null> {
return this.getOrganizationBy('kennitala', nationalId, lang, true)
}

async getOrganizationPage(
Expand Down

0 comments on commit 4a229a4

Please sign in to comment.