@quorum/schematics
Advanced tools
Comparing version 0.0.12 to 0.0.13
{ | ||
"name": "@quorum/schematics", | ||
"version": "0.0.12", | ||
"version": "0.0.13", | ||
"description": "A schematics Library for Quorum", | ||
@@ -5,0 +5,0 @@ "scripts": { |
import { isInt } from 'validator'; | ||
import * as md5 from 'md5'; | ||
import * as metrics from 'datadog-metrics'; | ||
import * as nconf from 'nconf'; | ||
@@ -7,57 +8,81 @@ import { Request as Req } from 'restify'; | ||
import { AuthenticationDatabase as Database } from '../../authentication/authentication.database'; | ||
import { Common } from '../shared/common.class'; | ||
import * as errors from 'common/errors/errors.class' | ||
import { Etag } from '../shared/etag.model'; | ||
import { Etag } from 'common/database'; | ||
import { InvalidParameterValueError } from 'common/errors'; | ||
import { Logger } from 'common/logger'; | ||
import { <%= classify(nameSingular) %>} from './<%= nameSingular %>.model'; | ||
import { <%= classify(nameSingular) %>PatchParameters } from './<%= nameSingular %>.patch-model'; | ||
import { <%= classify(nameSingular) %>QueryParameters } from './<%= nameSingular %>.query-model'; | ||
import { ResourceResponse } from '../shared/resource-response.model'; | ||
import * as metrics from 'datadog-metrics'; | ||
import { <%= classify(nameSingular) %>, <%= classify(nameSingular) %>EmbedTypes, <%= classify(nameSingular) %>PatchParameters, <%= classify(nameSingular) %>QueryParameters} from './'; | ||
export class <%= classify(name) %> { | ||
export class <%= classify(name) %>{ | ||
constructor() { } | ||
static async get<%= classify(nameSingular) %>(parameters: <%= classify(nameSingular) %>QueryParameters, userId: string): Promise<<%= classify(nameSingular) %>> { | ||
//parameter checks if necessary | ||
static async addEmbedResources(embedParameter: string, <%=name%>: <%= classify(nameSingular) %>[]): Promise<<%= classify(nameSingular) %>[]> { | ||
if (embedParameter && <%=name%>.length > 0) { | ||
embedParameter = embedParameter.toUpperCase(); | ||
let promiseArray: any[] = new Array(); | ||
promiseArray.push( | ||
//embedParameter.includes(<%= classify(nameSingular) %>EmbedTypes.'POPULATE'.toUpperCase()) ? 'POPULATE'.get<%=classify(name)%>(new 'POPULATE'QueryParameters({ id: <%=name%>.map((x) => { return x.'POPULATE'; }).join(',') })) : Promise.resolve({}) | ||
) | ||
await Promise.all(promiseArray).then((results: any[]) => { | ||
results.forEach((result: any, index: number) => { | ||
if (index === 0 && Array.isArray(result)) { | ||
//let <%=name%>: <%= classify(nameSingular) %>[] = result; | ||
//<%=name%>.forEach((<%=nameSingular%>: <%= classify(nameSingular) %>) => { | ||
// <%=nameSingular%>.lastEvent = events.find((event: Event) => { return event.conversationId === <%=nameSingular%>.id; }); | ||
//}); | ||
} | ||
}); | ||
}).catch(err => { | ||
Logger.error('An error happened when fullfilling embed property', err); | ||
}) | ||
} | ||
return <%=name%>; | ||
} | ||
static async get<%= classify(nameSingular) %>(id: string): Promise<<%= classify(nameSingular) %>> { | ||
if (!isInt(id.toString())) throw new InvalidParameterValueError('Id parameter must be a number.'); | ||
const request = new Request(Database.getInstance().pool); | ||
//populate parameters | ||
return await Common.getFirst(request, '<%= getProcedure %>'); | ||
request.input('ID', TYPES.VarChar(MAX), id); | ||
return await Database.getFirst(request, '<%= getProcedure %>'); | ||
} | ||
static async get<%= classify(nameSingular) %>s(parameters: <%= classify(nameSingular) %>QueryParameters, userId: string): Promise<<%= classify(nameSingular) %>[]> { | ||
static async get<%= classify(name) %>(parameters: <%= classify(nameSingular) %>QueryParameters): Promise<<%=classify(nameSingular)%>[] > { | ||
//parameter checks if necessary | ||
//if (!isInt(parameters.id.toString())) throw new InvalidParameterValueError('Id parameter must be a number.'); | ||
const request = new Request(Database.getInstance().pool); | ||
//populate parameters | ||
return await Common.get(request, '<%= getProcedure %>'); | ||
//request.input('ID', TYPES.VarChar(MAX), parameters.id); | ||
return await Database.get(request, '<%= getProcedure %>'); | ||
} | ||
static async patch<%= classify(nameSingular) %>(resourceId: number, parameters: <%= classify(nameSingular) %>PatchParameters, userId: string): Promise<ResourceResponse> { | ||
//parameter checks | ||
static async patch<%= classify(nameSingular) %>(id: number, parameters: <%= classify(nameSingular) %>PatchParameters): Promise<<%= classify(nameSingular) %>> { | ||
if (!isInt(id.toString())) throw new InvalidParameterValueError('Id parameter must be a number.'); | ||
const request = new Request(Database.getInstance().pool); | ||
//populate parameters | ||
return await Common.save(request, '<%= getProcedure %>'); | ||
request.input('ID', TYPES.Int, id); | ||
request.input('Json', TYPES.VarChar(1000), JSON.stringify(parameters)); | ||
return await Database.save(request, '<%= patchProcedure %>'); | ||
} | ||
static async post<%= classify(nameSingular) %>(<%= nameSingular %>: <%= classify(nameSingular) %>, userId: string): Promise<ResourceResponse> { | ||
static async post<%= classify(nameSingular) %>(<%= nameSingular %>: <%= classify(nameSingular) %>): Promise<<%= classify(nameSingular) %>> { | ||
//parameter checks | ||
const request = new Request(Database.getInstance().pool); | ||
//populate parameters | ||
return await Common.save(request, '<%= createProcedure %>'); | ||
//request.input('ID', TYPES.Int, id); | ||
return await Database.save(request, '<%= createProcedure %>'); | ||
} | ||
static async put<%= classify(nameSingular) %>(<%= nameSingular %>: <%= classify(nameSingular) %>, userId: string): Promise<ResourceResponse> { | ||
//parameter checks | ||
static async put<%= classify(nameSingular) %>(<%= nameSingular %>: <%= classify(nameSingular) %>): Promise <<%= classify(nameSingular) %>> { | ||
if (!isInt(<%= nameSingular %>.id.toString())) throw new InvalidParameterValueError('Id parameter must be a number.'); | ||
var request = new Request(Database.getInstance().pool); | ||
//populate parameters | ||
return Common.save(request, '<%= updateProcedure %>'); | ||
//request.input('ID', TYPES.Int, id); | ||
return Database.save(request, '<%= updateProcedure %>'); | ||
} | ||
static async verifyEtag(resourceId: number, userId: string, etag: string): Promise<Etag> { | ||
static async verifyEtag(resourceId: string, etag: string): Promise<Etag> { | ||
let parameters: <%= classify(nameSingular) %>QueryParameters = new <%= classify(nameSingular) %>QueryParameters(); | ||
//assign parameters | ||
let <%= nameSingular %>: <%= classify(nameSingular) %> = await <%= classify(name) %>.get<%= classify(nameSingular) %>(parameters, userId); | ||
let newEtag: string = md5(JSON.stringify(<%= nameSingular %>)); | ||
return (newEtag === etag) ? { isMatch: true, resource: <%= nameSingular %>, etag: newEtag } : { isMatch: false, resource: <%= nameSingular %>, etag: newEtag }; | ||
} | ||
parameters.id = resourceId; | ||
let <%= nameSingular %>: <%= classify(nameSingular) %> = await <%= classify(name) %>.get<%= classify(nameSingular) %>(parameters.id); | ||
let newEtag: string = md5(JSON.stringify(<%= nameSingular %>)); | ||
return (newEtag === etag) ? { isMatch: true, resource: <%= nameSingular %>, etag: newEtag } : { isMatch: false, resource: <%= nameSingular %>, etag: newEtag }; | ||
} | ||
} |
import { expect } from 'chai'; | ||
import { Request } from 'mssql'; | ||
import * as sinon from 'sinon'; | ||
import { Common } from '../shared/common.class'; | ||
import { <%= classify(name) %>EmbedTypes } from './embed-types.enum'; | ||
import { <%= classify(nameSingular) %>PatchParameters } from './<%= dasherize(nameSingular) %>-patch-model'; | ||
import { <%= classify(name) %>} from './<%= dasherize(name) %>.class'; | ||
import { <%= classify(nameSingular) %>QueryParameters } from './<%= dasherize(nameSingular) %>.query-model'; | ||
import { <%= classify(nameSingular) %> } from './<%= dasherize(nameSingular) %>.model'; | ||
import { expect } from 'chai'; | ||
import { Database } from 'common/database'; | ||
import { <%= classify(nameSingular) %>, <%= classify(name) %>, <%= classify(nameSingular) %>EmbedTypes, <%= classify(nameSingular) %>PatchParameters, <%= classify(nameSingular) %>QueryParameters} from './'; | ||
describe('<%= classify(nameSingular) %>PatchParameters Model', () => { | ||
it('expect to have no properties', () => { | ||
const <%= nameSingular %>PatchParameters: <%= classify(nameSingular) %>PatchParameters = new <%= classify(nameSingular) %>PatchParameters(); | ||
expect(<%= nameSingular %>PatchParameters).to.be.empty; | ||
const parameters: <%= classify(nameSingular) %>PatchParameters = new <%= classify(nameSingular) %>PatchParameters(); | ||
expect(parameters).to.be.empty; | ||
}) | ||
@@ -26,7 +21,7 @@ }) | ||
describe('<%= classify(nameSingular) %> Model', () => { | ||
const <%= nameSingular %>Properties = ['POPULATE_ME']; | ||
const properties = ['POPULATE_ME']; | ||
it('expect to have all properties = null', () => { | ||
const <%= nameSingular %>: <%= classify(nameSingular) %> = new <%= classify(nameSingular) %>(); | ||
expect(<%= nameSingular %>).to.have.all.keys(<%= nameSingular %>Properties); | ||
const model: <%= classify(nameSingular) %> = new <%= classify(nameSingular) %>(); | ||
expect(model).to.have.all.keys(properties); | ||
}) | ||
@@ -36,7 +31,7 @@ }) | ||
describe('<%= classify(nameSingular) %>QueryParameters Model', () => { | ||
const <%= nameSingular %>QueryParametersProperties = ['POPULATE_ME']; | ||
const properties = ['POPULATE_ME']; | ||
it('expect to have all properties with null values', () => { | ||
const <%= nameSingular %>QueryParameters: <%= classify(nameSingular) %>QueryParameters = new <%= classify(nameSingular) %>QueryParameters(); | ||
expect(<%= nameSingular %>QueryParameters).to.have.all.keys(<%= nameSingular %>QueryParametersProperties); | ||
const parameters: <%= classify(nameSingular) %>QueryParameters = new <%= classify(nameSingular) %>QueryParameters(); | ||
expect(parameters).to.have.all.keys(properties); | ||
}) | ||
@@ -46,17 +41,164 @@ }) | ||
describe('Get<%= classify(nameSingular) %>', () => { | ||
let getFirst: any; | ||
beforeEach(() => { getFirst = sinon.stub(Common, 'getFirst'); }); | ||
afterEach(() => { getFirst.restore(); }); | ||
let mssqlRequest: any; | ||
beforeEach(() => { mssqlRequest = sinon.stub(Request.prototype, 'execute'); }); | ||
afterEach(() => { mssqlRequest.restore(); }); | ||
const properties = ['PROPERTY1', 'PROPERTY2']; | ||
let resultSet = { | ||
recordsets: [[{ 'JSON_F52E2B61-18A1-11d1-B105-00805F49916B': '[{ "description": "Communicate with customers and co-workers", "icon": "chat", "id": "2", "isBeta": false, "isInternalApp": false, "name": "Communicator", "url": "http://localhost:8087"}]' }]], | ||
recordset: [{ 'JSON_F52E2B61-18A1-11d1-B105-00805F49916B': '[{ "description": "Communicate with customers and co-workers", "icon": "chat", "id": "2", "isBeta": false, "isInternalApp": false, "name": "Communicator", "url": "http://localhost:8087"}]' }], | ||
output: {}, | ||
rowsAffected: [1, 1], | ||
returnValue: 0 | ||
}; | ||
it('should return a <%= nameSingular %>', () => { | ||
let <%= nameSingular %>QueryParameters: <%= classify(nameSingular) %>QueryParameters = new <%= classify(nameSingular) %>QueryParameters({ id: 338 }); | ||
getFirst.returns(new <%= classify(nameSingular) %>({id:'POPULATE_THIS_OBJECT' })); | ||
let parameters: <%= classify(nameSingular) %>QueryParameters = new <%= classify(nameSingular) %>QueryParameters(); | ||
mssqlRequest.returns(resultSet); | ||
return <%= classify(name) %>.get<%= classify(nameSingular) %>(<%= nameSingular %>QueryParameters, 'POPULATE_ME').then( | ||
(<%= nameSingular %>:<%= classify(nameSingular) %>) => { | ||
expect(<%= nameSingular %>).to.be.a('object').to.include({ id: 'POPULATE_THIS_OBJECT' }); | ||
return <%= classify(name) %>.get<%= classify(nameSingular) %>(parameters.id).then( | ||
(resource: <%= classify(nameSingular) %>) => { | ||
expect(resource).to.be.a('object').to.include({ id: 'POPULATE_THIS_OBJECT' }); | ||
expect(resource).to.have.all.keys([properties]); | ||
} | ||
) | ||
}); | ||
}) | ||
describe('Get<%= classify(name) %>', () => { | ||
let mssqlRequest: any; | ||
beforeEach(() => { mssqlRequest = sinon.stub(Request.prototype, 'execute'); }); | ||
afterEach(() => { mssqlRequest.restore(); }); | ||
const properties = ['PROPERTY1', 'PROPERTY2']; | ||
let resultSet = { | ||
recordsets: [[{ 'JSON_F52E2B61-18A1-11d1-B105-00805F49916B': '[{ "description": "Communicate with customers and co-workers", "icon": "chat", "id": 2, "isBeta": false, "isInternalApp": false, "name": "Communicator", "url": "http://localhost:8087"},{ "description": "Manage your vehicle sales activities and prospects", "icon": "monetization_on", "id": "4", "isBeta": false, "isInternalApp": false, "name": "Sales Planner", "url": "http://localhost:8088/crm/salesplanner"}]' }]], | ||
recordset: [{ 'JSON_F52E2B61-18A1-11d1-B105-00805F49916B': '[{ "description": "Communicate with customers and co-workers", "icon": "chat", "id": 2, "isBeta": false, "isInternalApp": false, "name": "Communicator", "url": "http://localhost:8087"}]' }], | ||
output: {}, | ||
rowsAffected: [1, 1], | ||
returnValue: 0 | ||
}; | ||
describe('Get <%= classify(name) %> with no query parameters', () => { | ||
it('should return 2 <%= name %>', () => { | ||
let parameters: <%= classify(nameSingular) %>QueryParameters = new <%= classify(nameSingular) %>QueryParameters(); | ||
mssqlRequest.returns(resultSet); | ||
return <%= classify(name) %>.get<%= classify(nameSingular) %>s(parameters).then( | ||
(resource: <%= classify(nameSingular) %>[]) => { | ||
expect(resource).to.have.lengthOf(2); | ||
expect(resource[0]).to.be.a('object').to.include({ id: 'POPULATE_THIS_OBJECT' }); | ||
expect(resource[0]).to.have.all.keys(properties); | ||
}) | ||
}) | ||
}) | ||
describe('Get <%= classify(name) %> using query parameters', () => { | ||
it('should return 2 <%= name %>', () => { | ||
let parameters: <%= classify(nameSingular) %>QueryParameters = new <%= classify(nameSingular) %>QueryParameters({ isBeta: false }); | ||
mssqlRequest.returns(resultSet); | ||
return <%= classify(name) %>.get<%= classify(nameSingular) %>s(parameters).then( | ||
(resource: <%= classify(nameSingular) %>[]) => { | ||
expect(resource).to.have.lengthOf(2); | ||
expect(resource[0]).to.be.a('object').to.include({ id: 'POPULATE_THIS_OBJECT' }); | ||
expect(resource[0]).to.have.all.keys(properties); | ||
}) | ||
}) | ||
}) | ||
}) | ||
describe('Patch<%=classify(nameSingular)%>', () => { | ||
let mssqlRequest: any; | ||
beforeEach(() => { mssqlRequest = sinon.stub(Request.prototype, 'execute'); }); | ||
afterEach(() => { mssqlRequest.restore(); }); | ||
const properties = ['PROPERTY1']; | ||
let resultSet = { | ||
recordsets: [ | ||
[ | ||
{ 'JSON_F52E2B61-18A1-11d1-B105-00805F49916B': '[{"apiKey": "ydtd6d6dtyd6dyd6d6","apiUrl":"www.quorum-api.com","communicatorUrl": "www.qcommunicator.com","createdById": 1020}]' } | ||
] | ||
], | ||
recordset: [{ 'JSON_F52E2B61-18A1-11d1-B105-00805F49916B': '[{"apiKey": "ydtd6d6dtyd6dyd6d6","apiUrl":"www.quorum-api.com","communicatorUrl": "www.qcommunicator.com","createdById": 1020}]' }], | ||
output: {}, | ||
rowsAffected: [1, 1], | ||
returnValue: 0 | ||
}; | ||
it('should update and return a store', () => { | ||
mssqlRequest.returns(resultSet); | ||
let parameters: <%=classify(nameSingular)%>PatchParameters = new <%= classify(nameSingular)%>PatchParameters({ name: 'Quorum Motors' }); | ||
return <%= classify(name)%>.patch<%=classify(nameSingular)%>('SOME ID', parameters).then( | ||
(<%=nameSingular%>: <%= classify(nameSingular)%>) => { | ||
expect(<%=nameSingular%>).to.be.a('object').to.have.all.keys(properties); | ||
expect(<%=nameSingular%>).to.be.a('object').that.contains({ name: "Quorum Motors" }); | ||
} | ||
) | ||
}); | ||
}) | ||
describe('Post<%=classify(nameSingular)%>', () => { | ||
let mssqlRequest: any; | ||
beforeEach(() => { mssqlRequest = sinon.stub(Request.prototype, 'execute'); }); | ||
afterEach(() => { mssqlRequest.restore(); }); | ||
const properties = ['PROPERTY1']; | ||
let resultSet = { | ||
recordsets: [ | ||
[ | ||
{ 'JSON_F52E2B61-18A1-11d1-B105-00805F49916B': '[{"apiKey": "ydtd6d6dtyd6dyd6d6","apiUrl":"www.quorum-api.com","communicatorUrl": "www.qcommunicator.com","createdById": 1020}]' } | ||
] | ||
], | ||
recordset: [{ 'JSON_F52E2B61-18A1-11d1-B105-00805F49916B': '[{"apiKey": "ydtd6d6dtyd6dyd6d6","apiUrl":"www.quorum-api.com","communicatorUrl": "www.qcommunicator.com","createdById": 1020}]' }], | ||
output: {}, | ||
rowsAffected: [1, 1], | ||
returnValue: 0 | ||
}; | ||
it('should create and return a dataTerminal', () => { | ||
mssqlRequest.returns(resultSet); | ||
let <%=nameSingular%>: <%= classify(nameSingular)%> = new <%= classify(nameSingular)%>({ }); | ||
return <%= classify(name)%>.post<%= classify(nameSingular)%>(<%= nameSingular%>).then( | ||
(<%=nameSingular%>: <%=classify(nameSingular)%>) => { | ||
expect(<%=nameSingular%>).to.be.a('object').to.have.all.keys(properties); | ||
expect(<%=nameSingular%>).to.be.a('object').that.contains({ name: "Quorum Motors" }); | ||
} | ||
) | ||
}); | ||
}) | ||
describe('Put<%=classify(nameSingular)%>', () => { | ||
let mssqlRequest: any; | ||
beforeEach(() => { mssqlRequest = sinon.stub(Request.prototype, 'execute'); }); | ||
afterEach(() => { mssqlRequest.restore(); }); | ||
const properties = ['apiKey', 'apiUrl', 'communicatorUrl', 'createdById', 'databaseName', 'databaseServer', 'dataTerminalId', 'id', 'name', 'utcCreated', 'version', 'xselleratorIdentifier']; | ||
let resultSet = { | ||
recordsets: [ | ||
[ | ||
{ 'JSON_F52E2B61-18A1-11d1-B105-00805F49916B': '[{"apiKey": "ydtd6d6dtyd6dyd6d6","apiUrl":"www.quorum-api.com","communicatorUrl": "www.qcommunicator.com","createdById": 1020}]' } | ||
] | ||
], | ||
recordset: [{ 'JSON_F52E2B61-18A1-11d1-B105-00805F49916B': '[{"apiKey": "ydtd6d6dtyd6dyd6d6","apiUrl":"www.quorum-api.com","communicatorUrl": "www.qcommunicator.com","createdById": 1020}]' }], | ||
output: {}, | ||
rowsAffected: [1, 1], | ||
returnValue: 0 | ||
}; | ||
it('should update and return a dataTerminal', () => { | ||
mssqlRequest.returns(resultSet); | ||
let <%=nameSingular%>: <%=classify(nameSingular)%> = new <%=classify(nameSingular)%>({ }); | ||
return <%=classify(name)%>.put<%=classify(nameSingular)%>(<%=nameSingular%>).then( | ||
(<%=nameSingular%>: <%=classify(nameSingular)%>) => { | ||
expect(<%=nameSingular%>).to.be.a('object').to.have.all.keys(properties); | ||
expect(<%=nameSingular%>).to.be.a('object').that.contains({ name: "Quorum Motors" }); | ||
} | ||
) | ||
}); | ||
}) |
export class <%= classify(nameSingular) %> { | ||
id: number = null; | ||
constructor(obj?: any){ | ||
if (obj) Object.assign(this,obj); | ||
constructor(obj?: any) { | ||
if (obj) Object.assign(this, obj); | ||
} | ||
} |
export class <%= classify(nameSingular) %>PatchParameters { | ||
id: number; | ||
constructor(obj?: any){ | ||
if (obj) Object.assign(this,obj); | ||
constructor(obj ?: any){ | ||
if (obj) Object.assign(this, obj); | ||
} | ||
} |
export class <%= classify(nameSingular) %>QueryParameters { | ||
constructor(obj?: any){ | ||
if (obj) Object.assign(this,obj); | ||
embed: string = null; | ||
id: string = null; | ||
constructor(obj ?: any){ | ||
if (obj) Object.assign(this, obj); | ||
} | ||
} |
@@ -24,3 +24,4 @@ import { Server } from 'restify'; | ||
server: req.routingDetails.databaseServer, | ||
dsn: req.routingDetails.databaseName | ||
dsn: req.routingDetails.databaseName, | ||
version: parseInt(req.params.version) | ||
}) | ||
@@ -120,3 +121,4 @@ ); | ||
server: req.routingDetails.databaseServer, | ||
dsn: req.routingDetails.databaseName | ||
dsn: req.routingDetails.databaseName, | ||
version: parseInt(req.params.version) | ||
}) | ||
@@ -123,0 +125,0 @@ ); |
@@ -50,2 +50,3 @@ import { StoredProcedure } from "common/message-broker"; | ||
.then((res: BrokerMessage) => { | ||
res = response; | ||
<%= nameSingular %> = response.payloads[0].recordsets[0][0]; | ||
@@ -52,0 +53,0 @@ done(); |
export class <%= classify(nameSingular) %> { | ||
id: number; | ||
constructor(obj?: any){ | ||
@@ -4,0 +5,0 @@ if (obj) Object.assign(this,obj); |
47773
24
828