-
Notifications
You must be signed in to change notification settings - Fork 27
Expand file tree
/
Copy pathin-memory-user.repository.ts
More file actions
111 lines (100 loc) · 2.94 KB
/
in-memory-user.repository.ts
File metadata and controls
111 lines (100 loc) · 2.94 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
import { UserRepository } from '../user.repository';
import { Id, UserRole } from 'src/models/types';
import { User } from 'src/models/user';
export class InMemoryUserRepository implements UserRepository {
getUserById(id: Id, attachAccount = false): Promise<User> {
const user = USERS.find(user => user.id === id);
if (attachAccount) {
// TODO attach account in response
}
return new Promise((resolve, reject) => {
user ? resolve(user) : reject();
});
}
getUserByEmail(email: string): Promise<User> {
const user = USERS.find(user => user.email === email);
return new Promise((resolve, reject) => {
user ? resolve(user) : reject();
});
}
getUserByExternalId(provider: string, externalId: string): Promise<User> {
const user = USERS.find(user => !!externalId && !!user.externalId && user.externalId[provider] === externalId);
return new Promise((resolve, reject) => {
user ? resolve(user) : reject();
});
}
assertUserWithExternalIdNotExist(provider: string, externalId: string): Promise<void> {
return new Promise((resolve, reject) => {
this.getUserByExternalId(provider, externalId)
.then((user) => reject('User already exists'))
.catch(() => resolve());
});
}
getUsers(accountId: Id): Promise<User[]> {
const users = USERS.filter(user => user.accountId === accountId)
return Promise.resolve(users);
}
createUser(user: User): Promise<Id> {
user.id = (USERS.length + 1).toString();
USERS.push(user);
return Promise.resolve(user.id);
}
deleteUser(id: Id): Promise<void> {
USERS = USERS.filter(user => user.id !== id);
return Promise.resolve();
}
patchUser(id: Id, data: User): Promise<User> {
return this.getUserById(id)
.then(userToPatch => Object.assign(userToPatch, data));
}
}
let USERS: User[] = [
{
id: '1',
email: 'admin@app.com',
password: '$2y$10$k.58cTqd/rRbAOc8zc3nCupCC6QkfamoSoO2Hxq6HVs0iXe7uvS3e', // '123'
role: 'ADMIN',
confirmed: true,
createdWith: 'password'
},
{
id: '2',
accountId: '1',
email: 'bartosz@app.com',
password: '$2y$10$k.58cTqd/rRbAOc8zc3nCupCC6QkfamoSoO2Hxq6HVs0iXe7uvS3e', // '123'
role: 'OWNER',
confirmed: true,
createdWith: 'password',
tfa: true,
tfaSecret: 'FB2S2HQLIE2UIZQDGYLCMS3SNZMXQDSK'
},
{
id: '3',
accountId: '2',
email: 'john@app.com',
password: '$2y$10$k.58cTqd/rRbAOc8zc3nCupCC6QkfamoSoO2Hxq6HVs0iXe7uvS3e', // '123'
role: 'OWNER',
confirmed: true,
createdWith: 'password'
},
{
id: '4',
accountId: '2',
email: 'mike@app.com',
password: '$2y$10$k.58cTqd/rRbAOc8zc3nCupCC6QkfamoSoO2Hxq6HVs0iXe7uvS3e', // '123'
role: 'READER',
confirmed: true,
createdWith: 'password'
},
{
id: '5',
accountId: '1',
email: 'hi@bartosz.io',
role: 'OWNER',
confirmed: true,
externalId: {
github: '8076187'
},
createdWith: 'github'
}
];