Skip to content

Commit

Permalink
Updated clientModel
Browse files Browse the repository at this point in the history
  • Loading branch information
Nataniel López committed May 18, 2020
1 parent 0d73fcc commit 96eb272
Show file tree
Hide file tree
Showing 4 changed files with 35 additions and 98 deletions.
6 changes: 1 addition & 5 deletions lib/api-create.js
Original file line number Diff line number Diff line change
Expand Up @@ -19,10 +19,6 @@ class ClientCreateAPI extends API {
return this._clientModel;
}

get databaseSettingsSource() {
return this.clientModel.databaseKey;
}

async validate(data = this.data) {

if(data === null || typeof data !== 'object' || Array.isArray(data))
Expand All @@ -34,7 +30,7 @@ class ClientCreateAPI extends API {

async process({ clients: clientCodes } = this.data) {

const databaseSettings = Settings.get('database')[this.databaseSettingsSource];
const databaseSettings = Settings.get('database')[this.clientModel.databaseKey];

const clientDatabase = {
dbHost: databaseSettings.host
Expand Down
6 changes: 1 addition & 5 deletions lib/listener-created.js
Original file line number Diff line number Diff line change
Expand Up @@ -26,13 +26,9 @@ class ClientCreatedListener extends EventListener {
return this._clientModel;
}

get databaseSettingsSource() {
return this.clientModel.databaseKey;
}

async process(clientCode = this.eventId) {

const databaseSettings = Settings.get('database')[this.databaseSettingsSource];
const databaseSettings = Settings.get('database')[this.clientModel.databaseKey];

const clientDatabase = {
dbHost: databaseSettings.host,
Expand Down
9 changes: 7 additions & 2 deletions lib/model-client.js
Original file line number Diff line number Diff line change
@@ -1,15 +1,20 @@
'use strict';

const Model = require('@janiscommerce/model');
const Settings = require('@janiscommerce/settings');

class Client extends Model {

static get settings() {
return Settings.get('clients') || {};
}

get databaseKey() {
return 'newClients';
return this.constructor.settings.databaseKey || 'core';
}

static get table() {
return 'clients';
return this.settings.table || 'clients';
}

static get uniqueIndexes() {
Expand Down
112 changes: 26 additions & 86 deletions tests/api-create.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,52 +13,39 @@ const fakeWrongClientPath = path.join(process.cwd(), process.env.MS_PATH || '',

describe('APIs', () => {

describe('Client Create API', () => {
describe.only('Client Create API', () => {

This comment has been minimized.

Copy link
@juanhapes

juanhapes May 18, 2020

Member

👀👀👀


const fakeSettings = {
newClients: {
host: 'some-database-host'
},
otherDb: {
host: 'other-database-host'
}
};

const fakeFullSettings = {
newClients: {
host: 'some-database-host',
protocol: 'some-protocol://',
port: 27017,
user: 'some-user',
password: 'some-password'
database: {

core: {
host: 'core-database-host',
protocol: 'core-protocol://',
port: 27017,
user: 'core-user',
password: 'core-password'
},
newClients: {
host: 'some-database-host',
protocol: 'some-protocol://',
port: 27017,
user: 'some-user',
password: 'some-password'
}
},
otherDb: {
host: 'other-database-host',
protocol: 'other-protocol://',
port: 27017,
user: 'other-user',
password: 'other-password'
clients: {
databaseKey: 'newClients'
}
};

const expectedClientObject = {
code: 'some-client',
status: ClientModel.statuses.active,
dbHost: fakeSettings.newClients.host,
dbDatabase: 'janis-some-client'
};

const expectedOtherClientObject = {
...expectedClientObject,
dbHost: fakeSettings.otherDb.host
};

const expectedFullClientObject = {
...expectedClientObject,
dbProtocol: fakeFullSettings.newClients.protocol,
dbPort: fakeFullSettings.newClients.port,
dbUser: fakeFullSettings.newClients.user,
dbPassword: fakeFullSettings.newClients.password
dbProtocol: fakeSettings.database.newClients.protocol,
dbPort: fakeSettings.database.newClients.port,
dbUser: fakeSettings.database.newClients.user,
dbPassword: fakeSettings.database.newClients.password
};

APITest(ClientCreateAPI, '/api/client', [
Expand All @@ -82,7 +69,7 @@ describe('APIs', () => {
mockRequire(fakeClientPath, ClientModel);

sandbox.stub(Settings, 'get')
.returns(fakeSettings);
.callsFake(setting => fakeSettings[setting]);

sandbox.stub(ClientModel.prototype, 'multiSave')
.resolves(true);
Expand All @@ -106,7 +93,7 @@ describe('APIs', () => {
sandbox.assert.calledOnceWithExactly(ClientCreateAPI.prototype.postSaveHook, ['some-client', 'other-client'], fakeSettings.newClients);
mockRequire.stop(fakeClientPath);
}
},
}/* ,
{
description: 'Should save all the received new clients to clients DB with full database config',
session: true,
Expand Down Expand Up @@ -153,53 +140,6 @@ describe('APIs', () => {
mockRequire.stop(fakeClientPath);
}
},
{
description: 'Should save all the received new clients to clients DB using a different db config',
session: true,
request: {
data: {
clients: [
'some-client',
'other-client'
]
}
},
response: {
code: 200
},
before: sandbox => {

mockRequire(fakeClientPath, ClientModel);

sandbox.stub(ClientCreateAPI.prototype, 'databaseSettingsSource')
.get(() => 'otherDb');

sandbox.stub(Settings, 'get')
.returns(fakeSettings);

sandbox.stub(ClientModel.prototype, 'multiSave')
.resolves(true);

sandbox.stub(MongoDBIndexCreator.prototype, 'executeForClientDatabases')
.resolves();

sandbox.spy(ClientCreateAPI.prototype, 'postSaveHook');
},
after: (res, sandbox) => {

sandbox.assert.calledOnceWithExactly(ClientModel.prototype.multiSave, [
expectedOtherClientObject,
{
...expectedOtherClientObject,
code: 'other-client',
dbDatabase: 'janis-other-client'
}
]);

sandbox.assert.calledOnceWithExactly(ClientCreateAPI.prototype.postSaveHook, ['some-client', 'other-client'], fakeSettings.otherDb);
mockRequire.stop(fakeClientPath);
}
},
{
description: 'Should return 500 when the client model multiSave fails',
session: true,
Expand Down Expand Up @@ -323,7 +263,7 @@ describe('APIs', () => {
sandbox.assert.notCalled(MongoDBIndexCreator.prototype.executeForClientDatabases);
mockRequire.stop(fakeClientPath);
}
}
}*/
]);
});
});

0 comments on commit 96eb272

Please sign in to comment.