diff --git a/src/start_point/dtos/update-point.dto.ts b/src/start_point/dtos/update-point.dto.ts index a4a5aa4..adbf2ae 100644 --- a/src/start_point/dtos/update-point.dto.ts +++ b/src/start_point/dtos/update-point.dto.ts @@ -1,17 +1,16 @@ export interface UpdatePointInterface { - _id: string; - name: string; - description?: string; - user?: string; - journeys?: string[]; - order: Number; - createdAt: string; - __v: number; - updatedAt: string; - journey?: string; - - } + _id: string; + name: string; + description?: string; + user?: string; + journeys?: string[]; + order: Number; + createdAt: string; + __v: number; + updatedAt: string; + journey?: string; +} export class UpdatePointOrderDto { - points: UpdatePointInterface[] -} \ No newline at end of file + points: UpdatePointInterface[]; +} diff --git a/src/start_point/point.controller.ts b/src/start_point/point.controller.ts index 9e8a98b..8f475e0 100644 --- a/src/start_point/point.controller.ts +++ b/src/start_point/point.controller.ts @@ -14,7 +14,7 @@ import { import { PointService } from './point.service'; import { Request } from 'express'; import { CreateStartPointDto } from './dtos/create-start-point.dto'; -import { UpdatePointOrderDto} from './dtos/update-point.dto'; +import { UpdatePointOrderDto } from './dtos/update-point.dto'; @Controller('points') export class PointController { @@ -86,10 +86,10 @@ export class PointController { @Patch('/update-point-order') async updatePointOrder( @Body() - pointsDto: UpdatePointOrderDto + pointsDto: UpdatePointOrderDto, ) { - console.log(pointsDto) + console.log(pointsDto); const result = await this.pointService.updateOrder(pointsDto.points); - return result + return result; } } diff --git a/src/start_point/point.schema.ts b/src/start_point/point.schema.ts index 2f57a37..f402aff 100644 --- a/src/start_point/point.schema.ts +++ b/src/start_point/point.schema.ts @@ -6,7 +6,7 @@ export const PointSchema = new mongoose.Schema( description: { type: String, required: true }, user: { type: mongoose.Schema.Types.ObjectId, ref: 'User', required: true }, journeys: [{ type: mongoose.Schema.Types.ObjectId, ref: 'Journey' }], - order: { type:Number, default:0}, + order: { type: Number, default: 0 }, }, { timestamps: true, collection: 'startpoints' }, ); diff --git a/src/start_point/point.service.ts b/src/start_point/point.service.ts index e483eaa..a27878d 100644 --- a/src/start_point/point.service.ts +++ b/src/start_point/point.service.ts @@ -10,7 +10,7 @@ import { InjectModel } from '@nestjs/mongoose'; import { Model, Types } from 'mongoose'; import { Point } from './point.schema'; import { CreateStartPointDto } from './dtos/create-start-point.dto'; -import { UpdatePointInterface} from './dtos/update-point.dto'; +import { UpdatePointInterface } from './dtos/update-point.dto'; @Injectable() export class PointService { @@ -33,7 +33,6 @@ export class PointService { const existent_array = this.findAll(); - const newPoint = new this.pointModel({ ...createStartPointDto, user: userId, diff --git a/test/point.service.spec.ts b/test/point.service.spec.ts index 615c1e5..bc407cc 100644 --- a/test/point.service.spec.ts +++ b/test/point.service.spec.ts @@ -4,6 +4,8 @@ import { HttpService } from '@nestjs/axios'; import { NotFoundException, UnauthorizedException } from '@nestjs/common'; import { of } from 'rxjs'; import { PointService } from 'src/start_point/point.service'; +import { Types } from 'mongoose'; +import { UpdatePointInterface } from 'src/start_point/dtos/update-point.dto'; describe('PointService', () => { let service: PointService; @@ -19,6 +21,7 @@ describe('PointService', () => { findByIdAndDelete: jest.fn(), save: jest.fn(), exec: jest.fn(), + bulkWrite: jest.fn(), }; mockHttpService = { @@ -60,6 +63,40 @@ describe('PointService', () => { }); }); + describe('create', () => { + it('should throw UnauthorizedException if token is invalid', async () => { + const createStartPointDto = { + name: 'Test Point', + description: 'Description', + }; + const token = 'invalid-token'; + + jest.spyOn(service, 'validateTokenAndGetUserId').mockResolvedValue(null); + + await expect(service.create(createStartPointDto, token)).rejects.toThrow( + UnauthorizedException, + ); + }); + }); + + describe('addPointToUser', () => { + it('should call the user service to add the point', async () => { + const userId = 'user-id'; + const pointId = 'point-id'; + + // Simula uma resposta bem-sucedida do patch + jest.spyOn(mockHttpService, 'patch').mockReturnValue(of({})); + + await service.addPointToUser(userId, pointId); + + expect(mockHttpService.patch).toHaveBeenCalledWith( + `${process.env.USER_SERVICE_URL}/${userId}/add-point`, + { pointId } + ); + }); + + }); + describe('update', () => { it('should update a point and return the updated point', async () => { const updateStartPointDto = { @@ -227,4 +264,110 @@ describe('PointService', () => { expect(userId).toBeNull(); }); }); + + describe('findByUserId', () => { + it('should return an array of points for the given userId', async () => { + const points = [ + { _id: 'point-id-1', name: 'Point 1', user: 'user-id' }, + { _id: 'point-id-2', name: 'Point 2', user: 'user-id' }, + ]; + mockPointModel.find.mockReturnValueOnce({ + exec: jest.fn().mockResolvedValue(points), + }); + + const result = await service.findByUserId('user-id'); + + expect(result).toEqual(points); + expect(mockPointModel.find).toHaveBeenCalledWith({ user: 'user-id' }); + }); + }); + + describe('addJourneyToPoint', () => { + it('should add a journey to the point and return the updated point', async () => { + const pointId = new Types.ObjectId().toHexString(); // Use um ID válido para o ponto + const journeyId = new Types.ObjectId().toHexString(); // Use um ID válido para a jornada + const objectId = new Types.ObjectId(journeyId); + + const point = { + _id: pointId, + name: 'Test Point', + journeys: [], + save: jest.fn().mockResolvedValue({ + _id: pointId, + name: 'Test Point', + journeys: [objectId], + }), + }; + + mockPointModel.findById.mockReturnValueOnce({ + exec: jest.fn().mockResolvedValue(point), + }); + + const result = await service.addJourneyToPoint(pointId, journeyId); + + expect(result).toEqual({ + _id: pointId, + name: 'Test Point', + journeys: [objectId], + }); + expect(mockPointModel.findById).toHaveBeenCalledWith(pointId); + expect(point.journeys).toContainEqual(objectId); // Use toContainEqual aqui + expect(point.save).toHaveBeenCalled(); + }); + + it('should throw NotFoundException if point does not exist', async () => { + const pointId = new Types.ObjectId().toHexString(); // Use um ID válido para o ponto + const journeyId = new Types.ObjectId().toHexString(); // Use um ID válido para a jornada + + mockPointModel.findById.mockReturnValueOnce({ + exec: jest.fn().mockResolvedValue(null), + }); + + await expect( + service.addJourneyToPoint(pointId, journeyId), + ).rejects.toThrow(NotFoundException); + expect(mockPointModel.findById).toHaveBeenCalledWith(pointId); + }); + }); + + describe('updateOrder', () => { + it('should perform bulk updates and return the result', async () => { + const id1 = new Types.ObjectId(); + const id2 = new Types.ObjectId(); + + const journeys: UpdatePointInterface[] = [ + { + _id: id1.toHexString(), + order: 1, + name: 'Point A', + __v: 0, + createdAt: '', + updatedAt: '', + }, + { + _id: id2.toHexString(), + order: 2, + name: 'Point B', + __v: 0, + createdAt: '', + updatedAt: '', + }, + ]; + + const bulkOperations = journeys.map((trail) => ({ + updateOne: { + filter: { _id: new Types.ObjectId(trail._id) }, + update: { $set: { order: trail.order } }, + }, + })); + + const result = { modifiedCount: 2, matchedCount: 2 }; + mockPointModel.bulkWrite.mockResolvedValue(result); + + const response = await service.updateOrder(journeys); + + expect(mockPointModel.bulkWrite).toHaveBeenCalledWith(bulkOperations); + expect(response).toEqual(result); + }); + }); });