diff --git a/src/server/decoders/reference.ts b/src/server/decoders/reference.ts deleted file mode 100644 index a311aadf..00000000 --- a/src/server/decoders/reference.ts +++ /dev/null @@ -1,22 +0,0 @@ -// --------------------------------------------------------------------------------------------------------------------- -// Reference Decoders -// --------------------------------------------------------------------------------------------------------------------- - -import { object } from 'decoders'; -import { stringWithLength } from './utils'; - -// --------------------------------------------------------------------------------------------------------------------- - -export const referenceRecDecoder = object({ - abbr: stringWithLength(2, 255), - name: stringWithLength(3, 255), - productCode: stringWithLength(3, 255) -}); - -export const referenceJsonDecoder = object({ - abbr: stringWithLength(2, 255), - name: stringWithLength(3, 255), - productCode: stringWithLength(3, 255) -}); - -// --------------------------------------------------------------------------------------------------------------------- diff --git a/src/server/managers/reference.ts b/src/server/managers/reference.ts new file mode 100644 index 00000000..4381a126 --- /dev/null +++ b/src/server/managers/reference.ts @@ -0,0 +1,21 @@ +//---------------------------------------------------------------------------------------------------------------------- +// ReferenceManager +//---------------------------------------------------------------------------------------------------------------------- + +// Models +import { Reference } from '../../common/interfaces/models/reference'; + +// Resource Access +import * as referenceRA from '../resource-access/reference'; + +// Utilities +import { FilterToken } from '../routes/utils'; + +//---------------------------------------------------------------------------------------------------------------------- + +export async function list(filters : Record, tableName : string) : Promise +{ + return referenceRA.list(filters, tableName); +} + +//---------------------------------------------------------------------------------------------------------------------- diff --git a/src/server/managers/references.ts b/src/server/managers/references.ts deleted file mode 100644 index 64bb73f0..00000000 --- a/src/server/managers/references.ts +++ /dev/null @@ -1,38 +0,0 @@ -//---------------------------------------------------------------------------------------------------------------------- -// ReferenceManager -//---------------------------------------------------------------------------------------------------------------------- - -import _ from 'lodash'; - -// Utilities -import { getDB } from '../utils/database'; -import { applyFilters } from '../knex/utils'; -import { FilterToken } from '../routes/utils'; - -// Models -import { Reference } from '../models/reference'; - -//---------------------------------------------------------------------------------------------------------------------- - -class ReferenceManager -{ - async getFiltered(filters : Record, tableName : string) : Promise - { - const db = await getDB(); - let query = db(tableName) - .select(`${ tableName }.name`, `${ tableName }.abbr`, `${ tableName }.product_code as productCode`); - - if(filters && !_.isEmpty(filters)) - { - query = applyFilters(query, filters); - } - - return (await query).map(Reference.fromDB); - } -} - -//---------------------------------------------------------------------------------------------------------------------- - -export default new ReferenceManager(); - -//---------------------------------------------------------------------------------------------------------------------- diff --git a/src/server/models/reference.ts b/src/server/models/reference.ts deleted file mode 100644 index 3062bde2..00000000 --- a/src/server/models/reference.ts +++ /dev/null @@ -1,63 +0,0 @@ -//---------------------------------------------------------------------------------------------------------------------- -// Reference -//---------------------------------------------------------------------------------------------------------------------- - -import * as JsonDecoder from 'decoders'; - -// Decoders -import { referenceJsonDecoder, referenceRecDecoder } from '../decoders/reference'; - -// Models -import { ReferenceOptions } from '../../common/interfaces/models/reference'; - -//---------------------------------------------------------------------------------------------------------------------- - -export class Reference -{ - public readonly abbr : string; - public readonly name : string; - public readonly productCode : string; - - constructor(options : ReferenceOptions) - { - this.abbr = options.abbr; - this.name = options.name; - this.productCode = options.productCode; - } - - //------------------------------------------------------------------------------------------------------------------ - // Serialization - //------------------------------------------------------------------------------------------------------------------ - - public toJSON() : Record - { - return { - abbr: this.abbr, - name: this.name, - productCode: this.productCode - }; - } - - public toDB() : Record - { - return this.toJSON(); - } - - //------------------------------------------------------------------------------------------------------------------ - // Deserialization - //------------------------------------------------------------------------------------------------------------------ - - static fromDB(referenceRecord : Record) : Reference - { - const decoder = JsonDecoder.guard(referenceRecDecoder); - return new Reference(decoder(referenceRecord)); - } - - static fromJSON(jsonObj : Record) : Reference - { - const decoder = JsonDecoder.guard(referenceJsonDecoder); - return new Reference(decoder(jsonObj)); - } -} - -//---------------------------------------------------------------------------------------------------------------------- diff --git a/src/server/resource-access/reference.ts b/src/server/resource-access/reference.ts new file mode 100644 index 00000000..d96d289c --- /dev/null +++ b/src/server/resource-access/reference.ts @@ -0,0 +1,33 @@ +//---------------------------------------------------------------------------------------------------------------------- +// Reference Resource Access +//---------------------------------------------------------------------------------------------------------------------- + +// Models +import { Reference } from '../../common/interfaces/models/reference'; + +// Transforms +import * as ReferenceTrans from './transforms/reference'; + +// Utils +import { isEmpty } from '../utils/misc'; +import { getDB } from '../utils/database'; +import { applyFilters } from '../knex/utils'; +import { FilterToken } from '../routes/utils'; + +//---------------------------------------------------------------------------------------------------------------------- + +export async function list(filters : Record, tableName : string) : Promise +{ + const db = await getDB(); + let query = db(tableName) + .select(`${ tableName }.name`, `${ tableName }.abbr`, `${ tableName }.product_code as productCode`); + + if(filters && !isEmpty(filters)) + { + query = applyFilters(query, filters); + } + + return (await query).map(ReferenceTrans.fromDB); +} + +//---------------------------------------------------------------------------------------------------------------------- diff --git a/src/server/resource-access/transforms/reference.ts b/src/server/resource-access/transforms/reference.ts new file mode 100644 index 00000000..15cb5198 --- /dev/null +++ b/src/server/resource-access/transforms/reference.ts @@ -0,0 +1,35 @@ +// --------------------------------------------------------------------------------------------------------------------- +// Reference Database Transform +// --------------------------------------------------------------------------------------------------------------------- + +import { Reference } from '../../../common/interfaces/models/reference'; + +// --------------------------------------------------------------------------------------------------------------------- + +export interface ReferenceDBSchema { + abbr : string; + name : string; + productCode : string; +} + +// --------------------------------------------------------------------------------------------------------------------- + +export function toDB(reference : Reference) : ReferenceDBSchema +{ + return { + abbr: reference.abbr, + name: reference.name, + productCode: reference.productCode + }; +} + +export function fromDB(reference : ReferenceDBSchema) : Reference +{ + return { + abbr: reference.abbr, + name: reference.name, + productCode: reference.productCode + }; +} + +// --------------------------------------------------------------------------------------------------------------------- diff --git a/src/server/routes/systems/eote.ts b/src/server/routes/systems/eote.ts index 60739f3c..497ea19e 100644 --- a/src/server/routes/systems/eote.ts +++ b/src/server/routes/systems/eote.ts @@ -5,7 +5,7 @@ import express from 'express'; // Managers -import refMan from '../../managers/references'; +import * as refMan from '../../managers/reference'; // Utils import { buildSupplementRoute } from './utils/supplement'; @@ -34,7 +34,7 @@ router.get('/references', async(req, resp) => { const query = convertQueryToRecord(req); const filters = parseQuery(query); - resp.json(await refMan.getFiltered(filters, 'eote_reference')); + resp.json(await refMan.list(filters, 'eote_reference')); }); //---------------------------------------------------------------------------------------------------------------------- diff --git a/src/server/routes/systems/genesys.ts b/src/server/routes/systems/genesys.ts index cab6a631..72b65210 100644 --- a/src/server/routes/systems/genesys.ts +++ b/src/server/routes/systems/genesys.ts @@ -5,7 +5,7 @@ import express from 'express'; // Managers -import refMan from '../../managers/references'; +import * as refMan from '../../managers/reference'; // Utils import { buildSupplementRoute } from './utils/supplement'; @@ -34,7 +34,7 @@ router.get('/references', async(req, resp) => { const query = convertQueryToRecord(req); const filters = parseQuery(query); - resp.json(await refMan.getFiltered(filters, 'genesys_reference')); + resp.json(await refMan.list(filters, 'genesys_reference')); }); //----------------------------------------------------------------------------------------------------------------------