-
-
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
Closed
Closed
Changes from all commits
Commits
Show all changes
53 commits
Select commit
Hold shift + click to select a range
d7a6d36
calc tasks for notifications
jonathanbataire 4a8801f
fix date bug
jonathanbataire 7975c9c
clean up
jonathanbataire e29d488
add dbsync
jonathanbataire 08181fc
Merge branch 'master' into 9255-notifications
jonathanbataire a137242
refine service
jonathanbataire f23409a
fresh service :tada:
jonathanbataire f37c0ba
bug fix
jonathanbataire 21d2ed6
offline bug fix
jonathanbataire 24ad96c
Merge branch 'master' into 9255-notifications
jonathanbataire db78a22
clear sonar and lint errors
jonathanbataire dcb5540
tests
jonathanbataire 828d106
test clean up
jonathanbataire c768104
:heavy_check_mark: test
jonathanbataire 34aa546
Merge branch 'master' into 9255-notifications
jonathanbataire 6a179ac
:tada: tests
jonathanbataire 0abd99b
Merge branch 'master' into 9255-notifications
jonathanbataire e4ed7c1
clean up
jonathanbataire 317f5fd
refresh service
jonathanbataire 375b2b3
Merge branch 'master' into 9255-notifications
jonathanbataire 8dd47fd
Merge branch 'master' into 9255-notifications
jonathanbataire 3fc32da
address feedback
jonathanbataire ef69962
Merge branch 'master' into 9255-notifications
jonathanbataire 28e0b7b
Merge branch 'master' into 9255-notifications
jonathanbataire c174a7a
address feedback
jonathanbataire 5857bad
add warning for missing setting
jonathanbataire bd07cb6
Merge branch 'master' into 9255-notifications
jonathanbataire 30f944f
Merge branch 'master' into 9255-notifications
jonathanbataire 85ab4c7
fix compile error
jonathanbataire 021a7c6
feedback improvements
jonathanbataire 029cc9f
Merge branch 'master' into 9255-notifications
jonathanbataire 45dfbb8
fix tests
jonathanbataire 4f374ac
:tada:
jonathanbataire fa8479c
clean up tests
jonathanbataire 82226f6
Merge branch 'master' into 9255-notifications
jonathanbataire 3b5213d
:tada:
jonathanbataire cafa6f4
Merge branch 'master' into 9255-notifications
jonathanbataire 6226305
Update webapp/tests/karma/ts/services/task-notifications.service.spec.ts
jonathanbataire 133f1d8
Merge branch 'master' into 9255-notifications
jonathanbataire d217147
clean up
jonathanbataire 4331a8e
Merge branch 'master' into 9255-notifications
jonathanbataire 82d5dd4
Merge branch 'master' into 9255-notifications
jonathanbataire 5724e36
Merge branch 'master' into 9255-notifications
jonathanbataire d5cc452
Merge branch 'master' into 9255-notifications
jonathanbataire 8798459
Merge branch 'master' into 9255-notifications
jonathanbataire 7d0b09d
Merge branch 'master' into 9255-notifications
jonathanbataire ca3c951
Merge branch 'master' into 9255-notifications
jonathanbataire 3d95aee
add permission as default
jonathanbataire 40af7bc
Merge branch 'master' into 9255-notifications
jonathanbataire 8227757
Merge branch 'master' into 9255-notifications
jonathanbataire 679d801
Merge branch 'master' into 9255-notifications
jonathanbataire b6f2969
Merge branch 'master' into 9255-notifications
jonathanbataire 064ce3f
Merge branch 'master' into 9255-notifications
jonathanbataire File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Some comments aren't visible on the classic Files Changed page.
There are no files selected for viewing
This file contains hidden or 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 hidden or 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 hidden or 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 hidden or 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,117 @@ | ||
| import { Injectable } from '@angular/core'; | ||
| import * as moment from 'moment'; | ||
|
|
||
| import { RulesEngineService } from '@mm-services/rules-engine.service'; | ||
| import { TranslateService } from '@mm-services/translate.service'; | ||
| import { FormatDateService } from '@mm-services/format-date.service'; | ||
| import { SettingsService } from '@mm-services/settings.service'; | ||
| import { AuthService } from '@mm-services/auth.service'; | ||
|
|
||
| const TASK_NOTIFICATION_DAY = 'cht-task-notification-day'; | ||
| const LATEST_NOTIFICATION_TIMESTAMP = 'cht-task-notification-timestamp'; | ||
| const DEFAULT_MAX_NOTIFICATIONS = 10; | ||
|
|
||
| export interface Notification { | ||
| _id: string, | ||
| readyAt: number, | ||
| title: string, | ||
| contentText: string, | ||
| dueDate: string, | ||
| } | ||
|
|
||
| @Injectable({ | ||
| providedIn: 'root' | ||
| }) | ||
| export class TasksNotificationService { | ||
|
|
||
| constructor( | ||
| private readonly rulesEngineService: RulesEngineService, | ||
| private readonly translateService: TranslateService, | ||
| private readonly formatDateService: FormatDateService, | ||
| private readonly settingsService: SettingsService, | ||
| private readonly authService: AuthService, | ||
| ) { } | ||
|
|
||
| private async fetchNotifications(): Promise<Notification[]> { | ||
| try { | ||
| const isEnabled = await this.rulesEngineService.isEnabled(); | ||
| if (!isEnabled) { | ||
| return []; | ||
| } | ||
| const today = moment().format('YYYY-MM-DD'); | ||
| let latestNotificationTimestamp = this.getLatestNotificationTimestamp(); | ||
| const taskDocs = await this.rulesEngineService.fetchTaskDocsForAllContacts(); | ||
|
|
||
| let notifications: Notification[] = []; | ||
|
|
||
| taskDocs.forEach(task => { | ||
| const readyAt = this.getReadyStateTimestamp(task.stateHistory); | ||
| const dueDate = task.emission.dueDate; | ||
| if (dueDate <= today && readyAt > latestNotificationTimestamp) { | ||
| notifications.push({ | ||
| _id: task._id, | ||
| readyAt, | ||
| title: task.emission.title, | ||
| contentText: this.translateContentText( | ||
| task.emission.title, | ||
| task.emission.contact.name, | ||
| task.emission.dueDate | ||
| ), | ||
| dueDate, | ||
| }); | ||
| } | ||
| }); | ||
|
|
||
| notifications = notifications.sort((a, b) => b.readyAt - a.readyAt); | ||
| notifications = notifications.slice(0, await this.getMaxNotificationSettings()); | ||
| latestNotificationTimestamp = notifications[0]?.readyAt ?? latestNotificationTimestamp; | ||
| window.localStorage.setItem(LATEST_NOTIFICATION_TIMESTAMP, String(latestNotificationTimestamp)); | ||
| return notifications; | ||
|
|
||
| } catch (exception) { | ||
| console.error('fetchNotifications(): Error fetching tasks', exception); | ||
| return []; | ||
| } | ||
| } | ||
|
|
||
| private getReadyStateTimestamp(stateHistory): number { | ||
| const readyState = stateHistory.find(state => state.state === 'Ready'); | ||
| return readyState ? readyState.timestamp : 0; | ||
| } | ||
|
|
||
| private async getMaxNotificationSettings(): Promise<number> { | ||
| const settings = await this.settingsService.get(); | ||
| if (settings?.tasks?.max_task_notifications) { | ||
| return settings.tasks.max_task_notifications; | ||
| } | ||
| console.warn('Invalid or missing max_task_notifications setting, using default value'); | ||
| return DEFAULT_MAX_NOTIFICATIONS; | ||
| } | ||
|
|
||
| private getLatestNotificationTimestamp(): number { | ||
| if (this.isNewDay()) { | ||
| window.localStorage.setItem(TASK_NOTIFICATION_DAY, String(moment().startOf('day').valueOf())); | ||
| return 0; | ||
| } | ||
| return Number(window.localStorage.getItem(LATEST_NOTIFICATION_TIMESTAMP)); | ||
| } | ||
|
|
||
| private isNewDay(): boolean { | ||
| const timestampToday = Number(window.localStorage.getItem(TASK_NOTIFICATION_DAY)); | ||
| return !moment().isSame(timestampToday, 'day'); | ||
| } | ||
|
|
||
| private translateContentText(taskName: string, contact: string, dueDate: string): string { | ||
| const key = 'android.notification.tasks.contentText'; | ||
| const due = this.formatDateService.relative(dueDate, { task: true }); | ||
| return this.translateService.instant(key, { taskName, contact, due }); | ||
| } | ||
|
|
||
| async get(): Promise<Notification[]> { | ||
| const canGetTaskNotifications = await this.authService.has('can_get_task_notifications'); | ||
| if (!canGetTaskNotifications) { | ||
| return []; | ||
| } | ||
| return await this.fetchNotifications(); | ||
| } | ||
| } | ||
This file contains hidden or 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.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.