- Add these two modules to your NestJs Project
- "@repo/db" should be where your prisma client is generated.
Based on @nicheeer/prisma-transaction
@Injectable()
export class AuthService {
constructor(
private usersService: UsersService,
private emailService: EmailService,
private prisma: PrismaService
) {}
@Transaction()
async registerUser(input: RegisterDto) {
const existingEmail = await this.usersService.findOneByEmail(input.email);
if (existingEmail) {
throw new UnprocessableEntityException({
status: HttpStatus.UNPROCESSABLE_ENTITY,
error: "This email is already being used by another user.",
});
}
const existingUsername = await this.usersService.findOneByUsername(
input.username
);
if (existingUsername) {
throw new UnprocessableEntityException({
status: HttpStatus.UNPROCESSABLE_ENTITY,
error: "This username is already being used by another user.",
});
}
const hash = await this.hash(input.password);
const user = await this.usersService.create({
...input,
password: hash,
});
await this.prisma.expert.create({
data: {
userId: user.id,
},
});
await this.sendToken({
email: input.email,
type: "VERIFY_EMAIL",
});
return {
...user,
password: null,
};
}
}import { Module } from "@nestjs/common";
import { PrismaModule } from "../prisma/prisma.module";
...
@Module({
imports: [
...
PrismaModule,
],
controllers: [AuthController],
providers: [AuthService],
exports: [AuthService],
})
export class AuthModule {}