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
12 changed files
with
1,825 additions
and
1,188 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 |
---|---|---|
@@ -0,0 +1 @@ | ||
node_modules |
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
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 |
---|---|---|
@@ -1,9 +1,9 @@ | ||
'use strict'; | ||
|
||
const EventParser = require('./event-parser'); | ||
const EventParserError = require('./event-parser-error'); | ||
const ModelEvent = require('./model-event'); | ||
|
||
module.exports = { | ||
EventParser, | ||
EventParserError | ||
ModelEvent | ||
}; |
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,18 @@ | ||
'use strict'; | ||
|
||
class ModelEventError extends Error { | ||
static get codes() { | ||
return { | ||
INVALID_DATA: 1 | ||
}; | ||
} | ||
|
||
constructor(err, code) { | ||
super(err); | ||
this.message = err.message || err; | ||
this.code = code; | ||
this.name = 'ModelEventError'; | ||
} | ||
} | ||
|
||
module.exports = ModelEventError; |
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,79 @@ | ||
'use strict'; | ||
|
||
const Model = require('@janiscommerce/model'); | ||
const Settings = require('@janiscommerce/settings'); | ||
|
||
const ModelEventError = require('./model-event-error'); | ||
|
||
class ModelEvent extends Model { | ||
/** | ||
* Get the MS settings | ||
* | ||
* @return {Object} Setting | ||
*/ | ||
static get settings() { | ||
return Settings.get('events') || {}; | ||
} | ||
|
||
/** | ||
* Get Database key | ||
* | ||
* @return {String} Database key to search in settings | ||
*/ | ||
get databaseKey() { | ||
return this.constructor.settings.databaseKey || '_default'; | ||
} | ||
|
||
/** | ||
* Get table name | ||
* | ||
* @return {String} Table/Document name | ||
*/ | ||
static get table() { | ||
return this.settings.table || 'events'; | ||
} | ||
|
||
/** | ||
* Get fields for MySQL query | ||
* | ||
* @return {Object} Fields to include in get | ||
*/ | ||
static get fields() { | ||
return { | ||
client: true, | ||
entity: true, | ||
event: true | ||
}; | ||
} | ||
|
||
/** | ||
* Get event by filters | ||
* | ||
* @param {String} client The client | ||
* @param {String} entity The entity | ||
* @param {String} event The event | ||
* | ||
* @return {Object} The event | ||
*/ | ||
async getEvent(client, entity, event) { | ||
if(!entity || !event) { | ||
throw new ModelEventError( | ||
'Entity and event are required and must be strings', | ||
ModelEventError.codes.INVALID_DATA | ||
); | ||
} | ||
|
||
const events = await this.get({ | ||
filters: { | ||
client, | ||
entity, | ||
event | ||
}, | ||
limit: 1 | ||
}); | ||
|
||
return events && Array.isArray(events) && events.length > 0 ? events[0] : {}; | ||
} | ||
} | ||
|
||
module.exports = ModelEvent; |
Oops, something went wrong.