Skip to content

Commit

Permalink
v2024.05.18:
Browse files Browse the repository at this point in the history
commit 2ed88ec
Author: Jonas Simoen <[email protected]>
Date:   Sat May 18 23:00:58 2024 +0200

    chore: remove logs

commit 72be48a
Author: Jonas Simoen <[email protected]>
Date:   Sat May 18 22:57:52 2024 +0200

    fix(login): return to app when access denied

commit 26b55d7
Author: Jonas Simoen <[email protected]>
Date:   Sat May 18 22:45:34 2024 +0200

    feat(news): add news articles

commit 2b6a891
Author: Jonas Simoen <[email protected]>
Date:   Fri May 17 19:09:32 2024 +0200

    remove redirect_to_welcome

commit a3e0c89
Author: Jonas Simoen <[email protected]>
Date:   Fri May 17 13:05:20 2024 +0200

    feat(notif)

commit af72256
Author: Jonas Simoen <[email protected]>
Date:   Thu May 16 22:04:30 2024 +0200

    feat(audit): search audits

commit 72e7b42
Author: Jonas Simoen <[email protected]>
Date:   Thu May 16 20:40:40 2024 +0200

    chore(team): quicker db selects

commit 3cdb613
Author: Jonas Simoen <[email protected]>
Date:   Thu May 16 20:18:30 2024 +0200

    feat(general): user list

commit 22a9ca7
Author: Jonas Simoen <[email protected]>
Date:   Thu May 16 18:29:35 2024 +0200

    fix(boosters)
  • Loading branch information
jonassimoen committed May 18, 2024
1 parent c6c2288 commit 3691382
Show file tree
Hide file tree
Showing 20 changed files with 480 additions and 206 deletions.
11 changes: 10 additions & 1 deletion api/index.ts
Original file line number Diff line number Diff line change
@@ -1,14 +1,16 @@
import fastify from "fastify";
import cookies from "@fastify/cookie";
import cors from "@fastify/cors";
import { applicationDefault, initializeApp } from "firebase-admin/app";
import admin from "firebase-admin";
import { AdminRouter, PublicRouter } from "../src/routers/Main";
import HttpError from "../src/utils/HttpError";

import dotenv from "dotenv";
import { deserializeUser } from "../src/utils/DeserializeUser";
import fastifyStatic from "@fastify/static";
import path from "path";
import _default from "fastify-metrics";
import dotenv from "dotenv";
dotenv.config();

