Having trouble with my CommandHandler
. Encountering an error that's proving tricky to resolve. Any insights on what might be causing this issue?
Error message: Argument of type 'User' is not assignable to parameter of type 'AggregateRoot'. Type 'User' is missing the following properties from type 'AggregateRoot': [IS_AUTO_COMMIT_ENABLED], [INTERNAL_EVENTS]ts(2345)
// user.repository.ts
...
@Injectable()
export class UserRepository extends Repository<User> {
constructor(private dataSource: DataSource) {
super(User, dataSource.createEntityManager());
}
async createUser(userRegisterDto: UserRegisterDto) {
const id = uuidv4();
const user = await this.save(
super.create({ ...{ id }, ...userRegisterDto }),
);
user.create();
return user;
}
async updateUser(userDto: UserDto) {
const updatedUser = await super.findOne({
where: { id: userDto.id },
});
updatedUser.update();
return updatedUser;
}
async deleteUser(userDto: UserDto) {
const deletedUser = await super.findOne({
where: { id: userDto.id },
});
deletedUser.delete();
return await super.delete(userDto.id);
}
}
// create-user.handler.ts
...
@CommandHandler(CreateUserCommand)
export class CreateUserHandler implements ICommandHandler<CreateUserCommand> {
constructor(
private readonly _repository: UserRepository,
private readonly _publisher: EventPublisher,
) {}
async execute(command: CreateUserCommand): Promise<void> {
Logger.log('Async CreateUserHandler...', 'CreateUserCommand');
await this._repository
.findOne({ where: { email: command.userRegisterDto.email } })
.then((user) => {
if (user) {
throw new HttpException(
'User with this email already exists',
HttpStatus.CONFLICT,
);
}
});
const { userRegisterDto } = command;
const user = this._publisher.mergeObjectContext(
await this._repository.createUser(userRegisterDto),
);
user.commit();
}
}
The User class extends an AbstractEntity which extends AggregateRoot:
// abstract.entity.ts
'use strict';
import { AggregateRoot } from '@nestjs/cqrs';
import { IAggregateEvent } from 'nestjs-eventstore';
import {
CreateDateColumn,
PrimaryGeneratedColumn,
UpdateDateColumn,
} from 'typeorm';
export abstract class AbstractEntity extends AggregateRoot<IAggregateEvent> {
@PrimaryGeneratedColumn('uuid')
id!: string;
@CreateDateColumn({ type: 'timestamp', name: 'created_at' })
createdAt: Date;
@UpdateDateColumn({ type: 'timestamp', name: 'updated_at' })
updatedAt: Date;
abstract toDto();
}