Skip to content

Commit

Permalink
fix: minor fixes
Browse files Browse the repository at this point in the history
  • Loading branch information
jonassimoen committed Jun 1, 2024
1 parent 3552dc8 commit c6bdf41
Show file tree
Hide file tree
Showing 8 changed files with 53 additions and 7 deletions.
2 changes: 2 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
"dependencies": {
"@fastify/cookie": "^9.3.1",
"@fastify/cors": "^9.0.1",
"@fastify/schedule": "^4.1.1",
"@fastify/static": "^7.0.3",
"@fastify/swagger": "^8.14.0",
"@fastify/swagger-ui": "^3.0.0",
Expand All @@ -38,6 +39,7 @@
"qs": "^6.12.1",
"serverless-http": "^3.2.0",
"stripe": "^15.5.0",
"toad-scheduler": "^3.0.1",
"yaml": "^2.4.2"
},
"devDependencies": {
Expand Down
1 change: 1 addition & 0 deletions prisma/schema.prisma
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,7 @@ model Player {
star Int?
surname String?
value Float?
pSelections Int? @default(0)
selections Selection[]
club Club? @relation(fields: [clubId], references: [id], onDelete: Cascade)
Expand Down
6 changes: 6 additions & 0 deletions src/controllers/Page.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,9 @@ export const GetPagesHandler = async (req: any, rep: any) => {
},
include: {
translation: true
},
orderBy: {
id: 'asc'
}
});

Expand All @@ -23,6 +26,9 @@ export const GetPageHandler = async (req: any, rep: any) => {
},
include: {
translation: true
},
orderBy: {
id: 'asc'
}
});

Expand Down
3 changes: 0 additions & 3 deletions src/controllers/Player.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,5 @@
import { prisma } from "../db/client";
import { fetchPlayers, fetchPlayersPerClub } from "../utils/ExternalAPI";
import * as fs from "fs";
import axios from "axios";
import path from "path";
import HttpError from "../utils/HttpError";

export const GetPlayersHandler = async (req: any, rep: any) => {
Expand Down
10 changes: 7 additions & 3 deletions src/controllers/Team.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { finalWeekId, upcomingWeekId, validateStartingLineup } from '../utils/Common';
import { SavePlayersToJson, finalWeekId, upcomingWeekId, validateStartingLineup } from '../utils/Common';
import { prisma } from "../db/client"
import HttpError from "../utils/HttpError";
import { max } from "lodash";
Expand Down Expand Up @@ -103,7 +103,8 @@ export const PostAddTeamHandler = async (req: any, rep: any) => {
value: totalValue,
valid: true,
created: new Date(Date.now()),
name: req.body.teamName
name: req.body.teamName,
weekId
},
include: {
selections: true,
Expand All @@ -130,8 +131,9 @@ export const PostAddTeamHandler = async (req: any, rep: any) => {
}),
timestamp: new Date().toISOString(),
}
})
}),
])
SavePlayersToJson();
rep.send({
user: team.user,
team: {
Expand Down Expand Up @@ -501,6 +503,7 @@ export const PostEditTeamHandler = async (req: any, rep: any) => {
}
})
]);
SavePlayersToJson();
rep.send({
message: "Team successfully updated."
});
Expand Down Expand Up @@ -644,6 +647,7 @@ export const PostTransfersTeamHandler = async (req: any, rep: any) => {
}
});
});
SavePlayersToJson();
rep.send({ msg: "Transfers toegekend" });
} else {
rep.status(406).send({ msg: "No transfers included." });
Expand Down
3 changes: 2 additions & 1 deletion src/controllers/Week.ts
Original file line number Diff line number Diff line change
Expand Up @@ -247,7 +247,8 @@ export const GetDeadlineInfoHandler = async (req: any, rep: any) => {
deadlineWeek: deadlineWeek?.id || 0,
displayWeek: displayWeek?.id || deadlineWeek?.id,
endWeek: weeks[weeks.length - 1].id,
freeTransfers: deadlineWeek?.maxTransfers
fT: deadlineWeek?.maxTransfers,
sC: deadlineWeek?.maxSameClub,
},
weeks,
rft: (await prisma.refreshTime.findFirst())?.time
Expand Down
9 changes: 9 additions & 0 deletions src/utils/Common.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import { Player } from "@prisma/client";
import { prisma } from "../db/client"
import * as fs from "fs";

export const upcomingWeekId = async () => {
const week = await prisma.week.findFirst({
Expand Down Expand Up @@ -35,4 +36,12 @@ export const validateStartingLineup = (starting: number[]) => {
((starting[2] >= 3) && (starting[2] <= 5)) && // 3-5 DEF
((starting[3] >= 3) && (starting[3] <= 5)) && // 3-5 MID
((starting[4] >= 1) && (starting[4] <= 3)) // 1-3 FOR
}

export const SavePlayersToJson = async () => {
const players = await prisma.player.findMany({});

await fs.writeFile('static/players.json', JSON.stringify(players), (err) => {
if (err) throw err;
});
}
26 changes: 26 additions & 0 deletions src/utils/Jobs.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
import { prisma } from "../db/client";
import {SimpleIntervalJob, AsyncTask } from "toad-scheduler"

const percentageSelectionsTask = new AsyncTask(
'Update percentage of selections of players',
() => {
return prisma.$queryRaw`call "calculatePercentageSelections"()`;
},
(err: any) => {
console.error("UPDATING PLAYER SELECTIONS WENT WRONG!")
prisma.audit.create({
data: {
action: "UPDATE_PSELECTION",
params: "WENT WRONG",
timestamp: new Date(),
user: {
connect: {
id: 1,
}
},
}
});
}
);

export const percentageSelectionsJob = new SimpleIntervalJob({ hours: 1, }, percentageSelectionsTask)

0 comments on commit c6bdf41

Please sign in to comment.