Skip to content

Commit

Permalink
moved to plain account model.
Browse files Browse the repository at this point in the history
  • Loading branch information
Morgul committed May 20, 2024
1 parent 34962cb commit fdfe9ab
Show file tree
Hide file tree
Showing 22 changed files with 299 additions and 288 deletions.
5 changes: 3 additions & 2 deletions src/common/interfaces/models/account.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,8 @@
// Account
//----------------------------------------------------------------------------------------------------------------------

// eslint-disable-next-line @typescript-eslint/no-empty-interface
export interface AccountSettings {
// TODO: Figure out some settings...
colorMode ?: 'light' | 'dark' | 'auto';
}

export interface AccountOptions {
Expand All @@ -20,4 +19,6 @@ export interface AccountOptions {
// FIXME: Once Models are removed, `AccountOptions` should be named 'Account'.
export type Account = AccountOptions;

export type NewAccount = Omit<Account, 'id'>;

//----------------------------------------------------------------------------------------------------------------------
2 changes: 1 addition & 1 deletion src/server/auth/serialization.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ import passport from 'passport';
import * as accountMan from '../managers/account';

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

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

Expand Down
28 changes: 0 additions & 28 deletions src/server/decoders/account.ts

This file was deleted.

12 changes: 10 additions & 2 deletions src/server/express-shim.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,21 @@
// ---------------------------------------------------------------------------------------------------------------------

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

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

declare global {
namespace Express {
interface User {
email : string;
id : string;
}
}
}

declare global {
export namespace Express {
// eslint-disable-next-line @typescript-eslint/no-empty-interface
export interface User extends Account {}
export interface Request {
user ?: User;
Expand Down
154 changes: 13 additions & 141 deletions src/server/managers/account.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,174 +3,46 @@
// ---------------------------------------------------------------------------------------------------------------------

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

// Errors
import { MultipleResultsError, NotFoundError } from '../errors';

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

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

export interface AccountFilters {
id : unknown,
email : unknown,
name : unknown
}
// Resource Access
import * as accountRA from '../resource-access/account';

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

export async function list(filters : AccountFilters) : Promise<Account[]>
export async function list(filters : accountRA.AccountFilters) : Promise<Account[]>
{
const db = await getDB();
const query = db('account')
.select(
'account_id as id',
'email',
'name',
'avatar',
'permissions',
'settings'
);

if(filters.id)
{
query.where({ account_id: filters.id });
}

if(filters.email)
{
query.where({ email: filters.email });
}

if(filters.name)
{
query.where({ name: filters.name });
}

return (await query).map(Account.fromDB);
return accountRA.list(filters);
}

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);
return accountRA.getGroups(accountID);
}

export async function get(accountID : string) : Promise<Account>
{
const db = await getDB();
const accounts = await db('account')
.select(
'account_id as id',
'email',
'name',
'avatar',
'permissions',
'settings'
)
.where({
account_id: accountID
});

if(accounts.length > 1)
{
throw new MultipleResultsError('account');
}
else if(accounts.length === 0)
{
throw new NotFoundError(`No account record found for account '${ accountID }'.`);
}
else
{
const groups = await getGroups(accountID);
return Account.fromDB({ ...accounts[0], groups });
}
return accountRA.get(accountID);
}

export async function getByEmail(email : string) : Promise<Account>
{
const db = await getDB();
const accounts = await db('account')
.select(
'account_id as id',
'email',
'name',
'avatar',
'permissions',
'settings'
)
.where({ email });

if(accounts.length > 1)
{
throw new MultipleResultsError('account');
}
else if(accounts.length === 0)
{
throw new NotFoundError(`No account record found with email '${ email }'.`);
}
else
{
const groups = await getGroups(accounts[0].id);
return Account.fromDB({ ...accounts[0], groups });
}
return accountRA.getByEmail(email);
}

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

return get(account.id);
return accountRA.add(newAccount);
}

export async function update(accountID : string, accountUpdate : Record<string, unknown>) : Promise<Account>
export async function update(accountID : string, accountUpdate : Partial<Account>) : Promise<Account>
{
// Get the current account
const account = await get(accountID);

// Mix the current account with the allowed updates.
const allowedUpdate = {
...account.toJSON(),
name: accountUpdate.name ?? account.name,
avatar: accountUpdate.avatar ?? account.avatar,
settings: accountUpdate.settings ?? account.settings
};

// Make a new account object
const newAccount = Account.fromJSON(allowedUpdate);

// Update the database
const db = await getDB();
await db('account')
.update(newAccount.toDB())
.where({ account_id: accountID });

// Return the updated record
return get(accountID);
return accountRA.update(accountID, accountUpdate);
}

export async function remove(accountID : string) : Promise<{ status : 'ok' }>
{
const db = await getDB();
await db('account')
.where({ account_id: accountID })
.delete();

return { status: 'ok' };
return accountRA.remove(accountID);
}

// ---------------------------------------------------------------------------------------------------------------------
2 changes: 1 addition & 1 deletion src/server/managers/permissions.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ import tp from 'trivialperms';
import * as rolesMan from './roles';

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

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

Expand Down
2 changes: 1 addition & 1 deletion src/server/managers/supplement.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ import logging from '@strata-js/util-logging';
import * as permMan from './permissions';

// Models
import { Account } from '../models/account';
import { Account } from '../../common/interfaces/models/account';
import { Supplement } from '../models/supplement';

// Utilities
Expand Down
81 changes: 0 additions & 81 deletions src/server/models/account.ts

This file was deleted.

2 changes: 1 addition & 1 deletion src/server/models/character.ts
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,7 @@ export class Character<Details extends SystemDetails = SystemDetails>
campaign: this.campaign,
details: this.details
};
} // end
}

public toDB() : Record<string, unknown>
{
Expand Down
4 changes: 2 additions & 2 deletions src/server/models/notebook.ts
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ export class NotebookPage
content: this.content,
notebookID: this.notebookID
};
} // end
}

public toDB() : Record<string, unknown>
{
Expand Down Expand Up @@ -94,7 +94,7 @@ export class Notebook
id: this.id,
pages: this.pages.map((page) => page.toJSON())
};
} // end
}

public toDB() : Record<string, unknown>
{
Expand Down
Loading

0 comments on commit fdfe9ab

Please sign in to comment.