37 lines
1.1 KiB
TypeScript
37 lines
1.1 KiB
TypeScript
import { Injectable, BadRequestException } from '@nestjs/common';
|
|
import { userResetPasswordValidator } from './dtoValidator';
|
|
import { PrismaService } from '@/src/prisma.service';
|
|
import { PasswordHandlers } from '@/src/utils/store/loginHandler';
|
|
|
|
@Injectable()
|
|
export class ResetPasswordService {
|
|
constructor(
|
|
private readonly prisma: PrismaService,
|
|
private readonly passHandlers: PasswordHandlers,
|
|
) { }
|
|
async run(dto: userResetPasswordValidator) {
|
|
const foundUser = await this.prisma.users.findFirstOrThrow({
|
|
where: {
|
|
OR: [{ email: dto.accessKey }, { phone_number: dto.accessKey }],
|
|
},
|
|
});
|
|
if (!foundUser) {
|
|
throw new BadRequestException(
|
|
'User not found. Please check your email or phone number',
|
|
);
|
|
}
|
|
|
|
await this.prisma.users.update({
|
|
where: { id: foundUser.id },
|
|
data: {
|
|
password_token: this.passHandlers.generateRefreshToken(),
|
|
password_expiry_begins: new Date(),
|
|
password_expires_day: 30,
|
|
},
|
|
});
|
|
return {
|
|
message: 'Password reset token sent successfully',
|
|
};
|
|
}
|
|
}
|