From f032445d3de6188ab4c6cb2ec006900d1ab32ab1 Mon Sep 17 00:00:00 2001 From: sighphyre Date: Wed, 5 Jan 2022 10:26:20 +0200 Subject: [PATCH] fix: Improve responses from role resolution - getting a non existant role will throw a NotFound error --- src/lib/db/access-store.ts | 45 +++++++------------------------------- src/lib/db/role-store.ts | 3 +++ 2 files changed, 11 insertions(+), 37 deletions(-) diff --git a/src/lib/db/access-store.ts b/src/lib/db/access-store.ts index 364a01c9d1..c44c5859a3 100644 --- a/src/lib/db/access-store.ts +++ b/src/lib/db/access-store.ts @@ -10,6 +10,7 @@ import { } from '../types/stores/access-store'; import { IPermission } from 'lib/types/model'; import { roundToNearestMinutesWithOptions } from 'date-fns/fp'; +import NotFoundError from '../error/notfound-error'; const T = { ROLE_USER: 'role_user', @@ -67,11 +68,17 @@ export class AccessStore implements IAccessStore { } async get(key: number): Promise { - return this.db + const role = await this.db .select(['id', 'name', 'type', 'description']) .where('id', key) .first() .from(T.ROLES); + + if (!role) { + throw new NotFoundError(`Could not find role with id: ${key}`); + } + + return role; } async getAll(): Promise { @@ -85,42 +92,6 @@ export class AccessStore implements IAccessStore { .orWhere('type', 'environment') .from(`${T.PERMISSIONS} as p`); return rows.map(this.mapPermission); - // .map(mapPermission) - // const rows = await this.db - // .select(['p.id', 'p.permission', 'p.environment', 'p.display_name']) - // .join(`${T.ROLE_PERMISSION} AS rp`, 'rp.permission_id', 'p.id') - // .where('pt.type', 'project') - // .orWhere('pt.type', 'environment') - // .from(`${T.PERMISSIONS} as p`); - - // let projectPermissions: IPermission[] = []; - // let rawEnvironments = new Map(); - - // for (let permission of rows) { - // if (!permission.environment) { - // projectPermissions.push(this.mapPermission(permission)); - // } else { - // if (!rawEnvironments.get(permission.environment)) { - // rawEnvironments.set(permission.environment, []); - // } - // rawEnvironments.get(permission.environment).push(permission); - // } - // } - // let allEnvironmentPermissions: Array = - // Array.from(rawEnvironments).map( - // ([environmentName, environmentPermissions]) => { - // return { - // name: environmentName, - // permissions: environmentPermissions.map( - // this.mapPermission, - // ), - // }; - // }, - // ); - // return { - // project: projectPermissions, - // environments: allEnvironmentPermissions, - // }; } mapPermission(permission: IPermissionRow): IPermission { diff --git a/src/lib/db/role-store.ts b/src/lib/db/role-store.ts index 01e2433bb9..2e46372121 100644 --- a/src/lib/db/role-store.ts +++ b/src/lib/db/role-store.ts @@ -63,6 +63,9 @@ export default class RoleStore implements IRoleStore { async get(id: number): Promise { const rows = await this.db.select(COLUMNS).from(T.ROLES).where({ id }); + if (rows.length === 0) { + throw new NotFoundError(`Could not find role with id: ${id}`); + } return this.mapRow(rows[0]); }