From fc3e68362c8c91f78686dc4d5bde2f98a8d40b54 Mon Sep 17 00:00:00 2001 From: xmlking Date: Mon, 20 May 2019 18:36:25 -0700 Subject: [PATCH] fix(notifications): fix notifications delete and make-all-as-read commands --- .../notifications-make-all-as-read.handler.ts | 13 ++++++++++++ .../handlers/notifications.handler.ts | 6 +++++- .../notifications-make-all-as-read.command.ts | 7 +++++++ .../notification/notification.service.ts | 7 +++++++ .../src/lib/notifications.actions.ts | 1 - .../src/lib/notifications.state.ts | 20 ++++++++++++------- 6 files changed, 45 insertions(+), 9 deletions(-) create mode 100644 apps/api/src/app/notifications/notification/commands/handlers/notifications-make-all-as-read.handler.ts create mode 100644 apps/api/src/app/notifications/notification/commands/notifications-make-all-as-read.command.ts diff --git a/apps/api/src/app/notifications/notification/commands/handlers/notifications-make-all-as-read.handler.ts b/apps/api/src/app/notifications/notification/commands/handlers/notifications-make-all-as-read.handler.ts new file mode 100644 index 000000000..7c04d29ca --- /dev/null +++ b/apps/api/src/app/notifications/notification/commands/handlers/notifications-make-all-as-read.handler.ts @@ -0,0 +1,13 @@ +import { NotificationService } from '../../notification.service'; +import { NotificationsMarkAllAsReadCommand } from '../notifications-make-all-as-read.command'; +import { CommandHandler, ICommandHandler } from '@nestjs/cqrs'; + +@CommandHandler(NotificationsMarkAllAsReadCommand) +export class NotificationsMarkAsReadHandler implements ICommandHandler { + constructor(private readonly notificationService: NotificationService) {} + + public async execute(command: NotificationsMarkAllAsReadCommand): Promise { + console.log('command:NotificationsMarkAllAsReadCommand', command); + await this.notificationService.onMarkAllAsRead(command); + } +} diff --git a/apps/api/src/app/notifications/notification/commands/handlers/notifications.handler.ts b/apps/api/src/app/notifications/notification/commands/handlers/notifications.handler.ts index 244ec41eb..57f424571 100644 --- a/apps/api/src/app/notifications/notification/commands/handlers/notifications.handler.ts +++ b/apps/api/src/app/notifications/notification/commands/handlers/notifications.handler.ts @@ -3,6 +3,7 @@ import { CommandHandler, ICommandHandler } from '@nestjs/cqrs'; import { GenericCommand } from '../../../../shared'; import { NotificationsDeleteCommand } from '../notifications-delete.command'; import { NotificationsMarkAsReadCommand } from '../notifications-make-as-read.command'; +import { NotificationsMarkAllAsReadCommand } from '../notifications-make-all-as-read.command'; import { Logger } from '@nestjs/common'; @CommandHandler(GenericCommand) @@ -14,11 +15,14 @@ export class NotificationsHandler implements ICommandHandler { const { type, payload, user } = command; switch (type) { case NotificationsDeleteCommand.type: { - return await this.notificationService.onMarkAsRead(new NotificationsDeleteCommand(payload, user)); + return await this.notificationService.onDeleteNotification(new NotificationsDeleteCommand(payload, user)); } case NotificationsMarkAsReadCommand.type: { return await this.notificationService.onMarkAsRead(new NotificationsMarkAsReadCommand(payload, user)); } + case NotificationsMarkAllAsReadCommand.type: { + return await this.notificationService.onMarkAllAsRead(new NotificationsMarkAllAsReadCommand(payload, user)); + } default: { this.logger.error('received unknown command: ', command.type); // return this.commandBus.execute(command); diff --git a/apps/api/src/app/notifications/notification/commands/notifications-make-all-as-read.command.ts b/apps/api/src/app/notifications/notification/commands/notifications-make-all-as-read.command.ts new file mode 100644 index 000000000..162057d5d --- /dev/null +++ b/apps/api/src/app/notifications/notification/commands/notifications-make-all-as-read.command.ts @@ -0,0 +1,7 @@ +import { ICommand } from '@nestjs/cqrs'; +import { User } from '@ngx-starter-kit/models'; + +export class NotificationsMarkAllAsReadCommand implements ICommand { + static readonly type = '[Notifications] MarkAllAsRead'; + constructor(public readonly payload: any, public readonly user: User) {} +} diff --git a/apps/api/src/app/notifications/notification/notification.service.ts b/apps/api/src/app/notifications/notification/notification.service.ts index 7d98e553b..7b54fb1ec 100644 --- a/apps/api/src/app/notifications/notification/notification.service.ts +++ b/apps/api/src/app/notifications/notification/notification.service.ts @@ -77,6 +77,13 @@ export class NotificationService extends CrudService implements On ); } + async onMarkAllAsRead(command: NotificationsMarkAsReadCommand) { + await this.update( + { targetType: TargetType.USER, target: command.user.username }, + { read: true }, + ); + } + async onDeleteNotification(command: NotificationsDeleteCommand) { await this.update( { id: command.payload.id, targetType: TargetType.USER, target: command.user.username }, diff --git a/libs/notifications/src/lib/notifications.actions.ts b/libs/notifications/src/lib/notifications.actions.ts index 97dc72abb..bda2730c0 100644 --- a/libs/notifications/src/lib/notifications.actions.ts +++ b/libs/notifications/src/lib/notifications.actions.ts @@ -12,7 +12,6 @@ export class AddNotification { export class DeleteNotification { static readonly type = '[Notifications] Delete'; - constructor(public readonly payload: AppNotification) {} } diff --git a/libs/notifications/src/lib/notifications.state.ts b/libs/notifications/src/lib/notifications.state.ts index 6946fb8d1..6e3249bdd 100644 --- a/libs/notifications/src/lib/notifications.state.ts +++ b/libs/notifications/src/lib/notifications.state.ts @@ -1,5 +1,6 @@ import { Action, NgxsAfterBootstrap, Selector, State, StateContext } from '@ngxs/store'; import { ImmutableContext, ImmutableSelector } from '@ngxs-labs/immer-adapter'; +import { append, patch, removeItem, updateItem } from '@ngxs/store/operators'; import { tap } from 'rxjs/operators'; import { AppNotification } from './app-notification.model'; import { NotificationsService } from './notifications.service'; @@ -47,14 +48,19 @@ export class NotificationsState implements NgxsAfterBootstrap { return this.notificationsService.getAll().pipe(tap(res => setState(res))); } - @ImmutableContext() + // @ImmutableContext() + // @Action(DeleteNotification) + // delete(ctx: StateContext, { payload }: DeleteNotification) { + // ctx.setState((state: AppNotification[]) => { + // return state.splice(state.findIndex(note => note.id === payload.id), 1); + // // or (slower): + // // return state.filter(note => note.id !== payload.id); + // }); + // } + @Action(DeleteNotification) - delete(ctx: StateContext, { payload }: AddNotification) { - ctx.setState((state: AppNotification[]) => { - return state.splice(state.findIndex(note => note.id === payload.id), 1); - // or (slower): - // return state.filter(note => note.id !== payload.id); - }); + delete(ctx: StateContext, { payload }: DeleteNotification) { + ctx.setState(removeItem(note => note.id === payload.id)); } @ImmutableContext()