Skip to content

Commit

Permalink
Merge pull request #43 from x-team/develop
Browse files Browse the repository at this point in the history
Updating Firebase protcol & Type-safe models
  • Loading branch information
ccmoralesj authored Jun 17, 2022
2 parents 0fb93cf + 8d0f7e3 commit 6eaa17e
Show file tree
Hide file tree
Showing 49 changed files with 409 additions and 162 deletions.
8 changes: 7 additions & 1 deletion .env.test
Original file line number Diff line number Diff line change
Expand Up @@ -7,4 +7,10 @@ DB_USERNAME=postgres
DB_PASSWORD=*games-2021
DB_NAME=gameshq_api_test
DB_HOSTNAME=127.0.0.1
DB_PORT=5435
DB_PORT=5435

# FIREBASE
GOOGLE_APPLICATION_CREDENTIALS={"type": "fake", "project_id": "fake", "private_key": "fake", "client_email": "fake"}

# SLACK
FRONT_END_APP_BOT_TOKEN=fake
7 changes: 5 additions & 2 deletions buildspec.yml
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
version: 0.2

env:
shell: bash
git-credential-helper: yes
parameter-store:
DOCKER_HUB_USER: '/devops/shared/DOCKER_HUB_USER'
Expand All @@ -19,17 +20,19 @@ phases:
- sentry-cli --version
pre_build:
commands:
- eval $(./aws-env --recursive)
- export APP_BUILD_VERSION=${CODEBUILD_RESOLVED_SOURCE_VERSION}__$(date -u '+%Y-%m-%dT%T+00:00')
- export PKG_VERSION=$(node -p "require('./package.json').version")
- export SENTRY_VERSION=${ENV}-${PKG_VERSION}_BUILD_${CODEBUILD_RESOLVED_SOURCE_VERSION}
- echo $APP_BUILD_VERSION > ./src/.version
- cat ./src/.version
- aws ecr get-login-password --region us-east-1 | docker login --username AWS --password-stdin $DOCKER_REPOSITORY_URI
- IMAGE_TAG=$(echo $CODEBUILD_RESOLVED_SOURCE_VERSION | cut -c 1-7)

build:
commands:
#- chmod +x ./codebuild-git-wrapper.sh
#- ./codebuild-git-wrapper.sh "$REPO_URL" "$REPO_BRANCH"
- aws s3 cp s3://gameshq-build-pipeline-artifact-bucket/gameshq/google_credentials_games_api_staging.json ./google_credentials_games_api_staging.json
- aws s3 cp s3://gameshq-build-pipeline-artifact-bucket/gameshq/google_credentials_games_api_prod.json ./google_credentials_games_api_prod.json
- echo $DOCKER_HUB_PASSWORD | docker login --username $DOCKER_HUB_USER --password-stdin
- docker build -t $DOCKER_REPOSITORY_URI:latest .
- docker tag $DOCKER_REPOSITORY_URI:latest $DOCKER_REPOSITORY_URI:$IMAGE_TAG
Expand Down
23 changes: 17 additions & 6 deletions src/games/general/commands/register.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,13 @@ import Boom from '@hapi/boom';

import { USER_ROLE_LEVEL } from '../../../consts/model';
import { findOrganizationByName } from '../../../models/Organization';
import { createUser, userExists } from '../../../models/User';
import { upsertUser, userExists, getUserByEmail } from '../../../models/User';
import { createUserInFirebase } from '../../../plugins/firebasePlugin';
import { getGameResponse, getSlackUserInfo } from '../../utils';

