Skip to content

Commit

Permalink
start working on database binding for storing game events from bet pr…
Browse files Browse the repository at this point in the history
…oviders
  • Loading branch information
nigelnindodev committed Oct 18, 2023
1 parent 60ab526 commit 802133c
Show file tree
Hide file tree
Showing 7 changed files with 165 additions and 3 deletions.
82 changes: 82 additions & 0 deletions src/datastores/postgres/entities/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,82 @@
import { Entity, Column, PrimaryGeneratedColumn, Index} from "typeorm";

/**
* Table representing game events where there is no possibility of a draw.
* A good example is tennis.
*/
@Entity({name: "two_way_game_event"})
export class TwoWayGameEventEntity {
@PrimaryGeneratedColumn()
id: number

@Index("two_way_game_event_provider_id_idx")
@Column("string", {length: 100, nullable: false})
bet_provider_id: string

@Column("varchar", {length: 100, nullable: false})
club_a: string

@Column("varchar", {length: 100, nullable: false})
club_b: string

@Column("decimal", {nullable: false})
odds_a_win: number

@Column("decimal", {nullable: false})
odds_b_win: number

@Column("varchar", {length: 100, nullable: false})
bet_provider_name: string

@Column("varchar", {length: 100, nullable: false})
game_name: string

@Index("two_way_game_event_created_at_idx")
@Column("timestamptz", {nullable: false, default: () => "CURRENT_TIMESTAMP"})
created_at_utc: Date

@Column("timestamptz", {nullable: false, default: () => "CURRENT_TIMESTAMP", onUpdate: "CURRENT_TIMESTAMP"})
updated_at_utc: Date
}

/**
* Table representing game events where the is possibility of a draw. A great
* example is football.
*/
@Entity({name: "three_way_game_event"})
export class ThreeWayGameEventEntity {
@PrimaryGeneratedColumn()
id: number

@Index("three_way_game_event_provider_id_idx")
@Column("string", {length: 100, nullable: false})
bet_provider_id: string

@Column("varchar", {length: 100, nullable: false})
club_a: string

@Column("varchar", {length: 100, nullable: false})
club_b: string

@Column("decimal", {nullable: false})
odds_a_win: number

@Column("decimal", {nullable: false})
odds_b_win: number

@Column("decimal", {nullable: false})
odds_draw: number

@Column("varchar", {length: 100, nullable: false})
bet_provider: string

@Column("varchar", {length: 100, nullable: false})
game_name: string

@Index("three_way_game_event_created_at_idx")
@Column("timestamptz", {nullable: false, default: () => "CURRENT_TIMESTAMP"})
created_at_utc: Date

@Column("timestamptz", {nullable: false, default: () => "CURRENT_TIMESTAMP", onUpdate: "CURRENT_TIMESTAMP"})
updated_at_utc: Date
}
43 changes: 43 additions & 0 deletions src/datastores/postgres/queries/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
import {DataSource} from "typeorm";
import { ThreeWayGameEvent, TwoWayGameEvent } from "../../../utils/types/db";
import { ThreeWayGameEventEntity, TwoWayGameEventEntity } from "../entities";

export const insertTwoWayGameEvent = async (
dataSource: DataSource,
data: TwoWayGameEvent
) => {
const toDataBase = {
bet_provider_id: data.betProviderId,
bet_provider_name: data.betProviderName,
club_a: data.clubA,
club_b: data.clubB,
odds_a_win: data.oddsAWin,
odds_b_win: data.oddsBWin,
game_name: data.gameName
};
await dataSource.createQueryBuilder()
.insert()
.into(TwoWayGameEventEntity)
.values(toDataBase)
.execute();
};

export const insertThreeWayGameEvent = async (
dataSource: DataSource,
data: ThreeWayGameEvent
) => {
const toDataBase = {
bet_provider_id: data.betProviderId,
bet_provider_name: data.betProviderName,
club_a: data.clubA,
club_b: data.clubB,
odds_a_win: data.oddsAWin,
odds_b_win: data.oddsBWin,
game_name: data.gameName
}
await dataSource.createQueryBuilder()
.insert()
.into(ThreeWayGameEventEntity)
.values(toDataBase)
.execute();
};
2 changes: 1 addition & 1 deletion src/index.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import * as path from "path";

import "reflect-metadata";
import "dotenv/config";
import {ILogObj, Logger} from "tslog";

Expand Down
6 changes: 5 additions & 1 deletion src/utils/redis/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,5 +9,9 @@ import { BetProviderGameConfig } from "../types/common";
* @returns Channel name based on bet provider, game name and bet type. An example generated value is BETIKA_TennisSingles_TwoWay
*/
export function getRedisHtmlParserChannelName(betProvider: BetProvider, gameConfig: BetProviderGameConfig): string {
return `${betProvider.name}_${gameConfig.name.replace(" ", "")}_${gameConfig.betType.replace(" ", "")}`
return `${betProvider.name}_${gameConfig.name.replace(" ", "")}_${gameConfig.betType.replace(" ", "")}`;
}

export function getRedisEventsChannelName(betProvider: BetProvider, gameConfig: BetProviderGameConfig): string {
return `event:${betProvider.name}_${gameConfig.name.replace(" ", "")}_${gameConfig.betType.replace(" ", "")}`;
}
5 changes: 5 additions & 0 deletions src/utils/types/common/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -58,3 +58,8 @@ export enum TimeZones {
UTC = "UTC",
NAIROBI = "Africa/Nairobi"
}

// TODO: Work on adding this in the future
export enum GameStatus {

}
28 changes: 28 additions & 0 deletions src/utils/types/db/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
/**
* File contains data types for to be used to inert new data to the database.
* For reading data off the database, we'll make use of the entity itself as
* it has more meta data i.e insert_id, created/updated at.
*/

import { BetProviders, Games } from "../common";

export interface TwoWayGameEvent {
betProviderName: BetProviders;
betProviderId: string;
clubA: string;
clubB: string;
oddsAWin: number;
oddsBWin: number;
gameName: Games;
}

export interface ThreeWayGameEvent {
betProviderName: BetProviders;
betProviderId: string;
clubA: string;
clubB: string;
oddsAWin: number;
oddsBWin: number;
oddsDraw: number;
gameName: Games;
}
2 changes: 1 addition & 1 deletion tsconfig.json
Original file line number Diff line number Diff line change
Expand Up @@ -87,7 +87,7 @@
"strictNullChecks": true, /* When type checking, take into account 'null' and 'undefined'. */
"strictFunctionTypes": true, /* When assigning functions, check to ensure parameters and the return values are subtype-compatible. */
"strictBindCallApply": true, /* Check that the arguments for 'bind', 'call', and 'apply' methods match the original function. */
"strictPropertyInitialization": true, /* Check for class properties that are declared but not set in the constructor. */
"strictPropertyInitialization": false, /* Check for class properties that are declared but not set in the constructor. */
"noImplicitThis": true, /* Enable error reporting when 'this' is given the type 'any'. */
// "useUnknownInCatchVariables": true, /* Default catch clause variables as 'unknown' instead of 'any'. */
"alwaysStrict": true, /* Ensure 'use strict' is always emitted. */
Expand Down

0 comments on commit 802133c

Please sign in to comment.