Skip to content

Commit

Permalink
Merge pull request #29 from fga-eps-mds/feat#66/adds-subject
Browse files Browse the repository at this point in the history
[FEAT] Adiciona Disciplinas e Remove Starting Points (fga-eps-mds/2024.2-ARANDU-DOC#66)
  • Loading branch information
dartmol203 authored Jan 19, 2025
2 parents e738c14 + cc49ca1 commit ac1300a
Show file tree
Hide file tree
Showing 7 changed files with 42 additions and 26 deletions.
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
{
"name": "calculus-user",
"name": "user-api",
"version": "0.0.1",
"description": "",
"author": "",
Expand Down
2 changes: 1 addition & 1 deletion src/users/interface/user.interface.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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[];
Expand Down
2 changes: 1 addition & 1 deletion src/users/interface/user.schema.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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' },
],
Expand Down
9 changes: 5 additions & 4 deletions src/users/users.controller.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import {
Param,
Patch,
Post,
Put,
Query,
Req,
UseGuards,
Expand Down Expand Up @@ -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;
}
Expand Down
18 changes: 7 additions & 11 deletions src/users/users.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -112,24 +112,20 @@ export class UsersService {
return user;
}

async addPointToUser(userId: string, pointId: string): Promise<User> {
async addSubjectToUser(userId: string, subjectId: string): Promise<User> {
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<User> {
const user = await this.userModel.findById(userId).exec();
if (!user) {
Expand Down
16 changes: 8 additions & 8 deletions test/user.controller.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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),
Expand Down Expand Up @@ -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);
});

Expand Down
19 changes: 19 additions & 0 deletions test/user.service.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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] };
Expand Down

0 comments on commit ac1300a

Please sign in to comment.