Skip to content

Commit

Permalink
Merge 79c04d0 into 3475b16
Browse files Browse the repository at this point in the history
  • Loading branch information
Alejandro-Gonzalez committed May 11, 2020
2 parents 3475b16 + 79c04d0 commit 0bd8c62
Show file tree
Hide file tree
Showing 14 changed files with 557 additions and 26 deletions.
1 change: 1 addition & 0 deletions .nycrc
@@ -1,5 +1,6 @@
{
"exclude": [
"coverage/",
"coverages/",
"tests/",
"mocks/",
Expand Down
72 changes: 72 additions & 0 deletions README.md
Expand Up @@ -296,3 +296,75 @@ 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 {

get(items, session) {

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

const entityIds = this.getIds('entityName');

const msCall = new MicroServiceCall(session);

const { body, statusCode } = await microServiceCall.safeList('service', 'entity', { filters: { id: entityIds } );

if(statusCode >= 400)
return {};

return this.mapIdToEntity(body)
}

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

class SomeController extends ControllerExport {

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

}

```
#### UserHelper
Methods:
* static *getUsers* - return a object with entity data for userCerated and userModified. if fail request, return a empty object.
```js
const { UserHelper, ControllerExport } = require('@janiscommerce/export');

class SomeController extends ControllerExport {

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

}

```
2 changes: 1 addition & 1 deletion lib/api-export.js
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
62 changes: 62 additions & 0 deletions lib/export-formatters.js
@@ -0,0 +1,62 @@
'use strict';

class ExportFormatters {

/**
* Format a user into a single string
*
* @param {Object} user
* @returns {string|null}
* @memberof ExportFormatters
*/
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}
* @memberof ExportFormatters
*/
static formatDate(dateToFormat) {

if(!dateToFormat)
return null;

const date = new Date(dateToFormat);

return date.toLocaleString();
}

/**
* Matches a list of entity ids with a specified entity field
* Returns the matched list formatted into a single string
*
* @param {Array} list The entity ids to filter
* @param {Object} entities
* @param {string} field The entity field to format
* @returns {string}
* @memberof ExportFormatters
*/
static formatListBy(list, entities, fieldToFormat) {

return Object.values(entities).reduce((formatted, { id, [fieldToFormat]: item }) => {

if(!list.includes(id))
return formatted;
return formatted ? `${formatted}, ${item}` : item;
}, '');
}

}

module.exports = ExportFormatters;
44 changes: 44 additions & 0 deletions lib/export-helpers/export-helper.js
@@ -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;
59 changes: 59 additions & 0 deletions lib/export-helpers/user-helper.js
@@ -0,0 +1,59 @@
'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();

if(!userIds.length)
return {};

const microServiceCall = session.getSessionInstance(MsCall);

const { body: users, statusCode } = await microServiceCall.safeList('id', 'user', { filters: { id: userIds } });

if(statusCode >= 400)
return {};

return this.mapIdToEntity(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
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
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 0bd8c62

Please sign in to comment.