mirror of
https://github.com/Unleash/unleash.git
synced 2025-02-23 00:22:19 +01:00
create splash migration store and service file
This commit is contained in:
parent
09eb689355
commit
875fb7734c
111
src/lib/db/user-splash-store.ts
Normal file
111
src/lib/db/user-splash-store.ts
Normal file
@ -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<IUserSplash[]> {
|
||||
const userSplash = await this.db
|
||||
.table<IUserSplashTable>(TABLE)
|
||||
.select()
|
||||
.where({ user_id: userId });
|
||||
|
||||
return userSplash.map(rowToField);
|
||||
}
|
||||
|
||||
async getSplash(
|
||||
userId: number,
|
||||
splashId: string,
|
||||
): Promise<IUserSplash> {
|
||||
const userSplash = await this.db
|
||||
.table<IUserSplashTable>(TABLE)
|
||||
.select()
|
||||
.where({ user_id: userId, splash_id: splashId })
|
||||
.first();
|
||||
|
||||
return rowToField(userSplash);
|
||||
}
|
||||
|
||||
async updateSplash(splash: IUserSplash): Promise<IUserSplash> {
|
||||
const insertedSplash = await this.db
|
||||
.table<IUserSplashTable>(TABLE)
|
||||
.insert(fieldToRow(splash))
|
||||
.onConflict(['user_id', 'splash_id'])
|
||||
.merge()
|
||||
.returning(COLUMNS);
|
||||
|
||||
return rowToField(insertedSplash[0]);
|
||||
}
|
||||
|
||||
async delete({ userId, splashId }: IUserSplashKey): Promise<void> {
|
||||
await this.db(TABLE)
|
||||
.where({ user_id: userId, splash_id: splashId })
|
||||
.del();
|
||||
}
|
||||
|
||||
async deleteAll(): Promise<void> {
|
||||
await this.db(TABLE).del();
|
||||
}
|
||||
|
||||
destroy(): void {}
|
||||
|
||||
async exists({ userId, splashId }: IUserSplashKey): Promise<boolean> {
|
||||
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<IUserSplash> {
|
||||
return this.getSplash(userId, splashId);
|
||||
}
|
||||
|
||||
async getAll(): Promise<IUserSplash[]> {
|
||||
const userSplashs = await this.db
|
||||
.table<IUserSplashTable>(TABLE)
|
||||
.select();
|
||||
|
||||
return userSplashs.map(rowToField);
|
||||
}
|
||||
}
|
||||
|
||||
module.exports = UserSplashStore;
|
47
src/lib/services/user-splash-service.ts
Normal file
47
src/lib/services/user-splash-service.ts
Normal file
@ -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<IUnleashStores, 'userSplashStore'>,
|
||||
{ getLogger }: Pick<IUnleashConfig, 'getLogger'>,
|
||||
) {
|
||||
this.userSplashStore = userSplashStore;
|
||||
this.logger = getLogger('services/user-splash-service.js');
|
||||
}
|
||||
|
||||
async getAllUserSplash(user: User): Promise<IUserSplash[]> {
|
||||
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<IUserSplash> {
|
||||
return this.userSplashStore.getSplash(user_id, splash_id);
|
||||
}
|
||||
|
||||
async updateSplash(splash: IUserSplash): Promise<IUserSplash> {
|
||||
return this.userSplashStore.updateSplash(splash);
|
||||
}
|
||||
}
|
||||
|
||||
module.exports = UserSplashService;
|
@ -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;
|
||||
}
|
||||
|
19
src/lib/types/stores/user-splash-store.ts
Normal file
19
src/lib/types/stores/user-splash-store.ts
Normal file
@ -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<IUserSplash, IUserSplashKey> {
|
||||
getAllUserSplashs(userId: number): Promise<IUserSplash[]>;
|
||||
getSplash(userId: number, splashId: string): Promise<IUserSplash>;
|
||||
updateSplash(splash: IUserSplash): Promise<IUserSplash>;
|
||||
}
|
25
src/migrations/20211108130333-create-user-splash-table.js
Normal file
25
src/migrations/20211108130333-create-user-splash-table.js
Normal file
@ -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,
|
||||
);
|
||||
};
|
@ -8,7 +8,7 @@ process.nextTick(async () => {
|
||||
createConfig({
|
||||
db: {
|
||||
user: 'unleash_user',
|
||||
password: 'passord',
|
||||
password: 'some_password',
|
||||
host: 'localhost',
|
||||
port: 5432,
|
||||
database: 'unleash',
|
||||
|
Loading…
Reference in New Issue
Block a user