Skip to content

Commit

Permalink
Refactoring use of try/catch in NotificationTask
Browse files Browse the repository at this point in the history
  • Loading branch information
jeffsampaio committed Apr 15, 2020
1 parent f9dd570 commit 137149c
Showing 1 changed file with 31 additions and 24 deletions.
55 changes: 31 additions & 24 deletions src/background/task/notification.task.ts
Original file line number Diff line number Diff line change
Expand Up @@ -40,35 +40,42 @@ export class NotificationTask implements IBackgroundTask {

private sendNotification(children: Array<Child>): void {
for (const child of children) {
this._eventBus.bus
.pubSendNotification(this.buildNotification(child))
.then(() => {
this._logger.info('\'monitoring:miss_child_data\' notification sent')
})
.catch(err => {
this._logger.error(`An error occurred while trying to send a notification about the Child with ID: `
.concat(`${child.id}. ${err.message}`))
})
let notification

try {
notification = this.buildNotification(child)
} catch (err) {
this._logger.error(`An error occurred while trying to build the notification. ${err.message}`)
}

if (notification) {
this._eventBus.bus
.pubSendNotification(notification)
.then(() => {
this._logger.info('\'monitoring:miss_child_data\' notification sent')
})
.catch(err => {
this._logger.error(`An error occurred while trying to send a notification about the Child with ID: `
.concat(`${child.id}. ${err.message}`))
})
}
}
}

private buildNotification(child: Child): any {
try {
let calc_days_since = this.numberOfDays
if (child.last_sync) {
const now = new Date()
const last_sync: Date = child.last_sync
const diff = Math.abs(now.getTime() - last_sync.getTime())
calc_days_since = Math.trunc(diff / (1000 * 60 * 60 * 24))
}
let calc_days_since = this.numberOfDays

return {
notification_type: 'monitoring:miss_child_data',
id: child.id,
days_since: calc_days_since
}
} catch (err) {
this._logger.error(`An error occurred while trying to build the notification. ${err.message}`)
if (child.last_sync) {
const now = new Date()
const last_sync: Date = child.last_sync
const diff = Math.abs(now.getTime() - last_sync.getTime())
calc_days_since = Math.trunc(diff / (1000 * 60 * 60 * 24))
}

return {
notification_type: 'monitoring:miss_child_data',
id: child.id,
days_since: calc_days_since
}
}

Expand Down

0 comments on commit 137149c

Please sign in to comment.