Skip to content

Commit

Permalink
Prevent users submit multiple guesses
Browse files Browse the repository at this point in the history
  • Loading branch information
zaikivla committed Oct 25, 2024
1 parent c41d405 commit 0fff2ba
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 37 deletions.
38 changes: 9 additions & 29 deletions server/src/guess/guess.service.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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', () => {
Expand All @@ -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 () => {
Expand Down
26 changes: 18 additions & 8 deletions server/src/guess/guess.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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,
);
Expand All @@ -53,7 +56,12 @@ export class GuessService {
async validateGuess(guessId: string): Promise<any> {
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);
Expand All @@ -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,
);
}
Expand Down

0 comments on commit 0fff2ba

Please sign in to comment.