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} */ static async create(createReset: CreateResetCommand): Promise { 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} */ static async deleteByTokenAndMail(deleteReset: DeleteResetCommand): Promise { 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); }); } }