export const register = async (slackUserId: string) => {
const exists = await userExists(slackUserId);

if (exists) {
return getGameResponse(`Your user is already registered.`);
}
Expand All @@ -19,14 +21,23 @@ export const register = async (slackUserId: string) => {
}
const { email, image_512 } = profile;

await createUser({
email: email,
displayName: real_name,
firebaseUserUid: null,
const userInDb = await getUserByEmail(email);
let firebaseUserUid = userInDb?.firebaseUserUid;

if (!firebaseUserUid) {
const firebaseUser = await createUserInFirebase(email, real_name);
firebaseUserUid = firebaseUser.uid;
}

await upsertUser({
id: userInDb?.id,
_roleId: userInDb?._roleId || USER_ROLE_LEVEL.USER,
email,
displayName: userInDb?.displayName || real_name,
profilePictureUrl: image_512,
slackId: id,
_roleId: USER_ROLE_LEVEL.USER,
_organizationId: xteamOrganization?.id,
firebaseUserUid,
});

return getGameResponse(
Expand Down
3 changes: 2 additions & 1 deletion src/games/utils/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -178,7 +178,8 @@ export async function getSlackUserInfo(slackId: string): Promise<SlackUser> {
user: slackId,
}),
};
const response = await (await fetch(url, options)).json();
const rslt = await fetch(url, options);
const response = await rslt.json();
const responseUser: SlackUser = response as SlackUser;
return responseUser;
} catch (error) {
Expand Down
4 changes: 2 additions & 2 deletions src/models/AchievementUnlocked.ts
Original file line number Diff line number Diff line change
Expand Up @@ -73,14 +73,14 @@ export class AchievementUnlocked
onUpdate: 'CASCADE',
onDelete: 'CASCADE',
})
_user?: User;
declare _user?: User;

@BelongsTo(() => Achievement, {
foreignKey: '_achievementId',
onUpdate: 'CASCADE',
onDelete: 'CASCADE',
})
_achievement?: Achievement;
declare _achievement?: Achievement;

static associations: {
_user: Association<AchievementUnlocked, User>;
Expand Down
2 changes: 1 addition & 1 deletion src/models/Achievements.ts
Original file line number Diff line number Diff line change
Expand Up @@ -81,7 +81,7 @@ export class Achievement
onUpdate: 'CASCADE',
onDelete: 'CASCADE',
})
_gameType?: GameType;
declare _gameType?: GameType;

static associations: {
_gameType: Association<Achievement, GameType>;
Expand Down
4 changes: 2 additions & 2 deletions src/models/ArenaGame.ts
Original file line number Diff line number Diff line change
Expand Up @@ -88,10 +88,10 @@ export class ArenaGame
onUpdate: 'CASCADE',
onDelete: 'CASCADE',
})
_game?: Game;
declare _game?: Game;

@HasMany(() => ArenaRound, '_gameId')
_rounds?: ArenaRound[];
declare _rounds?: ArenaRound[];

static associations: {
_rounds: Association<ArenaGame, ArenaRound>;
Expand Down
4 changes: 2 additions & 2 deletions src/models/ArenaItemInventory.ts
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ export class ArenaItemInventory
declare _arenaPlayerId: number;

@BelongsTo(() => ArenaPlayer, '_arenaPlayerId')
_player?: ArenaPlayer;
declare _player: ArenaPlayer;

@ForeignKey(() => Item)
@Column(DataType.INTEGER)
Expand All @@ -60,7 +60,7 @@ export class ArenaItemInventory
onUpdate: 'CASCADE',
onDelete: 'CASCADE',
})
_item?: Item;
declare _item?: Item;

@Column(DataType.INTEGER)
declare remainingUses: number | null;
Expand Down
14 changes: 7 additions & 7 deletions src/models/ArenaPlayer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -178,7 +178,7 @@ export class ArenaPlayer
onUpdate: 'CASCADE',
onDelete: 'CASCADE',
})
_user?: User;
declare _user?: User;

@AllowNull(true)
@ForeignKey(() => Team)
Expand All @@ -190,7 +190,7 @@ export class ArenaPlayer
onUpdate: 'CASCADE',
onDelete: 'SET NULL',
})
_team?: Team | null;
declare _team?: Team | null;

@ForeignKey(() => Game)
@Column(DataType.INTEGER)
Expand All @@ -201,39 +201,39 @@ export class ArenaPlayer
onUpdate: 'CASCADE',
onDelete: 'SET NULL',
})
_game?: Game;
declare _game?: Game;

