Optimize database calls across multiple components by implementing Promise.all for parallel fetching of data, enhancing performance and reducing loading times.
All checks were successful
Deploy with Docker Compose / deploy (push) Successful in 2m40s
All checks were successful
Deploy with Docker Compose / deploy (push) Successful in 2m40s
This commit is contained in:
@@ -165,11 +165,15 @@ export class ChallengeService {
|
||||
winnerId: string,
|
||||
adminComment?: string
|
||||
): Promise<Challenge> {
|
||||
// Récupérer uniquement les champs nécessaires pour la validation
|
||||
const challenge = await prisma.challenge.findUnique({
|
||||
where: { id: challengeId },
|
||||
include: {
|
||||
challenger: true,
|
||||
challenged: true,
|
||||
select: {
|
||||
id: true,
|
||||
status: true,
|
||||
challengerId: true,
|
||||
challengedId: true,
|
||||
pointsReward: true,
|
||||
},
|
||||
});
|
||||
|
||||
@@ -194,32 +198,49 @@ export class ChallengeService {
|
||||
);
|
||||
}
|
||||
|
||||
// Mettre à jour le défi
|
||||
const updatedChallenge = await prisma.challenge.update({
|
||||
where: { id: challengeId },
|
||||
data: {
|
||||
status: "COMPLETED",
|
||||
adminId,
|
||||
adminComment: adminComment || null,
|
||||
winnerId,
|
||||
completedAt: new Date(),
|
||||
},
|
||||
include: {
|
||||
challenger: true,
|
||||
challenged: true,
|
||||
winner: true,
|
||||
},
|
||||
});
|
||||
|
||||
// Attribuer les points au gagnant
|
||||
await prisma.user.update({
|
||||
where: { id: winnerId },
|
||||
data: {
|
||||
score: {
|
||||
increment: challenge.pointsReward,
|
||||
// Paralléliser la mise à jour du défi et l'attribution des points
|
||||
const [updatedChallenge] = await Promise.all([
|
||||
prisma.challenge.update({
|
||||
where: { id: challengeId },
|
||||
data: {
|
||||
status: "COMPLETED",
|
||||
adminId,
|
||||
adminComment: adminComment || null,
|
||||
winnerId,
|
||||
completedAt: new Date(),
|
||||
},
|
||||
},
|
||||
});
|
||||
include: {
|
||||
challenger: {
|
||||
select: {
|
||||
id: true,
|
||||
username: true,
|
||||
avatar: true,
|
||||
},
|
||||
},
|
||||
challenged: {
|
||||
select: {
|
||||
id: true,
|
||||
username: true,
|
||||
avatar: true,
|
||||
},
|
||||
},
|
||||
winner: {
|
||||
select: {
|
||||
id: true,
|
||||
username: true,
|
||||
},
|
||||
},
|
||||
},
|
||||
}),
|
||||
prisma.user.update({
|
||||
where: { id: winnerId },
|
||||
data: {
|
||||
score: {
|
||||
increment: challenge.pointsReward,
|
||||
},
|
||||
},
|
||||
}),
|
||||
]);
|
||||
|
||||
return updatedChallenge;
|
||||
}
|
||||
@@ -264,14 +285,6 @@ export class ChallengeService {
|
||||
challengeId: string,
|
||||
data: UpdateChallengeInput
|
||||
): Promise<Challenge> {
|
||||
const challenge = await prisma.challenge.findUnique({
|
||||
where: { id: challengeId },
|
||||
});
|
||||
|
||||
if (!challenge) {
|
||||
throw new NotFoundError("Défi");
|
||||
}
|
||||
|
||||
const updateData: Prisma.ChallengeUpdateInput = {};
|
||||
|
||||
if (data.title !== undefined) {
|
||||
@@ -300,18 +313,31 @@ export class ChallengeService {
|
||||
: { disconnect: true };
|
||||
}
|
||||
|
||||
return prisma.challenge.update({
|
||||
where: { id: challengeId },
|
||||
data: updateData,
|
||||
});
|
||||
try {
|
||||
return await prisma.challenge.update({
|
||||
where: { id: challengeId },
|
||||
data: updateData,
|
||||
});
|
||||
} catch (error: any) {
|
||||
if (error?.code === "P2025") {
|
||||
// Record not found
|
||||
throw new NotFoundError("Défi");
|
||||
}
|
||||
throw error;
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Annule un défi (admin seulement)
|
||||
*/
|
||||
async adminCancelChallenge(challengeId: string): Promise<Challenge> {
|
||||
// Récupérer uniquement le statut pour la validation
|
||||
const challenge = await prisma.challenge.findUnique({
|
||||
where: { id: challengeId },
|
||||
select: {
|
||||
id: true,
|
||||
status: true,
|
||||
},
|
||||
});
|
||||
|
||||
if (!challenge) {
|
||||
@@ -336,8 +362,14 @@ export class ChallengeService {
|
||||
* Remet le défi en PENDING s'il n'avait jamais été accepté, sinon en ACCEPTED
|
||||
*/
|
||||
async reactivateChallenge(challengeId: string): Promise<Challenge> {
|
||||
// Récupérer uniquement les champs nécessaires
|
||||
const challenge = await prisma.challenge.findUnique({
|
||||
where: { id: challengeId },
|
||||
select: {
|
||||
id: true,
|
||||
status: true,
|
||||
acceptedAt: true,
|
||||
},
|
||||
});
|
||||
|
||||
if (!challenge) {
|
||||
@@ -367,17 +399,17 @@ export class ChallengeService {
|
||||
* Supprime un défi (admin seulement)
|
||||
*/
|
||||
async deleteChallenge(challengeId: string): Promise<void> {
|
||||
const challenge = await prisma.challenge.findUnique({
|
||||
where: { id: challengeId },
|
||||
});
|
||||
|
||||
if (!challenge) {
|
||||
throw new NotFoundError("Défi");
|
||||
try {
|
||||
await prisma.challenge.delete({
|
||||
where: { id: challengeId },
|
||||
});
|
||||
} catch (error: any) {
|
||||
if (error?.code === "P2025") {
|
||||
// Record not found
|
||||
throw new NotFoundError("Défi");
|
||||
}
|
||||
throw error;
|
||||
}
|
||||
|
||||
await prisma.challenge.delete({
|
||||
where: { id: challengeId },
|
||||
});
|
||||
}
|
||||
|
||||
/**
|
||||
|
||||
Reference in New Issue
Block a user