Skip to content

Commit

Permalink
Add trackers to user model
Browse files Browse the repository at this point in the history
  • Loading branch information
alicenstar committed Feb 11, 2021
1 parent 75ecb9b commit 9167952
Show file tree
Hide file tree
Showing 3 changed files with 27 additions and 45 deletions.
40 changes: 4 additions & 36 deletions src/entities/User.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ export interface IUser extends Document {
username: string;
pwdHash: string;
email: string;
trackers: ITracker | ITracker[];
trackers: ITracker[];
role: 'User' | 'Admin';
}

Expand All @@ -28,10 +28,10 @@ const userSchema: Schema = new Schema(
type: String,
required: [true, 'Email is required'],
},
trackers: {
trackers: [{
type: Schema.Types.ObjectId,
ref: 'Tracker',
},
}],
role: {
type: String,
required: [true, 'Role is required']
Expand All @@ -43,36 +43,4 @@ const userSchema: Schema = new Schema(
}
);

export default model<IUser>('User', userSchema);

// export class User implements IUser {

// public id: number;
// public name: string;
// public email: string;
// public role: UserRoles;
// public pwdHash: string;


// constructor(
// nameOrUser?: string | IUser,
// email?: string,
// role?: UserRoles,
// pwdHash?: string,
// id?: number,
// ) {
// if (typeof nameOrUser === 'string' || typeof nameOrUser === 'undefined') {
// this.name = nameOrUser || '';
// this.email = email || '';
// this.role = role || UserRoles.Standard;
// this.pwdHash = pwdHash || '';
// this.id = id || -1;
// } else {
// this.name = nameOrUser.name;
// this.email = nameOrUser.email;
// this.role = nameOrUser.role;
// this.pwdHash = nameOrUser.pwdHash;
// this.id = nameOrUser.id;
// }
// }
// }
export default model<IUser>('User', userSchema);
9 changes: 7 additions & 2 deletions src/routes/Auth.ts
Original file line number Diff line number Diff line change
Expand Up @@ -31,14 +31,18 @@ router.post('/login', async (req: Request, res: Response) => {
// Fetch user
let user: IUser | null;
try {
user = await User.findOne({ username: username });
user = await User
.findOne({ username: username })
.populate([{path: 'trackers', model: 'Tracker'}])
.exec();
if (user == null) {
return res.status(UNAUTHORIZED).json({ error: loginFailedErr });
}
} catch (err) {
return res.status(500).json({ message: err.message });
}

// user = await user.execPopulate([{path: 'trackers', model: 'Tracker'}]);
console.log('user', user);
// Check password
const pwdPassed = await bcrypt.compare(password, user.pwdHash);
if (!pwdPassed) {
Expand All @@ -60,6 +64,7 @@ router.post('/login', async (req: Request, res: Response) => {
id: user._id,
username: user.username,
},
trackers: user.trackers
});
});

Expand Down
23 changes: 16 additions & 7 deletions src/routes/Users.ts
Original file line number Diff line number Diff line change
Expand Up @@ -26,17 +26,26 @@ router.get('/all', adminMW, async (req: Request, res: Response) => {
});

/******************************************************************************
* Get One User - "GET /api/users/:id"
* Get A User - "GET /api/users/:id"
******************************************************************************/

router.get('/:id', async (req: Request, res: Response) => {
let user: IUser | null;
// let user: IUser | null;
try {
user = await User.findById(req.params.id);
if (user == null) {
return res.status(404).json({ message: 'Cannot find user' });
}
return res.status(OK).json({ user });
User
.findById(req.params.id)
.populate([{path: 'trackers', model: 'Tracker'}])
.exec(function (err, user) {
if (user == null) {
return res.status(404).json({ message: 'Cannot find user' });
}
return res.status(OK).json({ user });
});
// user = await User.findById(req.params.id);
// if (user == null) {
// return res.status(404).json({ message: 'Cannot find user' });
// }
// return res.status(OK).json({ user });
} catch (err) {
return res.status(500).json({ message: err.message });
}
Expand Down

0 comments on commit 9167952

Please sign in to comment.