Skip to content

Commit

Permalink
gh-133: Add strict as true
Browse files Browse the repository at this point in the history
  • Loading branch information
littlewhywhat committed Apr 26, 2020
1 parent 9ac97ba commit 3365a31
Show file tree
Hide file tree
Showing 13 changed files with 48 additions and 28 deletions.
4 changes: 3 additions & 1 deletion backend/bot/bot-factory.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,10 +17,12 @@ export class SingleChannelBot {
}
}

export const makeBot = (botToken: string, channelName: string): Promise<SingleChannelBot> => {
export const makeBot =
(botToken: string|undefined, channelName: string|undefined): Promise<SingleChannelBot> => {
return new Promise((resolve, reject): void => {
if (!botToken || !channelName) {
reject(Error('botToken or channelName missing'))
return
}

const slackbot = new SlackBot({
Expand Down
6 changes: 4 additions & 2 deletions backend/match-reporter/MatchReporter.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,8 @@ import { Match } from '../types/Match'

const DEFAULT_LEADERBOARD_SIZE = 5

const parseMatchReportDecorations = (config: string): Array<MatchReportDecoration> => config
const parseMatchReportDecorations =
(config: string | undefined): Array<MatchReportDecoration> => config
? config.split(';').map(configPart => {
const splitConfigPart = configPart.split(',')
if (splitConfigPart.length !== 4) {
Expand Down Expand Up @@ -111,13 +112,14 @@ export class MatchReporter {
readonly decorations: MatchReportDecoration[]
constructor(
readonly bot: SingleChannelBot,
matchReportPrefixSuffixConfig: string,
matchReportPrefixSuffixConfig: string | undefined,
readonly leaderboardSize = DEFAULT_LEADERBOARD_SIZE
) {
try {
this.decorations = parseMatchReportDecorations(matchReportPrefixSuffixConfig)
} catch (e) {
console.warn(`Parsing matchReportPrefixSuffixConfig failed: ${e.message}`)
this.decorations = []
}
}

Expand Down
4 changes: 2 additions & 2 deletions backend/repositories/GameRepository.test.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { mocked } from 'ts-jest/utils'
import { FOOSBALL_DATA } from '../tests/TestData'
import { FOOSBALL_DATA, FOOSBALL_GAME } from '../tests/TestData'

import { insertGame } from '../storage/Storage'
import { addGame } from './GameRepository'
Expand Down Expand Up @@ -30,7 +30,7 @@ describe('GameRepository', () => {
})
describe('when insertGame resolves', () => {
beforeEach(() => {
mockedInsertGame.mockResolvedValue(undefined)
mockedInsertGame.mockResolvedValue(FOOSBALL_GAME)
})
})
it('stores a valid foosball data via insertGame', async () => {
Expand Down
4 changes: 2 additions & 2 deletions backend/repositories/GameRepository.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,8 @@ import * as storage from '../storage/Storage'

const isValidGameData = (data: unknown): data is GameData => {
const gameData = data as GameData
return gameData.name &&
gameData.description &&
return !!gameData.name &&
!!gameData.description &&
gameData.name.trim() == gameData.name &&
gameData.name.toLowerCase() == gameData.name &&
!gameData.name.includes(' ') &&
Expand Down
2 changes: 1 addition & 1 deletion backend/repositories/MatchRepository.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ describe('MatchRepository', () => {
.mockResolvedValueOnce(RADEK_PLAYER)
.mockResolvedValueOnce(PETR_PLAYER)
mockedGetLatestMatchByGameId.mockResolvedValueOnce(FOOSBALL_MATCH_WITH_ID)
mockedStoreMatch.mockResolvedValueOnce(undefined)
mockedStoreMatch.mockResolvedValueOnce(FOOSBALL_MATCH_WITH_ID)
})
describe('called one minute later', () => {
lockDate(ONE_MINUTE_AFTER)
Expand Down
2 changes: 1 addition & 1 deletion backend/repositories/MatchRepository.ts
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ Promise<Match> => {
)
}

const getElapsedSecondsSinceLatestMatch = async (gameId: number): Promise<number> => {
const getElapsedSecondsSinceLatestMatch = async (gameId: number): Promise<number | null> => {
const latestMatch = await storage.getLatestMatchByGameId(gameId)
if (latestMatch == null) {
return null
Expand Down
7 changes: 5 additions & 2 deletions backend/storage/Storage.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,9 @@ import { Player } from '../types/Player'

export const makeStorageContext = async (): Promise<StorageContext> => {
const transaction = await dbTransactions.beginTransaction()
if (!transaction) {
throw new Error('Failed to create transaction')
}
return new StorageContext(transaction)
}

Expand Down Expand Up @@ -49,7 +52,7 @@ export const getAllMatches = async (): Promise<Array<MatchWithId>> => {
return executeAndCommit(context => context.getAllMatches())
}

export const getLatestMatchByGameId = async (gameId: number): Promise<MatchWithId> => {
export const getLatestMatchByGameId = async (gameId: number): Promise<MatchWithId | null> => {
return executeAndCommit(context => context.getLatestMatchByGameId(gameId))
}

Expand All @@ -61,7 +64,7 @@ export const getGameByName = async (name: string): Promise<Game> => {
return executeAndCommit(context => context.getGameByName(name))
}

export const insertGame = async (game: GameData): Promise<Game> => {
export const insertGame = async (game: GameData): Promise<Game|null> => {
return executeAndCommit(context => context.insertGame(game))
}

Expand Down
11 changes: 7 additions & 4 deletions backend/storage/StorageContext.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ describe('StorageContext', () => {
[ 'null', 'null', null, null ],
[ 'foosball row', 'foosball game', FOOSBALL_ROW, FOOSBALL_GAME ],
])('when executeSingleResultQuery resolves with %s', (res1Desc, res2Desc, row, result) => {
let foosballGame: Game
let foosballGame: Game | null
beforeEach(async () => {
TRANSACTION_MOCK.executeSingleResultQuery.mockResolvedValueOnce(row)
foosballGame = await context.insertGame(FOOSBALL_DATA)
Expand Down Expand Up @@ -181,9 +181,12 @@ describe('StorageContext', () => {
})
describe('when a new user Tonda is successfully added to foosball game', () => {
beforeEach(async () => {
// select Tonda as a user
TRANSACTION_MOCK.executeSingleResultQuery.mockResolvedValueOnce(null)
// insert Tonda as a user
TRANSACTION_MOCK.executeSingleResultQuery.mockResolvedValueOnce(TONDA_USER_ROW)
TRANSACTION_MOCK.executeSingleResultQuery.mockResolvedValueOnce(undefined)
// insert Tonda as a player
TRANSACTION_MOCK.executeSingleResultQuery.mockResolvedValueOnce(TONDA_PLAYER_ROW)
await context.addUserToGame(FOOSBALL_GAME.name, TONDA_PLAYER)
})
it('searches game by name', () => {
Expand Down Expand Up @@ -215,7 +218,7 @@ describe('StorageContext', () => {
describe('when an existing player Tonda is successfully added to foosball game', () => {
beforeEach(async () => {
TRANSACTION_MOCK.executeSingleResultQuery.mockResolvedValueOnce(TONDA_USER_ROW)
TRANSACTION_MOCK.executeSingleResultQuery.mockResolvedValueOnce(undefined)
TRANSACTION_MOCK.executeSingleResultQuery.mockResolvedValueOnce(null)
await context.addUserToGame(FOOSBALL_GAME.name, TONDA_PLAYER)
})
it('does not insert Tonda as a user', () => {
Expand Down Expand Up @@ -299,7 +302,7 @@ describe('StorageContext', () => {
})
describe('getLatestMatchByGameId', () => {
describe('called with foosball id', () => {
let matchWithId: MatchWithId
let matchWithId: MatchWithId | null
beforeEach(async () => {
TRANSACTION_MOCK.executeSingleResultQuery.mockResolvedValueOnce(FOOSBALL_MATCH_ROW)
matchWithId = await context.getLatestMatchByGameId(FOOSBALL_GAME.id)
Expand Down
19 changes: 11 additions & 8 deletions backend/storage/StorageContext.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@ import { MatchWithId, Match } from '../types/Match'
import { Game, GameData } from '../types/Game'
import { Player, NULL_PLAYER, PlayerData } from '../types/Player'
import { BadRequestError } from '../errors/BadRequestError'
import { QueryResultRow } from 'pg'

export class StorageContext {
constructor(private transaction: Transaction) {}
Expand Down Expand Up @@ -81,7 +80,7 @@ export class StorageContext {
return dbTransformations.createUserFromDbRow(row)
}

async getUserByName(userName: string): Promise<User> {
async getUserByName(userName: string): Promise<User | null> {
let row
try {
row = await this.transaction.executeSingleResultQuery(dbQueries.selectUserByName, [userName])
Expand Down Expand Up @@ -126,7 +125,7 @@ export class StorageContext {
async addUserToGame(gameName: string, { name, initialRating }: UserData): Promise<void> {
const game = await this.getGameByName(gameName)
if (!game) {
throw new Error(`Unable to find game '${game.name}'`)
throw new Error(`Unable to find game '${gameName}'`)
}
let user = await this.getUserByName(name)
if (!user) {
Expand All @@ -149,7 +148,9 @@ export class StorageContext {
}
throw new Error('Unable to add user')
}

if (!row) {
throw new Error(`No user was added with name '${name}'`)
}
return dbTransformations.createUserFromDbRow(row)
}

Expand Down Expand Up @@ -194,7 +195,9 @@ export class StorageContext {
}
throw new Error('Unable to create match')
}

if (!row) {
throw new Error('No match was created')
}
return dbTransformations.createMatchFromDbRow(row)
}

Expand All @@ -210,7 +213,7 @@ export class StorageContext {
return rows.map(dbTransformations.createMatchFromDbRow)
}

async getLatestMatchByGameId(gameId: number): Promise<MatchWithId> {
async getLatestMatchByGameId(gameId: number): Promise<MatchWithId | null> {
let row
try {
row = await this.transaction.executeSingleResultQuery(
Expand All @@ -233,7 +236,7 @@ export class StorageContext {
return rows.map(dbTransformations.createGameFromDbRow)
}

async insertGame(game: GameData): Promise<Game> {
async insertGame(game: GameData): Promise<Game | null> {
let row
try {
row = await this.transaction.executeSingleResultQuery(dbQueries.insertGame, [
Expand Down Expand Up @@ -263,7 +266,7 @@ export class StorageContext {
}

async getGameByName(name: string): Promise<Game> {
let row: QueryResultRow
let row
try {
row = await this.transaction.executeSingleResultQuery(dbQueries.selectGameByName, [name])
} catch (error) {
Expand Down
4 changes: 2 additions & 2 deletions backend/storage/db/db-transactions.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ export class Transaction {
}

async executeSingleResultQuery(query: string, values: QueryValues):
Promise<QueryResultRow> {
Promise<QueryResultRow | null> {
const rows = await this.executeQuery(query, values)
if (rows.length == 0) {
return null
Expand Down Expand Up @@ -52,7 +52,7 @@ export class Transaction {
}
}

export const beginTransaction = async (): Promise<Transaction> => {
export const beginTransaction = async (): Promise<Transaction | null> => {
const client = await pool.connect()
try {
await client.query('BEGIN')
Expand Down
3 changes: 2 additions & 1 deletion backend/tests/TestData.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import { MatchWithId, Match } from '../types/Match'
import { Player } from '../types/Player'
import { MatchDescription } from '../types/MatchDescription'
import * as moment from 'moment'
import { UserRow } from '../types/Database'

const NOW_MOMENT = moment('2020-03-25 10:00:00')
export const NOW = NOW_MOMENT.toDate()
Expand Down Expand Up @@ -115,7 +116,7 @@ export const FOOSBALL_MATCH: Match = {
gameId: 1,
}

export const TONDA_USER_ROW = {
export const TONDA_USER_ROW: UserRow = {
Id: '4',
Name: 'Tonda',
Active: 'true',
Expand Down
2 changes: 1 addition & 1 deletion backend/tsconfig.json
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
"module": "commonjs",
"target": "es6",
"sourceMap": true,
"noImplicitAny": true,
"strict": true,
"typeRoots": ["./types", "./node_modules/@types"],
},
"exclude": [
Expand Down
8 changes: 7 additions & 1 deletion backend/types/Player.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,4 +20,10 @@ export class Player extends User {
}
}

export const NULL_PLAYER = new Player(null, null, null, null, null)
export const NULL_PLAYER = {
id: null,
name: null,
rating: null,
active: null,
initialRating: null,
}

0 comments on commit 3365a31

Please sign in to comment.