-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge branch 'notification' into develop
- Loading branch information
Showing
24 changed files
with
677 additions
and
57 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,10 +1,9 @@ | ||
language: node_js | ||
node_js: | ||
- "8" | ||
- "9" | ||
- "10" | ||
- "11" | ||
- "12" | ||
- "13" | ||
env: | ||
- NODE_ENV=test RABBITMQ_URI=amqp://guest:[email protected]:5672 MONGODB_URI_TEST=mongodb://127.0.0.1:27017/ocariot-account-test | ||
dist: xenial | ||
|
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,4 +1,4 @@ | ||
FROM node:10.16.3 | ||
FROM node:12.13.1 | ||
|
||
# Create app directory | ||
RUN mkdir -p /usr/src/ac | ||
|
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
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,91 @@ | ||
import { IBackgroundTask } from '../../application/port/background.task.interface' | ||
import { inject, injectable } from 'inversify' | ||
import { Identifier } from '../../di/identifiers' | ||
import { IEventBus } from '../../infrastructure/port/eventbus.interface' | ||
import { ILogger } from '../../utils/custom.logger' | ||
import cron from 'cron' | ||
import { IChildRepository } from '../../application/port/child.repository.interface' | ||
import { Child } from '../../application/domain/model/child' | ||
|
||
@injectable() | ||
export class NotificationTask implements IBackgroundTask { | ||
private job: any | ||
|
||
constructor( | ||
@inject(Identifier.RABBITMQ_EVENT_BUS) private readonly _eventBus: IEventBus, | ||
@inject(Identifier.CHILD_REPOSITORY) private readonly _childRepository: IChildRepository, | ||
@inject(Identifier.LOGGER) private readonly _logger: ILogger, | ||
private readonly numberOfDays: number, | ||
private readonly expression_auto_notification?: string | ||
) { | ||
} | ||
|
||
public run(): void { | ||
try { | ||
if (this.expression_auto_notification) { | ||
this.job = new cron.CronJob(`${this.expression_auto_notification}`, () => this.checkInactivity()) | ||
this.job.start() | ||
} else this.checkInactivity() | ||
|
||
this._logger.debug('Notification task started successfully!') | ||
} catch (err) { | ||
this._logger.error(`An error occurred initializing the Notification task. ${err.message}`) | ||
} | ||
} | ||
|
||
public stop(): Promise<void> { | ||
if (this.expression_auto_notification) this.job.stop() | ||
return this._eventBus.dispose() | ||
} | ||
|
||
private sendNotification(children: Array<Child>): void { | ||
for (const child of children) { | ||
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 { | ||
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)) | ||
} | ||
|
||
return { | ||
notification_type: 'monitoring:miss_child_data', | ||
id: child.id, | ||
days_since: calc_days_since | ||
} | ||
} | ||
|
||
private checkInactivity(): void { | ||
this._childRepository.findInactiveChildren(this.numberOfDays) | ||
.then(result => { | ||
if (result.length) this.sendNotification(result) | ||
}) | ||
.catch(err => { | ||
this._logger.error(`An error occurred while trying to retrieve Child data. ${err.message}`) | ||
}) | ||
} | ||
} |
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
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
Oops, something went wrong.