"use strict";
var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
    if (k2 === undefined) k2 = k;
    Object.defineProperty(o, k2, { enumerable: true, get: function() { return m[k]; } });
}) : (function(o, m, k, k2) {
    if (k2 === undefined) k2 = k;
    o[k2] = m[k];
}));
var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) {
    Object.defineProperty(o, "default", { enumerable: true, value: v });
}) : function(o, v) {
    o["default"] = v;
});
var __importStar = (this && this.__importStar) || function (mod) {
    if (mod && mod.__esModule) return mod;
    var result = {};
    if (mod != null) for (var k in mod) if (k !== "default" && Object.hasOwnProperty.call(mod, k)) __createBinding(result, mod, k);
    __setModuleDefault(result, mod);
    return result;
};
var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
    function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
    return new (P || (P = Promise))(function (resolve, reject) {
        function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
        function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
        function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
        step((generator = generator.apply(thisArg, _arguments || [])).next());
    });
};
var __importDefault = (this && this.__importDefault) || function (mod) {
    return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.acceptInvite = exports.inviteUser = exports.deleteUser = exports.updateUser = exports.readUser = exports.readUsers = exports.createUser = void 0;
const ItemsService = __importStar(require("./items"));
const jsonwebtoken_1 = __importDefault(require("jsonwebtoken"));
const mail_1 = require("../mail");
const database_1 = __importDefault(require("../database"));
const argon2_1 = __importDefault(require("argon2"));
const exceptions_1 = require("../exceptions");
exports.createUser = (data, accountability) => __awaiter(void 0, void 0, void 0, function* () {
    return yield ItemsService.createItem('directus_users', data, accountability);
});
exports.readUsers = (query, accountability) => __awaiter(void 0, void 0, void 0, function* () {
    return yield ItemsService.readItems('directus_users', query, accountability);
});
exports.readUser = (pk, query, accountability) => __awaiter(void 0, void 0, void 0, function* () {
    return yield ItemsService.readItem('directus_users', pk, query, accountability);
});
exports.updateUser = (pk, data, accountability) => __awaiter(void 0, void 0, void 0, function* () {
    /**
     * @todo
     * Remove "other" refresh token sessions when changing password to enforce "logout everywhere" on password change
     *
     * Maybe make this an option?
     */
    return yield ItemsService.updateItem('directus_users', pk, data, accountability);
});
exports.deleteUser = (pk, accountability) => __awaiter(void 0, void 0, void 0, function* () {
    yield ItemsService.deleteItem('directus_users', pk, accountability);
});
exports.inviteUser = (email, role, accountability) => __awaiter(void 0, void 0, void 0, function* () {
    yield exports.createUser({ email, role, status: 'invited' }, accountability);
    const payload = { email };
    const token = jsonwebtoken_1.default.sign(payload, process.env.SECRET, { expiresIn: '7d' });
    const acceptURL = process.env.PUBLIC_URL + '/admin/accept-invite?token=' + token;
    yield mail_1.sendInviteMail(email, acceptURL);
});
exports.acceptInvite = (token, password) => __awaiter(void 0, void 0, void 0, function* () {
    const { email } = jsonwebtoken_1.default.verify(token, process.env.SECRET);
    const user = yield database_1.default
        .select('id', 'status')
        .from('directus_users')
        .where({ email })
        .first();
    if (!user || user.status !== 'invited') {
        throw new exceptions_1.InvalidPayloadException(`Email address ${email} hasn't been invited.`);
    }
    const passwordHashed = yield argon2_1.default.hash(password);
    yield database_1.default('directus_users')
        .update({ password: passwordHashed, status: 'active' })
        .where({ id: user.id });
});
