Skip to content

Commit

Permalink
Merge pull request #84 from ConducereIT/dev
Browse files Browse the repository at this point in the history
Release
  • Loading branch information
cristim67 authored May 11, 2024
2 parents b832406 + a78d290 commit 6d70755
Show file tree
Hide file tree
Showing 6 changed files with 158 additions and 32 deletions.
2 changes: 1 addition & 1 deletion client/src/routes/AllRoutes.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -72,5 +72,5 @@ export const allRoutes = createBrowserRouter([
{
path:"cronometrare",
element: <Cronometrare/>
}
},
]);
80 changes: 51 additions & 29 deletions client/src/views/Account.view.tsx
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
import React, { useState, useEffect } from "react";
import { useNavigate } from "react-router-dom";
import { AuthService } from "@genezio/auth";
import { BackendService } from "@genezio-sdk/apv-production";
import { Header } from "../components/Header.component";
import { Helmet } from "react-helmet";
import React, {useState, useEffect} from "react";
import {useNavigate} from "react-router-dom";
import {AuthService} from "@genezio/auth";
import {BackendService} from "@genezio-sdk/apv-production";
import {Header} from "../components/Header.component";
import {Helmet} from "react-helmet";

interface Race {
id: number;
Expand All @@ -30,6 +30,7 @@ const Account: React.FC = () => {
const [races, setRaces] = useState<Race[]>([]);
const navigate = useNavigate();
const [userPicture, setUserPicture] = useState<string>("");
const [locOcupat, setLocOcupat] = useState<number>(0);
useEffect(() => {
const isLoggedIn = async () => {
try {
Expand All @@ -44,8 +45,27 @@ const Account: React.FC = () => {
}
};

const takeLocOcupat = async () => {
try {
const response = await BackendService.getAllRaces()

let locOcupat= 1;
for (let i = 0; i < response.length; i++) {
if (response[i].categorie === races[0].categorie) {
if (response[i].timpAlergat < races[0].timpAlergat) {
locOcupat++;
}
}
}
setLocOcupat(locOcupat);
} catch (error) {
console.log(error);
}
}

isLoggedIn();
}, [navigate]);
takeLocOcupat();
}, [navigate,races]);

useEffect(() => {
const takeRaces = async () => {
Expand All @@ -65,7 +85,7 @@ const Account: React.FC = () => {
<Helmet>
<title>APV 2024 | Account</title>
</Helmet>
<Header />
<Header/>
<div className="bg-white rounded-lg shadow-md mx-auto max-w-7xl mt-48 p-6">
<h1 className="text-xl mb-6 font-semibold flex justify-center">
Profil - {userName}
Expand Down Expand Up @@ -98,29 +118,31 @@ const Account: React.FC = () => {
</h2>
<table className="table-auto border border-collapse border-gray-200 mx-auto">
<thead className="bg-gray-200">
<tr className="bg-gray-200">
<th className="px-4 py-2">Categorie</th>
<th className="px-4 py-2">Numar Tricou</th>
<th className="px-4 py-2">Timp Alergat</th>
</tr>
<tr className="bg-gray-200">
<th className="px-4 py-2">Categorie</th>
<th className="px-4 py-2">Numar Tricou</th>
<th className="px-4 py-2">Timp Alergat</th>
<th className="px-4 py-2">Loc ocupat</th>
</tr>
</thead>
<tbody>
{races.map((race, index) => (
<tr
key={index}
className={index % 2 === 0 ? "bg-gray-100" : ""}
>
<td className="px-4 py-2">
{racesContext[race.categorie]}
</td>
<td className="px-4 py-2">
{race.numarTricou ? race.numarTricou : "N/A"}
</td>
<td className="px-4 py-2">
{race.timpAlergat ? race.timpAlergat : "N/A"}
</td>
</tr>
))}
{races.map((race, index) => (
<tr
key={index}
className={index % 2 === 0 ? "bg-gray-100" : ""}
>
<td className="px-4 py-2 text-center">
{racesContext[race.categorie]}
</td>
<td className="px-4 py-2 text-center">
{race.numarTricou ? race.numarTricou : "N/A"}
</td>
<td className="px-4 py-2 text-center">
{race.timpAlergat ? race.timpAlergat : "N/A"}
</td>
<td className="px-4 py-2 text-center">{locOcupat===0 || race.timpAlergat === null || race.timpAlergat === "00:00:00" ? "N/A" : locOcupat}</td>
</tr>
))}
</tbody>
</table>
</div>
Expand Down
33 changes: 33 additions & 0 deletions client/src/views/Cronometrare.view.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -178,6 +178,7 @@ const Cronometrare: React.FC = () => {
</button>
</th>
<th className="border border-gray-400 px-4 py-2">Update</th>
<th className="border border-gray-400 px-4 py-2">Send email</th>
</tr>
</thead>
<tbody>
Expand Down Expand Up @@ -261,6 +262,38 @@ const Cronometrare: React.FC = () => {
className="bg-green-500 hover:bg-green-700 text-white font-bold py-2 px-4 rounded"
/>
</td>
<td className="border border-gray-400 px-4 py-2">
{
formDataList[index].emailTrimis === "DA" ? (
<input
type="button"
value="Trimis"
className="bg-green-500 hover:bg-green-700 text-white font-bold py-2 px-4 rounded"
disabled={true}
/>
) : (
<input
type="button"
value="Send"
onClick={() => {
const sendEmail = async () => {
try {
const response = await BackendService.sendRaceCompletionEmail(
formDataList[index].id)
if (response) {
alert(response.message)
window.location.reload();
}
} catch (error) {
console.log(error);
alert("Error sending email");
}
};
sendEmail();
}}
className="bg-blue-500 hover:bg-blue-700 text-white font-bold py-2 px-4 rounded"
/>)}
</td>
</tr>
))}
</tbody>
Expand Down
58 changes: 58 additions & 0 deletions server/backend.ts
Original file line number Diff line number Diff line change
Expand Up @@ -509,4 +509,62 @@ export class BackendService {
return createHTTPError(500, "Internal Server Error");
}
}

@GenezioAuth()
async sendRaceCompletionEmail(context: GnzContext, id: number) {
const userInfo = await this.prisma.cursa.findUnique({
where: {
id: id
}
})
if (!userInfo) {
throw createHTTPError(404, "User not exist");
}
const subject = `Felicitări pentru finalizarea cursei - Aleargă pentru Viață`;
const ora = userInfo.timpAlergat;
const cursa = userInfo.categorie;
const aiTrimis = userInfo.emailTrimis;
const name = userInfo.name;

if(aiTrimis === "DA"){
return {
status: 200,
message: "A fost trimis deja email-ul"
}
}


const userId = userInfo.userId;
const user = await pool.query(`SELECT "email" FROM "users" WHERE "userId" = $1`, [userId]);
if (user.rows[0] === undefined) {
throw createHTTPError(404, "User not exist");
}

const email = user.rows[0].email;

const response = await this.mailer.sendRaceCompletionEmail(
email,
subject,
name!,
cursa!,
ora!
);

if(response){
await this.prisma.cursa.update({
where: {
id: id
},
data: {
emailTrimis: "DA"
}
});
return {
status: 200,
message: "Successfully sent"
}
} else {
return createHTTPError(500, "Internal Server Error");
}
}
}
16 changes: 14 additions & 2 deletions server/mailer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -87,6 +87,18 @@ export class Mailer{
cursa: string,
minuteAlergate: string
){
const races = {
"0": "Cursa Copii",
"1": "Feminin 13-17 ani",
"2": "Masculin 13-17 ani",
"3": "Feminin 18-35 de ani",
"4": "Masculin 18-35 de ani",
"5": "Feminin 35+ de ani",
"6": "Masculin 35+ de ani",
"7": "Nu a selectat"
};
// @ts-expect-error: cursa is a
const cursaText = races[cursa];
const html = `<!doctype html>
<html lang="ro">
<head>
Expand All @@ -102,9 +114,9 @@ export class Mailer{
<a href="https://ibb.co/g9SP8mx"><img src="https://i.ibb.co/H4txyBj/logo-apv-XV.png" alt="Logo Aleargă pentru Viață" style="max-width: 75%; height: auto; display: inline-block; box-shadow: 0 0 20px rgba(0, 0, 0, 0.3);"></a>
</div>
<h1 style="color: #fff; text-align: center; margin-bottom: 20px;">Felicitari, ${nume}!</h1>
<p style="color: #fff; text-align: center; font-size: 14px;">Detalii ${cursa}, ediția a XV-a!</p>
<p style="color: #fff; text-align: center; font-size: 14px;">Detalii ${cursaText}, ediția a XV-a!</p>
<div style="background-color: #2E86AA; padding: 15px; border-radius: 5px; margin-top: 30px; margin-bottom: 30px; text-align: center;">
<h2 style="color: #fff; font-size: 18px; margin-top: 0;">Ai terminat ${cursa} în ${minuteAlergate} de minute!</h2>
<h2 style="color: #fff; font-size: 18px; margin-top: 0;">Ai terminat ${cursaText} în ${minuteAlergate} de minute!</h2>
</div>
<p style="color: #fff; text-align: center; font-size: 16px;">Iti multumim ca ai alergat alaturi de noi pentru o cauză nobilă!</p>
<p style="color: #fff; text-align: center; font-size: 16px;">Cu drag,</p>
Expand Down
1 change: 1 addition & 0 deletions server/prisma/schema.prisma
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@ model Cursa {
suma String?
checkin String?
inscriereFizic String?
emailTrimis String?
}

// exemple de user si curse:
Expand Down

0 comments on commit 6d70755

Please sign in to comment.