-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsequelize-presence.ts
64 lines (60 loc) · 1.81 KB
/
sequelize-presence.ts
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
import { Snowflake } from 'discord.js';
import { Model, DataTypes } from 'sequelize';
import { database } from './sequelize';
export class UserPresence extends Model {
public id!: string;
public lastSeenAt!: Date;
}
UserPresence.init(
{
id: {
type: new DataTypes.CHAR(32),
primaryKey: true,
allowNull: false
},
lastSeenAt: {
type: DataTypes.DATE,
allowNull: false,
defaultValue: 0
}
},
{
tableName: 'users-presences',
sequelize: database
}
);
if (process.argv.includes('--init')) setTimeout(() => UserPresence.sync({ force: true }).then(() => console.log('Users presences table created')), 5000);
if (process.argv.includes('--sync')) UserPresence.sync({ alter: true }).then(() => console.log('Users presences table synced'));
export const getUserLastSeenAt = (userID: Snowflake): Promise<any> => {
return new Promise((resolve) => {
UserPresence.findOne({
where: {
id: userID
}
}).then((presence) => resolve(presence?.lastSeenAt));
});
};
export const updateUserLastSeenAt = (userID: Snowflake): Promise<void> => {
return new Promise((resolve) => {
UserPresence.findOne({
where: {
id: userID
}
}).then((presence) => {
if (presence) {
UserPresence.update({
lastSeenAt: new Date()
}, {
where: {
id: userID
}
}).then(() => resolve());
} else {
UserPresence.create({
lastSeenAt: new Date(),
id: userID
}).then(() => resolve());
}
});
});
};