2021-08-12 15:04:37 +02:00
|
|
|
import User, { IUser } from '../../lib/types/user';
|
|
|
|
import {
|
|
|
|
ICreateUser,
|
|
|
|
IUserLookup,
|
|
|
|
IUserStore,
|
|
|
|
} from '../../lib/types/stores/user-store';
|
2021-04-09 13:46:53 +02:00
|
|
|
|
2021-08-12 15:04:37 +02:00
|
|
|
class UserStoreMock implements IUserStore {
|
|
|
|
data: IUser[];
|
2021-04-09 13:46:53 +02:00
|
|
|
|
|
|
|
idSeq: number;
|
|
|
|
|
|
|
|
constructor() {
|
|
|
|
this.idSeq = 1;
|
|
|
|
this.data = [];
|
|
|
|
}
|
|
|
|
|
|
|
|
async hasUser({
|
|
|
|
id,
|
|
|
|
username,
|
|
|
|
email,
|
|
|
|
}: IUserLookup): Promise<number | undefined> {
|
2021-08-12 15:04:37 +02:00
|
|
|
const user = this.data.find((i) => {
|
2021-04-09 13:46:53 +02:00
|
|
|
if (id && i.id === id) return true;
|
|
|
|
if (username && i.username === username) return true;
|
|
|
|
if (email && i.email === email) return true;
|
|
|
|
return false;
|
|
|
|
});
|
2021-08-12 15:04:37 +02:00
|
|
|
if (user) {
|
|
|
|
return user.id;
|
|
|
|
}
|
|
|
|
return undefined;
|
|
|
|
}
|
|
|
|
|
|
|
|
destroy(): void {}
|
|
|
|
|
|
|
|
async exists(key: number): Promise<boolean> {
|
|
|
|
return this.data.some((u) => u.id === key);
|
|
|
|
}
|
|
|
|
|
2021-08-27 10:10:14 +02:00
|
|
|
async count(): Promise<number> {
|
|
|
|
return this.data.length;
|
|
|
|
}
|
|
|
|
|
2021-08-12 15:04:37 +02:00
|
|
|
async get(key: number): Promise<IUser> {
|
|
|
|
return this.data.find((u) => u.id === key);
|
2021-04-09 13:46:53 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
async insert(user: User): Promise<User> {
|
|
|
|
// eslint-disable-next-line no-param-reassign
|
|
|
|
user.id = this.idSeq;
|
|
|
|
this.idSeq += 1;
|
|
|
|
this.data.push(user);
|
|
|
|
return Promise.resolve(user);
|
|
|
|
}
|
|
|
|
|
|
|
|
async update(id: number, user: User): Promise<User> {
|
|
|
|
// eslint-disable-next-line no-param-reassign
|
2021-08-12 15:04:37 +02:00
|
|
|
this.data = this.data.map((o) => {
|
2021-04-09 13:46:53 +02:00
|
|
|
if (o.id === id) return { ...o, name: user.name };
|
|
|
|
return o;
|
|
|
|
});
|
|
|
|
return Promise.resolve(user);
|
|
|
|
}
|
|
|
|
|
2021-08-12 15:04:37 +02:00
|
|
|
async getByQuery({ id, username, email }: IUserLookup): Promise<IUser> {
|
|
|
|
const user = this.data.find((i) => {
|
2021-04-09 13:46:53 +02:00
|
|
|
if (i.id && i.id === id) return true;
|
|
|
|
if (i.username && i.username === username) return true;
|
|
|
|
if (i.email && i.email === email) return true;
|
|
|
|
return false;
|
|
|
|
});
|
|
|
|
if (user) {
|
|
|
|
return user;
|
|
|
|
}
|
|
|
|
throw new Error('Could not find user');
|
|
|
|
}
|
|
|
|
|
2021-08-12 15:04:37 +02:00
|
|
|
async getAll(): Promise<IUser[]> {
|
2021-04-09 13:46:53 +02:00
|
|
|
return Promise.resolve(this.data);
|
|
|
|
}
|
|
|
|
|
|
|
|
async setPasswordHash(userId: number, passwordHash: string): Promise<void> {
|
2021-08-12 15:04:37 +02:00
|
|
|
const u = this.data.find((a) => a.id === userId);
|
2022-06-07 11:49:17 +02:00
|
|
|
// @ts-expect-error
|
2021-04-09 13:46:53 +02:00
|
|
|
u.passwordHash = passwordHash;
|
|
|
|
return Promise.resolve();
|
|
|
|
}
|
|
|
|
|
|
|
|
async getPasswordHash(id: number): Promise<string> {
|
2021-08-12 15:04:37 +02:00
|
|
|
const u = this.data.find((i) => i.id === id);
|
2022-06-07 11:49:17 +02:00
|
|
|
// @ts-expect-error
|
2021-04-09 13:46:53 +02:00
|
|
|
return Promise.resolve(u.passwordHash);
|
|
|
|
}
|
|
|
|
|
|
|
|
async delete(id: number): Promise<void> {
|
2021-08-12 15:04:37 +02:00
|
|
|
this.data = this.data.filter((item) => item.id !== id);
|
2021-04-09 13:46:53 +02:00
|
|
|
return Promise.resolve();
|
|
|
|
}
|
|
|
|
|
|
|
|
async successfullyLogin(user: User): Promise<void> {
|
2021-08-12 15:04:37 +02:00
|
|
|
if (!this.exists(user.id)) {
|
|
|
|
throw new Error('No such user');
|
|
|
|
}
|
2021-04-09 13:46:53 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
buildSelectUser(): any {
|
|
|
|
throw new Error('Not implemented');
|
|
|
|
}
|
|
|
|
|
2021-08-12 15:04:37 +02:00
|
|
|
async search(): Promise<IUser[]> {
|
2021-04-09 13:46:53 +02:00
|
|
|
throw new Error('Not implemented');
|
|
|
|
}
|
|
|
|
|
2021-08-12 15:04:37 +02:00
|
|
|
async getAllWithId(): Promise<IUser[]> {
|
2021-04-09 13:46:53 +02:00
|
|
|
throw new Error('Not implemented');
|
|
|
|
}
|
|
|
|
|
|
|
|
async incLoginAttempts(): Promise<void> {
|
|
|
|
throw new Error('Not implemented');
|
|
|
|
}
|
2021-08-12 15:04:37 +02:00
|
|
|
|
|
|
|
deleteAll(): Promise<void> {
|
|
|
|
return Promise.resolve(undefined);
|
|
|
|
}
|
|
|
|
|
|
|
|
upsert(user: ICreateUser): Promise<IUser> {
|
|
|
|
this.data.splice(this.data.findIndex((u) => u.email === user.email));
|
|
|
|
this.data.push({
|
|
|
|
id: this.data.length + 1,
|
|
|
|
createdAt: new Date(),
|
|
|
|
isAPI: false,
|
|
|
|
permissions: [],
|
|
|
|
loginAttempts: 0,
|
|
|
|
imageUrl: '',
|
|
|
|
...user,
|
|
|
|
});
|
|
|
|
return Promise.resolve(undefined);
|
|
|
|
}
|
2022-09-28 15:53:56 +02:00
|
|
|
|
|
|
|
// eslint-disable-next-line @typescript-eslint/no-unused-vars
|
|
|
|
getUserByPersonalAccessToken(secret: string): Promise<IUser> {
|
|
|
|
return Promise.resolve(undefined);
|
|
|
|
}
|
2022-11-30 07:10:31 +01:00
|
|
|
|
|
|
|
// eslint-disable-next-line @typescript-eslint/no-unused-vars
|
|
|
|
async markSeenAt(secrets: string[]): Promise<void> {
|
|
|
|
throw new Error('Not implemented');
|
|
|
|
}
|
2021-04-09 13:46:53 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
module.exports = UserStoreMock;
|
|
|
|
|
|
|
|
export default UserStoreMock;
|