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;