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.
- Loading branch information
Petter Andersson
committed
Dec 13, 2023
1 parent
e25eea9
commit 80d77c9
Showing
8 changed files
with
176 additions
and
2 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
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) | ||
}, | ||
}, | ||
}, | ||
}) |
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,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 | ||
} | ||
` |
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,3 @@ | ||
import { createAdvertFieldConfigGqlModule } from './config-gql-module' | ||
|
||
export { createAdvertFieldConfigGqlModule } |
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 { 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 }, | ||
]) | ||
}) |
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,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), | ||
}) |
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,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[] |
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