ff-admin-server/src/command/resetCommandHandler.ts

55 lines
1.6 KiB
TypeScript
Raw Normal View History

2024-11-23 11:11:19 +00:00
import { dataSource } from "../data-source";
import { reset } from "../entity/reset";
import InternalException from "../exceptions/internalException";
import { StringHelper } from "../helpers/stringHelper";
import { CreateResetCommand, DeleteResetCommand } from "./resetCommand";
export default abstract class ResetCommandHandler {
/**
* @description create user
* @param CreateResetCommand
* @returns {Promise<string>}
*/
static async create(createReset: CreateResetCommand): Promise<string> {
const token = StringHelper.random(32);
return await dataSource
.createQueryBuilder()
.insert()
.into(reset)
.values({
token: token,
mail: createReset.mail,
username: createReset.username,
secret: createReset.secret,
})
.orUpdate(["token", "secret"], ["mail"])
.execute()
.then((result) => {
return token;
})
.catch((err) => {
throw new InternalException("Failed saving reset", err);
});
}
/**
* @description delete reset by mail and token
* @param DeleteRefreshCommand
* @returns {Promise<any>}
*/
static async deleteByTokenAndMail(deleteReset: DeleteResetCommand): Promise<any> {
return await dataSource
.createQueryBuilder()
.delete()
.from(reset)
.where("reset.token = :token", { token: deleteReset.token })
.andWhere("reset.mail = :mail", { mail: deleteReset.mail })
.execute()
.then((res) => {})
.catch((err) => {
throw new InternalException("failed reset removal", err);
});
}
}