import { Inject , Injectable } from '@nestjs/common' ; import { CommandBus } from '@nestjs/cqrs' ; import { Model } from 'mongoose' ; import { CreateUserCommand , ChangeUserNameCommand , } from '../../application/command' ; import { USER_MODEL , UserView } from '../read-model/schema/user.schema' ; import { UserIdNotFoundError } from '../../domain/exception/user-id-not-found.error' ; import { ChangeUserPaypalCommand } from '../../application/command/change-user-paypal.command' ; import { MemberService } from '../../../member/infrastructure/service/member.service' ; @ Injectable ( ) export class UserService { constructor ( private readonly commandBus , @ Inject ( USER_MODEL ) private readonly userModel , private readonly memberService ) { } async createUser ( id , name , email , paypal ? ) { return this . commandBus . execute ( new CreateUserCommand ( id , name , email , paypal ) ) ; } async updateUser ( id , name , paypal ) { await this . commandBus . execute ( new ChangeUserNameCommand ( id , name ) ) ; if ( paypal !== '' ) { await this . commandBus . execute ( new ChangeUserPaypalCommand ( id , paypal ) ) ; } } async getUser ( id ) { const result = await this . userModel . findById ( id ) . exec ( ) ; if ( result == null ) { throw new UserIdNotFoundError ( id ) ; } return result ; } }