Skip to content

Commit

Permalink
Minor corrections and code improvement
Browse files Browse the repository at this point in the history
  • Loading branch information
Nataniel López committed Apr 26, 2022
1 parent e1ada47 commit ca922be
Show file tree
Hide file tree
Showing 7 changed files with 78 additions and 56 deletions.
18 changes: 2 additions & 16 deletions lib/api-create.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,9 @@ const { API } = require('@janiscommerce/api');
const { struct } = require('@janiscommerce/superstruct');

const { Invoker } = require('@janiscommerce/lambda');
const MicroserviceCall = require('@janiscommerce/microservice-call');

const ModelFetcher = require('./helpers/model-fetcher');
const getClients = require('./helpers/get-clients');

module.exports = class ClientCreateAPI extends API {

Expand All @@ -23,7 +23,7 @@ module.exports = class ClientCreateAPI extends API {
const ClientModel = ModelFetcher.get(); // se tiene que usar el modelo del servicio
const model = new ClientModel();

const clients = ClientModel.validateAdditionalFields() ? await this.getClients(clientCodes) : clientCodes.map(code => ({ code }));
const clients = ClientModel.validateAdditionalFields() ? await getClients(clientCodes) : clientCodes.map(code => ({ code }));

if(!clients) {
logger.error('Unable to get Janis ID clients, they won\'t be created.');
Expand All @@ -48,18 +48,4 @@ module.exports = class ClientCreateAPI extends API {
async postSaveHook() {
return true;
}

async getClients(clientCodes) {

const msCall = new MicroserviceCall();

const { statusCode, body } = await msCall.safeList('id', 'client', { filters: { clientCode: clientCodes } });

if(statusCode >= 500) {
const errorMessage = body && body.message ? `${body.message}` : 'Service failed';
throw new Error(`Failed to get Janis ID clients: ${errorMessage}`);
}

return statusCode < 400 && body.length && body;
}
};
12 changes: 6 additions & 6 deletions lib/helpers/client-formatter.js
Original file line number Diff line number Diff line change
Expand Up @@ -28,14 +28,14 @@ module.exports = class ClientFormatter {
status: this.statusActive
};

if(!additionalFields)
return formattedClient;
if(additionalFields) {

additionalFields.forEach(field => {
additionalFields.forEach(field => {

if(client[field])
formattedClient[field] = client[field];
});
if(client[field])
formattedClient[field] = client[field];
});
}

return formattedClient;
}
Expand Down
17 changes: 17 additions & 0 deletions lib/helpers/get-clients.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
'use strict';

const MicroserviceCall = require('@janiscommerce/microservice-call');

module.exports = async clientCodes => {

const msCall = new MicroserviceCall();

const { statusCode, body } = await msCall.safeList('id', 'client', { filters: { clientCode: clientCodes }, limit: clientCodes.length });

if(statusCode >= 500) {
const errorMessage = body && body.message ? `${body.message}` : 'Service failed';
throw new Error(`Failed to get Janis ID clients: ${errorMessage}`);
}

return statusCode < 400 && body.length && body;
};
15 changes: 3 additions & 12 deletions lib/listener-created.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,9 @@ const logger = require('lllog')();
const { EventListener } = require('@janiscommerce/event-listener');

const MongoDBIndexCreator = require('@janiscommerce/mongodb-index-creator');
const MicroserviceCall = require('@janiscommerce/microservice-call');

const ModelFetcher = require('./helpers/model-fetcher');
const getClients = require('./helpers/get-clients');

const mongoDBIndexCreator = new MongoDBIndexCreator();

Expand Down Expand Up @@ -41,17 +41,8 @@ module.exports = class ClientCreatedListener extends EventListener {
}

async getClient(clientCode) {

const msCall = new MicroserviceCall();

const { statusCode, body } = await msCall.safeList('id', 'client', { filters: { clientCode }, limit: 1 });

if(statusCode >= 500) {
const errorMessage = body && body.message ? `${body.message}` : 'Service failed';
throw new Error(`Failed to get Janis ID client: ${errorMessage}`);
}

return statusCode < 400 && body[0];
const client = await getClients([clientCode]);
return client && client[0];
}

/**
Expand Down
4 changes: 4 additions & 0 deletions lib/model-client.js
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,10 @@ module.exports = class Client extends Model {
return ['databases'];
}

/**
* Get additional fields
* @returns {undefined} returns undefined by default
*/
static get additionalFields() {
return undefined;
}
Expand Down
48 changes: 36 additions & 12 deletions tests/api-create.js
Original file line number Diff line number Diff line change
Expand Up @@ -106,7 +106,7 @@ describe('Client Create API', () => {
stubGetSecret(sandbox);

sandbox.stub(MicroserviceCall.prototype, 'safeList')
.withArgs('id', 'client', { filters: { clientCode: clients } })
.withArgs('id', 'client', { filters: { clientCode: clients }, limit: clients.length })
.resolves({ statusCode: 200, body: clientsToSaveWithAdditionalFields });

sandbox.stub(ModelClient.prototype, 'multiSave')
Expand All @@ -122,7 +122,11 @@ describe('Client Create API', () => {

assertSecretsGet(sandbox, janisServiceName);

sandbox.assert.calledOnceWithExactly(MicroserviceCall.prototype.safeList, 'id', 'client', { filters: { clientCode: clients } });
sandbox.assert.calledOnceWithExactly(MicroserviceCall.prototype.safeList, 'id', 'client', {
filters: { clientCode: clients },
limit: clients.length
});

sandbox.assert.calledOnceWithExactly(ModelClient.prototype.multiSave, clientsToSaveWithAdditionalFields);
sandbox.assert.calledOnceWithExactly(Invoker.call, 'MongoDBIndexCreator');

Expand Down Expand Up @@ -348,7 +352,7 @@ describe('Client Create API', () => {
stubGetSecret(sandbox);

sandbox.stub(MicroserviceCall.prototype, 'safeList')
.withArgs('id', 'client', { filters: { clientCode: clients } })
.withArgs('id', 'client', { filters: { clientCode: clients }, limit: clients.length })
.resolves({ statusCode: 400, body: {} });

sandbox.spy(ModelClient.prototype, 'multiSave');
Expand All @@ -360,7 +364,11 @@ describe('Client Create API', () => {

secretsNotCalled(sandbox);

sandbox.assert.calledOnceWithExactly(MicroserviceCall.prototype.safeList, 'id', 'client', { filters: { clientCode: clients } });
sandbox.assert.calledOnceWithExactly(MicroserviceCall.prototype.safeList, 'id', 'client', {
filters: { clientCode: clients },
limit: clients.length
});

sandbox.assert.notCalled(ModelClient.prototype.multiSave);
sandbox.assert.notCalled(Invoker.call);
sandbox.assert.notCalled(APICreate.prototype.postSaveHook);
Expand Down Expand Up @@ -389,7 +397,7 @@ describe('Client Create API', () => {
stubGetSecret(sandbox);

sandbox.stub(MicroserviceCall.prototype, 'safeList')
.withArgs('id', 'client', { filters: { clientCode: clients } })
.withArgs('id', 'client', { filters: { clientCode: clients }, limit: clients.length })
.resolves({ statusCode: 200, body: [clientsToSave[0]] });

sandbox.stub(ModelClient.prototype, 'multiSave')
Expand All @@ -405,7 +413,11 @@ describe('Client Create API', () => {

assertSecretsGet(sandbox, janisServiceName);

sandbox.assert.calledOnceWithExactly(MicroserviceCall.prototype.safeList, 'id', 'client', { filters: { clientCode: clients } });
sandbox.assert.calledOnceWithExactly(MicroserviceCall.prototype.safeList, 'id', 'client', {
filters: { clientCode: clients },
limit: clients.length
});

sandbox.assert.calledOnceWithExactly(ModelClient.prototype.multiSave, [clientsToSave[0]]);
sandbox.assert.calledOnceWithExactly(Invoker.call, 'MongoDBIndexCreator');

Expand Down Expand Up @@ -439,7 +451,7 @@ describe('Client Create API', () => {
stubGetSecret(sandbox);

sandbox.stub(MicroserviceCall.prototype, 'safeList')
.withArgs('id', 'client', { filters: { clientCode: clients } })
.withArgs('id', 'client', { filters: { clientCode: clients }, limit: clients.length })
.resolves({ statusCode: 200, body: [] });

sandbox.spy(ModelClient.prototype, 'multiSave');
Expand All @@ -451,7 +463,11 @@ describe('Client Create API', () => {

secretsNotCalled(sandbox);

sandbox.assert.calledOnceWithExactly(MicroserviceCall.prototype.safeList, 'id', 'client', { filters: { clientCode: clients } });
sandbox.assert.calledOnceWithExactly(MicroserviceCall.prototype.safeList, 'id', 'client', {
filters: { clientCode: clients },
limit: clients.length
});

sandbox.assert.notCalled(ModelClient.prototype.multiSave);
sandbox.assert.notCalled(Invoker.call);
sandbox.assert.notCalled(APICreate.prototype.postSaveHook);
Expand Down Expand Up @@ -480,7 +496,7 @@ describe('Client Create API', () => {
stubGetSecret(sandbox);

sandbox.stub(MicroserviceCall.prototype, 'safeList')
.withArgs('id', 'client', { filters: { clientCode: clients } })
.withArgs('id', 'client', { filters: { clientCode: clients }, limit: clients.length })
.resolves({ statusCode: 500, body: {} });

sandbox.spy(ModelClient.prototype, 'multiSave');
Expand All @@ -492,7 +508,11 @@ describe('Client Create API', () => {

secretsNotCalled(sandbox);

sandbox.assert.calledOnceWithExactly(MicroserviceCall.prototype.safeList, 'id', 'client', { filters: { clientCode: clients } });
sandbox.assert.calledOnceWithExactly(MicroserviceCall.prototype.safeList, 'id', 'client', {
filters: { clientCode: clients },
limit: clients.length
});

sandbox.assert.notCalled(ModelClient.prototype.multiSave);
sandbox.assert.notCalled(Invoker.call);
sandbox.assert.notCalled(APICreate.prototype.postSaveHook);
Expand Down Expand Up @@ -521,7 +541,7 @@ describe('Client Create API', () => {
stubGetSecret(sandbox);

sandbox.stub(MicroserviceCall.prototype, 'safeList')
.withArgs('id', 'client', { filters: { clientCode: clients } })
.withArgs('id', 'client', { filters: { clientCode: clients }, limit: clients.length })
.resolves({ statusCode: 500, body: { message: 'Some API Error' } });

sandbox.spy(ModelClient.prototype, 'multiSave');
Expand All @@ -533,7 +553,11 @@ describe('Client Create API', () => {

secretsNotCalled(sandbox);

sandbox.assert.calledOnceWithExactly(MicroserviceCall.prototype.safeList, 'id', 'client', { filters: { clientCode: clients } });
sandbox.assert.calledOnceWithExactly(MicroserviceCall.prototype.safeList, 'id', 'client', {
filters: { clientCode: clients },
limit: clients.length
});

sandbox.assert.notCalled(ModelClient.prototype.multiSave);
sandbox.assert.notCalled(Invoker.call);
sandbox.assert.notCalled(APICreate.prototype.postSaveHook);
Expand Down
20 changes: 10 additions & 10 deletions tests/listener-created.js
Original file line number Diff line number Diff line change
Expand Up @@ -170,7 +170,7 @@ describe('Client Created Listener', async () => {
delete ClientFormatter.settings;

sandbox.stub(MicroserviceCall.prototype, 'safeList')
.withArgs('id', 'client', { filters: { clientCode: validEvent.id }, limit: 1 })
.withArgs('id', 'client', { filters: { clientCode: [validEvent.id] }, limit: 1 })
.resolves({ statusCode: 200, body: [{ ...fakeClient, extraField: 'some-data', randomField: 'foobar' }] });

mockModelClient();
Expand Down Expand Up @@ -198,7 +198,7 @@ describe('Client Created Listener', async () => {

const expectedClientToSave = { ...fakeClient, extraField: 'some-data' };

sandbox.assert.calledOnceWithExactly(MicroserviceCall.prototype.safeList, 'id', 'client', { filters: { clientCode: validEvent.id }, limit: 1 });
sandbox.assert.calledOnceWithExactly(MicroserviceCall.prototype.safeList, 'id', 'client', { filters: { clientCode: [validEvent.id] }, limit: 1 });
sandbox.assert.calledOnceWithExactly(ModelClient.prototype.save, expectedClientToSave);
sandbox.assert.calledOnceWithExactly(MongoDBIndexCreator.prototype.executeForClientCode, validEvent.id);
sandbox.assert.calledOnceWithExactly(ListenerCreated.prototype.postSaveHook, validEvent.id, expectedClientToSave);
Expand All @@ -216,7 +216,7 @@ describe('Client Created Listener', async () => {
delete ClientFormatter.settings;

sandbox.stub(MicroserviceCall.prototype, 'safeList')
.withArgs('id', 'client', { filters: { clientCode: validEvent.id }, limit: 1 })
.withArgs('id', 'client', { filters: { clientCode: [validEvent.id] }, limit: 1 })
.resolves({ statusCode: 400, body: {} });

mockModelClient();
Expand All @@ -239,7 +239,7 @@ describe('Client Created Listener', async () => {

secretsNotCalled(sandbox);

sandbox.assert.calledOnceWithExactly(MicroserviceCall.prototype.safeList, 'id', 'client', { filters: { clientCode: validEvent.id }, limit: 1 });
sandbox.assert.calledOnceWithExactly(MicroserviceCall.prototype.safeList, 'id', 'client', { filters: { clientCode: [validEvent.id] }, limit: 1 });
sandbox.assert.notCalled(ModelClient.prototype.save);
sandbox.assert.notCalled(MongoDBIndexCreator.prototype.executeForClientCode);
sandbox.assert.notCalled(ListenerCreated.prototype.postSaveHook);
Expand All @@ -257,7 +257,7 @@ describe('Client Created Listener', async () => {
delete ClientFormatter.settings;

sandbox.stub(MicroserviceCall.prototype, 'safeList')
.withArgs('id', 'client', { filters: { clientCode: validEvent.id }, limit: 1 })
.withArgs('id', 'client', { filters: { clientCode: [validEvent.id] }, limit: 1 })
.resolves({ statusCode: 200, body: [] });

mockModelClient();
Expand All @@ -280,7 +280,7 @@ describe('Client Created Listener', async () => {

secretsNotCalled(sandbox);

sandbox.assert.calledOnceWithExactly(MicroserviceCall.prototype.safeList, 'id', 'client', { filters: { clientCode: validEvent.id }, limit: 1 });
sandbox.assert.calledOnceWithExactly(MicroserviceCall.prototype.safeList, 'id', 'client', { filters: { clientCode: [validEvent.id] }, limit: 1 });
sandbox.assert.notCalled(ModelClient.prototype.save);
sandbox.assert.notCalled(MongoDBIndexCreator.prototype.executeForClientCode);
sandbox.assert.notCalled(ListenerCreated.prototype.postSaveHook);
Expand All @@ -298,7 +298,7 @@ describe('Client Created Listener', async () => {
delete ClientFormatter.settings;

sandbox.stub(MicroserviceCall.prototype, 'safeList')
.withArgs('id', 'client', { filters: { clientCode: validEvent.id }, limit: 1 })
.withArgs('id', 'client', { filters: { clientCode: [validEvent.id] }, limit: 1 })
.resolves({ statusCode: 500, body: {} });

mockModelClient();
Expand All @@ -321,7 +321,7 @@ describe('Client Created Listener', async () => {

secretsNotCalled(sandbox);

sandbox.assert.calledOnceWithExactly(MicroserviceCall.prototype.safeList, 'id', 'client', { filters: { clientCode: validEvent.id }, limit: 1 });
sandbox.assert.calledOnceWithExactly(MicroserviceCall.prototype.safeList, 'id', 'client', { filters: { clientCode: [validEvent.id] }, limit: 1 });
sandbox.assert.notCalled(ModelClient.prototype.save);
sandbox.assert.notCalled(MongoDBIndexCreator.prototype.executeForClientCode);
sandbox.assert.notCalled(ListenerCreated.prototype.postSaveHook);
Expand All @@ -339,7 +339,7 @@ describe('Client Created Listener', async () => {
delete ClientFormatter.settings;

sandbox.stub(MicroserviceCall.prototype, 'safeList')
.withArgs('id', 'client', { filters: { clientCode: validEvent.id }, limit: 1 })
.withArgs('id', 'client', { filters: { clientCode: [validEvent.id] }, limit: 1 })
.resolves({ statusCode: 500, body: { message: 'Some API Error' } });

mockModelClient();
Expand All @@ -362,7 +362,7 @@ describe('Client Created Listener', async () => {

secretsNotCalled(sandbox);

sandbox.assert.calledOnceWithExactly(MicroserviceCall.prototype.safeList, 'id', 'client', { filters: { clientCode: validEvent.id }, limit: 1 });
sandbox.assert.calledOnceWithExactly(MicroserviceCall.prototype.safeList, 'id', 'client', { filters: { clientCode: [validEvent.id] }, limit: 1 });
sandbox.assert.notCalled(ModelClient.prototype.save);
sandbox.assert.notCalled(MongoDBIndexCreator.prototype.executeForClientCode);
sandbox.assert.notCalled(ListenerCreated.prototype.postSaveHook);
Expand Down

0 comments on commit ca922be

Please sign in to comment.