Skip to content

Commit

Permalink
fix(notifications): fix notifications delete and make-all-as-read com…
Browse files Browse the repository at this point in the history
…mands
  • Loading branch information
xmlking committed May 21, 2019
1 parent c36765f commit fc3e683
Show file tree
Hide file tree
Showing 6 changed files with 45 additions and 9 deletions.
@@ -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<NotificationsMarkAllAsReadCommand> {
constructor(private readonly notificationService: NotificationService) {}

public async execute(command: NotificationsMarkAllAsReadCommand): Promise<void> {
console.log('command:NotificationsMarkAllAsReadCommand', command);
await this.notificationService.onMarkAllAsRead(command);
}
}
Expand Up @@ -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)
Expand All @@ -14,11 +15,14 @@ export class NotificationsHandler implements ICommandHandler<GenericCommand> {
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);
Expand Down
@@ -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) {}
}
Expand Up @@ -77,6 +77,13 @@ export class NotificationService extends CrudService<Notification> 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 },
Expand Down
1 change: 0 additions & 1 deletion libs/notifications/src/lib/notifications.actions.ts
Expand Up @@ -12,7 +12,6 @@ export class AddNotification {

export class DeleteNotification {
static readonly type = '[Notifications] Delete';

constructor(public readonly payload: AppNotification) {}
}

Expand Down
20 changes: 13 additions & 7 deletions 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';
Expand Down Expand Up @@ -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<AppNotification[]>, { 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<AppNotification[]>, { 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<AppNotification[]>, { payload }: DeleteNotification) {
ctx.setState(removeItem<AppNotification>(note => note.id === payload.id));
}

@ImmutableContext()
Expand Down

0 comments on commit fc3e683

Please sign in to comment.