-
Notifications
You must be signed in to change notification settings - Fork 145
/
Copy pathUserInDatabase2.ts
65 lines (56 loc) · 1.87 KB
/
UserInDatabase2.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
65
import {AccessLevel} from '../../../shared/schema/constants/Auth';
import Users from '../../models/Users';
import type {Credentials} from '../../../shared/schema/Auth';
import type {RTorrentConnectionSettings} from '../../../shared/schema/ClientConnectionSettings';
import type {UserInDatabase1} from './types/UserInDatabase1';
const migrationError = (err?: Error) => {
if (err) {
console.error(err);
}
console.error('Migration failed. You need to reset the databases manually.');
process.exit();
};
const migration = () => {
return Users.listUsers().then((users) => {
return Promise.all(
users.map(async (user) => {
if (user.client != null) {
// No need to migrate.
return;
}
const userV1 = user as unknown as UserInDatabase1;
let connectionSettings: RTorrentConnectionSettings | null = null;
if (userV1.socketPath != null) {
connectionSettings = {
client: 'rTorrent',
type: 'socket',
version: 1,
socket: userV1.socketPath,
};
} else if (userV1.host != null && userV1.port != null) {
connectionSettings = {
client: 'rTorrent',
type: 'tcp',
version: 1,
host: userV1.host,
port: userV1.port,
};
}
if (connectionSettings == null) {
throw new Error('Corrupted client connection settings.');
}
const userV2: Credentials = {
username: userV1.username,
password: userV1.password,
client: connectionSettings,
level: userV1.isAdmin ? AccessLevel.ADMINISTRATOR : AccessLevel.USER,
};
await Users.removeUser(userV1.username);
await Users.createUser(userV2, false);
}),
).catch((err) => {
migrationError(err);
});
});
};
export default migration;