Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
14 changed files
with
557 additions
and
26 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,5 +1,6 @@ | ||
{ | ||
"exclude": [ | ||
"coverage/", | ||
"coverages/", | ||
"tests/", | ||
"mocks/", | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.