From 759eeb298cb6f5fea0b83e18453f689c36286678 Mon Sep 17 00:00:00 2001 From: Mayank Mishra Date: Thu, 4 Dec 2025 08:33:21 +0530 Subject: [PATCH] Update sorting logic for categories and subcategories to ensure proper order in responses. --- src/modules/host/services/host.service.ts | 91 ++++++++++------------- 1 file changed, 40 insertions(+), 51 deletions(-) diff --git a/src/modules/host/services/host.service.ts b/src/modules/host/services/host.service.ts index 909dbc2..9584285 100644 --- a/src/modules/host/services/host.service.ts +++ b/src/modules/host/services/host.service.ts @@ -1541,29 +1541,6 @@ export class HostService { ) { return await this.prisma.$transaction(async (tx) => { - // -------------- TYPES FIXED HERE ------------------- - type GroupedCategory = { - id: number; - categoryName: string; - displayOrder: number; - pqqsubCategories: { - id: number; - subCategoryName: string; - displayOrder: number; - questions: { - id: number; - questionName: string; - maxPoints: number; - displayOrder: number; - PQQAnswers: any[]; - suggestions: any[]; - supportings: any[]; - }[]; - }[]; - }; - - // --------------------------------------------------- - const host = await tx.hostHeader.findFirst({ where: { userXid: userId, isActive: true }, }); @@ -1644,8 +1621,13 @@ export class HostService { }); const pqqHeaderData = await tx.activityPQQheader.findMany({ - where: { activityXid: created.id, isActive: true }, + where: { + activityXid: created.id, + isActive: true, + }, select: { + comments: true, + pqqAnswerXid: true, pqqQuestions: { select: { id: true, @@ -1661,20 +1643,24 @@ export class HostService { select: { id: true, categoryName: true, - displayOrder: true, - }, - }, - }, + displayOrder: true + } + } + } }, - }, - }, - pqqAnswers: { - select: { - id: true, - answerName: true, - answerPoints: true, - displayOrder: true, - }, + + // 🔥 ALL ANSWER OPTIONS FOR THIS QUESTION + PQQAnswers: { + where: { isActive: true }, + select: { + id: true, + answerName: true, + answerPoints: true, + displayOrder: true + }, + orderBy: { displayOrder: "asc" } + } + } }, ActivityPQQSuggestions: { where: { isActive: true }, @@ -1682,46 +1668,48 @@ export class HostService { id: true, title: true, comments: true, - }, + activityPqqHeaderXid: true + } }, ActivityPQQSupportings: { where: { isActive: true }, select: { id: true, mediaType: true, - mediaFileName: true, - }, + mediaFileName: true + } }, }, - orderBy: { id: "asc" }, + orderBy: { id: "asc" } }); // ---------------- GROUPING ------------------ - const grouped: Record = {}; + const grouped: any = {}; for (const item of pqqHeaderData) { const q = item.pqqQuestions; const sub = q.pqqSubCategories; const cat = sub.category; + // 1️⃣ Category level if (!grouped[cat.id]) { grouped[cat.id] = { id: cat.id, categoryName: cat.categoryName, displayOrder: cat.displayOrder, - pqqsubCategories: [], + pqqsubCategories: [] }; } const category = grouped[cat.id]; - let subCat = category.pqqsubCategories.find((x) => x.id === sub.id); + let subCat = category.pqqsubCategories.find((s: any) => s.id === sub.id); if (!subCat) { subCat = { id: sub.id, subCategoryName: sub.subCategoryName, displayOrder: sub.displayOrder, - questions: [], + questions: [] }; category.pqqsubCategories.push(subCat); } @@ -1730,21 +1718,22 @@ export class HostService { id: q.id, questionName: q.questionName, maxPoints: q.maxPoints, + comments: item.comments || null, displayOrder: q.displayOrder, - PQQAnswers: item.pqqAnswers ? [item.pqqAnswers] : [], + allAnswerOptions: q.PQQAnswers || [], suggestions: item.ActivityPQQSuggestions || [], supportings: item.ActivityPQQSupportings || [], }); } - const sortedCategories = Object.values(grouped) as GroupedCategory[]; + const sortedCategories: any = Object.values(grouped) + .sort((a: any, b: any) => a.displayOrder - b.displayOrder); - // SORT - sortedCategories.sort((a, b) => a.displayOrder - b.displayOrder); for (const cat of sortedCategories) { - cat.pqqsubCategories.sort((a, b) => a.displayOrder - b.displayOrder); + cat.pqqsubCategories.sort((a: any, b: any) => a.displayOrder - b.displayOrder); + for (const sub of cat.pqqsubCategories) { - sub.questions.sort((a, b) => a.displayOrder - b.displayOrder); + sub.questions.sort((a: any, b: any) => a.displayOrder - b.displayOrder); } }