Convert backend to regular directory
This commit is contained in:
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