diff --git a/package.json b/package.json index d8eba09..e41f86d 100644 --- a/package.json +++ b/package.json @@ -1,5 +1,5 @@ { - "name": "calculus-user", + "name": "user-api", "version": "0.0.1", "description": "", "author": "", diff --git a/src/users/interface/user.interface.ts b/src/users/interface/user.interface.ts index 7a835bd..988453d 100644 --- a/src/users/interface/user.interface.ts +++ b/src/users/interface/user.interface.ts @@ -9,7 +9,7 @@ export interface User extends Document { verificationToken?: string; isVerified?: boolean; role?: UserRole; - points?: mongoose.Types.ObjectId[]; + subjects?: mongoose.Types.ObjectId[]; journeys?: mongoose.Types.ObjectId[]; subscribedJourneys?: mongoose.Types.ObjectId[]; completedTrails?: mongoose.Types.ObjectId[]; diff --git a/src/users/interface/user.schema.ts b/src/users/interface/user.schema.ts index 8a55bad..b244f61 100644 --- a/src/users/interface/user.schema.ts +++ b/src/users/interface/user.schema.ts @@ -16,7 +16,7 @@ export const UserSchema = new mongoose.Schema( enum: Object.values(UserRole), default: UserRole.ALUNO, }, - points: [{ type: mongoose.Schema.Types.ObjectId, ref: 'Point' }], + subjects: [{ type: mongoose.Schema.Types.ObjectId, ref: 'Subject' }], subscribedJourneys: [ { type: mongoose.Schema.Types.ObjectId, ref: 'Journey' }, ], diff --git a/src/users/users.controller.ts b/src/users/users.controller.ts index 1330665..35c8631 100644 --- a/src/users/users.controller.ts +++ b/src/users/users.controller.ts @@ -7,6 +7,7 @@ import { Param, Patch, Post, + Put, Query, Req, UseGuards, @@ -84,13 +85,13 @@ export class UsersController { return await this.usersService.getUsers(); } - @Patch(':id/add-point') - async addPointToUser( + @Put(':id/subjects/:subjectId/add') + async addSubjectToUser( @Param('id') id: string, - @Body() body: { pointId: string }, + @Param('subjectId') subjectId: string, ) { try { - return await this.usersService.addPointToUser(id, body.pointId); + return await this.usersService.addSubjectToUser(id, subjectId); } catch (error) { throw error; } diff --git a/src/users/users.service.ts b/src/users/users.service.ts index 350ea4e..d5f603f 100644 --- a/src/users/users.service.ts +++ b/src/users/users.service.ts @@ -112,24 +112,20 @@ export class UsersService { return user; } - async addPointToUser(userId: string, pointId: string): Promise { + async addSubjectToUser(userId: string, subjectId: string): Promise { const user = await this.userModel.findById(userId).exec(); - if (!user) { - throw new NotFoundException(`User with ID ${userId} not found`); - } - const objectId = new Types.ObjectId(pointId); + if (!user) throw new NotFoundException(`User with ID ${userId} not found`); - if (!user.points) { - user.points = []; - } + const objectId = new Types.ObjectId(subjectId); - if (!user.points.includes(objectId)) { - user.points.push(objectId); - } + user.subjects = ( user.subjects ? user.subjects : [] ) + + if (!user.subjects.includes(objectId)) user.subjects.push(objectId); return user.save(); } + async addJourneyToUser(userId: string, journeyId: string): Promise { const user = await this.userModel.findById(userId).exec(); if (!user) { diff --git a/test/user.controller.spec.ts b/test/user.controller.spec.ts index 85cfcf4..477e85e 100644 --- a/test/user.controller.spec.ts +++ b/test/user.controller.spec.ts @@ -33,7 +33,7 @@ describe('UsersController', () => { updateUser: jest.fn().mockResolvedValue(mockUpdatedUser), getSubscribedJourneys: jest.fn().mockResolvedValue([]), getUsers: jest.fn().mockResolvedValue([mockUser]), - addPointToUser: jest.fn().mockResolvedValue(mockUser), + addSubjectToUser: jest.fn().mockResolvedValue(mockUser), subscribeJourney: jest.fn().mockResolvedValue(mockUser), unsubscribeJourney: jest.fn().mockResolvedValue(mockUser), getUserById: jest.fn().mockResolvedValue(mockUser), @@ -119,22 +119,22 @@ describe('UsersController', () => { await expect(controller.getUsers()).resolves.toEqual([mockUser]); }); - it('should add a point to a user', async () => { + it('should add a subject to a user', async () => { const userId = 'mockUserId'; - const pointId = 'mockPointId'; + const subjectId = 'mockSubjectId'; await expect( - controller.addPointToUser(userId, { pointId }), + controller.addSubjectToUser(userId, subjectId), ).resolves.toEqual(mockUser); }); - it('should handle error when adding a point to a user', async () => { + it('should handle error when adding a subject to a user', async () => { const userId = 'mockUserId'; - const pointId = 'mockPointId'; - mockUserService.addPointToUser.mockRejectedValueOnce( + const subjectId = 'mockSubjectId'; + mockUserService.addSubjectToUser.mockRejectedValueOnce( new NotFoundException('User not found'), ); await expect( - controller.addPointToUser(userId, { pointId }), + controller.addSubjectToUser(userId, subjectId), ).rejects.toThrow(NotFoundException); }); diff --git a/test/user.service.spec.ts b/test/user.service.spec.ts index c97900f..42bf7d0 100644 --- a/test/user.service.spec.ts +++ b/test/user.service.spec.ts @@ -226,6 +226,25 @@ describe('UsersService', () => { ).rejects.toThrow(NotFoundException); }); + it('should add a subject to a user', async () => { + const subjectId = new Types.ObjectId(); + const userWithSubject = { ...mockUser, subjects: [subjectId] }; + + jest.spyOn(model, 'findById').mockReturnValueOnce({ + exec: jest.fn().mockResolvedValue(mockUser), + } as any); + jest.spyOn(mockUser, 'save').mockResolvedValue(userWithSubject as any); + + const result = await service.addSubjectToUser( + 'mockId', + subjectId.toHexString(), + ); + + expect(result.subjects).toContain(subjectId); + expect(result.subjects.length).toBe(1); + expect(mockUser.save).toHaveBeenCalled(); + }); + it('should add a journey to a user', async () => { const journeyId = new Types.ObjectId(); const userWithJourney = { ...mockUser, journeys: [journeyId] };