-
-
Notifications
You must be signed in to change notification settings - Fork 343
feat(#9255): adds androidApi service to calculate task notifications #9987
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from 21 commits
d7a6d36
4a8801f
7975c9c
e29d488
08181fc
a137242
f23409a
f37c0ba
21d2ed6
24ad96c
db78a22
dcb5540
828d106
c768104
34aa546
6a179ac
0abd99b
e4ed7c1
317f5fd
375b2b3
8dd47fd
3fc32da
ef69962
28e0b7b
c174a7a
5857bad
bd07cb6
30f944f
85ab4c7
021a7c6
029cc9f
45dfbb8
4f374ac
fa8479c
82226f6
3b5213d
cafa6f4
6226305
133f1d8
d217147
4331a8e
82d5dd4
5724e36
d5cc452
8798459
7d0b09d
ca3c951
3d95aee
40af7bc
8227757
679d801
b6f2969
064ce3f
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,103 @@ | ||
| import { Injectable } from '@angular/core'; | ||
| import * as moment from 'moment'; | ||
| import { orderBy } from 'lodash-es'; | ||
|
|
||
| import { RulesEngineService } from '@mm-services/rules-engine.service'; | ||
| import { TranslateService } from '@mm-services/translate.service'; | ||
| import { DBSyncService } from '@mm-services/db-sync.service'; | ||
|
|
||
| /* | ||
| ** avoid overloading app with too many notifications especially at once | ||
| ** 24 for android >= 10 | ||
| */ | ||
| const MAX_NOTIFICATIONS = 24; | ||
| const TODAY_TIMESTAMP = 'cht-today-timestamp'; | ||
| const LATEST_NOTIFICATION_TIMESTAMP = 'cht-latest-notification-timestamp'; | ||
dianabarsan marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
|
|
||
| export interface Notification { | ||
| _id: string, | ||
| authoredOn: number, | ||
| state: string, | ||
| title: string, | ||
| contentText: string, | ||
| dueDate: string, | ||
| } | ||
|
|
||
| @Injectable({ | ||
| providedIn: 'root' | ||
| }) | ||
| export class TasksNotificationService { | ||
|
|
||
| constructor( | ||
| private readonly rulesEngineService: RulesEngineService, | ||
| private readonly translateService: TranslateService, | ||
| private readonly dbSyncService: DBSyncService | ||
| ) { } | ||
|
|
||
| private async fetchNotifications(): Promise<Notification[]> { | ||
| try { | ||
| const today = moment().format('YYYY-MM-DD'); | ||
| let latestNotificationTimestamp = this.getLatestNotificationTimestamp(); | ||
| const isEnabled = await this.rulesEngineService.isEnabled(); | ||
dianabarsan marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| const taskDocs = isEnabled ? await this.rulesEngineService.fetchTaskDocsForAllContacts() : []; | ||
|
|
||
| let notifications = taskDocs | ||
| .filter(task => { | ||
| return task.state === 'Ready' && task.emission.dueDate === today && | ||
dianabarsan marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| task.authoredOn > latestNotificationTimestamp; | ||
dianabarsan marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| }) | ||
| .map(task => ({ | ||
| _id: task._id, | ||
| authoredOn: task.authoredOn, | ||
| state: task.state, | ||
| title: task.emission.title, | ||
| contentText: this.translateContentText(task.emission.title, task.emission.contact.name), | ||
| dueDate: task.emission.dueDate, | ||
| })); | ||
|
|
||
| notifications = orderBy(notifications, ['authoredOn'], ['desc']); | ||
dianabarsan marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| notifications = notifications.slice(0, MAX_NOTIFICATIONS); | ||
| latestNotificationTimestamp = notifications[0]?.authoredOn ?? latestNotificationTimestamp; | ||
dianabarsan marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| window.localStorage.setItem(LATEST_NOTIFICATION_TIMESTAMP, String(latestNotificationTimestamp)); | ||
| return notifications; | ||
|
|
||
| } catch (exception) { | ||
| console.error('fetchNotifications(): Error fetching tasks', exception); | ||
| return []; | ||
| } | ||
| } | ||
|
|
||
| private getLatestNotificationTimestamp(): number { | ||
| if (this.isNewDay()) { | ||
| return 0; | ||
| } | ||
| return Number(window.localStorage.getItem(LATEST_NOTIFICATION_TIMESTAMP)); | ||
| } | ||
|
|
||
| private isNewDay(): boolean { | ||
| const now = moment(); | ||
| const timestampToday = Number(window.localStorage.getItem(TODAY_TIMESTAMP)); | ||
| if (!now.isSame(timestampToday, 'day')) { | ||
| window.localStorage.setItem(TODAY_TIMESTAMP, String(moment().startOf('day').valueOf())); | ||
| return true; | ||
| } | ||
| return false; | ||
| } | ||
dianabarsan marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
|
||
| private translateContentText(task: string, contact: string): string { | ||
| const key = 'android.notification.tasks.contentText'; | ||
| return this.translateService.instant(key, { task, contact }); | ||
| } | ||
|
|
||
| async get(): Promise<Notification[]> { | ||
| return Promise.race([ | ||
| this.dbSyncService.sync(), | ||
| new Promise(resolve => setTimeout(() => resolve([]), 5 * 1000)) | ||
dianabarsan marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| ]).then(() => { | ||
| return this.fetchNotifications(); | ||
| }).catch((error) => { | ||
| console.error('get(): notifications error syncing db', error); | ||
| return this.fetchNotifications(); | ||
| }); | ||
| } | ||
| } | ||
Uh oh!
There was an error while loading. Please reload this page.