diff --git a/src/lib/db/user-splash-store.ts b/src/lib/db/user-splash-store.ts new file mode 100644 index 0000000000..77ddbc1c05 --- /dev/null +++ b/src/lib/db/user-splash-store.ts @@ -0,0 +1,111 @@ +import { Knex } from 'knex'; +import { EventEmitter } from 'events'; +import { LogProvider, Logger } from '../logger'; +import { + IUserSplash, + IUserSplashKey, + IUserSplashStore, +} from '../types/stores/user-splash-store'; + +const COLUMNS = ['user_id', 'splash_id', 'seen']; +const TABLE = 'user_splash'; + +interface IUserSplashTable { + seen?: boolean; + splash_id: string; + user_id: number; +} + +const fieldToRow = (fields: IUserSplash): IUserSplashTable => ({ + seen: fields.seen, + splash_id: fields.splashId, + user_id: fields.userId, +}); + +const rowToField = (row: IUserSplashTable): IUserSplash => ({ + seen: row.seen, + splashId: row.splash_id, + userId: row.user_id, +}); + +export default class UserSplashStore implements IUserSplashStore { + private db: Knex; + + private logger: Logger; + + constructor(db: Knex, eventBus: EventEmitter, getLogger: LogProvider) { + this.db = db; + this.logger = getLogger('user-splash-store.ts'); + } + + async getAllUserSplashs(userId: number): Promise { + const userSplash = await this.db + .table(TABLE) + .select() + .where({ user_id: userId }); + + return userSplash.map(rowToField); + } + + async getSplash( + userId: number, + splashId: string, + ): Promise { + const userSplash = await this.db + .table(TABLE) + .select() + .where({ user_id: userId, splash_id: splashId }) + .first(); + + return rowToField(userSplash); + } + + async updateSplash(splash: IUserSplash): Promise { + const insertedSplash = await this.db + .table(TABLE) + .insert(fieldToRow(splash)) + .onConflict(['user_id', 'splash_id']) + .merge() + .returning(COLUMNS); + + return rowToField(insertedSplash[0]); + } + + async delete({ userId, splashId }: IUserSplashKey): Promise { + await this.db(TABLE) + .where({ user_id: userId, splash_id: splashId }) + .del(); + } + + async deleteAll(): Promise { + await this.db(TABLE).del(); + } + + destroy(): void {} + + async exists({ userId, splashId }: IUserSplashKey): Promise { + const result = await this.db.raw( + `SELECT EXISTS (SELECT 1 FROM ${TABLE} WHERE user_id = ? AND splash_id = ?) AS present`, + [userId, splashId], + ); + const { present } = result.rows[0]; + return present; + } + + async get({ + userId, + splashId, + }: IUserSplashKey): Promise { + return this.getSplash(userId, splashId); + } + + async getAll(): Promise { + const userSplashs = await this.db + .table(TABLE) + .select(); + + return userSplashs.map(rowToField); + } +} + +module.exports = UserSplashStore; diff --git a/src/lib/services/user-splash-service.ts b/src/lib/services/user-splash-service.ts new file mode 100644 index 0000000000..a86b44f009 --- /dev/null +++ b/src/lib/services/user-splash-service.ts @@ -0,0 +1,47 @@ +import { Logger } from '../logger'; +import { IUnleashStores } from '../types/stores'; +import { IUnleashConfig } from '../types/option'; +import User from '../types/user'; +import { + IUserSplash, + IUserSplashStore, +} from '../types/stores/user-splash-store'; + +export default class UserSplashService { + private userSplashStore: IUserSplashStore; + + private logger: Logger; + + constructor( + { userSplashStore }: Pick, + { getLogger }: Pick, + ) { + this.userSplashStore = userSplashStore; + this.logger = getLogger('services/user-splash-service.js'); + } + + async getAllUserSplash(user: User): Promise { + if (user.isAPI) { + return []; + } + try { + return await this.userSplashStore.getAllUserSplashs(user.id); + } catch (err) { + this.logger.error(err); + return []; + } + } + + async getSplash( + user_id: number, + splash_id: string, + ): Promise { + return this.userSplashStore.getSplash(user_id, splash_id); + } + + async updateSplash(splash: IUserSplash): Promise { + return this.userSplashStore.updateSplash(splash); + } +} + +module.exports = UserSplashService; diff --git a/src/lib/types/stores.ts b/src/lib/types/stores.ts index ff22f24b3f..ff09b606a5 100644 --- a/src/lib/types/stores.ts +++ b/src/lib/types/stores.ts @@ -23,6 +23,8 @@ import { IFeatureStrategiesStore } from './stores/feature-strategies-store'; import { IEnvironmentStore } from './stores/environment-store'; import { IFeatureToggleClientStore } from './stores/feature-toggle-client-store'; import { IClientMetricsStoreV2 } from './stores/client-metrics-store-v2'; +import { IUserSplashStore } from './stores/user-splash-store'; + export interface IUnleashStores { accessStore: IAccessStore; @@ -50,4 +52,5 @@ export interface IUnleashStores { tagTypeStore: ITagTypeStore; userFeedbackStore: IUserFeedbackStore; userStore: IUserStore; + userSplashStore: IUserSplashStore; } diff --git a/src/lib/types/stores/user-splash-store.ts b/src/lib/types/stores/user-splash-store.ts new file mode 100644 index 0000000000..ebe833e823 --- /dev/null +++ b/src/lib/types/stores/user-splash-store.ts @@ -0,0 +1,19 @@ +import { Store } from './store'; + +export interface IUserSplash { + seen?: boolean; + splashId: string; + userId: number; +} + +export interface IUserSplashKey { + userId: number; + splashId: string; +} + +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 new file mode 100644 index 0000000000..067ec76bd3 --- /dev/null +++ b/src/migrations/20211108130333-create-user-splash-table.js @@ -0,0 +1,25 @@ +'use strict'; + +exports.up = function (db, cb) { + db.runSql( + ` + CREATE TABLE IF NOT EXISTS user_splash + (user_id INTEGER NOT NULL references users (id), + 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); + );`, + cb, + ); +}; + +exports.down = function (db, cb) { + db.runSql( + ` + DROP INDEX user_splash_user_id_idx; + DROP TABLE user_splash; +`, + cb, +); +}; diff --git a/src/server-dev.ts b/src/server-dev.ts index bf1cea9c95..34d7b1aec2 100644 --- a/src/server-dev.ts +++ b/src/server-dev.ts @@ -8,7 +8,7 @@ process.nextTick(async () => { createConfig({ db: { user: 'unleash_user', - password: 'passord', + password: 'some_password', host: 'localhost', port: 5432, database: 'unleash',