Skip to content

Commit

Permalink
split roles out from account RA.
Browse files Browse the repository at this point in the history
  • Loading branch information
Morgul committed May 21, 2024
1 parent 3aedf5e commit 311749a
Show file tree
Hide file tree
Showing 6 changed files with 56 additions and 100 deletions.
22 changes: 0 additions & 22 deletions src/server/decoders/role.ts

This file was deleted.

6 changes: 3 additions & 3 deletions src/server/managers/account.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ export async function list(filters : accountRA.AccountFilters) : Promise<Account
const accounts = await accountRA.list(filters);
return pMap(accounts, async (account) =>
{
const roles = await roleRA.getRoles(account.id);
const roles = await roleRA.getForAccount(account.id);
return {
...account,
groups: roles
Expand All @@ -29,7 +29,7 @@ export async function list(filters : accountRA.AccountFilters) : Promise<Account
export async function get(accountID : string) : Promise<Account>
{
const account = await accountRA.get(accountID);
const roles = await roleRA.getRoles(accountID);
const roles = await roleRA.getForAccount(accountID);

return {
...account,
Expand All @@ -40,7 +40,7 @@ export async function get(accountID : string) : Promise<Account>
export async function getByEmail(email : string) : Promise<Account>
{
const account = await accountRA.getByEmail(email);
const roles = await roleRA.getRoles(account.id);
const roles = await roleRA.getForAccount(account.id);

return {
...account,
Expand Down
10 changes: 4 additions & 6 deletions src/server/managers/roles.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,18 +3,16 @@
// ---------------------------------------------------------------------------------------------------------------------

// Models
import { Role } from '../models/role';
import { Role } from '../../common/interfaces/models/role';

// Utils
import { getDB } from '../utils/database';
// Resource Access
import * as roleRA from '../resource-access/role';

// ---------------------------------------------------------------------------------------------------------------------

export async function list() : Promise<Role[]>
{
const db = await getDB();
return (await db('role as r').select('r.role_id as id', 'r.name', 'r.permissions'))
.map(Role.fromDB);
return roleRA.list();
}

// ---------------------------------------------------------------------------------------------------------------------
68 changes: 0 additions & 68 deletions src/server/models/role.ts

This file was deleted.

15 changes: 14 additions & 1 deletion src/server/resource-access/role.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,24 @@
// Role Resource Access Layer
// ---------------------------------------------------------------------------------------------------------------------

import { Role } from '../../common/interfaces/models/role';

// Transforms
import * as RoleTransforms from './transforms/role';

// Utils
import { getDB } from '../utils/database';

// ---------------------------------------------------------------------------------------------------------------------

export async function getRoles(accountID : string) : Promise<string[]>
export async function list() : Promise<Role[]>
{
const db = await getDB();
return (await db('role as r').select('r.role_id as id', 'r.name', 'r.permissions'))
.map(RoleTransforms.fromDB);
}

export async function getForAccount(accountID : string) : Promise<string[]>
{
const db = await getDB();
const roles = await db('account as ac')
Expand Down
35 changes: 35 additions & 0 deletions src/server/resource-access/transforms/role.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
// ---------------------------------------------------------------------------------------------------------------------
// Role Database Transform
// ---------------------------------------------------------------------------------------------------------------------

import { Role } from '../../../common/interfaces/models/role';

// ---------------------------------------------------------------------------------------------------------------------

export interface RoleDBSchema {
role_id : number;
name : string;
permissions : string;
}

// ---------------------------------------------------------------------------------------------------------------------

export function toDB(role : Role) : RoleDBSchema
{
return {
role_id: role.id,
name: role.name,
permissions: JSON.stringify(role.permissions ?? [])
};
}

export function fromDB(role : RoleDBSchema) : Role
{
return {
id: role.role_id,
name: role.name,
permissions: JSON.parse(role.permissions) ?? []
};
}

// ---------------------------------------------------------------------------------------------------------------------

0 comments on commit 311749a

Please sign in to comment.