From d73293c5762d9667571b3819efef79fa1e013629 Mon Sep 17 00:00:00 2001 From: Fredrik Oseberg Date: Tue, 9 Nov 2021 11:52:02 +0100 Subject: [PATCH] fix: add migration --- src/lib/db/index.ts | 2 + src/lib/db/user-splash-store.ts | 10 +-- src/lib/routes/admin-api/user.ts | 6 ++ src/lib/services/index.ts | 3 + src/lib/services/user-splash-service.ts | 6 +- src/lib/types/services.ts | 2 + src/lib/types/stores/user-splash-store.ts | 5 +- ...20211108130333-create-user-splash-table.js | 11 ++-- ...211109103930-add-splash-entry-for-users.js | 15 +++++ src/server-dev.ts | 2 +- src/test/fixtures/fake-user-splash-store.ts | 65 +++++++++++++++++++ src/test/fixtures/store.ts | 2 + 12 files changed, 107 insertions(+), 22 deletions(-) create mode 100644 src/migrations/20211109103930-add-splash-entry-for-users.js create mode 100644 src/test/fixtures/fake-user-splash-store.ts diff --git a/src/lib/db/index.ts b/src/lib/db/index.ts index a7d0c07d04..fbe9cec78f 100644 --- a/src/lib/db/index.ts +++ b/src/lib/db/index.ts @@ -29,6 +29,7 @@ import EnvironmentStore from './environment-store'; import FeatureTagStore from './feature-tag-store'; import { FeatureEnvironmentStore } from './feature-environment-store'; import { ClientMetricsStoreV2 } from './client-metrics-store-v2'; +import UserSplashStore from './user-splash-store'; export const createStores = ( config: IUnleashConfig, @@ -85,6 +86,7 @@ export const createStores = ( eventBus, getLogger, ), + userSplashStore: new UserSplashStore(db, eventBus, getLogger), }; }; diff --git a/src/lib/db/user-splash-store.ts b/src/lib/db/user-splash-store.ts index 77ddbc1c05..6b867ee4d7 100644 --- a/src/lib/db/user-splash-store.ts +++ b/src/lib/db/user-splash-store.ts @@ -47,10 +47,7 @@ export default class UserSplashStore implements IUserSplashStore { return userSplash.map(rowToField); } - async getSplash( - userId: number, - splashId: string, - ): Promise { + async getSplash(userId: number, splashId: string): Promise { const userSplash = await this.db .table(TABLE) .select() @@ -92,10 +89,7 @@ export default class UserSplashStore implements IUserSplashStore { return present; } - async get({ - userId, - splashId, - }: IUserSplashKey): Promise { + async get({ userId, splashId }: IUserSplashKey): Promise { return this.getSplash(userId, splashId); } diff --git a/src/lib/routes/admin-api/user.ts b/src/lib/routes/admin-api/user.ts index 6b5299311f..1d2815a9c6 100644 --- a/src/lib/routes/admin-api/user.ts +++ b/src/lib/routes/admin-api/user.ts @@ -7,6 +7,7 @@ import { IUnleashServices } from '../../types/services'; import UserService from '../../services/user-service'; import SessionService from '../../services/session-service'; import UserFeedbackService from '../../services/user-feedback-service'; +import UserSplashService from 'lib/services/user-splash-service'; interface IChangeUserRequest { password: string; @@ -22,6 +23,8 @@ class UserController extends Controller { private sessionService: SessionService; + private userSplashService: UserSplashService; + constructor( config: IUnleashConfig, { @@ -29,12 +32,14 @@ class UserController extends Controller { userService, sessionService, userFeedbackService, + userSplashService, }: Pick< IUnleashServices, | 'accessService' | 'userService' | 'sessionService' | 'userFeedbackService' + | 'userSplashService' >, ) { super(config); @@ -57,6 +62,7 @@ class UserController extends Controller { const feedback = await this.userFeedbackService.getAllUserFeedback( user, ); + //const splash = await this.userSplashService.getAllUserSplash(user); // TODO: remove this line after we remove it from db. delete user.permissions; diff --git a/src/lib/services/index.ts b/src/lib/services/index.ts index 8e39e913cb..5d3ddc3e95 100644 --- a/src/lib/services/index.ts +++ b/src/lib/services/index.ts @@ -27,6 +27,7 @@ import FeatureToggleService from './feature-toggle-service'; import EnvironmentService from './environment-service'; import FeatureTagService from './feature-tag-service'; import ProjectHealthService from './project-health-service'; +import UserSplashService from './user-splash-service'; export const createServices = ( stores: IUnleashStores, @@ -72,6 +73,7 @@ export const createServices = ( accessService, featureToggleServiceV2, ); + const userSplashService = new UserSplashService(stores, config); return { accessService, @@ -100,6 +102,7 @@ export const createServices = ( userFeedbackService, featureTagService, projectHealthService, + userSplashService, }; }; diff --git a/src/lib/services/user-splash-service.ts b/src/lib/services/user-splash-service.ts index a86b44f009..9dfb163dd2 100644 --- a/src/lib/services/user-splash-service.ts +++ b/src/lib/services/user-splash-service.ts @@ -28,14 +28,12 @@ export default class UserSplashService { return await this.userSplashStore.getAllUserSplashs(user.id); } catch (err) { this.logger.error(err); + return []; } } - async getSplash( - user_id: number, - splash_id: string, - ): Promise { + async getSplash(user_id: number, splash_id: string): Promise { return this.userSplashStore.getSplash(user_id, splash_id); } diff --git a/src/lib/types/services.ts b/src/lib/types/services.ts index b2528df9f8..52dab846bb 100644 --- a/src/lib/types/services.ts +++ b/src/lib/types/services.ts @@ -23,6 +23,7 @@ import EnvironmentService from '../services/environment-service'; import FeatureTagService from '../services/feature-tag-service'; import ProjectHealthService from '../services/project-health-service'; import ClientMetricsServiceV2 from '../services/client-metrics/client-metrics-service-v2'; +import UserSplashService from '../services/user-splash-service'; export interface IUnleashServices { accessService: AccessService; @@ -51,4 +52,5 @@ export interface IUnleashServices { userFeedbackService: UserFeedbackService; userService: UserService; versionService: VersionService; + userSplashService: UserSplashService; } diff --git a/src/lib/types/stores/user-splash-store.ts b/src/lib/types/stores/user-splash-store.ts index ebe833e823..75938981ef 100644 --- a/src/lib/types/stores/user-splash-store.ts +++ b/src/lib/types/stores/user-splash-store.ts @@ -1,7 +1,7 @@ import { Store } from './store'; export interface IUserSplash { - seen?: boolean; + seen: boolean; splashId: string; userId: number; } @@ -11,8 +11,7 @@ export interface IUserSplashKey { splashId: string; } -export interface IUserSplashStore - extends Store { +export interface IUserSplashStore extends Store { getAllUserSplashs(userId: number): Promise; getSplash(userId: number, splashId: string): Promise; updateSplash(splash: IUserSplash): Promise; diff --git a/src/migrations/20211108130333-create-user-splash-table.js b/src/migrations/20211108130333-create-user-splash-table.js index 067ec76bd3..21953415b3 100644 --- a/src/migrations/20211108130333-create-user-splash-table.js +++ b/src/migrations/20211108130333-create-user-splash-table.js @@ -8,18 +8,17 @@ exports.up = function (db, cb) { splash_id TEXT, seen BOOLEAN NOT NULL DEFAULT false, PRIMARY KEY (user_id, splash_id)); - CREATE INDEX user_splash_user_id_idx ON user_splash (user_id); - );`, + CREATE INDEX user_splash_user_id_idx ON user_splash (user_id);`, cb, ); }; exports.down = function (db, cb) { - db.runSql( - ` + db.runSql( + ` DROP INDEX user_splash_user_id_idx; DROP TABLE user_splash; `, - cb, -); + cb, + ); }; diff --git a/src/migrations/20211109103930-add-splash-entry-for-users.js b/src/migrations/20211109103930-add-splash-entry-for-users.js new file mode 100644 index 0000000000..8a34421415 --- /dev/null +++ b/src/migrations/20211109103930-add-splash-entry-for-users.js @@ -0,0 +1,15 @@ +exports.up = function (db, cb) { + db.runSql(`SELECT * FROM users`, (err, results) => { + results.rows.forEach((user) => { + db.runSql( + `INSERT INTO user_splash(splash_id, user_id, seen) VALUES (?, ?, ?)`, + ['environments', user.id, false], + ); + }); + cb(); + }); +}; + +exports.down = function (db, cb) { + db.runSql('DELETE FROM user_splash', cb); +}; diff --git a/src/server-dev.ts b/src/server-dev.ts index 34d7b1aec2..bf1cea9c95 100644 --- a/src/server-dev.ts +++ b/src/server-dev.ts @@ -8,7 +8,7 @@ process.nextTick(async () => { createConfig({ db: { user: 'unleash_user', - password: 'some_password', + password: 'passord', host: 'localhost', port: 5432, database: 'unleash', diff --git a/src/test/fixtures/fake-user-splash-store.ts b/src/test/fixtures/fake-user-splash-store.ts new file mode 100644 index 0000000000..2fb6af87b8 --- /dev/null +++ b/src/test/fixtures/fake-user-splash-store.ts @@ -0,0 +1,65 @@ +import { + IUserSplashKey, + IUserSplash, + IUserSplashStore, +} from 'lib/types/stores/user-splash-store'; + +export default class FakeUserSplashStore implements IUserSplashStore { + // eslint-disable-next-line @typescript-eslint/no-unused-vars + getAllUserSplashs(userId: number): Promise { + return Promise.resolve([]); + } + + // eslint-disable-next-line @typescript-eslint/no-unused-vars + getSplash(userId: number, splashId: string): Promise { + return Promise.resolve({ seen: false, userId: 123, splashId: 'env' }); + } + + // eslint-disable-next-line @typescript-eslint/no-unused-vars + updateSplash(splash: IUserSplash): Promise { + return Promise.resolve({ seen: false, userId: 123, splashId: 'env' }); + } + + // eslint-disable-next-line @typescript-eslint/no-unused-vars + exists(key: IUserSplashKey): Promise { + return Promise.resolve(false); + } + + // eslint-disable-next-line @typescript-eslint/no-unused-vars + get(key: IUserSplashKey): Promise { + return Promise.resolve(undefined); + } + + // eslint-disable-next-line @typescript-eslint/no-unused-vars + getAll(): Promise { + return Promise.resolve([]); + } + + // eslint-disable-next-line @typescript-eslint/no-unused-vars + getAllUserFeedback(userId: number): Promise { + return Promise.resolve([]); + } + + // eslint-disable-next-line @typescript-eslint/no-unused-vars + getFeedback(userId: number, feedbackId: string): Promise { + return Promise.resolve(undefined); + } + + // eslint-disable-next-line @typescript-eslint/no-unused-vars + updateFeedback(feedback: IUserSplash): Promise { + return Promise.resolve(undefined); + } + + // eslint-disable-next-line @typescript-eslint/no-unused-vars + delete(key: IUserSplashKey): Promise { + return Promise.resolve(undefined); + } + + // eslint-disable-next-line @typescript-eslint/no-unused-vars + deleteAll(): Promise { + return Promise.resolve(undefined); + } + + // eslint-disable-next-line @typescript-eslint/no-unused-vars + destroy(): void {} +} diff --git a/src/test/fixtures/store.ts b/src/test/fixtures/store.ts index 5e36e35ff0..6b6bf25d04 100644 --- a/src/test/fixtures/store.ts +++ b/src/test/fixtures/store.ts @@ -24,6 +24,7 @@ import FakeFeatureTypeStore from './fake-feature-type-store'; import FakeResetTokenStore from './fake-reset-token-store'; import FakeFeatureToggleClientStore from './fake-feature-toggle-client-store'; import FakeClientMetricsStoreV2 from './fake-client-metrics-store-v2'; +import FakeUserSplashStore from './fake-user-splash-store'; const createStores: () => IUnleashStores = () => { const db = { @@ -59,6 +60,7 @@ const createStores: () => IUnleashStores = () => { featureTypeStore: new FakeFeatureTypeStore(), resetTokenStore: new FakeResetTokenStore(), sessionStore: new FakeSessionStore(), + userSplashStore: new FakeUserSplashStore(), }; };