summaryrefslogtreecommitdiff
path: root/src/routers/vault/utils/claim.js
blob: dda900147e5edb74db35b9b85d0ec84d9b443ecc (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
const { Op } = require("sequelize");
const LegacyAccount = require("../types/LegacyAccount.js");
const LegacyChar = require("../types/LegacyChar.js");

// claim by email // TODO: DRY this
const claim_accounts = async (req, email, vault_id, session = null) => {
    const locals = req.app.locals;

    if (email === null || email.length < 5 || email === "a@a.com")
        return Promise.resolve(false);

    if (session === null) {
        for (const [key, sess] of locals.session) {
            // try to find the session
            if (sess.authenticated === true && sess.vault === vault_id) {
                session = sess;
            }
        }
    }

    // TODO: make these operations less expensive (foreign keys could help)
    let already_claimed = await locals.vault.claimed_legacy_accounts.findAll({
        where: {vaultId: vault_id},
    });

    already_claimed = already_claimed.map(acc => {
        return {accountId: {
            [Op.not]: acc.accountId, // NOTE: if query is larger than 65535 this will throw
        }};
    });

    const to_claim = await locals.legacy.login.findAll({
        where: {
            email: email,
            [Op.and]: already_claimed,
        },
    });

    for (const acc of to_claim) {
        await locals.vault.claimed_legacy_accounts.create({
            accountId: acc.accountId,
            vaultId: vault_id,
        });

        req.app.locals.vault.account_log.create({
            vaultId: vault_id,
            accountType: "LEGACY",
            actionType: "LINK",
            accountId: acc.accountId,
            ip: req.ip,
        });

        if (session !== null) {
            const chars_ = await locals.legacy.char.findAll({
                where: {accountId: acc.accountId},
            });

            const legacy_account = new LegacyAccount(acc.accountId, acc.userid);
            legacy_account.revoltId = acc.revoltId;

            for (const char of chars_) {
                const legacy_char = new LegacyChar(legacy_account, char.charId, char.name);
                legacy_char.revoltId = char.revoltId;
                legacy_char.baseLevel = char.baseLevel;
                legacy_char.gender = char.sex;

                legacy_account.chars.push(legacy_char);
            }

            // add to session cache
            session.legacyAccounts.push(legacy_account);
        }

        locals.logger.info(`Vault.legacy.account: linked Legacy account ${acc.accountId} to Vault account <${vault_id}@vault> [${req.ip}]`);
    }

    // TODO: split TMWA claiming into its own function, add forums and wiki claiming
};

module.exports = {
    claim_accounts,
};