feat: widgets and ai chat

This commit is contained in:
Rotimi Best
2026-04-27 11:40:38 +01:00
parent ae1801f85f
commit 5717dd8004
391 changed files with 28177 additions and 22467 deletions
@@ -367,6 +367,33 @@ export const getUserOrgRole = async (orgId: string, profileId: string): Promise<
return result.length > 0 ? Number(result[0].roleId) : null;
};
/**
* Gets all org memberships for a user as { [orgId]: roleId }.
* Used to attach org roles to the Better Auth session so middleware can
* read membership/role from the session cookie cache instead of hitting the DB.
*/
export const getUserOrgRolesMap = async (profileId: string): Promise<Record<string, number>> => {
try {
const rows = await db
.select({
organizationId: schema.organizationmember.organizationId,
roleId: schema.organizationmember.roleId
})
.from(schema.organizationmember)
.where(eq(schema.organizationmember.profileId, profileId));
const map: Record<string, number> = {};
for (const row of rows) {
map[row.organizationId] = Number(row.roleId);
}
return map;
} catch (error) {
console.error('getUserOrgRolesMap error:', error);
throw new Error('Failed to fetch user org roles map');
}
};
/**
* Checks if a user is a team member (ADMIN or TUTOR) of an organization
* @param orgId Organization ID