mirror of
https://github.com/Unleash/unleash.git
synced 2025-01-31 00:16:47 +01:00
db0a0d7097
https://linear.app/unleash/issue/SR-379/refactor-pats This PR refactors PATs. - Adds a new `createPatSchema`, which better aligns with https://docs.getunleash.io/contributing/ADRs/overarching/separation-request-response-schemas - Drops the model type and class in favor of using the schema types directly, which is more consistent with the rest of the codebase and easier to maintain - Misc scouting, improvement and fixes This breaks Enterprise temporarily, but it's faster to move forward this way.
125 lines
3.2 KiB
TypeScript
125 lines
3.2 KiB
TypeScript
import { Logger, LogProvider } from '../logger';
|
|
import { IPatStore } from '../types/stores/pat-store';
|
|
import NotFoundError from '../error/notfound-error';
|
|
import { Db } from './db';
|
|
import { CreatePatSchema, PatSchema } from '../openapi';
|
|
|
|
const TABLE = 'personal_access_tokens';
|
|
|
|
const PAT_PUBLIC_COLUMNS = [
|
|
'id',
|
|
'description',
|
|
'user_id',
|
|
'expires_at',
|
|
'created_at',
|
|
'seen_at',
|
|
];
|
|
|
|
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,
|
|
});
|
|
|
|
export default class PatStore implements IPatStore {
|
|
private db: Db;
|
|
|
|
private logger: Logger;
|
|
|
|
constructor(db: Db, getLogger: LogProvider) {
|
|
this.db = db;
|
|
this.logger = getLogger('pat-store.ts');
|
|
}
|
|
|
|
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]);
|
|
}
|
|
|
|
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();
|
|
}
|
|
|
|
async deleteAll(): Promise<void> {
|
|
await this.db(TABLE).del();
|
|
}
|
|
|
|
destroy(): void {}
|
|
|
|
async exists(id: number): Promise<boolean> {
|
|
const result = await this.db.raw(
|
|
`SELECT EXISTS(SELECT 1 FROM ${TABLE} WHERE id = ?) AS present`,
|
|
[id],
|
|
);
|
|
const { present } = result.rows[0];
|
|
return present;
|
|
}
|
|
|
|
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;
|
|
}
|
|
|
|
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;
|
|
}
|
|
|
|
async get(id: number): Promise<PatSchema> {
|
|
const row = await this.db(TABLE).where({ id }).first();
|
|
if (!row) {
|
|
throw new NotFoundError('No PAT found.');
|
|
}
|
|
return rowToPat(row);
|
|
}
|
|
|
|
async getAll(): Promise<PatSchema[]> {
|
|
const pats = await this.db.select(PAT_PUBLIC_COLUMNS).from(TABLE);
|
|
return pats.map(rowToPat);
|
|
}
|
|
|
|
async getAllByUser(userId: number): Promise<PatSchema[]> {
|
|
const pats = await this.db
|
|
.select(PAT_PUBLIC_COLUMNS)
|
|
.from(TABLE)
|
|
.where('user_id', userId);
|
|
return pats.map(rowToPat);
|
|
}
|
|
}
|