forked from medic/cht-core
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtask-notifications.service.ts
More file actions
103 lines (90 loc) · 3.4 KB
/
task-notifications.service.ts
File metadata and controls
103 lines (90 loc) · 3.4 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
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';
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();
const taskDocs = isEnabled ? await this.rulesEngineService.fetchTaskDocsForAllContacts() : [];
let notifications = taskDocs
.filter(task => {
return task.state === 'Ready' && task.emission.dueDate === today &&
task.authoredOn > latestNotificationTimestamp;
})
.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']);
notifications = notifications.slice(0, MAX_NOTIFICATIONS);
latestNotificationTimestamp = notifications[0]?.authoredOn ?? latestNotificationTimestamp;
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;
}
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))
]).then(() => {
return this.fetchNotifications();
}).catch((error) => {
console.error('get(): notifications error syncing db', error);
return this.fetchNotifications();
});
}
}