Skip to content

Commit

Permalink
continued internationalization
Browse files Browse the repository at this point in the history
  • Loading branch information
JessicaMulein committed Dec 21, 2024
1 parent 02166e8 commit 086ee33
Show file tree
Hide file tree
Showing 50 changed files with 832 additions and 161 deletions.
4 changes: 2 additions & 2 deletions chili-and-cilantro-api/src/controllers/api/game.ts
Original file line number Diff line number Diff line change
Expand Up @@ -87,7 +87,7 @@ export class GameController extends BaseController {
.notEmpty()
.matches(
constants.USER_DISPLAY_NAME_REGEX,
constants.USER_DISPLAY_NAME_REGEX_ERROR,
translate(StringNames.Validation_DisplayNameRegexErrorTemplate),
),
body('password')
.optional()
Expand Down Expand Up @@ -128,7 +128,7 @@ export class GameController extends BaseController {
.notEmpty()
.matches(
constants.USER_DISPLAY_NAME_REGEX,
constants.USER_DISPLAY_NAME_REGEX_ERROR,
translate(StringNames.Validation_DisplayNameRegexErrorTemplate),
),
],
}),
Expand Down
4 changes: 3 additions & 1 deletion chili-and-cilantro-api/src/controllers/api/user.ts
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,9 @@ export class UserController extends BaseController {
),
body('displayname')
.matches(constants.USER_DISPLAY_NAME_REGEX)
.withMessage(constants.USER_DISPLAY_NAME_REGEX_ERROR),
.withMessage(
translate(StringNames.Validation_DisplayNameRegexErrorTemplate),
),
body('email')
.isEmail()
.withMessage(translate(StringNames.Validation_InvalidEmail)),
Expand Down
6 changes: 2 additions & 4 deletions chili-and-cilantro-api/src/controllers/base.ts
Original file line number Diff line number Diff line change
Expand Up @@ -383,14 +383,12 @@ export abstract class BaseController {
res,
next,
);
Promise.reject();
return;
return Promise.reject();
}
const user = await UserModel.findById(req.user.id);
if (!user) {
handleError(new UserNotFoundError(), res, next);
Promise.reject();
return;
return Promise.reject();
}
return user;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ export function findAuthToken(headers: IncomingHttpHeaders): string | null {

/**
* Middleware to authenticate a token
* @param getModel Function to get a model
* @param application The application
* @param req The request
* @param res The response
* @param next The next function
Expand Down
36 changes: 19 additions & 17 deletions chili-and-cilantro-api/src/services/game.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,17 +18,16 @@ import {
InvalidActionError,
InvalidGameError,
InvalidGameNameError,
InvalidGameParameterError,
InvalidGamePasswordError,
InvalidMessageError,
InvalidUserDisplayNameError,
ModelName,
MustBeMasterChefError,
NotEnoughChefsError,
NotMasterChefError,
NotYourTurnError,
OutOfIngredientError,
OutOfOrderError,
StringNames,
TooManyChefsInGameError,
TooManyChefsError,
TurnAction,
UsernameInUseError,
constants,
Expand Down Expand Up @@ -161,10 +160,11 @@ export class GameService extends BaseService {
) {
throw new InvalidGamePasswordError();
}
if (maxChefs < constants.MIN_CHEFS || maxChefs > constants.MAX_CHEFS) {
throw new InvalidGameParameterError(
`Must be between ${constants.MIN_CHEFS} and ${constants.MAX_CHEFS}.`,
);
if (maxChefs < constants.MIN_CHEFS) {
throw new NotEnoughChefsError(maxChefs);
}
if (maxChefs > constants.MAX_CHEFS) {
throw new TooManyChefsError();
}
}

Expand All @@ -175,6 +175,8 @@ export class GameService extends BaseService {
* @param gameName The name of the game
* @param password Optional password for the game. Empty string for no password.
* @param maxChefs The maximum number of chefs in the game. Must be between MIN_CHEFS and MAX_CHEFS.
* @param gameId The id of the game
* @param masterChefId The id of the master chef
* @returns
*/
public async createGameAsync(
Expand All @@ -184,7 +186,7 @@ export class GameService extends BaseService {
password: string,
maxChefs: number,
gameId: DefaultIdType = new Types.ObjectId(),
chefId: DefaultIdType = new Types.ObjectId(),
masterChefId: DefaultIdType = new Types.ObjectId(),
): Promise<{
game: IGameDocument;
chef: IChefDocument;
Expand All @@ -196,13 +198,13 @@ export class GameService extends BaseService {
{
_id: gameId,
cardsPlaced: 0,
chefIds: [chefId],
chefIds: [masterChefId],
code: gameCode,
currentBid: constants.NONE,
currentChef: constants.NONE,
currentPhase: GamePhase.LOBBY,
currentRound: constants.NONE,
masterChefId: chefId,
masterChefId: masterChefId,
masterChefUserId: user._id,
maxChefs: maxChefs,
name: gameName,
Expand All @@ -221,7 +223,7 @@ export class GameService extends BaseService {
user,
displayName,
true,
chefId,
masterChefId,
);
const action = await this.actionService.createGameAsync(game, chef, user);
return { game, chef, action };
Expand Down Expand Up @@ -313,7 +315,7 @@ export class GameService extends BaseService {
throw new GameInProgressError();
}
if (game.chefIds.length > game.maxChefs) {
throw new TooManyChefsInGameError();
throw new TooManyChefsError();
}
if (
!validator.matches(
Expand Down Expand Up @@ -502,13 +504,13 @@ export class GameService extends BaseService {
userId: DefaultIdType,
): Promise<void> {
if (!(await this.playerService.isMasterChefAsync(userId, game._id))) {
throw new NotMasterChefError();
throw new MustBeMasterChefError();
}
if (game.currentPhase !== GamePhase.LOBBY) {
throw new GameInProgressError();
}
if (game.chefIds.length < constants.MIN_CHEFS) {
throw new NotEnoughChefsError(game.chefIds.length, constants.MIN_CHEFS);
throw new NotEnoughChefsError(game.chefIds.length);
}
}

Expand Down Expand Up @@ -862,7 +864,7 @@ export class GameService extends BaseService {
}
const currentChefId = this.getGameCurrentChefId(game);
if (currentChefId.toString() !== chef._id.toString()) {
throw new OutOfOrderError();
throw new NotYourTurnError();
}
if (
chef.placedCards.length >= constants.HAND_SIZE ||
Expand Down Expand Up @@ -1140,7 +1142,7 @@ export class GameService extends BaseService {
return this.withTransaction<IGameChef>(async () => {
const game = await this.getGameByCodeOrThrowAsync(gameCode, true);
if (game.chefIds[game.currentChef].toString() !== user._id.toString()) {
throw new OutOfOrderError();
throw new NotYourTurnError();
}
const chef = await this.chefService.getGameChefOrThrowAsync(game, user);
// BID, INCREASE_BID, PASS, or PLACE_CARD
Expand Down
7 changes: 3 additions & 4 deletions chili-and-cilantro-api/src/services/jwt.ts
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ export class JwtService extends BaseService {
const token = sign(tokenUser, environment.jwtSecret, {
algorithm: constants.JWT_ALGO,
allowInsecureKeySizes: false,
expiresIn: constants.JWT_EXPIRATION,
expiresIn: constants.JWT_EXPIRATION_SEC,
});
return {
token,
Expand All @@ -60,11 +60,10 @@ export class JwtService extends BaseService {
return {
userId: decoded.userId as string,
};
} else {
throw new InvalidTokenError();
}
} catch (error) {
throw new InvalidTokenError();
// Do nothing
}
throw new InvalidTokenError();
}
}
11 changes: 6 additions & 5 deletions chili-and-cilantro-api/test/unit/gameService.createGame.test.ts
Original file line number Diff line number Diff line change
@@ -1,15 +1,16 @@
import {
AlreadyJoinedOtherError,
ChefAlreadyJoinedError,
constants,
IChefDocument,
ICreateGameActionDocument,
IGameDocument,
InvalidGameNameError,
InvalidGameParameterError,
InvalidGamePasswordError,
InvalidUserDisplayNameError,
IUserDocument,
ModelName,
NotEnoughChefsError,
TooManyChefsError,
} from '@chili-and-cilantro/chili-and-cilantro-lib';
import { IApplication } from '@chili-and-cilantro/chili-and-cilantro-node-lib';
import { Model } from 'mongoose';
Expand Down Expand Up @@ -108,7 +109,7 @@ describe('GameService', () => {
password,
maxChefs,
),
).rejects.toThrow(AlreadyJoinedOtherError);
).rejects.toThrow(ChefAlreadyJoinedError);
});

it('should throw an error for an invalid username with special characters', async () => {
Expand Down Expand Up @@ -299,7 +300,7 @@ describe('GameService', () => {
password,
maxChefs,
),
).rejects.toThrow(InvalidGameParameterError);
).rejects.toThrow(NotEnoughChefsError);
});

it('should throw an error to too many chefs', async () => {
Expand All @@ -320,7 +321,7 @@ describe('GameService', () => {
password,
maxChefs,
),
).rejects.toThrow(InvalidGameParameterError);
).rejects.toThrow(TooManyChefsError);
});
});

Expand Down
Original file line number Diff line number Diff line change
@@ -1,8 +1,7 @@
import {
AlreadyJoinedOtherError,
ChefAlreadyJoinedError,
constants,
DefaultIdType,
GameFullError,
GameInProgressError,
GamePasswordMismatchError,
GamePhase,
Expand All @@ -11,6 +10,7 @@ import {
InvalidUserDisplayNameError,
IUserDocument,
ModelName,
TooManyChefsError,
UsernameInUseError,
} from '@chili-and-cilantro/chili-and-cilantro-lib';
import { IApplication } from '@chili-and-cilantro/chili-and-cilantro-node-lib';
Expand Down Expand Up @@ -112,7 +112,7 @@ describe('GameService', () => {
userDisplayName,
game.password,
),
).rejects.toThrow(AlreadyJoinedOtherError);
).rejects.toThrow(ChefAlreadyJoinedError);
});
it('should throw an error when the chef name is already in the specified game', async () => {
// arrange
Expand Down Expand Up @@ -256,7 +256,7 @@ describe('GameService', () => {
userDisplayName,
game.password,
),
).rejects.toThrow(GameFullError);
).rejects.toThrow(TooManyChefsError);
});
it('should allow a user to join a game with no password as long as none is provided', async () => {
const game = generateGame(false); // Game with no password required
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,8 @@ import {
IncorrectGamePhaseError,
InvalidActionError,
ModelName,
NotYourTurnError,
OutOfIngredientError,
OutOfOrderError,
TurnAction,
constants,
} from '@chili-and-cilantro/chili-and-cilantro-lib';
Expand Down Expand Up @@ -68,7 +68,7 @@ describe('GameService', () => {
game.currentChef = 0;
expect(() =>
gameService.validatePlaceIngredientOrThrow(game, chef, CardType.CHILI),
).toThrow(OutOfOrderError);
).toThrow(NotYourTurnError);
});

it('should throw an error if the chef has placed all cards or has no cards left', () => {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,8 @@ import {
IGameDocument,
IUserDocument,
ModelName,
MustBeMasterChefError,
NotEnoughChefsError,
NotMasterChefError,
} from '@chili-and-cilantro/chili-and-cilantro-lib';
import { IApplication } from '@chili-and-cilantro/chili-and-cilantro-node-lib';
import { Model } from 'mongoose';
Expand Down Expand Up @@ -159,7 +159,7 @@ describe('gameService startGame', () => {

await expect(async () =>
gameService.validateStartGameOrThrowAsync(game, userId),
).rejects.toThrow(NotMasterChefError);
).rejects.toThrow(MustBeMasterChefError);
});
it('should throw if the game phase is not LOBBY', async () => {
isMasterChefAsync.mockResolvedValue(true);
Expand Down
4 changes: 2 additions & 2 deletions chili-and-cilantro-api/test/unit/userService.test.ts
Original file line number Diff line number Diff line change
@@ -1,14 +1,14 @@
import {
AccountDeletedError,
AccountLockedError,
AccountStatusError,
AccountStatusTypeEnum,
EmailInUseError,
EmailTokenExpiredError,
EmailTokenSentTooRecentlyError,
EmailTokenType,
EmailTokenUsedOrInvalidError,
EmailVerifiedError,
HandleableError,
IEmailTokenDocument,
IUser,
IUserDocument,
Expand Down Expand Up @@ -179,7 +179,7 @@ describe('UserService', () => {
},
{
status: 'InvalidStatus' as AccountStatusTypeEnum,
error: AccountStatusError,
error: HandleableError,
},
];

Expand Down
9 changes: 4 additions & 5 deletions chili-and-cilantro-lib/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -35,30 +35,29 @@ export * from './lib/errors/email-token-expired';
export * from './lib/errors/email-token-sent-too-recently';
export * from './lib/errors/email-token-used-or-invalid';
export * from './lib/errors/email-verified';
export * from './lib/errors/game-display-name-in-use';
export * from './lib/errors/game-in-progress';
export * from './lib/errors/game-password-mismatch';
export * from './lib/errors/game-username-in-use';
export * from './lib/errors/handleable-error';
export * from './lib/errors/incorrect-game-phase';
export * from './lib/errors/invalid-action';
export * from './lib/errors/invalid-credentials';
export * from './lib/errors/invalid-email';
export * from './lib/errors/invalid-game';
export * from './lib/errors/invalid-game-name';
export * from './lib/errors/invalid-game-parameter';
export * from './lib/errors/invalid-game-password';
export * from './lib/errors/invalid-message';
export * from './lib/errors/invalid-password';
export * from './lib/errors/invalid-token';
export * from './lib/errors/invalid-user-display-name';
export * from './lib/errors/invalid-username';
export * from './lib/errors/must-be-master-chef';
export * from './lib/errors/not-enough-chefs';
export * from './lib/errors/not-in-game';
export * from './lib/errors/not-master-chef';
export * from './lib/errors/not-your-turn';
export * from './lib/errors/out-of-ingredient';
export * from './lib/errors/out-of-order';
export * from './lib/errors/pending-email-verification';
export * from './lib/errors/too-many-chefs-in-game';
export * from './lib/errors/too-many-chefs';
export * from './lib/errors/user-not-found';
export * from './lib/errors/username-email-required';
export * from './lib/errors/username-in-use';
Expand Down
Loading

0 comments on commit 086ee33

Please sign in to comment.