Skip to content

Commit

Permalink
Merge 31595e7 into 3475b16
Browse files Browse the repository at this point in the history
  • Loading branch information
Alejandro-Gonzalez committed May 7, 2020
2 parents 3475b16 + 31595e7 commit aa88eac
Show file tree
Hide file tree
Showing 14 changed files with 452 additions and 26 deletions.
1 change: 1 addition & 0 deletions .nycrc
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
{
"exclude": [
"coverage/",
"coverages/",
"tests/",
"mocks/",
Expand Down
69 changes: 69 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -296,3 +296,72 @@ For Start Exporting data `ApiExport` received in the body:
sortDirection: 'desc'
}
```


### Export Helpers

#### ExportHelper

Methods:

* static *getIds* - return a list of ids from an entity.

* static *mapIdToEntity* - Returns an object with attribute the id of the entity and the value the entity itself.

Usage

```js
const { ExportHelper } = require('@janiscommerce/export');

class MyEntityHelper extends ExportHelper {

getEntity(items, session) {

// important!! define this.items
this.items = items;

const entityIds = this.getIds(items);

const msCall = new MicroServiceCall(session);

let entyData = await microServiceCall.list('service', 'entity', { filters: { id: entityIds } });

return this.mapIdToEntity(entyData)
}

}
```

```js
const { ControllerExport } = require('@janiscommerce/export');
const { MyEntityHelper } = require('../mi-entity-helper');

class SomeConstroller extends ControllerExport {

async someMethod(items, session) {
this.entityData = await MyEntityHelper.getEntity(items, session)
}

}

```

#### UserHelper

Methods:

* static *getUsers* - return a object with entity data for userCerated and userModified

```js
const { UserHelper, ControllerExport } = require('@janiscommerce/export');

class SomeConstroller extends ControllerExport {

async someMethod(items, session) {
this.userData = await UserHelper.getUsers(items, session)
}

}

```

2 changes: 1 addition & 1 deletion lib/api-export.js
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ class ApiExport extends ApiSaveData {
}

async getUserEmail(id) {
const response = await this.session.getSessionInstance(MsCall).get('id', 'user', 'get', null, null, { id });
const response = await this.session.getSessionInstance(MsCall).call('id', 'user', 'get', null, null, { id });

return response.body && response.body.email;
}
Expand Down
40 changes: 40 additions & 0 deletions lib/export-formatters.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
'use strict';

class ExportFormatters {

/**
* Format a user into a single string
*
* @param {Object} user
* @returns {string|null}
*/
static formatUser(user) {

if(!user)
return null;

const firstname = user.firstname || '-';
const lastname = user.lastname || '-';
const email = user.email || '-';
return `${firstname} ${lastname} (${email})`;
}

/**
* It formats a date into locale.
*
* @param {string} dateToFormat
* @returns {string|null}
*/
static formatDate(dateToFormat) {

if(!dateToFormat)
return null;

const date = new Date(dateToFormat);

return date.toLocaleString();
}

}

module.exports = ExportFormatters;
44 changes: 44 additions & 0 deletions lib/export-helpers/export-helper.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
'use strict';

class ExportHelper {

/**
* return a list of ids from an entity.
*
* @static
* @param {string} entity
* @returns
* @memberof ExportHelper
*/
static getIds(entity) {

const ids = new Set();

this.items.forEach(({ [entity]: itemEntity }) => {

if(!itemEntity)
return;

if(!Array.isArray(itemEntity))
itemEntity = [itemEntity];

itemEntity.forEach(id => ids.add(id));
});

return [...ids];
}

/**
* Returns an object with atributte the id of the entity and the value the enitity itself.
*
* @param {Array} entities
* @memberof ExportHelper
*/
static mapIdToEntity(entities) {
return entities.reduce((entityMappedById, entity) => {
return { ...entityMappedById, [entity.id]: entity };
}, {});
}
}

module.exports = ExportHelper;
55 changes: 55 additions & 0 deletions lib/export-helpers/user-helper.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
'use strict';

const MsCall = require('@janiscommerce/microservice-call');
const ExportHelper = require('./export-helper');

class ExportUserHelper extends ExportHelper {

/**
*
* @static
* @param {Array} items
* @param {Object} session
* @returns
* @memberof ExportUserHelper
*/
static async getUsers(items, session) {
this.items = items;

const userIds = this.getIds();

const microServiceCall = new MsCall(session);

let users = await microServiceCall.list('id', 'user', { filters: { id: userIds } });

users = this.mapIdToEntity(users);

return users;
}

/**
* return a list of ids from users.
*
* @static
* @param {string} entity
* @returns
* @memberof ExportUserHelper
*/
static getIds() {

const ids = new Set();

this.items.forEach(({ userCreated, userModified }) => {

if(userCreated)
ids.add(userCreated);

if(userModified)
ids.add(userModified);
});

return [...ids];
}
}

module.exports = ExportUserHelper;
6 changes: 6 additions & 0 deletions lib/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,12 +6,18 @@ const ControllerExport = require('./controller-export');
const CreatedListener = require('./created-listener');
const ProcessedListener = require('./processed-listener');
const exportFunctions = require('./serverless-functions');
const ExportHelper = require('./export-helpers/export-helper');
const UserHelper = require('./export-helpers/user-helper');
const ExportFormatters = require('./export-formatters');

module.exports = {
ApiExport,
CreatedListener,
ProcessedListener,
ModelExport,
ControllerExport,
ExportHelper,
UserHelper,
ExportFormatters,
exportFunctions
};
44 changes: 33 additions & 11 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@
"@janiscommerce/event-emitter": "^2.0.2",
"@janiscommerce/event-listener": "^1.1.1",
"@janiscommerce/mail": "^2.0.0",
"@janiscommerce/microservice-call": "^3.0.1",
"@janiscommerce/microservice-call": "^4.0.0",
"@janiscommerce/model": "^3.6.3",
"@janiscommerce/s3": "^1.2.1",
"exceljs": "^3.6.1",
Expand Down

0 comments on commit aa88eac

Please sign in to comment.