@AllowNull(true)
@ForeignKey(() => ArenaZone)
@Column(DataType.INTEGER)
declare _arenaZoneId: number | null;

@BelongsTo(() => ArenaZone)
_zone?: ArenaZone;
declare _zone?: ArenaZone;

@BelongsToMany(() => Item, {
through: () => ArenaItemInventory,
foreignKey: '_arenaPlayerId',
otherKey: '_itemId',
as: '_weapons',
})
_weapons?: Array<Item & { ArenaItemInventory: ArenaItemInventory }>;
declare _weapons?: Array<Item & { ArenaItemInventory: ArenaItemInventory }>;

@BelongsToMany(() => Item, {
through: () => ArenaItemInventory,
foreignKey: '_arenaPlayerId',
otherKey: '_itemId',
as: '_armors',
})
_armors?: Array<Item & { ArenaItemInventory: ArenaItemInventory }>;
declare _armors?: Array<Item & { ArenaItemInventory: ArenaItemInventory }>;

@BelongsToMany(() => Item, {
through: () => ArenaItemInventory,
foreignKey: '_arenaPlayerId',
otherKey: '_itemId',
as: '_healthkits',
})
_healthkits?: Array<Item & { ArenaItemInventory: ArenaItemInventory }>;
declare _healthkits?: Array<Item & { ArenaItemInventory: ArenaItemInventory }>;

static associations: {
_user: Association<ArenaPlayer, User>;
Expand Down
4 changes: 2 additions & 2 deletions src/models/ArenaPlayerPerformance.ts
Original file line number Diff line number Diff line change
Expand Up @@ -51,15 +51,15 @@ export class ArenaPlayerPerformance
declare _arenaPlayerId: number;

@BelongsTo(() => ArenaPlayer, '_arenaPlayerId')
_player?: ArenaPlayer;
declare _player?: ArenaPlayer;

@PrimaryKey
@ForeignKey(() => Game)
@Column(DataType.INTEGER)
declare _gameId: number;

@BelongsTo(() => Game, '_gameId')
_game?: Game;
declare _game?: Game;

@Default(ZERO)
@Column(DataType.INTEGER)
Expand Down
6 changes: 3 additions & 3 deletions src/models/ArenaRound.ts
Original file line number Diff line number Diff line change
Expand Up @@ -110,17 +110,17 @@ export class ArenaRound
declare _gameId: number;

@BelongsTo(() => Game, '_gameId')
_game?: Game;
declare _game?: Game;

@ForeignKey(() => User)
@Column(DataType.INTEGER)
declare _createdById: number;

@BelongsTo(() => User, '_createdById')
_createdBy?: User;
declare _createdBy?: User;

@HasMany(() => ArenaRoundAction, '_arenaRoundId')
_actions?: ArenaRoundAction[];
declare _actions?: ArenaRoundAction[];

static associations: {
_game: Association<ArenaRound, Game>; // TBD association with ArenaGame?
Expand Down
6 changes: 3 additions & 3 deletions src/models/ArenaRoundAction.ts
Original file line number Diff line number Diff line change
Expand Up @@ -54,15 +54,15 @@ export class ArenaRoundAction
declare _arenaPlayerId: number;

@BelongsTo(() => ArenaPlayer, '_arenaPlayerId')
_player?: ArenaPlayer;
declare _player?: ArenaPlayer;

@PrimaryKey
@ForeignKey(() => ArenaRound)
@Column(DataType.INTEGER)
declare _arenaRoundId: number;

@BelongsTo(() => ArenaRound, '_arenaRoundId')
_round?: ArenaRound;
declare _round?: ArenaRound;

@Default(false)
@Column(DataType.BOOLEAN)
Expand All @@ -84,7 +84,7 @@ export class ArenaRoundAction
onUpdate: 'CASCADE',
onDelete: 'CASCADE',
})
_action?: AvailableAction;
declare _action?: AvailableAction;

