diff --git a/src/lib/db/feature-type-store.ts b/src/lib/db/feature-type-store.ts index 9415ab4f8d..3f4fb32511 100644 --- a/src/lib/db/feature-type-store.ts +++ b/src/lib/db/feature-type-store.ts @@ -67,6 +67,22 @@ class FeatureTypeStore implements IFeatureTypeStore { const { present } = result.rows[0]; return present; } + + async updateLifetime( + name: string, + newLifetimeDays: number | null, + ): Promise { + const [updatedType] = await this.db(TABLE) + .update({ lifetime_days: newLifetimeDays }) + .where({ name }) + .returning(['*']); + + if (updatedType) { + return this.rowToFeatureType(updatedType); + } else { + return undefined; + } + } } export default FeatureTypeStore; module.exports = FeatureTypeStore; diff --git a/src/lib/types/stores/feature-type-store.ts b/src/lib/types/stores/feature-type-store.ts index dfc34e43e3..24ac6b923e 100644 --- a/src/lib/types/stores/feature-type-store.ts +++ b/src/lib/types/stores/feature-type-store.ts @@ -9,4 +9,8 @@ export interface IFeatureType { export interface IFeatureTypeStore extends Store { getByName(name: string): Promise; + updateLifetime( + name: string, + newLifetimeDays: number | null, + ): Promise; } diff --git a/src/test/e2e/stores/feature-type-store.e2e.test.ts b/src/test/e2e/stores/feature-type-store.e2e.test.ts index e7da2e08d3..bf90fcc6c8 100644 --- a/src/test/e2e/stores/feature-type-store.e2e.test.ts +++ b/src/test/e2e/stores/feature-type-store.e2e.test.ts @@ -39,3 +39,28 @@ test('should be possible to delete by id', async () => { const typesAfterDelete = await featureTypeStore.getAll(); expect(typesAfterDelete.length).toBe(4); }); + +describe('update lifetimes', () => { + test.each([null, 5])('it sets lifetimeDays to %s', async (newLifetime) => { + const featureTypes = await featureTypeStore.getAll(); + + for (const type of featureTypes) { + const updated = await featureTypeStore.updateLifetime( + type.name, + newLifetime, + ); + + expect(updated?.lifetimeDays).toBe(newLifetime); + + expect(updated).toMatchObject( + await featureTypeStore.getByName(type.name), + ); + } + }); + + test("It returns undefined if you try to update a feature type that doesn't exist", async () => { + expect( + await featureTypeStore.updateLifetime('bogus-type', 40), + ).toBeUndefined(); + }); +}); diff --git a/src/test/fixtures/fake-feature-type-store.ts b/src/test/fixtures/fake-feature-type-store.ts index 028f700cb3..2e40e63f06 100644 --- a/src/test/fixtures/fake-feature-type-store.ts +++ b/src/test/fixtures/fake-feature-type-store.ts @@ -45,4 +45,18 @@ export default class FakeFeatureTypeStore implements IFeatureTypeStore { `Could not find feature type with name: ${name}`, ); } + + async updateLifetime( + name: string, + newLifetimeDays: number | null, + ): Promise { + const featureType = this.featureTypes.find( + ({ name: type }) => type === name, + ); + if (!featureType) { + return undefined; + } + featureType.lifetimeDays = newLifetimeDays; + return featureType; + } }