Skip to content

Commit

Permalink
Using ApiSession to dropDatabase in removed listener
Browse files Browse the repository at this point in the history
  • Loading branch information
juanhapes committed Dec 2, 2021
1 parent b6a1ca8 commit 422e215
Show file tree
Hide file tree
Showing 4 changed files with 31 additions and 14 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.2.5] - 2021-12-02
### Fixed
- `RemovedListener` using ApiSession to make `dropDatabase()` possible

## [5.2.4] - 2021-12-02
### Fixed
- Now `RemovedListener` uses `event.id` instead of `event.client`
Expand Down
33 changes: 23 additions & 10 deletions lib/listener-removed.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
'use strict';

const { ApiSession } = require('@janiscommerce/api-session');
const { EventListener } = require('@janiscommerce/event-listener');
const Model = require('@janiscommerce/model');

Expand All @@ -21,33 +22,45 @@ module.exports = class ClientRemovedListener extends EventListener {
return this._model;
}

async process() {
get clientCode() {
return this.eventId;
}

const code = this.eventId;
async process() {

const clientToDrop = await this.model.getBy('code', code, { unique: true });
await this.setClient();

if(!clientToDrop) {
this.setCode(404).setBody('Client not found in Service');
if(!this.client) {
this.setCode(404)
.setBody('Client not found in Service');
return;
}

await this.dropDatabase();

await this.removeClient(code);
await this.removeClient();

return this.postRemovedHook(code);
return this.postRemovedHook(this.clientCode);
}

async setClient() {
this.client = await this.model.getBy('code', this.clientCode, { unique: true });
}

dropDatabase() {

const modelWithSession = this.session.getSessionInstance(Model);
const session = new ApiSession({
clientId: this.client.id,
clientCode: this.client.code
}, this.client);

const modelWithSession = session.getSessionInstance(Model);

return modelWithSession.dropDatabase();
}

removeClient(code) {
return this.model.remove({ code });
removeClient() {
return this.model.remove({ code: this.clientCode });
}

/**
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@
},
"dependencies": {
"@janiscommerce/api": "^6.2.1",
"@janiscommerce/api-session": "^3.1.1",
"@janiscommerce/api-session": "^3.2.0",
"@janiscommerce/aws-secrets-manager": "^0.2.0",
"@janiscommerce/event-listener": "^3.0.0",
"@janiscommerce/lambda": "^3.2.0",
Expand Down
6 changes: 3 additions & 3 deletions tests/listener-removed.js
Original file line number Diff line number Diff line change
Expand Up @@ -113,7 +113,7 @@ describe('Client Removed Listener', async () => {

mockModelClient();

sandbox.stub(ModelClient.prototype, 'getBy').resolves([client]);
sandbox.stub(ModelClient.prototype, 'getBy').resolves(client);

sandbox.stub(Model.prototype, 'dropDatabase').rejects();

Expand All @@ -135,7 +135,7 @@ describe('Client Removed Listener', async () => {

mockModelClient();

sandbox.stub(ModelClient.prototype, 'getBy').resolves([client]);
sandbox.stub(ModelClient.prototype, 'getBy').resolves(client);

sandbox.stub(Model.prototype, 'dropDatabase').resolves(true);

Expand All @@ -160,7 +160,7 @@ describe('Client Removed Listener', async () => {

mockModelClient();

sandbox.stub(ModelClient.prototype, 'getBy').resolves([client]);
sandbox.stub(ModelClient.prototype, 'getBy').resolves(client);

sandbox.stub(Model.prototype, 'dropDatabase').resolves(true);

Expand Down

0 comments on commit 422e215

Please sign in to comment.