Convert backend to regular directory
This commit is contained in:
292
backend/src/controllers/admin.controller.js
Normal file
292
backend/src/controllers/admin.controller.js
Normal file
@@ -0,0 +1,292 @@
|
||||
const bcrypt = require("bcrypt");
|
||||
const prisma = require("../config/prisma");
|
||||
|
||||
const getStats = async (req, res) => {
|
||||
try {
|
||||
const [
|
||||
ticketsTotal,
|
||||
ticketsUsed,
|
||||
participations,
|
||||
claimsPending,
|
||||
claimsDelivered,
|
||||
claimsCancelled,
|
||||
users,
|
||||
men,
|
||||
women,
|
||||
] = await Promise.all([
|
||||
prisma.ticket.count(),
|
||||
prisma.ticket.count({ where: { isUsed: true } }),
|
||||
prisma.participation.count(),
|
||||
prisma.claim.count({ where: { status: "PENDING" } }),
|
||||
prisma.claim.count({ where: { status: "DELIVERED" } }),
|
||||
prisma.claim.count({ where: { status: "CANCELLED" } }),
|
||||
prisma.user.count({ where: { role: "CLIENT" } }),
|
||||
prisma.user.count({ where: { role: "CLIENT", gender: "Homme" } }),
|
||||
prisma.user.count({ where: { role: "CLIENT", gender: "Femme" } }),
|
||||
]);
|
||||
|
||||
const lotsStats = await prisma.lot.findMany({
|
||||
select: {
|
||||
id: true,
|
||||
name: true,
|
||||
percentage: true,
|
||||
_count: {
|
||||
select: {
|
||||
tickets: true,
|
||||
participations: true,
|
||||
},
|
||||
},
|
||||
},
|
||||
});
|
||||
|
||||
return res.json({
|
||||
ticketsTotal,
|
||||
ticketsUsed,
|
||||
ticketsRemaining: ticketsTotal - ticketsUsed,
|
||||
participations,
|
||||
claimsPending,
|
||||
claimsDelivered,
|
||||
claimsCancelled,
|
||||
users,
|
||||
genderStats: {
|
||||
men,
|
||||
women,
|
||||
other: users - men - women,
|
||||
},
|
||||
lotsStats,
|
||||
});
|
||||
} catch (error) {
|
||||
console.error("Erreur stats admin:", error);
|
||||
return res.status(500).json({
|
||||
message: "Erreur serveur lors de la récupération des statistiques.",
|
||||
});
|
||||
}
|
||||
};
|
||||
|
||||
const getUsers = async (req, res) => {
|
||||
try {
|
||||
const users = await prisma.user.findMany({
|
||||
where: { role: "CLIENT" },
|
||||
select: {
|
||||
id: true,
|
||||
firstname: true,
|
||||
lastname: true,
|
||||
email: true,
|
||||
gender: true,
|
||||
birthdate: true,
|
||||
marketingOptIn: true,
|
||||
createdAt: true,
|
||||
_count: {
|
||||
select: {
|
||||
participations: true,
|
||||
},
|
||||
},
|
||||
},
|
||||
orderBy: {
|
||||
createdAt: "desc",
|
||||
},
|
||||
});
|
||||
|
||||
return res.json({ users });
|
||||
} catch (error) {
|
||||
console.error("Erreur liste utilisateurs:", error);
|
||||
return res.status(500).json({
|
||||
message: "Erreur serveur lors de la récupération des utilisateurs.",
|
||||
});
|
||||
}
|
||||
};
|
||||
|
||||
const getEmailingContacts = async (req, res) => {
|
||||
try {
|
||||
const contacts = await prisma.user.findMany({
|
||||
where: {
|
||||
role: "CLIENT",
|
||||
marketingOptIn: true,
|
||||
},
|
||||
select: {
|
||||
firstname: true,
|
||||
lastname: true,
|
||||
email: true,
|
||||
createdAt: true,
|
||||
},
|
||||
orderBy: {
|
||||
createdAt: "desc",
|
||||
},
|
||||
});
|
||||
|
||||
return res.json({
|
||||
total: contacts.length,
|
||||
contacts,
|
||||
});
|
||||
} catch (error) {
|
||||
console.error("Erreur emailing:", error);
|
||||
return res.status(500).json({
|
||||
message: "Erreur serveur lors de la récupération des contacts emailing.",
|
||||
});
|
||||
}
|
||||
};
|
||||
|
||||
const createEmployee = async (req, res) => {
|
||||
try {
|
||||
const { firstname, lastname, email, password, birthdate, gender } = req.body;
|
||||
|
||||
const existingUser = await prisma.user.findUnique({
|
||||
where: { email },
|
||||
});
|
||||
|
||||
if (existingUser) {
|
||||
return res.status(409).json({
|
||||
message: "Un compte existe déjà avec cet email.",
|
||||
});
|
||||
}
|
||||
|
||||
const hashedPassword = await bcrypt.hash(password, 10);
|
||||
|
||||
const employee = await prisma.user.create({
|
||||
data: {
|
||||
firstname,
|
||||
lastname,
|
||||
email,
|
||||
password: hashedPassword,
|
||||
birthdate: birthdate ? new Date(birthdate) : null,
|
||||
gender,
|
||||
role: "EMPLOYEE",
|
||||
marketingOptIn: false,
|
||||
},
|
||||
select: {
|
||||
id: true,
|
||||
firstname: true,
|
||||
lastname: true,
|
||||
email: true,
|
||||
role: true,
|
||||
createdAt: true,
|
||||
},
|
||||
});
|
||||
|
||||
return res.status(201).json({
|
||||
message: "Employé créé avec succès.",
|
||||
employee,
|
||||
});
|
||||
} catch (error) {
|
||||
console.error("Erreur création employé:", error);
|
||||
return res.status(500).json({
|
||||
message: "Erreur serveur lors de la création de l'employé.",
|
||||
});
|
||||
}
|
||||
};
|
||||
|
||||
const drawWinner = async (req, res) => {
|
||||
try {
|
||||
const existingWinner = await prisma.grandPrizeWinner.findFirst({
|
||||
include: {
|
||||
user: {
|
||||
select: {
|
||||
id: true,
|
||||
firstname: true,
|
||||
lastname: true,
|
||||
email: true,
|
||||
},
|
||||
},
|
||||
},
|
||||
});
|
||||
|
||||
if (existingWinner) {
|
||||
return res.status(409).json({
|
||||
message: "Le tirage au sort a déjà été effectué.",
|
||||
winner: existingWinner,
|
||||
});
|
||||
}
|
||||
|
||||
const participants = await prisma.user.findMany({
|
||||
where: {
|
||||
role: "CLIENT",
|
||||
participations: {
|
||||
some: {},
|
||||
},
|
||||
},
|
||||
select: {
|
||||
id: true,
|
||||
firstname: true,
|
||||
lastname: true,
|
||||
email: true,
|
||||
},
|
||||
});
|
||||
|
||||
if (participants.length === 0) {
|
||||
return res.status(400).json({
|
||||
message: "Aucun participant disponible pour le tirage.",
|
||||
});
|
||||
}
|
||||
|
||||
const randomIndex = Math.floor(Math.random() * participants.length);
|
||||
const winner = participants[randomIndex];
|
||||
|
||||
const savedWinner = await prisma.grandPrizeWinner.create({
|
||||
data: {
|
||||
userId: winner.id,
|
||||
},
|
||||
include: {
|
||||
user: {
|
||||
select: {
|
||||
id: true,
|
||||
firstname: true,
|
||||
lastname: true,
|
||||
email: true,
|
||||
},
|
||||
},
|
||||
},
|
||||
});
|
||||
|
||||
return res.status(201).json({
|
||||
message: "Tirage au sort effectué avec succès.",
|
||||
totalEligibleParticipants: participants.length,
|
||||
winner: savedWinner,
|
||||
});
|
||||
} catch (error) {
|
||||
console.error("Erreur tirage au sort:", error);
|
||||
return res.status(500).json({
|
||||
message: "Erreur serveur lors du tirage au sort.",
|
||||
});
|
||||
}
|
||||
};
|
||||
|
||||
const getGrandPrizeWinner = async (req, res) => {
|
||||
try {
|
||||
const winner = await prisma.grandPrizeWinner.findFirst({
|
||||
include: {
|
||||
user: {
|
||||
select: {
|
||||
id: true,
|
||||
firstname: true,
|
||||
lastname: true,
|
||||
email: true,
|
||||
},
|
||||
},
|
||||
},
|
||||
});
|
||||
|
||||
if (!winner) {
|
||||
return res.status(404).json({
|
||||
message: "Aucun gagnant du gros lot n'a encore été tiré.",
|
||||
});
|
||||
}
|
||||
|
||||
return res.json({
|
||||
winner,
|
||||
});
|
||||
} catch (error) {
|
||||
console.error("Erreur récupération gagnant:", error);
|
||||
return res.status(500).json({
|
||||
message: "Erreur serveur lors de la récupération du gagnant.",
|
||||
});
|
||||
}
|
||||
};
|
||||
|
||||
module.exports = {
|
||||
getStats,
|
||||
getUsers,
|
||||
getEmailingContacts,
|
||||
createEmployee,
|
||||
drawWinner,
|
||||
getGrandPrizeWinner,
|
||||
};
|
||||
134
backend/src/controllers/auth.controller.js
Normal file
134
backend/src/controllers/auth.controller.js
Normal file
@@ -0,0 +1,134 @@
|
||||
const bcrypt = require("bcrypt");
|
||||
const jwt = require("jsonwebtoken");
|
||||
const prisma = require("../config/prisma");
|
||||
|
||||
const register = async (req, res) => {
|
||||
try {
|
||||
const {
|
||||
firstname,
|
||||
lastname,
|
||||
email,
|
||||
password,
|
||||
birthdate,
|
||||
gender,
|
||||
marketingOptIn,
|
||||
} = req.body;
|
||||
|
||||
if (!firstname || !lastname || !email || !password) {
|
||||
return res.status(400).json({
|
||||
message: "Tous les champs obligatoires doivent être renseignés.",
|
||||
});
|
||||
}
|
||||
|
||||
const existingUser = await prisma.user.findUnique({
|
||||
where: { email },
|
||||
});
|
||||
|
||||
if (existingUser) {
|
||||
return res.status(409).json({
|
||||
message: "Un compte existe déjà avec cet email.",
|
||||
});
|
||||
}
|
||||
|
||||
const hashedPassword = await bcrypt.hash(password, 10);
|
||||
|
||||
const user = await prisma.user.create({
|
||||
data: {
|
||||
firstname,
|
||||
lastname,
|
||||
email,
|
||||
password: hashedPassword,
|
||||
birthdate: birthdate ? new Date(birthdate) : null,
|
||||
gender,
|
||||
marketingOptIn: marketingOptIn || false,
|
||||
},
|
||||
select: {
|
||||
id: true,
|
||||
firstname: true,
|
||||
lastname: true,
|
||||
email: true,
|
||||
role: true,
|
||||
marketingOptIn: true,
|
||||
createdAt: true,
|
||||
},
|
||||
});
|
||||
|
||||
return res.status(201).json({
|
||||
message: "Inscription réussie.",
|
||||
user,
|
||||
});
|
||||
} catch (error) {
|
||||
console.error("Erreur register:", error);
|
||||
return res.status(500).json({
|
||||
message: "Erreur serveur lors de l'inscription.",
|
||||
});
|
||||
}
|
||||
};
|
||||
|
||||
const login = async (req, res) => {
|
||||
try {
|
||||
const { email, password } = req.body;
|
||||
|
||||
if (!email || !password) {
|
||||
return res.status(400).json({
|
||||
message: "Email et mot de passe requis.",
|
||||
});
|
||||
}
|
||||
|
||||
const user = await prisma.user.findUnique({
|
||||
where: { email },
|
||||
});
|
||||
|
||||
if (!user) {
|
||||
return res.status(401).json({
|
||||
message: "Identifiants invalides.",
|
||||
});
|
||||
}
|
||||
|
||||
const isPasswordValid = await bcrypt.compare(password, user.password);
|
||||
|
||||
if (!isPasswordValid) {
|
||||
return res.status(401).json({
|
||||
message: "Identifiants invalides.",
|
||||
});
|
||||
}
|
||||
|
||||
const token = jwt.sign(
|
||||
{
|
||||
id: user.id,
|
||||
role: user.role,
|
||||
},
|
||||
process.env.JWT_SECRET,
|
||||
{ expiresIn: "24h" }
|
||||
);
|
||||
|
||||
return res.json({
|
||||
message: "Connexion réussie.",
|
||||
token,
|
||||
user: {
|
||||
id: user.id,
|
||||
firstname: user.firstname,
|
||||
lastname: user.lastname,
|
||||
email: user.email,
|
||||
role: user.role,
|
||||
},
|
||||
});
|
||||
} catch (error) {
|
||||
console.error("Erreur login:", error);
|
||||
return res.status(500).json({
|
||||
message: "Erreur serveur lors de la connexion.",
|
||||
});
|
||||
}
|
||||
};
|
||||
|
||||
const me = async (req, res) => {
|
||||
return res.json({
|
||||
user: req.user,
|
||||
});
|
||||
};
|
||||
|
||||
module.exports = {
|
||||
register,
|
||||
login,
|
||||
me,
|
||||
};
|
||||
115
backend/src/controllers/employee.controller.js
Normal file
115
backend/src/controllers/employee.controller.js
Normal file
@@ -0,0 +1,115 @@
|
||||
const prisma = require("../config/prisma");
|
||||
|
||||
const getGainByTicketCode = async (req, res) => {
|
||||
try {
|
||||
const { code } = req.params;
|
||||
|
||||
const ticket = await prisma.ticket.findUnique({
|
||||
where: { code: code.toUpperCase() },
|
||||
include: {
|
||||
lot: true,
|
||||
user: {
|
||||
select: {
|
||||
id: true,
|
||||
firstname: true,
|
||||
lastname: true,
|
||||
email: true,
|
||||
},
|
||||
},
|
||||
participation: {
|
||||
include: {
|
||||
claim: true,
|
||||
},
|
||||
},
|
||||
},
|
||||
});
|
||||
|
||||
if (!ticket) {
|
||||
return res.status(404).json({
|
||||
message: "Ticket introuvable.",
|
||||
});
|
||||
}
|
||||
|
||||
if (!ticket.isUsed || !ticket.participation) {
|
||||
return res.status(400).json({
|
||||
message: "Ce ticket n'a pas encore été utilisé par un client.",
|
||||
});
|
||||
}
|
||||
|
||||
return res.json({
|
||||
ticket: {
|
||||
code: ticket.code,
|
||||
usedAt: ticket.usedAt,
|
||||
},
|
||||
client: ticket.user,
|
||||
lot: {
|
||||
name: ticket.lot.name,
|
||||
description: ticket.lot.description,
|
||||
value: ticket.lot.value,
|
||||
},
|
||||
claim: {
|
||||
id: ticket.participation.claim.id,
|
||||
status: ticket.participation.claim.status,
|
||||
claimedAt: ticket.participation.claim.claimedAt,
|
||||
},
|
||||
});
|
||||
} catch (error) {
|
||||
console.error("Erreur recherche gain employé:", error);
|
||||
return res.status(500).json({
|
||||
message: "Erreur serveur lors de la recherche du gain.",
|
||||
});
|
||||
}
|
||||
};
|
||||
|
||||
const deliverClaim = async (req, res) => {
|
||||
try {
|
||||
const { id } = req.params;
|
||||
const employeeId = req.user.id;
|
||||
|
||||
const claim = await prisma.claim.findUnique({
|
||||
where: { id: Number(id) },
|
||||
});
|
||||
|
||||
if (!claim) {
|
||||
return res.status(404).json({
|
||||
message: "Réclamation introuvable.",
|
||||
});
|
||||
}
|
||||
|
||||
if (claim.status === "DELIVERED") {
|
||||
return res.status(409).json({
|
||||
message: "Ce lot a déjà été remis.",
|
||||
});
|
||||
}
|
||||
|
||||
if (claim.status === "CANCELLED") {
|
||||
return res.status(400).json({
|
||||
message: "Ce gain est expiré et ne peut plus être remis.",
|
||||
});
|
||||
}
|
||||
|
||||
const updatedClaim = await prisma.claim.update({
|
||||
where: { id: Number(id) },
|
||||
data: {
|
||||
status: "DELIVERED",
|
||||
claimedAt: new Date(),
|
||||
validatedByEmployeeId: employeeId,
|
||||
},
|
||||
});
|
||||
|
||||
return res.json({
|
||||
message: "Lot marqué comme remis avec succès.",
|
||||
claim: updatedClaim,
|
||||
});
|
||||
} catch (error) {
|
||||
console.error("Erreur remise lot:", error);
|
||||
return res.status(500).json({
|
||||
message: "Erreur serveur lors de la remise du lot.",
|
||||
});
|
||||
}
|
||||
};
|
||||
|
||||
module.exports = {
|
||||
getGainByTicketCode,
|
||||
deliverClaim,
|
||||
};
|
||||
120
backend/src/controllers/game.controller.js
Normal file
120
backend/src/controllers/game.controller.js
Normal file
@@ -0,0 +1,120 @@
|
||||
const prisma = require("../config/prisma");
|
||||
|
||||
const participate = async (req, res) => {
|
||||
try {
|
||||
const { code } = req.body;
|
||||
const userId = req.user.id;
|
||||
|
||||
const ticket = await prisma.ticket.findUnique({
|
||||
where: { code },
|
||||
include: {
|
||||
lot: true,
|
||||
},
|
||||
});
|
||||
|
||||
if (!ticket) {
|
||||
return res.status(404).json({
|
||||
message: "Code invalide.",
|
||||
});
|
||||
}
|
||||
|
||||
if (ticket.isUsed) {
|
||||
return res.status(409).json({
|
||||
message: "Ce code a déjà été utilisé.",
|
||||
});
|
||||
}
|
||||
|
||||
const result = await prisma.$transaction(async (tx) => {
|
||||
const updatedTicket = await tx.ticket.update({
|
||||
where: { id: ticket.id },
|
||||
data: {
|
||||
isUsed: true,
|
||||
usedAt: new Date(),
|
||||
userId,
|
||||
},
|
||||
include: {
|
||||
lot: true,
|
||||
},
|
||||
});
|
||||
|
||||
const participation = await tx.participation.create({
|
||||
data: {
|
||||
userId,
|
||||
ticketId: updatedTicket.id,
|
||||
lotId: updatedTicket.lotId,
|
||||
},
|
||||
});
|
||||
|
||||
const claim = await tx.claim.create({
|
||||
data: {
|
||||
participationId: participation.id,
|
||||
status: "PENDING",
|
||||
},
|
||||
});
|
||||
|
||||
return {
|
||||
ticket: updatedTicket,
|
||||
participation,
|
||||
claim,
|
||||
};
|
||||
});
|
||||
|
||||
return res.status(201).json({
|
||||
message: "Participation enregistrée avec succès.",
|
||||
gain: {
|
||||
name: result.ticket.lot.name,
|
||||
description: result.ticket.lot.description,
|
||||
value: result.ticket.lot.value,
|
||||
},
|
||||
claim: {
|
||||
id: result.claim.id,
|
||||
status: result.claim.status,
|
||||
},
|
||||
});
|
||||
} catch (error) {
|
||||
console.error("Erreur participation:", error);
|
||||
return res.status(500).json({
|
||||
message: "Erreur serveur lors de la participation.",
|
||||
});
|
||||
}
|
||||
};
|
||||
|
||||
const myGains = async (req, res) => {
|
||||
try {
|
||||
const userId = req.user.id;
|
||||
|
||||
const participations = await prisma.participation.findMany({
|
||||
where: { userId },
|
||||
include: {
|
||||
ticket: true,
|
||||
lot: true,
|
||||
claim: true,
|
||||
},
|
||||
orderBy: {
|
||||
createdAt: "desc",
|
||||
},
|
||||
});
|
||||
|
||||
return res.json({
|
||||
gains: participations.map((participation) => ({
|
||||
participationId: participation.id,
|
||||
ticketCode: participation.ticket.code,
|
||||
lot: participation.lot.name,
|
||||
description: participation.lot.description,
|
||||
value: participation.lot.value,
|
||||
claimStatus: participation.claim?.status,
|
||||
wonAt: participation.createdAt,
|
||||
})),
|
||||
});
|
||||
} catch (error) {
|
||||
console.error("Erreur historique gains:", error);
|
||||
return res.status(500).json({
|
||||
message: "Erreur serveur lors de la récupération des gains.",
|
||||
});
|
||||
}
|
||||
};
|
||||
|
||||
module.exports = {
|
||||
participate,
|
||||
myGains,
|
||||
};
|
||||
Reference in New Issue
Block a user