forked from sqlpad/sqlpad
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathconnection-accesses.js
More file actions
122 lines (105 loc) · 3.12 KB
/
connection-accesses.js
File metadata and controls
122 lines (105 loc) · 3.12 KB
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
const consts = require('../lib/consts');
const { Op } = require('@rickbergfalk/sequelize');
class ConnectionAccesses {
/**
* @param {import('../sequelize-db')} sequelizeDb
* @param {import('../lib/config')} config
*/
constructor(sequelizeDb, config) {
this.sequelizeDb = sequelizeDb;
this.config = config;
}
async create(data) {
if (!data.connectionId) {
throw new Error('connectionId required when saving connection access');
}
if (!data.userId) {
throw new Error('userId required when saving connection access');
}
if (data.duration > 0) {
data.expiryDate = new Date(new Date().getTime() + data.duration * 1000);
} else {
data.expiryDate = new Date().setFullYear(2099);
}
await this.sequelizeDb.ConnectionAccesses.create({
connectionId: data.connectionId,
connectionName: data.connectionName,
userId: data.userId,
userEmail: data.userEmail,
duration: data.duration,
expiryDate: data.expiryDate,
});
return this.findOneActiveByConnectionIdAndUserId(
data.connectionId,
data.userId
);
}
async expire(id) {
const connectionAccess = await this.sequelizeDb.ConnectionAccesses.findOne({
where: { id },
});
if (!connectionAccess) {
throw new Error('Connection access not found');
}
await this.sequelizeDb.ConnectionAccesses.update(
{ expiryDate: new Date() },
{ where: { id } }
);
return this.findOneById(id);
}
findOneById(id) {
return this.sequelizeDb.ConnectionAccesses.findOne({ where: { id } });
}
findOneActiveByConnectionIdAndUserId(connectionId, userId) {
const where = {
[Op.and]: {
[Op.or]: [
{
connectionId: {
[Op.in]: [connectionId, consts.EVERY_CONNECTION_ID],
},
userId,
},
{
connectionId,
userId: { [Op.in]: [userId, consts.EVERYONE_ID] },
},
{
connectionId: consts.EVERY_CONNECTION_ID,
userId: consts.EVERYONE_ID,
},
],
},
expiryDate: { [Op.gt]: new Date() },
};
return this.sequelizeDb.ConnectionAccesses.findOne({ where });
}
findAllActiveByConnectionId(connectionId) {
const where = {
connectionId: { [Op.in]: [connectionId, consts.EVERY_CONNECTION_ID] },
expiryDate: { [Op.gt]: new Date() },
};
return this.sequelizeDb.ConnectionAccesses.findOne({ where });
}
findAllByConnectionId(connectionId) {
const where = {
connectionId: { [Op.in]: [connectionId, consts.EVERY_CONNECTION_ID] },
};
return this.sequelizeDb.ConnectionAccesses.findAll({ where });
}
findAllActive() {
return this.sequelizeDb.ConnectionAccesses.findAll({
where: { expiryDate: { [Op.gt]: new Date() } },
order: [['expiryDate', 'DESC']],
});
}
findAll() {
return this.sequelizeDb.ConnectionAccesses.findAll({
order: [['expiryDate', 'DESC']],
});
}
removeById(id) {
return this.sequelizeDb.ConnectionAccesses.destroy({ where: { id } });
}
}
module.exports = ConnectionAccesses;