export const server = fastify({
Expand All @@ -18,6 +20,13 @@ export const server = fastify({
disableRequestLogging: true,
});


export const app = initializeApp({
credential: admin.credential.cert(JSON.parse(
process.env.FIREBASE_ACCOUNT_KEY as string
))
});

server.addHook("preHandler", deserializeUser);

server.register(require("fastify-list-routes"), { colors: true });
Expand Down
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@
"fastify-list-routes": "^1.0.0",
"fastify-metrics": "^10.6.0",
"fastify-stripe": "^2.4.1",
"firebase-admin": "^12.1.0",
"fs": "^0.0.1-security",
"jsonwebtoken": "^9.0.0",
"lodash": "^4.17.21",
Expand Down
29 changes: 26 additions & 3 deletions prisma/schema.prisma
Original file line number Diff line number Diff line change
Expand Up @@ -47,8 +47,10 @@ model User {
role Int
payed Boolean? @default(false)
teams Team[]
Audit Audit[]
teams Team[]
Audit Audit[]
NotificationToken NotificationToken[]
Article Article[]
}

model Player {
Expand Down Expand Up @@ -120,7 +122,7 @@ model Statistic {
blocks Int @default(0)
interceptions Int @default(0)
tackles Int @default(0)
lineClearances Int @default(0)
lineClearances Int @default(0)
// Dribbles
dribblesAttempted Int @default(0)
dribblesSuccess Int @default(0)
Expand Down Expand Up @@ -314,6 +316,27 @@ model Audit {
user User @relation(fields: [userId], references: [id])
}

model NotificationToken {
id Int @id @default(autoincrement())
token String
timestamp DateTime
user User @relation(fields: [userId], references: [id])
userId Int
}

model Article {
id Int @id @default(autoincrement())
slug String
title String
description String
imageUrl String?
readMore Boolean?
timestampCreated DateTime
timestampUpdated DateTime?
author User @relation(fields: [authorId], references: [id])
authorId Int
}

// model WeeksOnTeams {
// week Week @relation(fields: [weekId], references: [id])
// team Team @relation(fields: [teamId], references: [id])
Expand Down
88 changes: 80 additions & 8 deletions src/controllers/General.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import { upcomingWeekId } from "../utils/Common";
import { prisma } from "../db/client";

export const GeneralInfoHandler = async (req: any, rep: any) => {
Expand All @@ -6,14 +7,6 @@ export const GeneralInfoHandler = async (req: any, rep: any) => {
userCount: await prisma.user.count(),
teamCount: await prisma.team.count(),
clubWinner: await prisma.club.findFirst({where: {winner: true}}),
users: await prisma.user.findMany({
select: {
firstName: true,
lastName: true,
email: true,
payed: true,
}
})
// teamCount: await prisma.team.count(),
});
}
Expand All @@ -29,4 +22,83 @@ export const PostClubWinnerHandler = async (req: any, rep: any) => {
});
await prisma.$queryRaw`CALL "processWinner"()`;
rep.send({message: "Winnaar succesvol gewijzigd."})
}

export const GetUserOverview = async(req: any, rep: any) => {
const weekId = await upcomingWeekId();

const [users, teamsWithActiveBoosters, audits] = await Promise.all([
prisma.user.findMany({
select: {
id: true,
email: true,
firstName: true,
lastName: true,
payed: true,
}
}),
prisma.team.findMany({
select: {
user: {
select: {
email: true,
}
},
tripleCaptain: true,
viceVictory: true,
freeHit: true,
superSubs: true,
hiddenGem: true,
goalRush: true,
},
where: {
OR: [
{tripleCaptain: weekId},
{viceVictory: weekId},
{freeHit: weekId},
{superSubs: weekId},
{hiddenGem: weekId},
{goalRush: weekId},
],
}
}),
prisma.audit.findMany({
take: 50,
select: {
id: true,
timestamp: true,
action: true,
user: {
select: {
email: true,
}
},
},
orderBy: {
timestamp: "desc"
}
})
]);

rep.send({
users,
activeBoosters: teamsWithActiveBoosters.map((u: any) => ({
user: u.user.email,
boosters: Object.keys(u).filter((v: string) => u[v] == weekId)
})),
audits
})
}

export const GetAuditHandler = async(req: any, rep: any) => {
const audit = await prisma.audit.findFirst({
where: {
id: +req.params.id
},
select: {
action: true,
params: true,
}
});
rep.send(audit);
}
57 changes: 57 additions & 0 deletions src/controllers/News.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
import { prisma } from "../db/client"

export const GetArticlesHandler = async (req: any, rep: any) => {
const page = +req.query.page || 1;
const [articleCount, articles] = await Promise.all([
await prisma.article.count(),
await prisma.article.findMany({
select: {
id: true,
slug: true,
title: true,
description: true,
timestampCreated: true,
timestampUpdated: true,
readMore: true,
imageUrl: true,
author: {
select: {
firstName: true,
}
}
},
orderBy: {
timestampCreated: 'desc'
},
take: 5,
skip: (page - 1) * 5
}),
])
rep.send({
articles,
count: articleCount,
})
}
export const GetArticleHandler = async (req: any, rep: any) => {
const articles = await prisma.article.findFirst({
select: {
id: true,
slug: true,
title: true,
description: true,
timestampCreated: true,
timestampUpdated: true,
imageUrl: true,
readMore: true,
author: {
select: {
firstName: true,
}
}
},
where: {
slug: req.params.slug
}
});
rep.send(articles)
}
38 changes: 38 additions & 0 deletions src/controllers/Notification.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
import { getMessaging } from "firebase-admin/messaging";
import { app } from "../../api/index"
import { prisma } from "../db/client";

export const RegisterTokenHandler = async(req: any, rep: any) => {
getMessaging(app).subscribeToTopic(req.body.token, "edd-app");
const token = await prisma.notificationToken.create({
data: {
token: req.body.token,
user: {
connect: {
id: +req.user.id
}
},
timestamp: new Date().toISOString(),
}
})
rep.status(200);
}

export const PushNotificationHandler = async(req: any, rep: any) => {

const id = await getMessaging(app).send({
topic: 'edd-app',
webpush: {
notification: {
title: req.body.title || "",
body: req.body.body || "",
badge: req.body.photo || "",
icon: req.body.photo || "",
},
fcmOptions: {
link: req.body.link || "",
}
}
})
rep.send(id);
}
2 changes: 0 additions & 2 deletions src/controllers/Statistic.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,6 @@ export const GetPlayerStatisticsHandler = async (req: any, rep: any) => {
})).length;

const matchdayFilter = req.query.matchday ? { id: { equals: +req.query.matchday } } : { validated: true }
console.log(matchdayFilter);

const players = await prisma.player.findMany({
cacheStrategy: {
Expand Down Expand Up @@ -361,7 +360,6 @@ export const ImportMatchStatisticHandler = async (req: any, rep: any) => {
}
const converted = res.data.response.map((resp: any) => {
return resp.players.map((player: any) => {
// console.log(player)
const stats = player.statistics[0];

return ({
Expand Down
Loading

0 comments on commit 3691382

Please sign in to comment.