import db from '../db/database.js';
import * as uuid from 'uuid';
import { isEnterprise } from '@nangohq/utils';
class UserService {
    async getUserById(id) {
        const result = await db.knex.select('*').from(`_nango_users`).where({ id });
        if (result == null || result.length == 0 || result[0] == null) {
            return null;
        }
        if (result[0].suspended) {
            return null;
        }
        return result[0];
    }
    async getUsersByAccountId(accountId) {
        const result = await db.knex.select('id', 'name', 'email', 'suspended').from(`_nango_users`).where({ account_id: accountId });
        if (result == null || result.length == 0 || result[0] == null) {
            return [];
        }
        return result;
    }
    async getAnUserByAccountId(accountId) {
        const result = await db.knex
            .select('*')
            .from(`_nango_users`)
            .where({
            account_id: accountId,
            suspended: false
        })
            .orderBy('id', 'asc')
            .limit(1);
        if (result == null || result.length == 0 || result[0] == null) {
            return null;
        }
        return result[0];
    }
    async getUserByEmail(email) {
        const result = await db.knex.select('*').from(`_nango_users`).where({ email: email });
        if (result == null || result.length == 0 || result[0] == null) {
            return null;
        }
        return result[0];
    }
    async getUserByResetPasswordToken(link) {
        const result = await db.knex.select('*').from(`_nango_users`).where({ reset_password_token: link });
        if (result == null || result.length == 0 || result[0] == null) {
            return null;
        }
        return result[0];
    }
    async createUser(email, name, hashedPassword, salt, accountId) {
        const result = await db.knex
            .from(`_nango_users`)
            .insert({ email: email, name: name, hashed_password: hashedPassword, salt: salt, account_id: accountId }, ['id']);
        if (Array.isArray(result) && result.length === 1 && result[0] != null && 'id' in result[0]) {
            const userId = result[0]['id'];
            return this.getUserById(userId);
        }
        return null;
    }
    async editUserPassword(user) {
        return db.knex.from(`_nango_users`).where({ id: user.id }).update({
            reset_password_token: user.reset_password_token,
            hashed_password: user.hashed_password
        });
    }
    async editUserName(name, id) {
        return db.knex.from(`_nango_users`).where({ id }).update({ name, updated_at: new Date() });
    }
    async changePassword(newPassword, oldPassword, id) {
        return db.knex.from(`_nango_users`).where({ id }).update({
            hashed_password: newPassword,
            salt: oldPassword
        });
    }
    async suspendUser(id) {
        if (id !== null && id !== undefined) {
            await db.knex.from(`_nango_users`).where({ id }).update({ suspended: true, suspended_at: new Date() });
        }
    }
    async inviteUser(email, name, accountId, inviter_id) {
        const token = uuid.v4();
        const expires_at = new Date(new Date().getTime() + 7 * 24 * 60 * 60 * 1000);
        const result = await db.knex
            .from(`_nango_invited_users`)
            .insert({
            email,
            name,
            account_id: accountId,
            invited_by: inviter_id,
            token,
            expires_at
        })
            .returning('*');
        if (result == null || result.length == 0 || result[0] == null) {
            return null;
        }
        return result[0];
    }
    async getInvitedUsersByAccountId(accountId) {
        const date = new Date();
        const result = await db.knex.select('*').from(`_nango_invited_users`).where({ account_id: accountId }).whereRaw('expires_at > ?', date);
        return result || [];
    }
    async getInvitedUserByToken(token) {
        const date = new Date();
        if (isEnterprise && process.env['NANGO_ADMIN_INVITE_TOKEN'] === token) {
            return {
                id: 1,
                email: '',
                name: '',
                account_id: 0,
                invited_by: 0,
                token: '',
                expires_at: new Date(),
                accepted: true
            };
        }
        const result = await db.knex.select('*').from(`_nango_invited_users`).where({ token }).whereRaw('expires_at > ?', date);
        if (result == null || result.length == 0 || result[0] == null) {
            return null;
        }
        return result[0];
    }
    async markAcceptedInvite(token) {
        const result = await db.knex.from(`_nango_invited_users`).where({ token }).update({ accepted: true });
        return result;
    }
}
export default new UserService();
//# sourceMappingURL=user.service.js.map