diff --git a/server/src/guess/guess.service.spec.ts b/server/src/guess/guess.service.spec.ts index 22aef64..95c9b47 100644 --- a/server/src/guess/guess.service.spec.ts +++ b/server/src/guess/guess.service.spec.ts @@ -94,38 +94,13 @@ describe('GuessService', () => { ).rejects.toThrow( new HttpException( { - message: 'Guess already submitted', + message: 'Wait for more time before submitting the next guess.', timestamp: activeGuess.timestamp, }, HttpStatus.BAD_REQUEST, ), ); }); - - it('should delete an expired active guess and allow a new guess', async () => { - const userId = 'testUser'; - const direction = DIRECTION.UP; - const price = 10000; - const activeGuess = { - guessId: 'activeGuessId', - userId, - direction, - price, - timestamp: Date.now() - 5000, - }; - - (guessRepository.getActiveGuess as jest.Mock).mockResolvedValue( - activeGuess, - ); - (guessRepository.deleteGuess as jest.Mock).mockResolvedValue(undefined); - (guessRepository.createGuess as jest.Mock).mockResolvedValue(undefined); - - const result = await guessService.submitGuess(userId, direction, price); - - expect(guessRepository.deleteGuess).toHaveBeenCalledWith(userId); - expect(guessRepository.createGuess).toHaveBeenCalled(); - expect(result).toHaveProperty('message', 'Guess submitted'); - }); }); describe('validateGuess', () => { @@ -134,9 +109,14 @@ describe('GuessService', () => { (guessRepository.getGuess as jest.Mock).mockResolvedValue(null); - const result = await guessService.validateGuess(guessId); - - expect(result).toEqual({ message: 'No guess with given ID found' }); + await expect(guessService.validateGuess(guessId)).rejects.toThrow( + new HttpException( + { + message: 'No guess with given ID found.', + }, + HttpStatus.BAD_REQUEST, + ), + ); }); it('should resolve the guess if it exists', async () => { diff --git a/server/src/guess/guess.service.ts b/server/src/guess/guess.service.ts index dda4d41..47b97a6 100644 --- a/server/src/guess/guess.service.ts +++ b/server/src/guess/guess.service.ts @@ -22,15 +22,18 @@ export class GuessService { const activeGuess = await this.guessRepository.getActiveGuess(userId); if (activeGuess) { - const timeElapsed = Date.now() - activeGuess.timestamp; - if (timeElapsed > appConfig.guessTimeout) { - // when submitting a new guess after the timeout, delete the old guess - await this.guessRepository.deleteGuess(userId); + if (Date.now() - activeGuess.timestamp < appConfig.guessTimeout * 1000) { + throw new HttpException( + { + message: 'Wait for more time before submitting the next guess.', + }, + HttpStatus.BAD_REQUEST, + ); } else { throw new HttpException( { - message: 'Guess already submitted', - timestamp: activeGuess.timestamp, + message: + 'You already have an active guess. Resolve it before submitting a new guess.', }, HttpStatus.BAD_REQUEST, ); @@ -53,7 +56,12 @@ export class GuessService { async validateGuess(guessId: string): Promise { const guess = await this.guessRepository.getGuess(guessId); if (!guess) { - return { message: 'No guess with given ID found' }; + throw new HttpException( + { + message: 'No guess with given ID found.', + }, + HttpStatus.BAD_REQUEST, + ); } return await this.resolveGuess(guess); @@ -80,7 +88,9 @@ export class GuessService { if (Date.now() - timestamp < appConfig.guessTimeout * 1000) { throw new HttpException( - 'Wait for more time before resolving the guess', + { + message: 'Wait for more time before resolving the guess.', + }, HttpStatus.BAD_REQUEST, ); }