"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.VerifyEmailCommandHandler = void 0;
const TokenService_1 = require("../../Services/TokenService");
const UserAggregate_1 = require("../../../Domain/User/UserAggregate");
const Logger_1 = require("../../Services/Logger");
class VerifyEmailCommandHandler {
    constructor(userRepo) {
        this.userRepo = userRepo;
    }
    async execute(cmd) {
        try {
            if (!cmd.token) {
                throw new Error('Verification token is required');
            }
            // Hash the token to compare with stored value
            const hashedToken = await TokenService_1.TokenService.hashToken(cmd.token);
            // Find user with this verification token
            const user = await this.userRepo.findByToken(hashedToken);
            if (!user) {
                throw new Error('Invalid or expired verification token');
            }
            // Check if token is expired
            if (user.TokenExpires && user.TokenExpires < new Date()) {
                throw new Error('Verification token has expired');
            }
            // Update user verification status
            user.token = null;
            user.TokenExpires = null;
            user.state = UserAggregate_1.UserState.VERIFIED_REGULAR;
            await this.userRepo.update(user.id, user);
            return true;
        }
        catch (error) {
            (0, Logger_1.logError)('Email verification error', error instanceof Error ? error : new Error(String(error)));
            throw error;
        }
    }
}
exports.VerifyEmailCommandHandler = VerifyEmailCommandHandler;
//# sourceMappingURL=VerifyEmailCommandHandler.js.map