From 07b473651b49ff2a6d5bdd9b7611ac7b3103eb94 Mon Sep 17 00:00:00 2001 From: Jonas Simoen Date: Mon, 10 Jun 2024 21:14:24 +0200 Subject: [PATCH] v2024.06.10 commit 18361641f66b1657ae54936907da83decab08614 Author: Jonas Simoen Date: Mon Jun 10 20:47:25 2024 +0200 decrease booster limit commit aaf561105a466a22756288e3b572edb54a71ed8e Author: Jonas Simoen Date: Mon Jun 10 20:44:13 2024 +0200 feat: show update notification commit 5890cf9690dc57fb99201e06ddd06ea460e7f35e Author: Jonas Simoen Date: Mon Jun 10 19:05:35 2024 +0200 fix(booster): disable double booster on single player commit 456659b3a1b2193399d0c083c462df9b5c39f99c Merge: ce8f1d3 155139e Author: Jonas Simoen Date: Mon Jun 10 18:54:07 2024 +0200 Merge branch 'staging' of github.com:jonassimoen/edd-api into staging commit ce8f1d3368946eac1e989c301fae4c52003351f2 Author: Jonas Simoen Date: Mon Jun 10 18:53:14 2024 +0200 minor updates commit 155139ed65a2280d32597eed08707470f847b0a2 Author: Jonas Simoen Date: Sun Jun 2 02:07:29 2024 +0200 fix: typo in schema commit b5249e0b62d55ac767d867c94d6071b332f5fea5 Author: Jonas Simoen Date: Sat Jun 1 23:08:52 2024 +0200 fix: change price commit 3fdc4c15a513ec562b7b5ca4dac1d4ffd87f0d9d Author: Jonas Simoen Date: Sat Jun 1 22:50:26 2024 +0200 fix --- prisma/schema.prisma | 9 +++++---- src/controllers/Statistic.ts | 3 +++ src/controllers/Team.ts | 32 +++++++++++++++++++------------- src/controllers/User.ts | 20 +++++++++++++++++--- src/controllers/UserAuth.ts | 1 + src/utils/DeserializeUser.ts | 2 +- 6 files changed, 46 insertions(+), 21 deletions(-) diff --git a/prisma/schema.prisma b/prisma/schema.prisma index 587eabc..9a5a2b4 100644 --- a/prisma/schema.prisma +++ b/prisma/schema.prisma @@ -29,8 +29,8 @@ model User { payed Boolean? @default(false) banned Boolean? @default(false) - teams Team[] - Audit Audit[] + teams Team[] + Audit Audit[] } model Player { @@ -54,7 +54,7 @@ model Player { star Int? surname String? value Float? - pSelections Int? @default(0) + pSelections Int? @default(0) selections Selection[] club Club? @relation(fields: [clubId], references: [id], onDelete: Cascade) @@ -273,7 +273,8 @@ model Translation { } model RefreshTime { - time DateTime @id @unique + time DateTime @id @unique + update String? } model Audit { diff --git a/src/controllers/Statistic.ts b/src/controllers/Statistic.ts index ad5d937..131973f 100644 --- a/src/controllers/Statistic.ts +++ b/src/controllers/Statistic.ts @@ -146,6 +146,9 @@ export const PutMatchStatisticHandler = async (req: any, rep: any) => { throw new HttpError('Match has no home or away team assigned.',400); } + // TODO: replace with a function to only update the Statistic's table & then call a DB function to calculate the points (see saved scripts in DBeaver) ==> Performance reason + // TODO: validate if the current solution is working or not, otherwise the above mentioned! + const playersWithPositionIds = await prisma.player.findMany({ select: { id: true, diff --git a/src/controllers/Team.ts b/src/controllers/Team.ts index b161e86..8b4024d 100644 --- a/src/controllers/Team.ts +++ b/src/controllers/Team.ts @@ -395,8 +395,8 @@ export const PostBoosterTeamHandler = async (req: any, rep: any) => { throw new HttpError("No team found", 404) if(teamWithBoosters[req.body.type]) throw new HttpError("Booster already used", 403) - if(teamWithBoosters.selections && teamWithBoosters.selections.length >= 2) - throw new HttpError("Already 2 boosters this week", 403) + if(teamWithBoosters.selections && teamWithBoosters.selections.length >= 1) + throw new HttpError("Already used a booster this week", 403) await prisma.$transaction(async (prisma) => { await prisma.team.update({ @@ -409,18 +409,24 @@ export const PostBoosterTeamHandler = async (req: any, rep: any) => { }); if(isPlayerBooster) { - await prisma.selection.update({ - where: { - playerId_teamId_weekId: { - teamId: +req.params.id, - weekId: currentWeek, - playerId: req.body.playerId + try { + await prisma.selection.update({ + where: { + playerId_teamId_weekId: { + teamId: +req.params.id, + weekId: currentWeek, + playerId: req.body.playerId + }, + captain: 0, + }, + data: { + booster: boosterUnCC } - }, - data: { - booster: boosterUnCC - } - }) + }); + } + catch { + throw new HttpError("You can't use a Player Booster on your (vice)captain!", 403); + } } await prisma.audit.create({ diff --git a/src/controllers/User.ts b/src/controllers/User.ts index 7a4f1a8..9414160 100644 --- a/src/controllers/User.ts +++ b/src/controllers/User.ts @@ -8,7 +8,8 @@ export const LogoutHandler = async (req: any, rep: any) => { } export const GetProfileHandler = async (req: any, rep: any) => { - const user = await prisma.user.findUnique({ + const [user, updates] = await Promise.all([ + await prisma.user.findUnique({ cacheStrategy: { ttl: 30, swr: 60, @@ -16,8 +17,21 @@ export const GetProfileHandler = async (req: any, rep: any) => { where: { id: req.user.id } - }) - rep.send(user); + }), + await prisma.refreshTime.findFirst({ + cacheStrategy: { + ttl: 60, + swr: 60, + }, + orderBy: { + time: 'desc' + } + }), + ]) + rep.send({ + ...user, + notification: {...updates} + }); } export const PaymentIntentHandler = async (req: any, rep: any) => { diff --git a/src/controllers/UserAuth.ts b/src/controllers/UserAuth.ts index b431c40..5a8393f 100644 --- a/src/controllers/UserAuth.ts +++ b/src/controllers/UserAuth.ts @@ -19,6 +19,7 @@ export const refreshTokenCookieOptions: CookieSerializeOptions = { }; export const GoogleAuthHandler = async (req: AccessTokenRequest, rep: any) => { + console.log("ok"); if(req.query.error) { rep.redirect(process.env.WEBAPP_URL); } diff --git a/src/utils/DeserializeUser.ts b/src/utils/DeserializeUser.ts index 0a77fab..32f6c78 100644 --- a/src/utils/DeserializeUser.ts +++ b/src/utils/DeserializeUser.ts @@ -31,7 +31,7 @@ export const reIssueAccessToken = async ({ refreshToken }: { refreshToken: strin export const deserializeUser = async(req: any, rep: any) => { - const accessToken = req.cookies["token"] || req.headers.authorization?.replace(/^Bearer\s/, "") || ""; + const accessToken = req.cookies["token"] || req.headers.authorization?.replace(/^Bearer\s/, "") || ""; const refreshToken = req.cookies["refreshToken"] || req.headers["x-refresh"] || ""; if(!accessToken) { return;