Skip to content

Commit

Permalink
Lambda invoke for MongoDbIndexCreator
Browse files Browse the repository at this point in the history
  • Loading branch information
juanhapes committed Feb 5, 2021
1 parent 46e1b32 commit 44ed4e3
Show file tree
Hide file tree
Showing 4 changed files with 27 additions and 20 deletions.
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,10 @@ and this project adheres to [Semantic Versioning](http://semver.org/spec/v2.0.0.

## [Unreleased]

## [5.1.1] - 2021-02-105
### Changed
- API Create - Using Lambda.Invoker to create Indexes in MongoDB

## [5.1.0] - 2020-12-15
### Added
- `UpdatedListener` to activate or deactivate a client
Expand Down
8 changes: 4 additions & 4 deletions lib/api-create.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,9 @@

const { API } = require('@janiscommerce/api');

const MongoDBIndexCreator = require('@janiscommerce/mongodb-index-creator');
const ModelGetter = require('./helper/model-getter');
const { Invoker } = require('@janiscommerce/lambda');

const mongoDBIndexCreator = new MongoDBIndexCreator();
const ModelGetter = require('./helper/model-getter');

module.exports = class ClientCreateAPI extends API {

Expand All @@ -27,7 +26,8 @@ module.exports = class ClientCreateAPI extends API {
const clientsToCreate = clientCodes.map(code => this.model.getFormattedClient(code));

await this.model.multiSave(clientsToCreate);
await mongoDBIndexCreator.executeForClientDatabases();

await Invoker.call('MongoDBIndexCreator');

return this.postSaveHook(clientCodes);
}
Expand Down
15 changes: 8 additions & 7 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -26,13 +26,13 @@
"@janiscommerce/api-test": "^4.0.0",
"@janiscommerce/event-listener-test": "^3.0.0",
"eslint-config-airbnb-base": "^13.1.0",
"eslint-plugin-import": "^2.22.0",
"eslint": "^7.8.1",
"husky": "^4.3.6",
"eslint-plugin-import": "^2.22.1",
"eslint": "^7.19.0",
"husky": "^4.3.8",
"mocha": "^8.2.1",
"mock-require": "^3.0.3",
"nyc": "^15.1.0",
"sinon": "^9.2.2"
"sinon": "^9.2.4"
},
"files": [
"lib/"
Expand All @@ -41,11 +41,12 @@
"test": "tests"
},
"dependencies": {
"@janiscommerce/api": "^6.2.0",
"@janiscommerce/api-session": "^3.0.0",
"@janiscommerce/api": "^6.2.1",
"@janiscommerce/api-session": "^3.1.1",
"@janiscommerce/event-listener": "^3.0.0",
"@janiscommerce/lambda": "^3.1.0",
"@janiscommerce/microservice-call": "^4.2.0",
"@janiscommerce/model": "^5.3.0",
"@janiscommerce/model": "^5.3.1",
"@janiscommerce/mongodb-index-creator": "^2.2.3",
"@janiscommerce/settings": "^1.0.1"
}
Expand Down
20 changes: 11 additions & 9 deletions tests/api-create.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
'use strict';

const APITest = require('@janiscommerce/api-test');
const MongoDBIndexCreator = require('@janiscommerce/mongodb-index-creator');
const { Invoker } = require('@janiscommerce/lambda');
const Settings = require('@janiscommerce/settings');
const mockRequire = require('mock-require');
const path = require('path');
Expand Down Expand Up @@ -36,7 +36,7 @@ describe('Client Create API', () => {
sandbox.stub(ModelClient.prototype, 'multiSave')
.resolves(true);

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

sandbox.spy(APICreate.prototype, 'postSaveHook');
Expand All @@ -45,6 +45,8 @@ describe('Client Create API', () => {

sandbox.assert.calledOnceWithExactly(ModelClient.prototype.multiSave, clientsToSave);

sandbox.assert.calledOnceWithExactly(Invoker.call, 'MongoDBIndexCreator');

sandbox.assert.calledOnceWithExactly(
APICreate.prototype.postSaveHook,
clients
Expand All @@ -68,18 +70,18 @@ describe('Client Create API', () => {
sandbox.stub(ModelClient.prototype, 'multiSave')
.rejects();

sandbox.stub(MongoDBIndexCreator.prototype, 'executeForClientDatabases')
sandbox.stub(Invoker, 'call')
.resolves();
},
after: (res, sandbox) => {

mockRequire.stop(fakeClientPath);
sandbox.assert.calledOnceWithExactly(ModelClient.prototype.multiSave, clientsToSave);
sandbox.assert.notCalled(MongoDBIndexCreator.prototype.executeForClientDatabases);
sandbox.assert.notCalled(Invoker.call);
}
},
{
description: 'Should return 500 when the index creator fails',
description: 'Should return 500 when invoking the index creator lambda fails',
request: {
data: { clients }
},
Expand All @@ -93,14 +95,14 @@ describe('Client Create API', () => {
sandbox.stub(ModelClient.prototype, 'multiSave')
.resolves();

sandbox.stub(MongoDBIndexCreator.prototype, 'executeForClientDatabases')
sandbox.stub(Invoker, 'call')
.rejects();
},
after: (res, sandbox) => {

sandbox.assert.calledOnceWithExactly(ModelClient.prototype.multiSave, clientsToSave);

sandbox.assert.calledOnce(MongoDBIndexCreator.prototype.executeForClientDatabases);
sandbox.assert.calledOnceWithExactly(Invoker.call, 'MongoDBIndexCreator');
mockRequire.stop(fakeClientPath);
}
},
Expand Down Expand Up @@ -143,13 +145,13 @@ describe('Client Create API', () => {
sandbox.stub(ModelClient.prototype, 'multiSave')
.resolves(true);

sandbox.stub(MongoDBIndexCreator.prototype, 'executeForClientDatabases')
sandbox.stub(Invoker, 'call')
.resolves();
},
after: (res, sandbox) => {

sandbox.assert.notCalled(ModelClient.prototype.multiSave);
sandbox.assert.notCalled(MongoDBIndexCreator.prototype.executeForClientDatabases);
sandbox.assert.notCalled(Invoker.call);
mockRequire.stop(fakeClientPath);
}
}
Expand Down

0 comments on commit 44ed4e3

Please sign in to comment.