"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.UserAuthenticationService = void 0;
const tslib_1 = require("tslib");
const authentication_jwt_1 = require("@loopback/authentication-jwt");
const repository_1 = require("@loopback/repository");
const security_1 = require("@loopback/security");
const bcryptjs_1 = require("bcryptjs");
const application_error_1 = require("./../helper/application-error");
const entity_name_1 = require("./../models/entity-name");
/* eslint-disable*/
let UserAuthenticationService = class UserAuthenticationService {
    constructor(userRepository) {
        this.userRepository = userRepository;
    }
    async registerUser(newUserRequest) {
        const newUser = {
            email: newUserRequest.email,
            firstname: newUserRequest.firstname,
            lastname: newUserRequest.lastname,
            name: newUserRequest.name,
        };
        if (!newUser.email) {
            throw application_error_1.ApplicationError.missingParameter('email');
        }
        let savedUser = await this.userRepository.findOne({
            where: { email: newUser.email },
        });
        if (savedUser) {
            const credentialsFound = await this.userRepository.findCredentials(savedUser.id);
            if (credentialsFound) {
                throw application_error_1.ApplicationError.conflict({ email: newUser.email });
            }
            await this.userRepository.updateById(savedUser.id, newUser);
        }
        else {
            savedUser = await this.userRepository.create(newUser);
        }
        const password = await bcryptjs_1.hash(savedUser.id + newUserRequest.password, await bcryptjs_1.genSalt());
        await this.userRepository.userCredentials(savedUser.id).create({ password });
        return this.convertToUserProfile(savedUser);
    }
    async verifyCredentials(credentials) {
        const invalidCredentialsError = 'Invalid email or password.';
        const foundUser = await this.userRepository.findOne({
            where: { email: credentials.email },
        });
        if (!foundUser) {
            throw application_error_1.ApplicationError.authenticationFailed();
        }
        const credentialsFound = await this.userRepository.findCredentials(foundUser.id);
        if (!credentialsFound) {
            throw application_error_1.ApplicationError.authenticationFailed();
        }
        const passwordMatched = await bcryptjs_1.compare(foundUser.id + credentials.password, credentialsFound.password);
        if (!passwordMatched) {
            throw application_error_1.ApplicationError.authenticationFailed();
        }
        return foundUser;
    }
    convertToUserProfile(user) {
        return {
            entityName: entity_name_1.EntityName.user,
            [security_1.securityId]: user.id.toString(),
            id: user.id,
            name: user.username,
            email: user.email,
            firstname: user.firstname,
            lastname: user.lastname,
        };
    }
    async completeUserProfile(currentUserProfile) {
        return this.convertToUserProfile(await this.findUserById(currentUserProfile[security_1.securityId]));
    }
    //function to find user by id
    async findUserById(id) {
        const userNotfound = 'invalid User';
        const foundUser = await this.userRepository.findOne({
            where: { id: id },
        });
        if (!foundUser) {
            throw application_error_1.ApplicationError.notFound({ user: id });
        }
        return foundUser;
    }
};
UserAuthenticationService = tslib_1.__decorate([
    tslib_1.__param(0, repository_1.repository(authentication_jwt_1.UserRepository)),
    tslib_1.__metadata("design:paramtypes", [authentication_jwt_1.UserRepository])
], UserAuthenticationService);
exports.UserAuthenticationService = UserAuthenticationService;
//# sourceMappingURL=user-authentication.service.js.map