Skip to content

Commit

Permalink
getCurrentSeason function created
Browse files Browse the repository at this point in the history
  • Loading branch information
tobyrushton committed Jan 26, 2024
1 parent da364e3 commit af4eafb
Show file tree
Hide file tree
Showing 8 changed files with 57 additions and 38 deletions.
2 changes: 1 addition & 1 deletion __tests__/integration/update-season-average.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,7 @@ describe('updateSeasonAverages', () => {

expect(secondCount).toBe(firstCount)

firstAverages.forEach((average, index) => {
firstAverages.forEach(average => {
const secondAverage = secondAverages.find(
avg => avg.id === average.id
)
Expand Down
17 changes: 17 additions & 0 deletions __tests__/lib/getCurrentSeason.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
import { describe, it, expect, vi } from 'vitest'
import { getCurrentSeason } from '@/lib/getCurrentSeason'


describe('getCurrentSeason', () => {
it('should return the correct season when its the year behind', () => {
vi.useFakeTimers().setSystemTime(new Date('2023-10-10'))
const season = getCurrentSeason()
expect(season).toBe(2024)
})

it('should return the correct season when its the current year', () => {
vi.useFakeTimers().setSystemTime(new Date('2024-01-01'))
const season = getCurrentSeason()
expect(season).toBe(2024)
})
})
36 changes: 6 additions & 30 deletions __tests__/server/functions/update-season-average.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,36 +3,12 @@ import { faker } from '@faker-js/faker'
import { scrapeSeasonAverages } from '../../../src/server/scrapes/scrape-season-averages'
import { updateSeasonAverages } from '../../../src/server/functions/update-season-averages'
import { prismaMock } from '../../singleton'
import { generatePlayer } from './update-player.test'
import { generateSeasonAverage, generatePlayer } from '../../helpers/generators'

vi.mock('../../../src/server/scrapes/scrape-season-averages', () => ({
scrapeSeasonAverages: vi.fn(),
}))

const generateSeasonAverage = () => ({
player_name: faker.person.fullName(),
games_played: faker.number.int({ min: 0, max: 82 }),
min: faker.number.float({ min: 0, max: 48 }),
pts: faker.number.float({ min: 0, max: 40 }),
oreb: faker.number.float({ min: 0, max: 5 }),
dreb: faker.number.float({ min: 0, max: 10 }),
reb: faker.number.float({ min: 0, max: 15 }),
ast: faker.number.float({ min: 0, max: 15 }),
stl: faker.number.float({ min: 0, max: 5 }),
blk: faker.number.float({ min: 0, max: 5 }),
turnover: faker.number.float({ min: 0, max: 10 }),
pf: faker.number.float({ min: 0, max: 6 }),
fgm: faker.number.float({ min: 0, max: 20 }),
fga: faker.number.float({ min: 0, max: 20 }),
fg_pct: faker.number.float({ min: 0, max: 20 }),
fg3a: faker.number.float({ min: 0, max: 20 }),
fg3m: faker.number.float({ min: 0, max: 20 }),
fg3_pct: faker.number.float({ min: 0, max: 100 }),
ftm: faker.number.float({ min: 0, max: 20 }),
fta: faker.number.float({ min: 0, max: 20 }),
ft_pct: faker.number.float({ min: 0, max: 100 }),
})

const mockSeasonAverages = Array.from({ length: 10 }, generateSeasonAverage)

describe('updateSeasonAverages', () => {
Expand All @@ -46,7 +22,7 @@ describe('updateSeasonAverages', () => {
({ player_name, ...seasonAverage }) => ({
...seasonAverage,
id: faker.string.uuid(),
season: '2023-24',
season: 2023,
min: seasonAverage.min.toString(),
player_id: faker.string.uuid(),
games_played: 100, // this should cause an update
Expand All @@ -73,7 +49,7 @@ describe('updateSeasonAverages', () => {
...seasonAverage,
player_id: mockSeasonAveragesInDb[index].player_id,
id: mockSeasonAveragesInDb[index].id,
season: '2023-24',
season: 2023,
min: seasonAverage.min.toString(),
},
where: {
Expand All @@ -89,7 +65,7 @@ describe('updateSeasonAverages', () => {
({ player_name, ...seasonAverage }) => ({
...seasonAverage,
id: faker.string.uuid(),
season: '2023-24',
season: 2023,
min: seasonAverage.min.toString(),
player_id: faker.string.uuid(),
player: {
Expand Down Expand Up @@ -121,7 +97,7 @@ describe('updateSeasonAverages', () => {
({ player_name, ...seasonAverage }) => ({
...seasonAverage,
id: faker.string.uuid(),
season: '2023-24',
season: 2023,
min: seasonAverage.min.toString(),
player_id: faker.string.uuid(),
player: {
Expand Down Expand Up @@ -172,7 +148,7 @@ describe('updateSeasonAverages', () => {
data: {
...newSeasonAverage,
player_id: mockPlayer.id,
season: '2023-24',
season: 2023,
min: newSeasonAverage.min.toString(),
},
})
Expand Down
2 changes: 1 addition & 1 deletion prisma/schema.prisma
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ model SeasonAverages {
games_played Int
player Player @relation(fields: [player_id], references: [id])
player_id String @unique
season String
season Int
min String
fgm Float
fga Float
Expand Down
18 changes: 18 additions & 0 deletions src/lib/getCurrentSeason.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
/**
* Returns the current season based on the current month and year.
* If the current month is greater than or equal to August (8th month),
* it returns the next year as the current season.
* Otherwise, it returns the current year as the current season.
* @returns The current season as a number.
*/
export const getCurrentSeason = (): number => {
const date = new Date()

const currentMonth = date.getMonth()
const currentyear = date.getFullYear()

if (currentMonth >= 8) {
return currentyear + 1
}
return currentyear
}
7 changes: 5 additions & 2 deletions src/server/functions/update-game-stats.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import { getDateOfGame } from '@/lib/getDateOfGame'
import { getCurrentSeason } from '@/lib/getCurrentSeason'
import prisma from '../db/client'
import { scrapeGameStats, getLogLinks } from '../scrapes/scrape-game-stats'

Expand All @@ -21,9 +22,11 @@ export const updateGameStats = async (): Promise<void> => {
await Promise.all(
links.map(async link => {
const playerNameWithDash = link.split('/')[9]
// TODO: Function to update the year !!
const gameStats = await scrapeGameStats(
link.replace(playerNameWithDash, 'type/nba/year/2024')
link.replace(
playerNameWithDash,
`type/nba/year/${getCurrentSeason()}`
)
)

const playerName = getPlayerName(playerNameWithDash)
Expand Down
11 changes: 8 additions & 3 deletions src/server/functions/update-season-averages.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import { seasonAverageIsEqual } from '@/lib/seasonAverageIsEqual'
import { getCurrentSeason } from '@/lib/getCurrentSeason'
import prisma from '../db/client'
import { scrapeSeasonAverages } from '../scrapes/scrape-season-averages'

Expand Down Expand Up @@ -36,15 +37,19 @@ export const updateSeasonAverages = async (): Promise<void> => {
const { player: __, id, player_id, ...restInDb } = seasonAverageInDb
if (
seasonAverageIsEqual(
{ ...rest, season: '2023-24', min: rest.min.toString() },
{
...rest,
season: getCurrentSeason(),
min: rest.min.toString(),
},
restInDb
)
)
return undefined
return {
...rest,
player_id,
season: '2023-24',
season: getCurrentSeason(),
min: rest.min.toString(),
id,
}
Expand Down Expand Up @@ -79,7 +84,7 @@ export const updateSeasonAverages = async (): Promise<void> => {
data: {
...rest,
player_id: player.id,
season: '2023-24',
season: getCurrentSeason(),
min: seasonAverage.min.toString(),
},
})
Expand Down
2 changes: 1 addition & 1 deletion src/typings/player.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ namespace player {
interface ISeasonAverage {
games_played: number
player_id: string
season: string
season: number
min: string
fgm: number
fga: number
Expand Down

0 comments on commit af4eafb

Please sign in to comment.