Skip to content
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

1741 group multiple delete create notification requests into a single request #1744

Open
wants to merge 7 commits into
base: dev
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

55 changes: 55 additions & 0 deletions src/app/core/services/app-server/app-server.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,8 @@ export class AppServerService {
QUESTIONNAIRE_TASK = 'questionnaire/task'
QUESTIONNAIRE_STATE_EVENTS_PATH = 'state_events'
NOTIFICATIONS_PATH = 'messaging/notifications'
NOTIFICATIONS_BATCH_PATH = 'messaging/notifications/batch'
ALL_PATH = 'all'
STATE_EVENTS_PATH = 'state_events'
private tokenSubscription: Subscription = null

Expand Down Expand Up @@ -367,6 +369,25 @@ export class AppServerService {
)
}

deleteAllUserNotifications(subject) {
return this.getHeaders().then(headers =>
this.http
.delete(
urljoin(
this.getAppServerURL(),
this.PROJECT_PATH,
subject.projectId,
this.SUBJECT_PATH,
subject.subjectId,
this.NOTIFICATIONS_PATH,
this.ALL_PATH
),
{ headers }
)
.toPromise()
)
}

updateTaskState(taskId, state) {
return Promise.all([
this.subjectConfig.getParticipantLogin(),
Expand Down Expand Up @@ -419,6 +440,40 @@ export class AppServerService {
)
}

addNotificationsBundle(notifications, subjectId, projectId): Promise<any> {
return this.getHeaders().then(headers =>
this.http
.post(
urljoin(
this.getAppServerURL(),
this.PROJECT_PATH,
projectId,
this.SUBJECT_PATH,
subjectId,
this.NOTIFICATIONS_BATCH_PATH,
),
notifications,
{ headers, observe: 'response' }
)
.toPromise()
.then((res: HttpResponse<FcmNotificationDto>) => {
this.logger.log('Successfully sent! Updating notification Id')
return res.body
})
.catch((err: HttpErrorResponse) => {
this.logger.log('Http request returned an error: ' + err.message)
// const data: FcmNotificationError = err.error
// if (err.status == 409) {
// this.logger.log(
// 'Notification already exists, storing notification data..'
// )
// return data.dto ? data.dto : notification.notification
// }
return this.logger.error('Failed to send notification', err)
})
)
}

public addNotification(notification, subjectId, projectId): Promise<any> {
return this.getHeaders().then(headers =>
this.http
Expand Down
29 changes: 16 additions & 13 deletions src/app/core/services/config/config.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -246,19 +246,22 @@ export class ConfigService {
}

rescheduleNotifications(cancel?: boolean) {
return (cancel ? this.cancelNotifications() : Promise.resolve([]))
.then(() =>
this.notifications.publish(NotificationActionType.SCHEDULE_ALL)
)
.then(() => console.log('NOTIFICATIONS scheduled after config change'))
.then(() =>
cancel
? this.sendConfigChangeEvent(NotificationEventType.RESCHEDULED)
: this.sendConfigChangeEvent(NotificationEventType.REFRESHED)
)
.catch(e => {
throw this.logger.error('Failed to reschedule notifications', e)
})
if (cancel) {
this.cancelNotifications()
.then(() => this.notifications.publish(NotificationActionType.SCHEDULE_ALL))
.then(() => console.log('NOTIFICATIONS scheduled after config change'))
.then(() => this.sendConfigChangeEvent(NotificationEventType.RESCHEDULED))
.catch(e => {
throw this.logger.error('Failed to reschedule notifications', e)
})
} else {
this.notifications.publish(NotificationActionType.SCHEDULE_ALL)
.then(() => console.log('NOTIFICATIONS scheduled after config change'))
.then(() => this.sendConfigChangeEvent(NotificationEventType.REFRESHED))
.catch(e => {
throw this.logger.error('Failed to reschedule notifications', e)
})
}
}

cancelNotifications() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@ import { NotificationGeneratorService } from './notification-generator.service'

@Injectable()
export class FcmRestNotificationService extends FcmNotificationService {

NOTIFICATIONS_PATH = 'messaging/notifications'
SUBJECT_PATH = 'users'
PROJECT_PATH = 'projects'
Expand Down Expand Up @@ -122,11 +123,8 @@ export class FcmRestNotificationService extends FcmNotificationService {
.map(t => this.format(t, subject))
this.logger.log('NOTIFICATIONS Scheduling FCM notifications')
this.logger.log(fcmNotifications)
return Promise.all(
fcmNotifications.map(n =>
this.sendNotification(n, subject.subjectId, subject.projectId)
)
)
const notifications = fcmNotifications.map(n => n.notificationDto)
return this.sendNotificationsBundle({notifications}, subject.subjectId, subject.projectId)
})
}

Expand All @@ -149,21 +147,20 @@ export class FcmRestNotificationService extends FcmNotificationService {
})
}

cancelAllNotifications(subject): Promise<any> {
return this.appServerService
.pullAllPublishedNotifications(subject)
.then((res: FcmNotifications) => {
const now = Date.now()
const notifications = res.notifications
.map(n => ({
id: n.id,
timestamp: getMilliseconds({ seconds: n.scheduledTime })
}))
.filter(n => n.timestamp > now)
notifications.map(o => this.cancelSingleNotification(subject, o))
sendNotificationsBundle(notifications, subjectId, projectId) {
return this.appServerService.addNotificationsBundle(notifications, subjectId, projectId)
.then((resultNotification: FcmNotificationDto) => {
this.setLastNotificationUpdate(Date.now())
// notification.notification.id = resultNotification.id
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Would it be possible to still store back the notification id? We would still need it when we want to cancel reminders/future notifications for a task once it has been completed.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

That would be done bij uncommenting the lines below right?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

No cause I think the request would return multiple notifications and each would be needed to get stored back to the task object.

// return (notification.notification.messageId =
// resultNotification.fcmMessageId)
})
}

cancelAllNotifications(subject){
return this.appServerService.deleteAllUserNotifications(subject);
}

cancelSingleNotification(subject, notification: SingleNotification) {
if (notification.id) {
return this.appServerService
Expand Down
Loading