2022-09-16 09:54:27 +02:00
|
|
|
import { Knex } from 'knex';
|
|
|
|
import { Logger, LogProvider } from '../logger';
|
|
|
|
import { IPatStore } from '../types/stores/pat-store';
|
|
|
|
import Pat, { IPat } from '../types/models/pat';
|
|
|
|
import NotFoundError from '../error/notfound-error';
|
|
|
|
|
|
|
|
const TABLE = 'personal_access_tokens';
|
|
|
|
|
2022-10-10 12:35:12 +02:00
|
|
|
const PAT_PUBLIC_COLUMNS = [
|
|
|
|
'id',
|
2022-09-26 14:42:39 +02:00
|
|
|
'description',
|
2022-09-16 09:54:27 +02:00
|
|
|
'user_id',
|
|
|
|
'expires_at',
|
|
|
|
'created_at',
|
|
|
|
'seen_at',
|
|
|
|
];
|
|
|
|
|
|
|
|
const fromRow = (row) => {
|
|
|
|
if (!row) {
|
|
|
|
throw new NotFoundError('No PAT found');
|
|
|
|
}
|
|
|
|
return new Pat({
|
2022-10-10 12:35:12 +02:00
|
|
|
id: row.id,
|
2022-09-16 09:54:27 +02:00
|
|
|
secret: row.secret,
|
|
|
|
userId: row.user_id,
|
2022-09-26 14:42:39 +02:00
|
|
|
description: row.description,
|
2022-09-16 09:54:27 +02:00
|
|
|
createdAt: row.created_at,
|
|
|
|
seenAt: row.seen_at,
|
|
|
|
expiresAt: row.expires_at,
|
|
|
|
});
|
|
|
|
};
|
|
|
|
|
2022-09-26 14:42:39 +02:00
|
|
|
const toRow = (pat: IPat) => ({
|
|
|
|
secret: pat.secret,
|
|
|
|
description: pat.description,
|
|
|
|
user_id: pat.userId,
|
|
|
|
expires_at: pat.expiresAt,
|
2022-09-16 09:54:27 +02:00
|
|
|
});
|
|
|
|
|
|
|
|
export default class PatStore implements IPatStore {
|
|
|
|
private db: Knex;
|
|
|
|
|
|
|
|
private logger: Logger;
|
|
|
|
|
|
|
|
constructor(db: Knex, getLogger: LogProvider) {
|
|
|
|
this.db = db;
|
|
|
|
this.logger = getLogger('pat-store.ts');
|
|
|
|
}
|
|
|
|
|
|
|
|
async create(token: IPat): Promise<IPat> {
|
|
|
|
const row = await this.db(TABLE).insert(toRow(token)).returning('*');
|
|
|
|
return fromRow(row[0]);
|
|
|
|
}
|
|
|
|
|
2022-10-10 12:35:12 +02:00
|
|
|
async delete(id: number): Promise<void> {
|
|
|
|
return this.db(TABLE).where({ id: id }).del();
|
|
|
|
}
|
|
|
|
|
|
|
|
async deleteForUser(id: number, userId: number): Promise<void> {
|
|
|
|
return this.db(TABLE).where({ id: id, user_id: userId }).del();
|
2022-09-16 09:54:27 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
async deleteAll(): Promise<void> {
|
|
|
|
await this.db(TABLE).del();
|
|
|
|
}
|
|
|
|
|
|
|
|
destroy(): void {}
|
|
|
|
|
2022-10-10 12:35:12 +02:00
|
|
|
async exists(id: number): Promise<boolean> {
|
2022-09-16 09:54:27 +02:00
|
|
|
const result = await this.db.raw(
|
2022-10-10 12:35:12 +02:00
|
|
|
`SELECT EXISTS(SELECT 1 FROM ${TABLE} WHERE id = ?) AS present`,
|
|
|
|
[id],
|
2022-09-16 09:54:27 +02:00
|
|
|
);
|
|
|
|
const { present } = result.rows[0];
|
|
|
|
return present;
|
|
|
|
}
|
|
|
|
|
2022-10-14 14:28:29 +02:00
|
|
|
async existsWithDescriptionByUser(
|
|
|
|
description: string,
|
|
|
|
userId: number,
|
|
|
|
): Promise<boolean> {
|
|
|
|
const result = await this.db.raw(
|
|
|
|
`SELECT EXISTS(SELECT 1 FROM ${TABLE} WHERE description = ? AND user_id = ?) AS present`,
|
|
|
|
[description, userId],
|
|
|
|
);
|
|
|
|
const { present } = result.rows[0];
|
|
|
|
return present;
|
|
|
|
}
|
|
|
|
|
2022-10-10 12:35:12 +02:00
|
|
|
async get(id: number): Promise<Pat> {
|
|
|
|
const row = await this.db(TABLE).where({ id }).first();
|
2022-09-16 09:54:27 +02:00
|
|
|
return fromRow(row);
|
|
|
|
}
|
|
|
|
|
|
|
|
async getAll(): Promise<Pat[]> {
|
2022-10-10 12:35:12 +02:00
|
|
|
const groups = await this.db.select(PAT_PUBLIC_COLUMNS).from(TABLE);
|
2022-09-16 09:54:27 +02:00
|
|
|
return groups.map(fromRow);
|
|
|
|
}
|
|
|
|
|
|
|
|
async getAllByUser(userId: number): Promise<Pat[]> {
|
|
|
|
const groups = await this.db
|
2022-10-10 12:35:12 +02:00
|
|
|
.select(PAT_PUBLIC_COLUMNS)
|
2022-09-16 09:54:27 +02:00
|
|
|
.from(TABLE)
|
|
|
|
.where('user_id', userId);
|
|
|
|
return groups.map(fromRow);
|
|
|
|
}
|
|
|
|
}
|