Skip to content

Kakfa Fix for Center and User Data#727

Merged
Shubham4026 merged 2 commits into
mainfrom
sdbv_rbac_changes
Apr 24, 2026
Merged

Kakfa Fix for Center and User Data#727
Shubham4026 merged 2 commits into
mainfrom
sdbv_rbac_changes

Conversation

@Shubham4026
Copy link
Copy Markdown
Collaborator

@Shubham4026 Shubham4026 commented Apr 24, 2026

Summary by CodeRabbit

  • Bug Fixes
    • Enhanced cohort and batch metadata retrieval to correctly handle both record types when fetching academic year and cohort information. The system now properly maps parent cohort relationships for more accurate data lookups.

@Shubham4026 Shubham4026 merged commit df8e7bc into main Apr 24, 2026
0 of 2 checks passed
@coderabbitai
Copy link
Copy Markdown

coderabbitai Bot commented Apr 24, 2026

Caution

Review failed

The pull request is closed.

ℹ️ Recent review info
⚙️ Run configuration

Configuration used: Path: .coderabbit.yaml

Review profile: CHILL

Plan: Pro

Run ID: 787d50da-537e-4191-aaed-73f5905c42cb

📥 Commits

Reviewing files that changed from the base of the PR and between 7320fbf and 406ff27.

📒 Files selected for processing (1)
  • src/user/user.service.ts

Cache: Disabled due to data retention organization setting

Knowledge base: Disabled due to data retention organization setting


Walkthrough

Updated the Kafka user-event cohort query in UserService to retrieve academic-year and cohort metadata for both BATCH and COHORT type rows. Modified the type filter, cohort name/type derivation via COALESCE, parent cohort joins, and conditional CohortAcademicYear join logic.

Changes

Cohort / File(s) Summary
Kafka Cohort Query Logic
src/user/user.service.ts
Updated internal SQL query to support BATCH and COHORT row types. Modified type filter, added conditional COALESCE expressions for cohort name/type derivation, adjusted parent cohort join targets, and made CohortAcademicYear join key conditional based on row type.

Estimated code review effort

🎯 3 (Moderate) | ⏱️ ~20 minutes

✨ Finishing Touches
📝 Generate docstrings
  • Create stacked PR
  • Commit on current branch
🧪 Generate unit tests (beta)
  • Create PR with unit tests
  • Commit unit tests in branch sdbv_rbac_changes

Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out.

❤️ Share

Comment @coderabbitai help to get the list of available commands and usage tips.

@sonarqubecloud
Copy link
Copy Markdown

Copy link
Copy Markdown

@gemini-code-assist gemini-code-assist Bot left a comment

Choose a reason for hiding this comment

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

Code Review

This pull request updates the cohortQuery in UserService to include users directly assigned to cohorts in addition to those in batches. The changes introduce conditional logic for selecting cohort names and joining academic years. Feedback suggests that the current implementation for resolving cohortName and cohortType is inconsistent when a parent cohort exists. A suggestion was provided to simplify the query by resolving the appropriate ID within the Common Table Expression (CTE) to ensure consistent joins and data selection.

Comment thread src/user/user.service.ts
Comment on lines 3518 to 3537
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"
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"

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant