71 lines
2.2 KiB
TypeScript
71 lines
2.2 KiB
TypeScript
import { userCreatePasswordValidator } from './dtoValidator';
|
|
import { PrismaService } from '@/src/prisma.service';
|
|
import { PasswordHandlers } from '@/src/utils/store/loginHandler';
|
|
import { Injectable, BadRequestException } from '@nestjs/common';
|
|
|
|
@Injectable()
|
|
export class CreatePasswordService {
|
|
constructor(
|
|
private readonly prisma: PrismaService,
|
|
private readonly passHandlers: PasswordHandlers,
|
|
) { }
|
|
|
|
async run(dto: userCreatePasswordValidator) {
|
|
if (dto.password !== dto.rePassword) {
|
|
throw new BadRequestException('Passwords do not match');
|
|
}
|
|
const foundUser = await this.prisma.users.findFirstOrThrow({
|
|
where: { password_token: dto.passwordToken },
|
|
});
|
|
|
|
const passwordExpiryDate = new Date(foundUser.password_expiry_begins);
|
|
const passwordExpiryDay = foundUser.password_expires_day;
|
|
const dayInMilliseconds = 24 * 60 * 60 * 1000;
|
|
console.log(
|
|
'exp : ',
|
|
passwordExpiryDate,
|
|
passwordExpiryDay,
|
|
dayInMilliseconds,
|
|
);
|
|
const today = new Date().getTime();
|
|
const comparasionDate = new Date(
|
|
passwordExpiryDate.getTime() + passwordExpiryDay * dayInMilliseconds,
|
|
).getTime();
|
|
if (today >= comparasionDate) {
|
|
throw new BadRequestException(
|
|
'Password token is expired contact to your admin',
|
|
);
|
|
}
|
|
|
|
const hashPassword = this.passHandlers.create_hashed_password(
|
|
foundUser.uu_id,
|
|
dto.password,
|
|
);
|
|
|
|
const updatedUser = await this.prisma.users.update({
|
|
where: { id: foundUser.id },
|
|
data: {
|
|
hash_password: hashPassword,
|
|
password_token: '',
|
|
password_expiry_begins: new Date(),
|
|
},
|
|
});
|
|
console.log('updatedUser');
|
|
console.dir(updatedUser);
|
|
await this.prisma.password_history.create({
|
|
data: {
|
|
userUUID: foundUser.uu_id,
|
|
password: hashPassword,
|
|
old_password_first: dto.password,
|
|
old_password_first_modified_at: new Date(),
|
|
old_password_second: '',
|
|
old_password_second_modified_at: new Date(),
|
|
old_password_third: '',
|
|
old_password_third_modified_at: new Date(),
|
|
},
|
|
});
|
|
|
|
return { message: 'Password created successfully' };
|
|
}
|
|
}
|