@Column(DataType.JSONB)
declare actionJSON: ArenaAction;
Expand Down
4 changes: 2 additions & 2 deletions src/models/ArenaZone.ts
Original file line number Diff line number Diff line change
Expand Up @@ -88,10 +88,10 @@ export class ArenaZone
onUpdate: 'CASCADE',
as: '_organization',
})
_organization?: Organization;
declare _organization?: Organization;

@HasMany(() => ArenaPlayer, '_arenaZoneId')
_players?: ArenaPlayer[];
declare _players?: ArenaPlayer[];

static associations: {
_players: Association<ArenaZone, ArenaPlayer>;
Expand Down
8 changes: 4 additions & 4 deletions src/models/Enemy.ts
Original file line number Diff line number Diff line change
Expand Up @@ -95,7 +95,7 @@ export class Enemy

@AllowNull(true)
@Column(DataType.TEXT)
gifUrl?: string;
declare gifUrl?: string;

@Default(Ability.defaultProps())
@Column(DataType.JSONB)
Expand All @@ -110,7 +110,7 @@ export class Enemy
onUpdate: 'CASCADE',
onDelete: 'SET NULL',
})
_enemyPattern?: EnemyPattern;
declare _enemyPattern?: EnemyPattern;

@ForeignKey(() => Organization)
@Column(DataType.INTEGER)
Expand All @@ -122,15 +122,15 @@ export class Enemy
onUpdate: 'CASCADE',
as: '_organization',
})
_organization?: Organization;
declare _organization?: Organization;

@BelongsToMany(() => Trait, {
through: () => EnemyTrait,
foreignKey: '_enemyId',
otherKey: '_traitId',
as: '_traits',
})
_traits?: Array<Trait & { EnemyTrait: EnemyTrait }>;
declare _traits?: Array<Trait & { EnemyTrait: EnemyTrait }>;

static associations: {
_enemyPattern: Association<Enemy, EnemyPattern>;
Expand Down
4 changes: 2 additions & 2 deletions src/models/EnemyTrait.ts
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ export class EnemyTrait
onUpdate: 'CASCADE',
onDelete: 'CASCADE',
})
_enemy?: Enemy;
declare _enemy?: Enemy;

@ForeignKey(() => Trait)
@Column(DataType.TEXT)
Expand All @@ -45,7 +45,7 @@ export class EnemyTrait
onUpdate: 'CASCADE',
onDelete: 'CASCADE',
})
_trait?: Trait;
declare _trait?: Trait;

static associations: {
_enemy: Association<EnemyTrait, Enemy>;
Expand Down
6 changes: 3 additions & 3 deletions src/models/Game.ts
Original file line number Diff line number Diff line change
Expand Up @@ -118,13 +118,13 @@ export class Game extends Model<GameAttributes, GameCreationAttributes> implemen
onUpdate: 'CASCADE',
onDelete: 'CASCADE',
})
_createdBy?: User;
declare _createdBy?: User;

@HasOne(() => ArenaGame)
_arena?: ArenaGame;
declare _arena?: ArenaGame;

@HasOne(() => TowerGame)
_tower?: TowerGame;
declare _tower?: TowerGame;

static associations: {
_arena: Association<Game, ArenaGame>;
Expand Down
4 changes: 2 additions & 2 deletions src/models/GameItemAvailability.ts
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@ export class GameItemAvailability
onUpdate: 'CASCADE',
onDelete: 'CASCADE',
})
_gameType?: GameType;
declare _gameType?: GameType;

@PrimaryKey
@ForeignKey(() => Item)
Expand All @@ -67,7 +67,7 @@ export class GameItemAvailability
onUpdate: 'CASCADE',
onDelete: 'CASCADE',
})
_item?: Item;
declare _item?: Item;

@Default(true)
@Column(DataType.BOOLEAN)
Expand Down
Loading

0 comments on commit 6eaa17e

Please sign in to comment.