This commit is contained in:
Yanis Rigaudeau 2023-03-30 22:58:48 +02:00
parent 39ba4856a2
commit e86d909e07
Signed by: yanis
GPG Key ID: 4DD2841DF1C94D83
2 changed files with 23 additions and 7 deletions

View File

@ -7,19 +7,20 @@ import {
UserWithPasswordCtor,
} from '@core';
import { getHashedPassword } from '../functions/password';
import { Card } from './card';
import { Entity } from './entity';
export class User extends Entity implements UserInfo {
username: string;
role: keyof typeof UserRoles;
cards: CardInfo[];
cards: Card[];
constructor(raw: UserCtor) {
super(raw);
this.username = raw.username || '';
this.role = raw.role || UserRoles.USER;
this.cards = this.cards || '';
this.cards = raw.cards ? raw.cards.map(card => new Card(card)) : [];
}
Info(): UserInfo {
@ -27,7 +28,7 @@ export class User extends Entity implements UserInfo {
uuid: this.uuid,
username: this.username,
role: this.role,
cards: this.cards,
cards: this.cards.map(card => card.Info()),
};
}
}
@ -46,7 +47,7 @@ export class UserWithPassword extends User implements UserInfoWithPassword {
uuid: this.uuid,
username: this.username,
role: this.role,
cards: this.cards,
cards: this.cards.map(card => card.Info()),
password: this.password,
};
}

View File

@ -1,5 +1,5 @@
import { Collection, Db } from 'mongodb';
import { Card, CardInfo, LoginUserBody, UpdateUserBody } from '@core';
import { Card, CardInfo, LoginUserBody, UpdateUserBody, UserInfo } from '@core';
import { User, UserWithPassword } from '../../entities/user';
import { log } from '../../functions/logger';
import { getHashedPassword } from '../../functions/password';
@ -54,7 +54,22 @@ class UserModel {
}
public async read(tracker: string, uuid: string): Promise<User> {
const userDocument = await this.collection.findOne({ uuid });
const [userDocument] = await this.collection.aggregate<UserInfo>([
{
$match:{
uuid
}
},
{
$lookup: {
from: "cards",
localField: "cards",
foreignField: "_id",
as: "cards"
}
}
]).toArray();
console.log(JSON.stringify(userDocument));
if (!userDocument) {
log(tracker, 'User Not Found');
throw new Error();
@ -118,7 +133,7 @@ class UserModel {
updatedAt: new Date(),
},
$addToSet: {
cards: card,
cards: card._id,
},
},
);