import { Inject } from '@nestjs/common' ; import { CommandHandler , ICommandHandler } from '@nestjs/cqrs' ; import { v4 as uuid } from 'uuid' ; import { GroupNameAlreadyRegisteredError , GroupIdAlreadyRegisteredError , } from '../../domain/exception' ; import { Group , GroupName , GroupId , GroupCurrencyCode , } from '../../domain/model' ; import { GROUPS , Groups } from '../../domain/repository' ; import { CreateGroupCommand } from '../command/create-group.command' ; import { Member } from '../../../member/domain/model/member' ; import { UserId } from '../../../user/domain/model/user-id' ; import { MemberId } from '../../../member/domain/model/member-id' ; import { MemberName } from '../../../member/domain/model/member-name' ; import { CHECK_UNIQUE_GROUP_NAME , CheckUniqueGroupName , } from '../../domain/services/check-unique-group-name.service' ; import { MEMBERS , Members } from '../../../member/domain/repository/index' ; @ CommandHandler ( CreateGroupCommand ) export class CreateGroupHandler implements ICommandHandler < CreateGroupCommand > { constructor ( @ Inject ( GROUPS ) private readonly groups , @ Inject ( MEMBERS ) private readonly members , @ Inject ( CHECK_UNIQUE_GROUP_NAME ) private readonly checkUniqueGroupName ) { } async execute ( command ) { const groupId = GroupId . fromString ( command . groupId ) ; const name = GroupName . fromString ( command . name ) ; const currencyCode = GroupCurrencyCode . fromString ( command . currencyCode ) ; const groupMembers = command . members ; const ownerId = UserId . fromString ( command . owner . _id ) ; if ( ( await this . groups . find ( groupId ) ) instanceof Group ) { throw GroupIdAlreadyRegisteredError . withString ( command . groupId ) ; } if ( ( await this . checkUniqueGroupName . with ( name , ownerId ) ) instanceof GroupId ) { throw GroupNameAlreadyRegisteredError . withString ( command . name ) ; } const group = Group . add ( groupId , name , currencyCode , UserId . fromString ( command . owner . _id ) ) ; this . groups . save ( group ) ; const membersAdded = [ ] ; membersAdded . push ( group . addMember ( MemberId . fromString ( uuid ( ) ) , MemberName . fromString ( command . owner . name ) , UserId . fromString ( command . owner . _id ) ) ) ; groupMembers . forEach ( member => { membersAdded . push ( group . addMember ( MemberId . fromString ( member . _id ) , MemberName . fromString ( member . name ) ) ) ; } ) ; membersAdded . map ( member => this . members . save ( member ) ) ; } }