generated from helsingborg-stad/gdi-template
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
tag-descriptions as 1:st class entities
- Loading branch information
Showing
7 changed files
with
133 additions
and
3 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,45 @@ | ||
import { uniqueBy } from '../lib' | ||
import type { TagDescription } from './types' | ||
|
||
const isString = (v: any) => typeof v === 'string' | ||
const isStringOrUndefined = (v: any) => | ||
typeof v === 'string' || typeof v === 'undefined' | ||
|
||
const empty: TagDescription = { tag: '', label: '', description: '' } | ||
|
||
// In ancient times, tags descriptions were stored as options as (tag, description) pairs | ||
const tryMigrateFromLegacyOptionsFormat = (v: any): TagDescription | null => | ||
isString(v?.key) && isString(v?.value) | ||
? { | ||
tag: v.key, | ||
label: '', | ||
description: v.value, | ||
} | ||
: null | ||
|
||
const tryNormalizeTagDescription = ( | ||
d?: Partial<TagDescription> | ||
): TagDescription | null => | ||
d && | ||
isString(d.tag) && | ||
isStringOrUndefined(d.label) && | ||
isStringOrUndefined(d.description) | ||
? { | ||
tag: d.tag?.trim() || '', | ||
label: d.label?.trim() || '', | ||
description: d.description?.trim() || '', | ||
} | ||
: null | ||
|
||
export const normalizeTagDescriptions = ( | ||
descriptions: Partial<TagDescription>[] | null | ||
): TagDescription[] => | ||
(descriptions || []) | ||
.map( | ||
d => | ||
tryMigrateFromLegacyOptionsFormat(d) || | ||
tryNormalizeTagDescription(d) || | ||
empty | ||
) | ||
.filter(d => d.tag && (d.label || d.description)) | ||
.filter(uniqueBy(d => d?.tag)) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
import type { SettingsService } from '../settings/types' | ||
import { normalizeTagDescriptions } from './mappers' | ||
import type { TagDescription } from './types' | ||
|
||
export const tagsAdapter = (settings: SettingsService) => ({ | ||
getTagDescriptions: () => | ||
settings | ||
.getSetting<TagDescription[]>('options-tag-descriptions') | ||
.then(normalizeTagDescriptions), | ||
updateTagDescriptions: (descriptions: Partial<TagDescription>[]) => | ||
settings | ||
.updateSetting( | ||
'options-tag-descriptions', | ||
normalizeTagDescriptions(descriptions) | ||
) | ||
.then(value => value), | ||
}) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
import HttpStatusCodes from 'http-status-codes' | ||
import type { Services } from '../types' | ||
import { normalizeRoles } from '../login' | ||
import { tagsGqlSchema } from './tags.gql.schema' | ||
import { tagsAdapter } from './tags-adapter' | ||
import type { GraphQLModule } from '../lib/gdi-api-node' | ||
|
||
export const createTagsGqlModule = ({ | ||
settings, | ||
}: Pick<Services, 'settings'>): GraphQLModule => ({ | ||
schema: tagsGqlSchema, | ||
resolvers: { | ||
Query: { | ||
// https://www.graphql-tools.com/docs/resolvers | ||
tagDescriptions: async () => tagsAdapter(settings).getTagDescriptions(), | ||
}, | ||
Mutation: { | ||
updateTagDescriptions: async ({ ctx, args: { input } }) => { | ||
const { user } = ctx | ||
if (!normalizeRoles(user?.roles).canEditTerms) { | ||
ctx.throw(HttpStatusCodes.UNAUTHORIZED) | ||
} | ||
return tagsAdapter(settings).updateTagDescriptions(input) | ||
}, | ||
}, | ||
}, | ||
}) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
export const tagsGqlSchema = /* GraphQL */ ` | ||
type Query { | ||
tagDescriptions: [TagDescription] | ||
} | ||
type Mutation { | ||
updateTagDescriptions(input: [TagDescriptionInput]!): [TagDescription] | ||
} | ||
type TagDescription { | ||
tag: String! | ||
label: String! | ||
description: String! | ||
} | ||
input TagDescriptionInput { | ||
tag: String! | ||
label: String! | ||
description: String! | ||
} | ||
` |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
export interface TagDescription { | ||
tag: string | ||
label: string | ||
description: string | ||
} |