/**
*
*/
class JobNotificationDTO {
/**
*
* @param {any} json
*/
constructor (json) {
this.json = json
this.expression = Expression.fromRule(this)
}
/**
* @type {string}
*/
get id () {
return this.json.id
}
/**
* @type {string}
*/
get rule () {
return this.json.rule
}
set rule (s) {
this.json.rule = s
}
/**
* @type {string}
*/
get addressesString () {
const addressString = this.json.addresses.map(a => a.address).join(', ')
console.info('get addresses: ' + addressString)
return addressString
}
set addressesString (s) {
let addressArray = s.split(',').map(a => a.trim())
// @ts-ignore
addressArray = addressArray.map(a => { return { address: a, channel: 'EMAIL' }})
this.json.addresses = addressArray
}
/**
* @returns {boolean}
*/
isConditional () {
return this.expression.type === 'CONDITIONAL'
}
}
/**
*
*/
class Expression {
/**
*
* @param {JobNotificationDTO} notification
* @returns {Expression}
*/
static fromRule (notification) {
const type = notification.rule === '*' ? 'ALWAYS' : 'CONDITIONAL'
let property
let operator
let value
if (type === 'CONDITIONAL') {
const parts = notification.rule.split(' ')
property = parts[0]
operator = parts[1]
value = parts[2]
}
const expression = new Expression(type, property, operator, value)
return expression
}
/**
* @param {string} type
* @param {string} [property]
* @param {string} [operator]
* @param {string} [value]
*/
constructor (type, property, operator, value) {
this.type = type
this.property = property
this.operator = operator
this.value = value
}
/**
*
* @returns {string}
*/
toRule () {
if (this.type === 'ALWAYS') {
return '*'
} else {
return `${this.property} ${this.operator} ${this.value}`
}
}
}
export default JobNotificationDTO