feat(cohorts): rename Programs to Cohorts with data migration (#694)

* feat(cohorts): rename Programs to Cohorts with data migration

- Add cohort database tables and Drizzle migration alongside legacy program tables
- Introduce migrate-programs-to-cohorts script to copy existing program data safely
- Rename API routes (/cohort), services, queries, validation, and error codes
- Rename dashboard feature module, routes (/cohorts), navigation, and translations
- Update docs, emails, and internal compliance cron endpoints for cohort terminology
- Keep legacy program tables in place for rollback until explicitly removed

Co-authored-by: Rotimi Best <rotimi-best@users.noreply.github.com>

* fix(db): restore migrate:answer-data script alongside cohort migration

Co-authored-by: Rotimi Best <rotimi-best@users.noreply.github.com>

* fix(db): cast program enums when migrating data to cohort tables

Co-authored-by: Rotimi Best <rotimi-best@users.noreply.github.com>

* fix(db): truncate cohort tables before program-to-cohort copy

Co-authored-by: Rotimi Best <rotimi-best@users.noreply.github.com>

* fix(db): report accurate row counts in programs-to-cohorts migration

Co-authored-by: Rotimi Best <rotimi-best@users.noreply.github.com>

---------

Co-authored-by: Cursor Agent <cursoragent@cursor.com>
Co-authored-by: Rotimi Best <rotimi-best@users.noreply.github.com>
This commit is contained in:
Rotimi Best
2026-07-02 12:10:31 +01:00
committed by GitHub
co-authored by Rotimi Best Cursor Agent
parent fe9f8c441f
commit ec61bfe7d9
115 changed files with 19703 additions and 6046 deletions
@@ -1,13 +1,13 @@
import { Context, Next } from 'hono';
import { ErrorCodes } from '@api/utils/errors';
import { isOrgAdminByProgramId, isProgramMember } from '@cio/db/queries/program';
import { isOrgAdminByCohortId, isCohortMember } from '@cio/db/queries/cohort';
/**
* Middleware to check if the authenticated user is a member of the program
* or an org admin for the program's organization.
* Middleware to check if the authenticated user is a member of the cohort
* or an org admin for the cohort's organization.
*/
export const programMemberMiddleware = async (c: Context, next: Next) => {
export const cohortMemberMiddleware = async (c: Context, next: Next) => {
try {
const user = c.get('user');
if (!user) {
@@ -21,21 +21,21 @@ export const programMemberMiddleware = async (c: Context, next: Next) => {
);
}
const programId = c.req.param('programId');
if (!programId) {
const cohortId = c.req.param('cohortId');
if (!cohortId) {
return c.json(
{
success: false,
error: 'Program ID is required',
code: 'PROGRAM_ID_REQUIRED'
code: 'COHORT_ID_REQUIRED'
},
400
);
}
const [member, isOrgAdmin] = await Promise.all([
isProgramMember(programId, user.id),
isOrgAdminByProgramId(programId, user.id)
isCohortMember(cohortId, user.id),
isOrgAdminByCohortId(cohortId, user.id)
]);
if (member || isOrgAdmin) {
@@ -45,18 +45,18 @@ export const programMemberMiddleware = async (c: Context, next: Next) => {
return c.json(
{
success: false,
error: 'You must be a member of this program to perform this action',
code: ErrorCodes.PROGRAM_FORBIDDEN
error: 'You must be a member of this cohort to perform this action',
code: ErrorCodes.COHORT_FORBIDDEN
},
403
);
} catch (error) {
console.error('Error in programMemberMiddleware:', error);
console.error('Error in cohortMemberMiddleware:', error);
return c.json(
{
success: false,
error: 'Failed to verify program membership',
code: ErrorCodes.PROGRAM_MEMBER_CHECK_FAILED
error: 'Failed to verify cohort membership',
code: ErrorCodes.COHORT_MEMBER_CHECK_FAILED
},
500
);
@@ -1,15 +1,15 @@
import { Context, Next } from 'hono';
import { ErrorCodes } from '@api/utils/errors';
import {
getProgramMemberByProfileId,
getProgramMemberRole,
getProgramNewsfeedById,
getProgramNewsfeedCommentById,
isOrgAdminByProgramId
} from '@cio/db/queries/program';
getCohortMemberByProfileId,
getCohortMemberRole,
getCohortNewsfeedById,
getCohortNewsfeedCommentById,
isOrgAdminByCohortId
} from '@cio/db/queries/cohort';
import { ROLE } from '@cio/utils/constants';
export const programNewsfeedCommentAuthorOrTeamMiddleware = async (c: Context, next: Next) => {
export const cohortNewsfeedCommentAuthorOrTeamMiddleware = async (c: Context, next: Next) => {
try {
const user = c.get('user');
if (!user) {
@@ -35,44 +35,44 @@ export const programNewsfeedCommentAuthorOrTeamMiddleware = async (c: Context, n
);
}
const comment = await getProgramNewsfeedCommentById(Number(commentId));
if (!comment?.programNewsfeedId) {
const comment = await getCohortNewsfeedCommentById(Number(commentId));
if (!comment?.cohortNewsfeedId) {
return c.json(
{
success: false,
error: 'Comment not found',
code: ErrorCodes.PROGRAM_NEWSFEED_COMMENT_NOT_FOUND
code: ErrorCodes.COHORT_NEWSFEED_COMMENT_NOT_FOUND
},
404
);
}
const feed = await getProgramNewsfeedById(comment.programNewsfeedId);
if (!feed?.programId) {
const feed = await getCohortNewsfeedById(comment.cohortNewsfeedId);
if (!feed?.cohortId) {
return c.json(
{
success: false,
error: 'Program newsfeed item not found',
code: ErrorCodes.PROGRAM_NEWSFEED_NOT_FOUND
error: 'Cohort newsfeed item not found',
code: ErrorCodes.COHORT_NEWSFEED_NOT_FOUND
},
404
);
}
const member = await getProgramMemberByProfileId(feed.programId, user.id);
const member = await getCohortMemberByProfileId(feed.cohortId, user.id);
const isAuthor = comment.authorId === member?.id;
if (isAuthor) {
await next();
return;
}
const isOrgAdmin = await isOrgAdminByProgramId(feed.programId, user.id);
const isOrgAdmin = await isOrgAdminByCohortId(feed.cohortId, user.id);
if (isOrgAdmin) {
await next();
return;
}
const roleId = await getProgramMemberRole(feed.programId, user.id);
const roleId = await getCohortMemberRole(feed.cohortId, user.id);
const isTeamMember = roleId === ROLE.ADMIN || roleId === ROLE.TUTOR;
if (isTeamMember) {
await next();
@@ -88,7 +88,7 @@ export const programNewsfeedCommentAuthorOrTeamMiddleware = async (c: Context, n
403
);
} catch (error) {
console.error('Error in programNewsfeedCommentAuthorOrTeamMiddleware:', error);
console.error('Error in cohortNewsfeedCommentAuthorOrTeamMiddleware:', error);
return c.json(
{
success: false,
@@ -1,14 +1,14 @@
import { Context, Next } from 'hono';
import { ErrorCodes } from '@api/utils/errors';
import { getProgramMemberRole, isOrgAdminByProgramId } from '@cio/db/queries/program';
import { getCohortMemberRole, isOrgAdminByCohortId } from '@cio/db/queries/cohort';
import { ROLE } from '@cio/utils/constants';
/**
* Middleware to check if the authenticated user is a program teacher or org admin.
* Middleware to check if the authenticated user is a cohort teacher or org admin.
* Mirrors the course-team-member pattern for program routes.
*/
export const programTeamMemberMiddleware = async (c: Context, next: Next) => {
export const cohortTeamMemberMiddleware = async (c: Context, next: Next) => {
try {
const user = c.get('user');
if (!user) {
@@ -22,21 +22,21 @@ export const programTeamMemberMiddleware = async (c: Context, next: Next) => {
);
}
const programId = c.req.param('programId');
if (!programId) {
const cohortId = c.req.param('cohortId');
if (!cohortId) {
return c.json(
{
success: false,
error: 'Program ID is required',
code: 'PROGRAM_ID_REQUIRED'
code: 'COHORT_ID_REQUIRED'
},
400
);
}
const [roleId, isOrgAdmin] = await Promise.all([
getProgramMemberRole(programId, user.id),
isOrgAdminByProgramId(programId, user.id)
getCohortMemberRole(cohortId, user.id),
isOrgAdminByCohortId(cohortId, user.id)
]);
if (roleId === ROLE.TUTOR || roleId === ROLE.ADMIN || isOrgAdmin) {
@@ -52,12 +52,12 @@ export const programTeamMemberMiddleware = async (c: Context, next: Next) => {
403
);
} catch (error) {
console.error('Error in programTeamMemberMiddleware:', error);
console.error('Error in cohortTeamMemberMiddleware:', error);
return c.json(
{
success: false,
error: 'Failed to verify program team membership',
code: 'PROGRAM_TEAM_MEMBER_CHECK_FAILED'
code: 'COHORT_TEAM_MEMBER_CHECK_FAILED'
},
500
);