diff --git a/api/src/entities/user.ts b/api/src/entities/user.ts index 23b1b6f..49d12b4 100644 --- a/api/src/entities/user.ts +++ b/api/src/entities/user.ts @@ -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, }; } diff --git a/api/src/framework/mongo/user.ts b/api/src/framework/mongo/user.ts index 3e4f577..b97a908 100644 --- a/api/src/framework/mongo/user.ts +++ b/api/src/framework/mongo/user.ts @@ -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 { - const userDocument = await this.collection.findOne({ uuid }); + const [userDocument] = await this.collection.aggregate([ + { + $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, }, }, );