import ObjectID from "../../Types/ObjectID";
import UserMiddleware from "../Middleware/UserAuthorization";
import UserTotpAuthService from "../Services/UserTotpAuthService";
import BaseAPI from "./BaseAPI";
import UserTotpAuth from "../../Models/DatabaseModels/UserTotpAuth";
import BadDataException from "../../Types/Exception/BadDataException";
import TotpAuth from "../Utils/TotpAuth";
import Response from "../Utils/Response";
import UserService from "../Services/UserService";
export default class UserTotpAuthAPI extends BaseAPI {
    constructor() {
        var _a;
        super(UserTotpAuth, UserTotpAuthService);
        this.router.post(`${(_a = new this.entityType().getCrudApiPath()) === null || _a === void 0 ? void 0 : _a.toString()}/validate`, UserMiddleware.getUserMiddleware, async (req, res, next) => {
            var _a, _b;
            try {
                const userTotpAuth = await UserTotpAuthService.findOneById({
                    id: new ObjectID(req.body["id"]),
                    select: {
                        twoFactorSecret: true,
                        userId: true,
                    },
                    props: {
                        isRoot: true,
                    },
                });
                if (!userTotpAuth) {
                    throw new BadDataException("TOTP auth not found");
                }
                if (((_a = userTotpAuth.userId) === null || _a === void 0 ? void 0 : _a.toString()) !==
                    ((_b = req.userAuthorization) === null || _b === void 0 ? void 0 : _b.userId.toString())) {
                    throw new BadDataException("Two factor auth not found");
                }
                if (!userTotpAuth.userId) {
                    throw new BadDataException("User not found");
                }
                // get user email.
                const user = await UserService.findOneById({
                    id: userTotpAuth.userId,
                    select: {
                        email: true,
                    },
                    props: {
                        isRoot: true,
                    },
                });
                if (!user) {
                    throw new BadDataException("User not found");
                }
                if (!user.email) {
                    throw new BadDataException("User email not found");
                }
                const isValid = TotpAuth.verifyToken({
                    secret: userTotpAuth.twoFactorSecret || "",
                    token: req.body["code"] || "",
                    email: user.email,
                });
                if (!isValid) {
                    throw new BadDataException("Invalid code");
                }
                // update this 2fa code as verified
                await UserTotpAuthService.updateOneById({
                    id: userTotpAuth.id,
                    data: {
                        isVerified: true,
                    },
                    props: {
                        isRoot: true,
                    },
                });
                return Response.sendEmptySuccessResponse(req, res);
            }
            catch (err) {
                next(err);
            }
        });
    }
}
//# sourceMappingURL=UserTotpAuthAPI.js.map