diff --git a/api/src/controllers/group.ts b/api/src/controllers/group.ts index 3a6ff66..9a3bc40 100644 --- a/api/src/controllers/group.ts +++ b/api/src/controllers/group.ts @@ -20,7 +20,7 @@ export interface GroupResponse { description: string } -interface GroupDetailsResponse { +export interface GroupDetailsResponse { groupId: number name: string description: string @@ -249,9 +249,10 @@ const updateUsersListInGroup = async ( message: 'User not found.' } - const updatedGroup = (action === 'addUser' - ? await group.addUser(user._id) - : await group.removeUser(user._id)) as unknown as GroupDetailsResponse + const updatedGroup = + action === 'addUser' + ? await group.addUser(user) + : await group.removeUser(user) if (!updatedGroup) throw { @@ -260,9 +261,6 @@ const updateUsersListInGroup = async ( message: 'Unable to update group.' } - if (action === 'addUser') user.addGroup(group._id) - else user.removeGroup(group._id) - return { groupId: updatedGroup.groupId, name: updatedGroup.name, diff --git a/api/src/model/Group.ts b/api/src/model/Group.ts index 36b7842..6341825 100644 --- a/api/src/model/Group.ts +++ b/api/src/model/Group.ts @@ -1,5 +1,6 @@ import mongoose, { Schema, model, Document, Model } from 'mongoose' -import User from './User' +import { GroupDetailsResponse } from '../controllers' +import User, { IUser } from './User' const AutoIncrement = require('mongoose-sequence')(mongoose) export interface GroupPayload { @@ -27,8 +28,9 @@ interface IGroupDocument extends GroupPayload, Document { } interface IGroup extends IGroupDocument { - addUser(userObjectId: Schema.Types.ObjectId): Promise - removeUser(userObjectId: Schema.Types.ObjectId): Promise + addUser(user: IUser): Promise + removeUser(user: IUser): Promise + hasUser(user: IUser): boolean } interface IGroupModel extends Model {} @@ -70,28 +72,31 @@ groupSchema.pre('remove', async function () { }) // Instance Methods -groupSchema.method( - 'addUser', - async function (userObjectId: Schema.Types.ObjectId) { - const userIdIndex = this.users.indexOf(userObjectId) - if (userIdIndex === -1) { - this.users.push(userObjectId) - } - this.markModified('users') - return this.save() +groupSchema.method('addUser', async function (user: IUser) { + const userObjectId = user._id + const userIdIndex = this.users.indexOf(userObjectId) + if (userIdIndex === -1) { + this.users.push(userObjectId) + user.addGroup(this._id) } -) -groupSchema.method( - 'removeUser', - async function (userObjectId: Schema.Types.ObjectId) { - const userIdIndex = this.users.indexOf(userObjectId) - if (userIdIndex > -1) { - this.users.splice(userIdIndex, 1) - } - this.markModified('users') - return this.save() + this.markModified('users') + return this.save() +}) +groupSchema.method('removeUser', async function (user: IUser) { + const userObjectId = user._id + const userIdIndex = this.users.indexOf(userObjectId) + if (userIdIndex > -1) { + this.users.splice(userIdIndex, 1) + user.removeGroup(this._id) } -) + this.markModified('users') + return this.save() +}) +groupSchema.method('hasUser', function (user: IUser) { + const userObjectId = user._id + const userIdIndex = this.users.indexOf(userObjectId) + return userIdIndex > -1 +}) export const Group: IGroupModel = model( 'Group', diff --git a/api/src/model/User.ts b/api/src/model/User.ts index b70807d..dd0123b 100644 --- a/api/src/model/User.ts +++ b/api/src/model/User.ts @@ -35,6 +35,7 @@ export interface UserPayload { } interface IUserDocument extends UserPayload, Document { + _id: Schema.Types.ObjectId id: number isAdmin: boolean isActive: boolean @@ -43,7 +44,7 @@ interface IUserDocument extends UserPayload, Document { tokens: [{ [key: string]: string }] } -interface IUser extends IUserDocument { +export interface IUser extends IUserDocument { comparePassword(password: string): boolean addGroup(groupObjectId: Schema.Types.ObjectId): Promise removeGroup(groupObjectId: Schema.Types.ObjectId): Promise