File size: 3,883 Bytes
91d9d20
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
var { apis } = global.mira;
var log = require("../../lib/log");
var { getType } = require("../../lib/utils");

module.exports = async function UserDataBase(SQL, sql) {
    var User = SQL.define("User", {
        userID: {
            type: sql.BIGINT,
            unique: true
        },
        name: {
            type: sql.STRING,
            allowNull: false,
            validate: {
                notEmpty: true
            }
        },
        money: {
            type: sql.BIGINT,
            defaultValue: 0,
            validate: {
                isInt: true,
                min: 0
            }
        },
        info: {
            type: sql.JSON,
            validate: {
                isJSON: value => JSON.stringify(value)
            }
        },
        data: {
            type: sql.JSON,
            validate: {
                isJSON: value => JSON.stringify(value)
            }
        },
        banAt: {
            type: sql.BIGINT,
            defaultValue: 0,
            validate: {
                isInt: true,
                min: 0
            }
        },
        reason: {
            type: sql.STRING,
            allowNull: true,
            validate: {
                notEmpty: true
            }
        }
    });

    User.sync({ force: false });

    function getAll() {
        return User.findAll();
    }

    function find(conditions = {}) {
        if (getType(conditions) !== "Object") 
            throw new Error("conditions must be an JSON Object.");

        return User.findAll({ where: conditions });
    }

    async function getInfo(userID) {
        if (isNaN(parseInt(userID))) 
            throw new Error("userID must be a string number.");

        var infoObj = await apis.getUserInfo(userID);
        return infoObj[userID] || {}
    }

    async function findOne(userID) {
        if (isNaN(parseInt(userID))) 
            throw new Error("userID must be a string number.");

        var userData = await User.findOne({ where: { userID } });
        if (!userData) {
            await createData(userID);
            return await findOne(userID);
        }

        return userData.get({ plain: true });
    }

    async function createData(userID) {
        var info = await getInfo(userID);
        var infoObj = {
            userID,
            name: info.name || "User",
            money: 0,
            info,
            data: {},
            banAt: 0,
            reason: null
        }
        delete infoObj.info.name;
        await User.findOrCreate({ where: { userID }, defaults: infoObj });
        log.info("database.create.success", userID);
        return;
    }

    async function setData(userID, data) {
        if (isNaN(parseInt(userID))) 
            throw new Error("userID must be a string number.");
        if (getType(data) !== "Object") 
            throw new Error("data must be an JSON Object.");

        await User.update(data, { where: { userID } });
        return;
    }

    async function setDataAll(data, conditions = {}) {
        if (getType(conditions) !== "Object") 
            throw new Error("conditions must be an JSON Object.");
        if (getType(data) !== "Object") 
            throw new Error("data must be an JSON Object.");

        await User.update(data, { where: conditions });
        return;
    }

    async function deleteUser(userID) {
        if (isNaN(parseInt(userID))) 
            throw new Error("userID must be a string number.");
        await User.destroy({ where: { userID } });
        return;
    }

    log.info("database.model", "User.sqlite3");
    return global.database.model.User = {
        getAll,
        find,
        findOne,
        setData,
        setDataAll,
        deleteUser
    }
}