diff --git a/src/lib/db/context-field-store.ts b/src/lib/db/context-field-store.ts index 89e77f024e..ab578bde3c 100644 --- a/src/lib/db/context-field-store.ts +++ b/src/lib/db/context-field-store.ts @@ -2,6 +2,7 @@ import { Knex } from 'knex'; import { Logger, LogProvider } from '../logger'; import { IContextField, + IContextFieldDto, IContextFieldStore, } from '../types/stores/context-field-store'; @@ -29,7 +30,7 @@ interface ICreateContextField { description: string; stickiness: boolean; sort_order: number; - legal_values?: string[]; + legal_values?: string; updated_at: Date; } @@ -43,15 +44,15 @@ class ContextFieldStore implements IContextFieldStore { this.logger = getLogger('context-field-store.ts'); } - // eslint-disable-next-line @typescript-eslint/explicit-module-boundary-types - fieldToRow(data): ICreateContextField { + fieldToRow( + data: IContextFieldDto, + ): Omit { return { name: data.name, description: data.description, stickiness: data.stickiness, sort_order: data.sortOrder, // eslint-disable-line - legal_values: data.legalValues ? data.legalValues.join(',') : null, // eslint-disable-line - updated_at: data.createdAt, // eslint-disable-line + legal_values: data.legalValues ? data.legalValues.join(',') : undefined, // eslint-disable-line }; } @@ -87,20 +88,19 @@ class ContextFieldStore implements IContextFieldStore { return present; } - // eslint-disable-next-line @typescript-eslint/explicit-module-boundary-types - async create(contextField): Promise { + async create(contextField: IContextFieldDto): Promise { const row = await this.db(TABLE) .insert(this.fieldToRow(contextField)) .returning('*'); return mapRow(row); } - // eslint-disable-next-line @typescript-eslint/explicit-module-boundary-types - async update(data): Promise { - const row = this.db(TABLE) + async update(data: IContextFieldDto): Promise { + const row = await this.db(TABLE) .where({ name: data.name }) .update(this.fieldToRow(data)) .returning('*'); + return mapRow(row); } @@ -109,4 +109,3 @@ class ContextFieldStore implements IContextFieldStore { } } export default ContextFieldStore; -module.exports = ContextFieldStore; diff --git a/src/test/e2e/api/admin/context.e2e.test.ts b/src/test/e2e/api/admin/context.e2e.test.ts index 5d7d15c622..b771b2fe7a 100644 --- a/src/test/e2e/api/admin/context.e2e.test.ts +++ b/src/test/e2e/api/admin/context.e2e.test.ts @@ -153,3 +153,29 @@ test('should validate name to not ok for non url-friendly', async () => { .set('Content-Type', 'application/json') .expect(400); }); + +test('should update context field with stickiness', async () => { + const name = 'with-sticky-update'; + await app.request + .post('/api/admin/context') + .send({ + name, + description: 'A context field supporting stickiness', + }) + .set('Content-Type', 'application/json'); + await app.request + .put(`/api/admin/context/${name}`) + .send({ + description: 'asd', + legalValues: [], + name, + stickiness: true, + }) + .set('Content-Type', 'application/json'); + + const res = await app.request.get(`/api/admin/context/${name}`); + const contextField = res.body; + + expect(contextField.description).toBe('asd'); + expect(contextField.stickiness).toBe(true); +});