Skip to content

Commit

Permalink
fix: Improve responses from role resolution - getting a non existant …
Browse files Browse the repository at this point in the history
…role will throw a NotFound error
  • Loading branch information
sighphyre authored and ivarconr committed Jan 11, 2022
1 parent 40bdab7 commit f032445
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 37 deletions.
45 changes: 8 additions & 37 deletions src/lib/db/access-store.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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',
Expand Down Expand Up @@ -67,11 +68,17 @@ export class AccessStore implements IAccessStore {
}

async get(key: number): Promise<IRole> {
return this.db
const role = await this.db
.select(['id', 'name', 'type', 'description'])
.where('id', key)
.first()
.from<IRole>(T.ROLES);

if (!role) {
throw new NotFoundError(`Could not find role with id: ${key}`);
}

return role;
}

async getAll(): Promise<IRole[]> {
Expand All @@ -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<string, IPermissionRow[]>();

// 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<IEnvironmentPermission> =
// Array.from(rawEnvironments).map(
// ([environmentName, environmentPermissions]) => {
// return {
// name: environmentName,
// permissions: environmentPermissions.map(
// this.mapPermission,
// ),
// };
// },
// );
// return {
// project: projectPermissions,
// environments: allEnvironmentPermissions,
// };
}

mapPermission(permission: IPermissionRow): IPermission {
Expand Down
3 changes: 3 additions & 0 deletions src/lib/db/role-store.ts
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,9 @@ export default class RoleStore implements IRoleStore {

async get(id: number): Promise<ICustomRole> {
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]);
}

Expand Down

0 comments on commit f032445

Please sign in to comment.