Skip to content

Commit

Permalink
add advertconfig service
Browse files Browse the repository at this point in the history
  • Loading branch information
Petter Andersson committed Dec 13, 2023
1 parent e25eea9 commit 80d77c9
Show file tree
Hide file tree
Showing 8 changed files with 176 additions and 2 deletions.
27 changes: 27 additions & 0 deletions src/advert-field-config/config-gql-module.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
import HttpStatusCodes from 'http-status-codes'
import type { GraphQLModule } from '@helsingborg-stad/gdi-api-node'
import { advertFieldConfigGqlSchema } from './config.gql.schema'
import type { Services } from '../types'
import { normalizeRoles } from '../login'
import { advertFieldConfigAdapter } from './mappers'

export const createAdvertFieldConfigGqlModule = ({
settings,
}: Pick<Services, 'settings'>): GraphQLModule => ({
schema: advertFieldConfigGqlSchema,
resolvers: {
Query: {
advertFieldConfig: () =>
advertFieldConfigAdapter(settings).getFieldConfig(),
},
Mutation: {
updateFieldConfig: async ({ ctx, args: { input } }) => {
const { user } = ctx
if (!normalizeRoles(user?.roles).canEditTerms) {
ctx.throw(HttpStatusCodes.UNAUTHORIZED)
}
return advertFieldConfigAdapter(settings).updateFieldConfig(input)
},
},
},
})
20 changes: 20 additions & 0 deletions src/advert-field-config/config.gql.schema.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
export const advertFieldConfigGqlSchema = /* GraphQL */ `
type Query {
advertFieldConfig: [FieldConfig]
}
type Mutation {
updateFieldConfig(input: [FieldConfigInput!]): [FieldConfig]
}
type FieldConfig {
name: String
visible: Boolean
mandatory: Boolean
}
input FieldConfigInput {
name: String
visible: Boolean
mandatory: Boolean
}
`
3 changes: 3 additions & 0 deletions src/advert-field-config/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
import { createAdvertFieldConfigGqlModule } from './config-gql-module'

export { createAdvertFieldConfigGqlModule }
45 changes: 45 additions & 0 deletions src/advert-field-config/mappers.spec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
import { normalizeFieldConfig } from './mappers'

it('should override default values with input', () => {
const output = normalizeFieldConfig([
{ name: 'tags', visible: false, mandatory: true },
{ name: 'material', visible: false, mandatory: false },
{ name: 'width', visible: true, mandatory: true },
])
expect(output).toEqual([
{ name: 'description', visible: true, mandatory: false },
{ name: 'unit', visible: true, mandatory: false },
{ name: 'width', visible: true, mandatory: true },
{ name: 'height', visible: true, mandatory: false },
{ name: 'depth', visible: true, mandatory: false },
{ name: 'weight', visible: true, mandatory: false },
{ name: 'size', visible: true, mandatory: false },
{ name: 'material', visible: false, mandatory: false },
{ name: 'condition', visible: true, mandatory: false },
{ name: 'usage', visible: true, mandatory: false },
{ name: 'category', visible: true, mandatory: false },
{ name: 'reference', visible: true, mandatory: false },
{ name: 'organization', visible: true, mandatory: false },
{ name: 'tags', visible: false, mandatory: true },
])
})

it('should handle null document', () => {
const output = normalizeFieldConfig(null)
expect(output).toEqual([
{ name: 'description', visible: true, mandatory: false },
{ name: 'unit', visible: true, mandatory: false },
{ name: 'width', visible: true, mandatory: false },
{ name: 'height', visible: true, mandatory: false },
{ name: 'depth', visible: true, mandatory: false },
{ name: 'weight', visible: true, mandatory: false },
{ name: 'size', visible: true, mandatory: false },
{ name: 'material', visible: true, mandatory: false },
{ name: 'condition', visible: true, mandatory: false },
{ name: 'usage', visible: true, mandatory: false },
{ name: 'category', visible: true, mandatory: false },
{ name: 'reference', visible: true, mandatory: false },
{ name: 'organization', visible: true, mandatory: false },
{ name: 'tags', visible: true, mandatory: false },
])
})
35 changes: 35 additions & 0 deletions src/advert-field-config/mappers.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
import type { SettingsService } from '../settings/types'
import type { AdvertFieldConfig, FieldConfig } from './types'
import { ConfigurableFields } from './types'

const createEmptyConfiguration = (): AdvertFieldConfig =>
ConfigurableFields.map(name => ({
name,
visible: true,
mandatory: false,
}))

export const normalizeFieldConfig = (fieldConfig: AdvertFieldConfig | null) => {
const fieldList = [...createEmptyConfiguration(), ...(fieldConfig || [])]

const mapper = new Map<string, FieldConfig>()

fieldList.forEach(f => {
mapper.set(f.name, {
...(mapper.get(f.name) || {}),
...f,
})
})
return Array.from(mapper.values())
}

export const advertFieldConfigAdapter = (settings: SettingsService) => ({
getFieldConfig: () =>
settings
.getSetting<AdvertFieldConfig>('advert-field-config')
.then(normalizeFieldConfig),
updateFieldConfig: (fieldConfig: AdvertFieldConfig) =>
settings
.updateSetting('advert-field-config', fieldConfig)
.then(normalizeFieldConfig),
})
43 changes: 43 additions & 0 deletions src/advert-field-config/types.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
import type { Advert, AdvertContact } from '../adverts/types'

export type Fields = Pick<
Advert & AdvertContact,
| 'description'
| 'unit'
| 'width'
| 'height'
| 'depth'
| 'weight'
| 'size'
| 'material'
| 'condition'
| 'usage'
| 'category'
| 'reference'
| 'organization'
| 'tags'
>

export const ConfigurableFields: Array<keyof Fields> = [
'description',
'unit',
'width',
'height',
'depth',
'weight',
'size',
'material',
'condition',
'usage',
'category',
'reference',
'organization',
'tags',
]

export interface FieldConfig {
name: keyof Fields
visible: boolean
mandatory: boolean
}
export type AdvertFieldConfig = FieldConfig[]
1 change: 0 additions & 1 deletion src/content/mappers.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
import type { FilesService } from '../files/types'
import type { ContentModule, ViewComposition } from './types'

export const createEmptyModule = (): ContentModule => ({
Expand Down
4 changes: 3 additions & 1 deletion src/haffa/haffa-gql-module.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ import { createEventsGqlModule } from '../events/events-gql-module'
import { createSubscriptionsGqlModule } from '../subscriptions/subscriptions-gql-module'
import { haffaGqlSchema } from './haffa.gql.schema'
import { createContentGqlModule } from '../content/content-gql-module'
import { createAdvertFieldConfigGqlModule } from '../advert-field-config'

export const createStandardGqlModule = (): GraphQLModule => ({
schema: haffaGqlSchema,
Expand All @@ -34,7 +35,8 @@ export const createHaffaGqlModule = (services: Services): GraphQLModule =>
createOptionsGqlModule(services),
createEventsGqlModule(services),
createSubscriptionsGqlModule(services),
createContentGqlModule(services)
createContentGqlModule(services),
createAdvertFieldConfigGqlModule(services)
)

const mergeModules = (...modules: GraphQLModule[]): GraphQLModule => ({
Expand Down

0 comments on commit 80d77c9

Please sign in to comment.