mirror of
https://github.com/Unleash/unleash.git
synced 2025-07-21 13:47:39 +02:00
51 lines
1.4 KiB
TypeScript
51 lines
1.4 KiB
TypeScript
import type { Logger, LogProvider } from '../../logger';
|
|
import type { Db } from '../../db/db';
|
|
import type {
|
|
UnsubscribeEntry,
|
|
IUserUnsubscribeStore,
|
|
} from './user-unsubscribe-store-type';
|
|
|
|
const COLUMNS = ['user_id', 'subscription', 'created_at'];
|
|
export const TABLE = 'user_unsubscription';
|
|
|
|
interface IUserUnsubscribeTable {
|
|
user_id: number;
|
|
subscription: string;
|
|
created_at?: Date;
|
|
}
|
|
|
|
const rowToField = (row: IUserUnsubscribeTable): UnsubscribeEntry => ({
|
|
userId: row.user_id,
|
|
subscription: row.subscription,
|
|
createdAt: row.created_at,
|
|
});
|
|
|
|
export class UserUnsubscribeStore implements IUserUnsubscribeStore {
|
|
private db: Db;
|
|
|
|
private logger: Logger;
|
|
|
|
constructor(db: Db, getLogger: LogProvider) {
|
|
this.db = db;
|
|
this.logger = getLogger('user-unsubscribe-store.ts');
|
|
}
|
|
|
|
async insert({ userId, subscription }) {
|
|
await this.db
|
|
.table<IUserUnsubscribeTable>(TABLE)
|
|
.insert({ user_id: userId, subscription: subscription })
|
|
.onConflict(['user_id', 'subscription'])
|
|
.ignore()
|
|
.returning(COLUMNS);
|
|
}
|
|
|
|
async delete({ userId, subscription }): Promise<void> {
|
|
await this.db
|
|
.table<IUserUnsubscribeTable>(TABLE)
|
|
.where({ user_id: userId, subscription: subscription })
|
|
.del();
|
|
}
|
|
|
|
destroy(): void {}
|
|
}
|