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 c0e3a16 commit 3aedf5e
Show file tree
Hide file tree
Showing 6 changed files with 104 additions and 39 deletions.
43 changes: 43 additions & 0 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@
"mailgun": "^0.5.0",
"nanoid": "^3.3.6",
"nanoid-dictionary": "^4.3.0",
"p-map": "^4.0.0",
"passport": "^0.7.0",
"passport-google-oauth20": "^2.0.0",
"passport-google-web": "^1.1.0",
Expand Down
40 changes: 30 additions & 10 deletions src/server/managers/account.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,42 +2,62 @@
// Account Manager
// ---------------------------------------------------------------------------------------------------------------------

import pMap from 'p-map';

// Models
import { Account, NewAccount } from '../../common/interfaces/models/account';

// Resource Access
import * as accountRA from '../resource-access/account';
import * as roleRA from '../resource-access/role';

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

export async function list(filters : accountRA.AccountFilters) : Promise<Account[]>
{
return accountRA.list(filters);
}

export async function getGroups(accountID : string) : Promise<string[]>
{
return accountRA.getGroups(accountID);
const accounts = await accountRA.list(filters);
return pMap(accounts, async (account) =>
{
const roles = await roleRA.getRoles(account.id);
return {
...account,
groups: roles
};
}, { concurrency: 10 });
}

export async function get(accountID : string) : Promise<Account>
{
return accountRA.get(accountID);
const account = await accountRA.get(accountID);
const roles = await roleRA.getRoles(accountID);

return {
...account,
groups: roles
};
}

export async function getByEmail(email : string) : Promise<Account>
{
return accountRA.getByEmail(email);
const account = await accountRA.getByEmail(email);
const roles = await roleRA.getRoles(account.id);

return {
...account,
groups: roles
};
}

export async function add(newAccount : NewAccount) : Promise<Account>
{
return accountRA.add(newAccount);
const accountID = await accountRA.add(newAccount);
return get(accountID);
}

export async function update(accountID : string, accountUpdate : Partial<Account>) : Promise<Account>
{
return accountRA.update(accountID, accountUpdate);
await accountRA.update(accountID, accountUpdate);
return get(accountID);
}

export async function remove(accountID : string) : Promise<{ status : 'ok' }>
Expand Down
33 changes: 7 additions & 26 deletions src/server/resource-access/account.ts
Original file line number Diff line number Diff line change
Expand Up @@ -77,20 +77,6 @@ export async function list(filters : AccountFilters) : Promise<Account[]>
return (await query).map(AccountTransforms.fromDB);
}

export async function getGroups(accountID : string) : Promise<string[]>
{
const db = await getDB();
const roles = await db('account as ac')
.select('r.name as name', 'r.role_id as id')
.join('account_role as ar', 'ac.account_id', '=', 'ar.account_id')
.join('role as r', 'ar.role_id', '=', 'r.role_id')
.where({
'ac.account_id': accountID
});

return roles.map((role) => role.name);
}

export async function get(accountID : string) : Promise<Account>
{
const db = await getDB();
Expand All @@ -117,8 +103,7 @@ export async function get(accountID : string) : Promise<Account>
}
else
{
const groups = await getGroups(accountID);
return AccountTransforms.fromDB({ ...accounts[0], groups });
return AccountTransforms.fromDB(accounts[0]);
}
}

Expand Down Expand Up @@ -146,22 +131,21 @@ export async function getByEmail(email : string) : Promise<Account>
}
else
{
const groups = await getGroups(accounts[0].account_id);
return AccountTransforms.fromDB({ ...accounts[0], groups });
return AccountTransforms.fromDB(accounts[0]);
}
}

export async function add(newAccount : NewAccount) : Promise<Account>
export async function add(newAccount : NewAccount) : Promise<string>
{
const account = AccountTransforms.toDB(newAccount);
const account = AccountTransforms.toDB({ ...newAccount, id: shortID() });
const db = await getDB();
await db('account')
.insert({ ...account, id: shortID(), created: db.fn.now() });
.insert({ ...account, created: db.fn.now() });

return get(account.account_id);
return account.account_id;
}

export async function update(accountID : string, accountUpdate : Partial<Account>) : Promise<Account>
export async function update(accountID : string, accountUpdate : Partial<Account>) : Promise<void>
{
// Get the current account
const account = await get(accountID);
Expand All @@ -179,9 +163,6 @@ export async function update(accountID : string, accountUpdate : Partial<Account
await db('account')
.update(AccountTransforms.toDB(allowedUpdate))
.where({ account_id: accountID });

// Return the updated record
return get(accountID);
}

export async function remove(accountID : string) : Promise<{ status : 'ok' }>
Expand Down
23 changes: 23 additions & 0 deletions src/server/resource-access/role.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
// ---------------------------------------------------------------------------------------------------------------------
// Role Resource Access Layer
// ---------------------------------------------------------------------------------------------------------------------

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

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

export async function getRoles(accountID : string) : Promise<string[]>
{
const db = await getDB();
const roles = await db('account as ac')
.select('r.name as name', 'r.role_id as id')
.join('account_role as ar', 'ac.account_id', '=', 'ar.account_id')
.join('role as r', 'ar.role_id', '=', 'r.role_id')
.where({
'ac.account_id': accountID
});

return roles.map((role) => role.name);
}

// ---------------------------------------------------------------------------------------------------------------------
3 changes: 0 additions & 3 deletions src/server/routes/characters.ts
Original file line number Diff line number Diff line change
Expand Up @@ -50,9 +50,6 @@ router.get('/', async(req, resp) =>
}

const filters = parseQuery(query);

console.log('filters:', filters);

resp.json(await charMan.list(filters));
});
});
Expand Down

0 comments on commit 3aedf5e

Please sign in to comment.