-
-
Notifications
You must be signed in to change notification settings - Fork 129
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(api): experimenting with @nestjs/cqrs
- Loading branch information
Showing
27 changed files
with
228 additions
and
88 deletions.
There are no files selected for viewing
This file contains 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,39 @@ | ||
import { NestFactory } from '@nestjs/core'; | ||
import { AppModule } from '../app.module'; | ||
import { cli } from 'cli-ux'; | ||
import { UserService } from '../user'; | ||
|
||
/** | ||
* Create admin account using command line interface | ||
*/ | ||
const main = async () => { | ||
const app = await NestFactory.createApplicationContext(AppModule, { | ||
logger: false, | ||
}); | ||
|
||
const firstName = await cli.prompt('First name'); | ||
const lastName = await cli.prompt('Last name'); | ||
const email = await cli.prompt('Email'); | ||
const username = await cli.prompt('Username'); | ||
const password = await cli.prompt('Password', { type: 'mask' }); | ||
|
||
try { | ||
cli.action.start('creating an admin'); | ||
|
||
await app.get(UserService).create({ | ||
firstName, | ||
lastName, | ||
email, | ||
username, | ||
}); | ||
|
||
cli.action.stop(); | ||
} catch (error) { | ||
cli.action.stop(); | ||
console.error(error); | ||
} finally { | ||
await app.close(); | ||
} | ||
}; | ||
|
||
main(); |
File renamed without changes.
5 changes: 5 additions & 0 deletions
5
apps/api/src/app/notifications/notification/commands/handlers/index.ts
This file contains 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,5 @@ | ||
import { NotificationsHandler } from './notifications.handler'; | ||
import { NotificationsDeleteHandler } from './notifications-delete.handler'; | ||
import { NotificationsMarkAsReadHandler } from './notifications-make-as-read.handler'; | ||
|
||
export const CommandHandlers = [NotificationsHandler, NotificationsDeleteHandler, NotificationsMarkAsReadHandler]; |
14 changes: 14 additions & 0 deletions
14
.../api/src/app/notifications/notification/commands/handlers/notifications-delete.handler.ts
This file contains 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,14 @@ | ||
import { CommandHandler, ICommandHandler } from '@nestjs/cqrs'; | ||
|
||
import { NotificationService } from '../../notification.service'; | ||
import { NotificationsDeleteCommand } from '../notifications-delete.command'; | ||
|
||
@CommandHandler(NotificationsDeleteCommand) | ||
export class NotificationsDeleteHandler implements ICommandHandler<NotificationsDeleteCommand> { | ||
constructor(private readonly notificationService: NotificationService) {} | ||
|
||
public async execute(command: NotificationsDeleteCommand): Promise<void> { | ||
console.log('command:DeleteNotificationCommand', command); | ||
await this.notificationService.onDeleteNotification(command); | ||
} | ||
} |
13 changes: 13 additions & 0 deletions
13
...rc/app/notifications/notification/commands/handlers/notifications-make-as-read.handler.ts
This file contains 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,13 @@ | ||
import { NotificationService } from '../../notification.service'; | ||
import { NotificationsMarkAsReadCommand } from '../notifications-make-as-read.command'; | ||
import { CommandHandler, ICommandHandler } from '@nestjs/cqrs'; | ||
|
||
@CommandHandler(NotificationsMarkAsReadCommand) | ||
export class NotificationsMarkAsReadHandler implements ICommandHandler<NotificationsMarkAsReadCommand> { | ||
constructor(private readonly notificationService: NotificationService) {} | ||
|
||
public async execute(command: NotificationsMarkAsReadCommand): Promise<void> { | ||
console.log('command:NotificationsMarkAsReadCommand', command); | ||
await this.notificationService.onMarkAsRead(command); | ||
} | ||
} |
28 changes: 28 additions & 0 deletions
28
apps/api/src/app/notifications/notification/commands/handlers/notifications.handler.ts
This file contains 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,28 @@ | ||
import { NotificationService } from '../../notification.service'; | ||
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 { Logger } from '@nestjs/common'; | ||
|
||
@CommandHandler(GenericCommand) | ||
export class NotificationsHandler implements ICommandHandler<GenericCommand> { | ||
private readonly logger = new Logger(NotificationsHandler.name); | ||
constructor(private readonly notificationService: NotificationService) {} | ||
|
||
public async execute(command: GenericCommand): Promise<void> { | ||
const { type, payload, user } = command; | ||
switch (type) { | ||
case NotificationsDeleteCommand.type: { | ||
return await this.notificationService.onMarkAsRead(new NotificationsDeleteCommand(payload, user)); | ||
} | ||
case NotificationsMarkAsReadCommand.type: { | ||
return await this.notificationService.onMarkAsRead(new NotificationsMarkAsReadCommand(payload, user)); | ||
} | ||
default: { | ||
this.logger.error('received unknown command: ', command.type); | ||
// return this.commandBus.execute(command); | ||
} | ||
} | ||
} | ||
} |
2 changes: 2 additions & 0 deletions
2
apps/api/src/app/notifications/notification/commands/index.ts
This file contains 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,2 @@ | ||
export { NotificationsDeleteCommand } from './notifications-delete.command' | ||
export { NotificationsMarkAsReadCommand } from './notifications-make-as-read.command' |
7 changes: 7 additions & 0 deletions
7
apps/api/src/app/notifications/notification/commands/notifications-delete.command.ts
This file contains 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,7 @@ | ||
import { ICommand } from '@nestjs/cqrs'; | ||
import { User } from '@ngx-starter-kit/models'; | ||
|
||
export class NotificationsDeleteCommand implements ICommand { | ||
static readonly type = '[Notifications] Delete'; | ||
constructor(public readonly payload: any, public readonly user: User) {} | ||
} |
7 changes: 7 additions & 0 deletions
7
apps/api/src/app/notifications/notification/commands/notifications-make-as-read.command.ts
This file contains 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,7 @@ | ||
import { ICommand } from '@nestjs/cqrs'; | ||
import { User } from '@ngx-starter-kit/models'; | ||
|
||
export class NotificationsMarkAsReadCommand implements ICommand { | ||
static readonly type = '[Notifications] MarkAsRead'; | ||
constructor(public readonly payload: any, public readonly user: User) {} | ||
} |
This file contains 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 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
2 changes: 1 addition & 1 deletion
2
apps/api/src/app/notifications/notification/notification.entity.ts
This file contains 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 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 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 |
---|---|---|
@@ -1,18 +1,19 @@ | ||
import { Module } from '@nestjs/common'; | ||
import { TypeOrmModule } from '@nestjs/typeorm'; | ||
import { CqrsModule } from '@nestjs/cqrs'; | ||
import { SharedModule } from '../shared'; | ||
import { NotificationController } from './notification/notification.controller'; | ||
import { NotificationService } from './notification/notification.service'; | ||
import { CommandHandlers } from './notification/commands/handlers'; | ||
import { SubscriptionController } from './subscription/subscription.controller'; | ||
import { SubscriptionService } from './subscription/subscription.service'; | ||
import { PushService } from './notification/push.service'; | ||
import { Notification } from './notification/notification.entity'; | ||
import { Subscription } from './subscription/subscription.entity'; | ||
|
||
@Module({ | ||
// imports: [SharedModule, TypeOrmModule.forFeature([NotificationsRepository])], | ||
imports: [SharedModule, TypeOrmModule.forFeature([Notification, Subscription])], | ||
providers: [PushService, SubscriptionService, NotificationService], | ||
imports: [SharedModule, CqrsModule, TypeOrmModule.forFeature([Notification, Subscription])], | ||
providers: [PushService, SubscriptionService, NotificationService, ...CommandHandlers], | ||
controllers: [NotificationController, SubscriptionController], | ||
}) | ||
export class NotificationsModule {} |
This file contains 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
2 changes: 1 addition & 1 deletion
2
apps/api/src/app/notifications/subscription/subscription.entity.ts
This file contains 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 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 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,6 @@ | ||
import { ICommand } from '@nestjs/cqrs'; | ||
import { User } from '@ngx-starter-kit/models'; | ||
|
||
export class GenericCommand implements ICommand { | ||
constructor(public readonly type: string, public readonly payload: any, public readonly user: User) {} | ||
} |
Oops, something went wrong.