Skip to content

Commit

Permalink
Merge pull request #4 from fabrix-app/v1.1
Browse files Browse the repository at this point in the history
[chore] update packages
  • Loading branch information
scott-wyatt authored Aug 1, 2018
2 parents f6c0e34 + 7f78c41 commit 4edc080
Show file tree
Hide file tree
Showing 4 changed files with 80 additions and 79 deletions.
65 changes: 33 additions & 32 deletions lib/api/models/Notification.ts
Original file line number Diff line number Diff line change
Expand Up @@ -182,15 +182,6 @@ export class NotificationResolver extends SequelizeResolver {
}
}

export interface Notification {
setSent(app: FabrixApp, options): any
send(app: FabrixApp, options): any
click(app: FabrixApp, user, options): any
open(app: FabrixApp, user, options): any
userOpened(app: FabrixApp, user, options): any
resolveUsers(app: FabrixApp, options): any
resolveEmailUsers(app: FabrixApp, options): any
}


/**
Expand Down Expand Up @@ -325,33 +316,43 @@ export class Notification extends Model {
}
}

export interface Notification {
setSent(options): any
send(options): any
click(user, options): any
open(user, options): any
userOpened(user, options): any
resolveUsers(options): any
resolveEmailUsers(options): any
}

/**
*
*/
Notification.prototype.setSent = function(app: FabrixApp) {
Notification.prototype.setSent = function() {
this.sent = true
this.sent_at = new Date(Date.now())
return this
}
/**
*
*/
Notification.prototype.send = function(app: FabrixApp, options = {}) {
Notification.prototype.send = function(options = {}) {
const sendType = this.template_name && this.template_name !== '' ? 'sendTemplate' : 'send'

if (typeof this.send_email !== 'undefined' && this.send_email === false) {
return Promise.resolve(this)
}

return this.resolveEmailUsers(app, {transaction: options.transaction || null})
return this.resolveEmailUsers({transaction: options.transaction || null})
.then(emailUsers => {
if (emailUsers && emailUsers.length > 0) {
const _emailUsers = this.users.filter(user => user.email)
const users = _emailUsers.map(user => {
if (user) {
return {
email: user.email,
name: user.first_name || user.username || app.config.get('notifications.to.default_name')
name: user.first_name || user.username || this.app.config.get('notifications.to.default_name')
}
}
})
Expand All @@ -362,17 +363,17 @@ Notification.prototype.send = function(app: FabrixApp, options = {}) {
text: this.text,
html: this.html,
to: users,
reply_to: this.reply_to || app.config.get('notifications.from.email'),
reply_to: this.reply_to || this.app.config.get('notifications.from.email'),
from: {
email: app.config.get('notifications.from.email'),
name: app.config.get('notifications.from.name')
email: this.app.config.get('notifications.from.email'),
name: this.app.config.get('notifications.from.name')
},
template_name: this.template_name,
template_content: this.template_content
}
return app.services.EmailGenericService[sendType](message)
return this.app.services.EmailGenericService[sendType](message)
.catch(err => {
app.log.error(err)
this.app.log.error(err)
return null
})

Expand All @@ -384,8 +385,8 @@ Notification.prototype.send = function(app: FabrixApp, options = {}) {
.then(emails => {
// emails = emails.filter(email => email)
if (emails.length > 0) {
app.log.debug('EMAILS SENT', this.token, emails.length)
return this.setSent(app, options).save({ transaction: options.transaction || null})
this.app.log.debug('EMAILS SENT', this.token, emails.length)
return this.setSent(options).save({ transaction: options.transaction || null})
}
else {
return this
Expand All @@ -395,27 +396,27 @@ Notification.prototype.send = function(app: FabrixApp, options = {}) {
/**
*
*/
Notification.prototype.click = function(app: FabrixApp, user, options = {}) {
Notification.prototype.click = function(user, options = {}) {
this.total_clicks++
return Promise.resolve(this)
}
/**
*
*/
Notification.prototype.open = function(app: FabrixApp, user, options = {}) {
Notification.prototype.open = function(user, options = {}) {
this.total_opens++
if (!user) {
return Promise.resolve(this)
}
else {
return this.userOpened(app, user, options)
return this.userOpened(user, options)
}
}
/**
* Register that a User Opened a Notification
*/
Notification.prototype.userOpened = function (app: FabrixApp, user, options = {}) {
return app.models['ItemNotification'].update({ opened: true }, {
Notification.prototype.userOpened = function (user, options: {[key: string]: any} = {}) {
return this.app.models['ItemNotification'].update({ opened: true }, {
where: {
notification_id: this.id,
model: 'user',
Expand All @@ -427,18 +428,18 @@ Notification.prototype.userOpened = function (app: FabrixApp, user, options = {}
return this
})
.catch(err => {
app.log.error(err)
this.app.log.error(err)
return this
})
}
/**
*
*/
Notification.prototype.resolveUsers = function(app, options = {}) {
Notification.prototype.resolveUsers = function(options: {[key: string]: any} = {}) {
if (
this.users
&& this.users.length > 0
&& this.users.every(u => u instanceof app.models['User'].resolver.sequelizeModel)
&& this.users.every(u => u instanceof this.app.models['User'].instance)
&& options.reload !== true
) {
return Promise.resolve(this)
Expand All @@ -458,9 +459,9 @@ Notification.prototype.resolveUsers = function(app, options = {}) {
*
*/
// TODO, refactor to something pleasant.
Notification.prototype.resolveEmailUsers = function(app: FabrixApp, options = {}) {
Notification.prototype.resolveEmailUsers = function(options = {}) {
let emailUsers = []
return this.resolveUsers(app, {
return this.resolveUsers({
transaction: options.transaction || null,
reload: options.reload || null
})
Expand All @@ -486,7 +487,7 @@ Notification.prototype.resolveEmailUsers = function(app: FabrixApp, options = {}
user.preferences = JSON.parse(user.preferences)
}
catch (err) {
app.log.error('Unable to parse user.preferences')
this.app.log.error('Unable to parse user.preferences')
user.preferences = {}
}
}
Expand Down Expand Up @@ -526,7 +527,7 @@ Notification.prototype.resolveEmailUsers = function(app: FabrixApp, options = {}
return emailUsers
})
.catch(err => {
app.log.error(err)
this.app.log.error(err)
return emailUsers
})
}
8 changes: 4 additions & 4 deletions lib/api/services/NotificationService.ts
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ export class NotificationService extends Service {
return resNotification.setUsers(_users.map(u => u.id), {transaction: options.transaction || null})
})
.then(() => {
return resNotification.send(this.app, {transaction: options.transaction})
return resNotification.send({transaction: options.transaction})
})
}

Expand All @@ -52,7 +52,7 @@ export class NotificationService extends Service {
throw new ModelError('E_NOT_FOUND', `Notification ${notification} not found`)
}
resNotification = _notification
return resNotification.send(this.app, {transaction: options.transaction || null})
return resNotification.send({transaction: options.transaction || null})
})
}

Expand All @@ -69,7 +69,7 @@ export class NotificationService extends Service {
throw new ModelError('E_NOT_FOUND', `Notification ${notification} not found`)
}
resNotification = _notification
return resNotification.click(this.app, user, {transaction: options.transaction || null})
return resNotification.click(user, {transaction: options.transaction || null})
})
.then(() => {
return resNotification.save({transaction: options.transaction || null})
Expand All @@ -89,7 +89,7 @@ export class NotificationService extends Service {
throw new ModelError('E_NOT_FOUND', `Notification ${notification} not found`)
}
resNotification = _notification
return resNotification.open(this.app, user, {transaction: options.transaction || null})
return resNotification.open(user, {transaction: options.transaction || null})
})
.then(() => {
return resNotification.save({transaction: options.transaction || null})
Expand Down
68 changes: 34 additions & 34 deletions package-lock.json

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

Loading

0 comments on commit 4edc080

Please sign in to comment.