* 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 <[email protected]> * fix(db): restore migrate:answer-data script alongside cohort migration Co-authored-by: Rotimi Best <[email protected]> * fix(db): cast program enums when migrating data to cohort tables Co-authored-by: Rotimi Best <[email protected]> * fix(db): truncate cohort tables before program-to-cohort copy Co-authored-by: Rotimi Best <[email protected]> * fix(db): report accurate row counts in programs-to-cohorts migration Co-authored-by: Rotimi Best <[email protected]> --------- Co-authored-by: Cursor Agent <[email protected]> Co-authored-by: Rotimi Best <[email protected]>
65 lines
1.5 KiB
TypeScript
65 lines
1.5 KiB
TypeScript
import { Context, Next } from 'hono';
|
|
|
|
import { ErrorCodes } from '@api/utils/errors';
|
|
import { isOrgAdminByCohortId, isCohortMember } from '@cio/db/queries/cohort';
|
|
|
|
/**
|
|
* Middleware to check if the authenticated user is a member of the cohort
|
|
* or an org admin for the cohort's organization.
|
|
*/
|
|
export const cohortMemberMiddleware = async (c: Context, next: Next) => {
|
|
try {
|
|
const user = c.get('user');
|
|
if (!user) {
|
|
return c.json(
|
|
{
|
|
success: false,
|
|
error: 'Unauthorized',
|
|
code: 'UNAUTHORIZED'
|
|
},
|
|
401
|
|
);
|
|
}
|
|
|
|
const cohortId = c.req.param('cohortId');
|
|
if (!cohortId) {
|
|
return c.json(
|
|
{
|
|
success: false,
|
|
error: 'Program ID is required',
|
|
code: 'COHORT_ID_REQUIRED'
|
|
},
|
|
400
|
|
);
|
|
}
|
|
|
|
const [member, isOrgAdmin] = await Promise.all([
|
|
isCohortMember(cohortId, user.id),
|
|
isOrgAdminByCohortId(cohortId, user.id)
|
|
]);
|
|
|
|
if (member || isOrgAdmin) {
|
|
return next();
|
|
}
|
|
|
|
return c.json(
|
|
{
|
|
success: false,
|
|
error: 'You must be a member of this cohort to perform this action',
|
|
code: ErrorCodes.COHORT_FORBIDDEN
|
|
},
|
|
403
|
|
);
|
|
} catch (error) {
|
|
console.error('Error in cohortMemberMiddleware:', error);
|
|
return c.json(
|
|
{
|
|
success: false,
|
|
error: 'Failed to verify cohort membership',
|
|
code: ErrorCodes.COHORT_MEMBER_CHECK_FAILED
|
|
},
|
|
500
|
|
);
|
|
}
|
|
};
|