2022-09-16 09:54:27 +02:00
|
|
|
import { Logger, LogProvider } from '../logger';
|
|
|
|
import { IPatStore } from '../types/stores/pat-store';
|
|
|
|
import NotFoundError from '../error/notfound-error';
|
2023-01-30 09:02:44 +01:00
|
|
|
import { Db } from './db';
|
2024-02-01 15:28:46 +01:00
|
|
|
import { CreatePatSchema, PatSchema } from '../openapi';
|
2022-09-16 09:54:27 +02:00
|
|
|
|
|
|
|
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',
|
|
|
|
];
|
|
|
|
|
2024-02-01 15:28:46 +01:00
|
|
|
const rowToPat = ({
|
|
|
|
id,
|
|
|
|
description,
|
|
|
|
expires_at,
|
|
|
|
user_id,
|
|
|
|
created_at,
|
|
|
|
seen_at,
|
|
|
|
}): PatSchema => ({
|
|
|
|
id,
|
|
|
|
description,
|
|
|
|
expiresAt: expires_at,
|
|
|
|
userId: user_id,
|
|
|
|
createdAt: created_at,
|
|
|
|
seenAt: seen_at,
|
|
|
|
});
|
|
|
|
|
|
|
|
const patToRow = ({ description, expiresAt }: CreatePatSchema) => ({
|
|
|
|
description,
|
|
|
|
expires_at: expiresAt,
|
2022-09-16 09:54:27 +02:00
|
|
|
});
|
|
|
|
|
|
|
|
export default class PatStore implements IPatStore {
|
2023-01-30 09:02:44 +01:00
|
|
|
private db: Db;
|
2022-09-16 09:54:27 +02:00
|
|
|
|
|
|
|
private logger: Logger;
|
|
|
|
|
2023-01-30 09:02:44 +01:00
|
|
|
constructor(db: Db, getLogger: LogProvider) {
|
2022-09-16 09:54:27 +02:00
|
|
|
this.db = db;
|
|
|
|
this.logger = getLogger('pat-store.ts');
|
|
|
|
}
|
|
|
|
|
2024-02-01 15:28:46 +01:00
|
|
|
async create(
|
|
|
|
pat: CreatePatSchema,
|
|
|
|
secret: string,
|
|
|
|
userId: number,
|
|
|
|
): Promise<PatSchema> {
|
|
|
|
const rows = await this.db(TABLE)
|
|
|
|
.insert({ ...patToRow(pat), secret, user_id: userId })
|
|
|
|
.returning('*');
|
|
|
|
return rowToPat(rows[0]);
|
2022-09-16 09:54:27 +02:00
|
|
|
}
|
|
|
|
|
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-11-02 09:11:35 +01:00
|
|
|
async countByUser(userId: number): Promise<number> {
|
|
|
|
const result = await this.db.raw(
|
|
|
|
`SELECT COUNT(*) AS count FROM ${TABLE} WHERE user_id = ?`,
|
|
|
|
[userId],
|
|
|
|
);
|
|
|
|
const { count } = result.rows[0];
|
|
|
|
return count;
|
|
|
|
}
|
|
|
|
|
2024-02-01 15:28:46 +01:00
|
|
|
async get(id: number): Promise<PatSchema> {
|
2022-10-10 12:35:12 +02:00
|
|
|
const row = await this.db(TABLE).where({ id }).first();
|
2024-02-01 15:28:46 +01:00
|
|
|
if (!row) {
|
|
|
|
throw new NotFoundError('No PAT found.');
|
|
|
|
}
|
|
|
|
return rowToPat(row);
|
2022-09-16 09:54:27 +02:00
|
|
|
}
|
|
|
|
|
2024-02-01 15:28:46 +01:00
|
|
|
async getAll(): Promise<PatSchema[]> {
|
|
|
|
const pats = await this.db.select(PAT_PUBLIC_COLUMNS).from(TABLE);
|
|
|
|
return pats.map(rowToPat);
|
2022-09-16 09:54:27 +02:00
|
|
|
}
|
|
|
|
|
2024-02-01 15:28:46 +01:00
|
|
|
async getAllByUser(userId: number): Promise<PatSchema[]> {
|
|
|
|
const pats = 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);
|
2024-02-01 15:28:46 +01:00
|
|
|
return pats.map(rowToPat);
|
2022-09-16 09:54:27 +02:00
|
|
|
}
|
|
|
|
}
|