I have an application I am current working on and all other things are working fine except for one endpoint which is update IsTokenExpired. This holds a boolean which could either be true or false. I have been trying to update this isTokenExpired for some time and it seems to have given up on me. Below is the code list:
password-reset.service.ts
...
async updateExpiryStatus(
accessToken: string,
isExpired: boolean
): Promise<any> {
const expiryStatus = await this.passwordRepository.findOne({
where: { accessToken },
});
const result = await this.passwordRepository
.createQueryBuilder('password_entity')
.update(PasswordEntity)
.set({
isExpired,
})
.where('accessToken = :accessToken', { accessToken })
.execute();
console.log('====================================');
console.log({ expiryStatus, accessToken, isExpired });
console.log('====================================');
return result;
}
...
password-reset.controller.ts
@Put('updateToken/:accessToken')
updateExpiryStatus(
@Param('accessToken') accessToken: string,
@Body() isExpired: boolean
): Promise<string> {
return this.passwordService.updateExpiryStatus(accessToken, isExpired);
}
}
...
password-reset.entity.ts
import { Column, Entity, PrimaryGeneratedColumn } from 'typeorm';
@Entity('password_reset')
export class PasswordEntity {
@PrimaryGeneratedColumn()
id: number;
@Column()
email: string;
@Column({ unique: true })
accessToken: string;
@Column({ default: true })
isExpired: boolean;
}
If you already query the PasswordEntity with this
const expiryStatus = await this.passwordRepository.findOne({
where: { accessToken },
});
Then you can do this to update the isExpired
expiryStatus.isExpired = isExpired;
await this.passwordRepository.save(expiryStatus);
This will update the status for the row that you fetch with the accessToken.
this.passwordRepository.save(expiryStatus) will return the newly updated row.