Skip to content
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
19 changes: 12 additions & 7 deletions src/user/user.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3506,7 +3506,7 @@ export class UserService {
// Enhanced query to fetch batch, parent cohort, and academic year details
const cohortQuery = `
WITH BatchData AS (
SELECT
SELECT
cm."cohortId" as "batchId",
cm."createdAt" as "joinedAt",
cm."status" as "cohortMemberStatus",
Expand All @@ -3518,17 +3518,22 @@ export class UserService {
batch."parentId" as "cohortId"
FROM public."CohortMembers" cm
JOIN public."Cohort" batch ON cm."cohortId" = batch."cohortId"
WHERE cm."userId" = $1 AND batch."type" = 'BATCH'
WHERE cm."userId" = $1 AND batch."type" IN ('BATCH', 'COHORT')
)
SELECT
SELECT
bd.*,
cohort."name" as "cohortName",
cohort."type" as "cohortType",
COALESCE(
parent_cohort."name",
CASE WHEN bd."batchType" = 'COHORT' THEN bd."batchName" END
) as "cohortName",
COALESCE(parent_cohort."type", CASE WHEN bd."batchType" = 'COHORT' THEN 'COHORT' END) as "cohortType",
cay."academicYearId",
ay."session" as "academicYearSession"
FROM BatchData bd
LEFT JOIN public."Cohort" cohort ON bd."cohortId":: UUID = cohort."cohortId" AND cohort."type" = 'COHORT'
LEFT JOIN public."CohortAcademicYear" cay ON bd."cohortId":: UUID = cay."cohortId"
LEFT JOIN public."Cohort" parent_cohort ON bd."cohortId"::uuid = parent_cohort."cohortId" AND parent_cohort."type" = 'COHORT'
LEFT JOIN public."CohortAcademicYear" cay ON (
CASE WHEN bd."batchType" = 'BATCH' THEN bd."cohortId"::uuid ELSE bd."batchId"::uuid END
) = cay."cohortId"
LEFT JOIN public."AcademicYears" ay ON cay."academicYearId" = ay."id"
Comment on lines 3518 to 3537
Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

high

The current logic for determining cohortName, cohortType, and the join for CohortAcademicYear is inconsistent for users directly assigned to a COHORT (Center). While the cay join correctly uses the cohort's own ID when the type is 'COHORT', the COALESCE logic for cohortName and cohortType still prefers the parent cohort if one exists (e.g., a District). This results in the Kafka message reporting the parent's name as the cohort name, while using the cohort's own academic year.

A cleaner and more consistent approach is to resolve the "Center-level" ID in the BatchData CTE. By setting cohortId to the parent ID for batches and the cohort's own ID for cohorts, all subsequent joins and selections can be simplified and will remain consistent.

Suggested change
batch."parentId" as "cohortId"
FROM public."CohortMembers" cm
JOIN public."Cohort" batch ON cm."cohortId" = batch."cohortId"
WHERE cm."userId" = $1 AND batch."type" = 'BATCH'
WHERE cm."userId" = $1 AND batch."type" IN ('BATCH', 'COHORT')
)
SELECT
SELECT
bd.*,
cohort."name" as "cohortName",
cohort."type" as "cohortType",
COALESCE(
parent_cohort."name",
CASE WHEN bd."batchType" = 'COHORT' THEN bd."batchName" END
) as "cohortName",
COALESCE(parent_cohort."type", CASE WHEN bd."batchType" = 'COHORT' THEN 'COHORT' END) as "cohortType",
cay."academicYearId",
ay."session" as "academicYearSession"
FROM BatchData bd
LEFT JOIN public."Cohort" cohort ON bd."cohortId":: UUID = cohort."cohortId" AND cohort."type" = 'COHORT'
LEFT JOIN public."CohortAcademicYear" cay ON bd."cohortId":: UUID = cay."cohortId"
LEFT JOIN public."Cohort" parent_cohort ON bd."cohortId"::uuid = parent_cohort."cohortId" AND parent_cohort."type" = 'COHORT'
LEFT JOIN public."CohortAcademicYear" cay ON (
CASE WHEN bd."batchType" = 'BATCH' THEN bd."cohortId"::uuid ELSE bd."batchId"::uuid END
) = cay."cohortId"
LEFT JOIN public."AcademicYears" ay ON cay."academicYearId" = ay."id"
CASE WHEN batch."type" = 'BATCH' THEN batch."parentId" ELSE batch."cohortId"::text END as "cohortId"
FROM public."CohortMembers" cm
JOIN public."Cohort" batch ON cm."cohortId" = batch."cohortId"
WHERE cm."userId" = $1 AND batch."type" IN ('BATCH', 'COHORT')
)
SELECT
bd.*,
parent_cohort."name" as "cohortName",
parent_cohort."type" as "cohortType",
cay."academicYearId",
ay."session" as "academicYearSession"
FROM BatchData bd
LEFT JOIN public."Cohort" parent_cohort ON bd."cohortId"::uuid = parent_cohort."cohortId" AND parent_cohort."type" = 'COHORT'
LEFT JOIN public."CohortAcademicYear" cay ON bd."cohortId"::uuid = cay."cohortId"
LEFT JOIN public."AcademicYears" ay ON cay."academicYearId" = ay."id"

`;

Expand Down
Loading