2025-11-24 01:00:50 +01:00
|
|
|
import {
|
|
|
|
|
ZCancelOrgPlan,
|
|
|
|
|
ZCreateOrgPlan,
|
2025-12-02 09:54:38 +01:00
|
|
|
ZCreateOrganization,
|
2025-11-24 01:00:50 +01:00
|
|
|
ZGetCoursesBySiteName,
|
2025-12-02 09:54:38 +01:00
|
|
|
ZGetOrgSetup,
|
2025-11-24 01:00:50 +01:00
|
|
|
ZGetOrganizationAudience,
|
|
|
|
|
ZGetOrganizationTeam,
|
|
|
|
|
ZGetOrganizations,
|
2025-12-02 09:54:38 +01:00
|
|
|
ZInviteTeamMembers,
|
|
|
|
|
ZRemoveTeamMember,
|
|
|
|
|
ZUpdateOrgPlan,
|
|
|
|
|
ZUpdateOrganization,
|
|
|
|
|
ZUpdateOrganizationParam
|
2025-11-24 01:00:50 +01:00
|
|
|
} from '@cio/utils/validation/organization';
|
|
|
|
|
import {
|
|
|
|
|
cancelOrgPlan,
|
2025-12-02 09:54:38 +01:00
|
|
|
createOrg,
|
2025-11-24 01:00:50 +01:00
|
|
|
createOrgPlan,
|
|
|
|
|
getCoursesByOrgSiteName,
|
|
|
|
|
getOrgAudience,
|
2025-12-02 09:54:38 +01:00
|
|
|
getOrgSetupData,
|
2025-11-24 01:00:50 +01:00
|
|
|
getOrgTeam,
|
|
|
|
|
getOrganizationsWithFilters,
|
2025-12-02 09:54:38 +01:00
|
|
|
inviteTeamMembers,
|
|
|
|
|
removeTeamMember,
|
|
|
|
|
updateOrg,
|
2025-11-24 01:00:50 +01:00
|
|
|
updateOrgPlan
|
|
|
|
|
} from '@api/services/organization';
|
|
|
|
|
|
|
|
|
|
import { Hono } from '@api/utils/hono';
|
2025-12-02 09:54:38 +01:00
|
|
|
import { TOrganization } from '@db/types';
|
2025-11-24 01:00:50 +01:00
|
|
|
import { authMiddleware } from '@api/middlewares/auth';
|
2025-12-02 09:54:38 +01:00
|
|
|
import { authOrApiKeyMiddleware } from '@api/middlewares/auth-or-api-key';
|
2025-11-24 01:00:50 +01:00
|
|
|
import { handleError } from '@api/utils/errors';
|
2025-12-02 09:54:38 +01:00
|
|
|
import { orgAdminMiddleware } from '@api/middlewares/org-admin';
|
2025-11-24 01:00:50 +01:00
|
|
|
import { zValidator } from '@hono/zod-validator';
|
|
|
|
|
|
|
|
|
|
export const organizationRouter = new Hono()
|
|
|
|
|
/**
|
|
|
|
|
* GET /organization
|
|
|
|
|
* Gets organizations with optional filters
|
|
|
|
|
* Query params: siteName (string, optional), customDomain (string, optional), isCustomDomainVerified (boolean, optional)
|
2025-12-02 09:54:38 +01:00
|
|
|
* Accepts: User session OR API key (for server-side routes)
|
2025-11-24 01:00:50 +01:00
|
|
|
*/
|
2025-12-02 09:54:38 +01:00
|
|
|
.get('/', authOrApiKeyMiddleware, zValidator('query', ZGetOrganizations), async (c) => {
|
2025-11-24 01:00:50 +01:00
|
|
|
try {
|
|
|
|
|
const filters = c.req.valid('query');
|
2025-12-02 09:54:38 +01:00
|
|
|
console.log('filters', filters);
|
2025-11-24 01:00:50 +01:00
|
|
|
const organizations = await getOrganizationsWithFilters(filters);
|
|
|
|
|
|
|
|
|
|
return c.json(
|
|
|
|
|
{
|
|
|
|
|
success: true,
|
|
|
|
|
data: organizations
|
|
|
|
|
},
|
|
|
|
|
200
|
|
|
|
|
);
|
|
|
|
|
} catch (error) {
|
2025-12-02 09:54:38 +01:00
|
|
|
console.error('Error fetching organizations:', error);
|
2025-11-24 01:00:50 +01:00
|
|
|
return handleError(c, error, 'Failed to fetch organizations');
|
|
|
|
|
}
|
|
|
|
|
})
|
|
|
|
|
/**
|
|
|
|
|
* GET /organization/:orgId/team
|
|
|
|
|
* Gets organization team members (non-students)
|
|
|
|
|
* Requires authentication
|
|
|
|
|
*/
|
|
|
|
|
.get('/:orgId/team', authMiddleware, zValidator('param', ZGetOrganizationTeam), async (c) => {
|
|
|
|
|
try {
|
|
|
|
|
const { orgId } = c.req.valid('param');
|
|
|
|
|
const team = await getOrgTeam(orgId);
|
|
|
|
|
|
|
|
|
|
return c.json(
|
|
|
|
|
{
|
|
|
|
|
success: true,
|
|
|
|
|
data: team
|
|
|
|
|
},
|
|
|
|
|
200
|
|
|
|
|
);
|
|
|
|
|
} catch (error) {
|
|
|
|
|
return handleError(c, error, 'Failed to fetch organization team');
|
|
|
|
|
}
|
|
|
|
|
})
|
2025-12-02 09:54:38 +01:00
|
|
|
/**
|
|
|
|
|
* POST /organization/:orgId/team/invite
|
|
|
|
|
* Invites team members to an organization
|
|
|
|
|
* Requires authentication and admin role
|
|
|
|
|
*/
|
|
|
|
|
.post(
|
|
|
|
|
'/:orgId/team/invite',
|
|
|
|
|
authMiddleware,
|
|
|
|
|
orgAdminMiddleware,
|
|
|
|
|
zValidator('param', ZGetOrganizationTeam),
|
|
|
|
|
zValidator('json', ZInviteTeamMembers),
|
|
|
|
|
async (c) => {
|
|
|
|
|
try {
|
|
|
|
|
const { orgId } = c.req.valid('param');
|
|
|
|
|
const { emails, roleId } = c.req.valid('json');
|
|
|
|
|
|
|
|
|
|
const members = await inviteTeamMembers(orgId, emails, roleId);
|
|
|
|
|
|
|
|
|
|
return c.json(
|
|
|
|
|
{
|
|
|
|
|
success: true,
|
|
|
|
|
data: members
|
|
|
|
|
},
|
|
|
|
|
201
|
|
|
|
|
);
|
|
|
|
|
} catch (error) {
|
|
|
|
|
return handleError(c, error, 'Failed to invite team members');
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
)
|
|
|
|
|
/**
|
|
|
|
|
* DELETE /organization/:orgId/team/:memberId
|
|
|
|
|
* Removes a team member from an organization
|
|
|
|
|
* Requires authentication and admin role
|
|
|
|
|
*/
|
|
|
|
|
.delete(
|
|
|
|
|
'/:orgId/team/:memberId',
|
|
|
|
|
authMiddleware,
|
|
|
|
|
orgAdminMiddleware,
|
|
|
|
|
zValidator('param', ZRemoveTeamMember),
|
|
|
|
|
async (c) => {
|
|
|
|
|
try {
|
|
|
|
|
const { orgId, memberId } = c.req.valid('param');
|
|
|
|
|
|
|
|
|
|
await removeTeamMember(orgId, memberId);
|
|
|
|
|
|
|
|
|
|
return c.json(
|
|
|
|
|
{
|
|
|
|
|
success: true
|
|
|
|
|
},
|
|
|
|
|
200
|
|
|
|
|
);
|
|
|
|
|
} catch (error) {
|
|
|
|
|
return handleError(c, error, 'Failed to remove team member');
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
)
|
2025-11-24 01:00:50 +01:00
|
|
|
/**
|
|
|
|
|
* GET /organization/:orgId/audience
|
|
|
|
|
* Gets organization audience (students)
|
|
|
|
|
* Requires authentication
|
|
|
|
|
*/
|
|
|
|
|
.get('/:orgId/audience', authMiddleware, zValidator('param', ZGetOrganizationAudience), async (c) => {
|
|
|
|
|
try {
|
|
|
|
|
const { orgId } = c.req.valid('param');
|
|
|
|
|
const audience = await getOrgAudience(orgId);
|
|
|
|
|
|
|
|
|
|
return c.json(
|
|
|
|
|
{
|
|
|
|
|
success: true,
|
|
|
|
|
data: audience
|
|
|
|
|
},
|
|
|
|
|
200
|
|
|
|
|
);
|
|
|
|
|
} catch (error) {
|
|
|
|
|
return handleError(c, error, 'Failed to fetch organization audience');
|
|
|
|
|
}
|
|
|
|
|
})
|
|
|
|
|
/**
|
|
|
|
|
* GET /organization/courses
|
|
|
|
|
* Gets courses by organization siteName
|
|
|
|
|
* Query params: siteName (string)
|
|
|
|
|
*/
|
2025-12-02 09:54:38 +01:00
|
|
|
.get(
|
|
|
|
|
'/courses',
|
|
|
|
|
// TODO: Ratelimit this endpoint
|
|
|
|
|
// TODO: Add cache
|
|
|
|
|
// TODO: Add pagination
|
|
|
|
|
// TODO: Add sorting
|
|
|
|
|
// TODO: Add filtering
|
|
|
|
|
zValidator('query', ZGetCoursesBySiteName),
|
|
|
|
|
async (c) => {
|
|
|
|
|
try {
|
|
|
|
|
const { siteName } = c.req.valid('query');
|
|
|
|
|
const courses = await getCoursesByOrgSiteName(siteName);
|
|
|
|
|
|
|
|
|
|
return c.json(
|
|
|
|
|
{
|
|
|
|
|
success: true,
|
|
|
|
|
data: courses
|
|
|
|
|
},
|
|
|
|
|
200
|
|
|
|
|
);
|
|
|
|
|
} catch (error) {
|
|
|
|
|
return handleError(c, error, 'Failed to fetch courses');
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
)
|
|
|
|
|
/**
|
|
|
|
|
* GET /organization/setup
|
|
|
|
|
* Gets setup data for an organization (courses, lessons, exercises, org info)
|
|
|
|
|
* Query params: siteName (string)
|
|
|
|
|
*/
|
|
|
|
|
.get('/setup', zValidator('query', ZGetOrgSetup), async (c) => {
|
2025-11-24 01:00:50 +01:00
|
|
|
try {
|
|
|
|
|
const { siteName } = c.req.valid('query');
|
2025-12-02 09:54:38 +01:00
|
|
|
const setupData = await getOrgSetupData(siteName);
|
2025-11-24 01:00:50 +01:00
|
|
|
|
|
|
|
|
return c.json(
|
|
|
|
|
{
|
|
|
|
|
success: true,
|
2025-12-02 09:54:38 +01:00
|
|
|
data: setupData
|
2025-11-24 01:00:50 +01:00
|
|
|
},
|
|
|
|
|
200
|
|
|
|
|
);
|
|
|
|
|
} catch (error) {
|
2025-12-02 09:54:38 +01:00
|
|
|
return handleError(c, error, 'Failed to fetch setup data');
|
2025-11-24 01:00:50 +01:00
|
|
|
}
|
|
|
|
|
})
|
|
|
|
|
/**
|
|
|
|
|
* POST /organization/plan
|
|
|
|
|
* Creates a new organization plan
|
2025-12-02 09:54:38 +01:00
|
|
|
* Requires authentication (user session or API key)
|
2025-11-24 01:00:50 +01:00
|
|
|
*/
|
2025-12-02 09:54:38 +01:00
|
|
|
.post('/plan', authOrApiKeyMiddleware, zValidator('json', ZCreateOrgPlan), async (c) => {
|
2025-11-24 01:00:50 +01:00
|
|
|
try {
|
|
|
|
|
const data = c.req.valid('json');
|
|
|
|
|
const plan = await createOrgPlan(data);
|
|
|
|
|
|
|
|
|
|
return c.json(
|
|
|
|
|
{
|
|
|
|
|
success: true,
|
|
|
|
|
data: plan
|
|
|
|
|
},
|
|
|
|
|
201
|
|
|
|
|
);
|
|
|
|
|
} catch (error) {
|
|
|
|
|
return handleError(c, error, 'Failed to create organization plan');
|
|
|
|
|
}
|
|
|
|
|
})
|
|
|
|
|
/**
|
|
|
|
|
* PUT /organization/plan
|
|
|
|
|
* Updates an organization plan by subscription ID
|
2025-12-02 09:54:38 +01:00
|
|
|
* Requires authentication (user session or API key)
|
2025-11-24 01:00:50 +01:00
|
|
|
*/
|
2025-12-02 09:54:38 +01:00
|
|
|
.put('/plan', authOrApiKeyMiddleware, zValidator('json', ZUpdateOrgPlan), async (c) => {
|
2025-11-24 01:00:50 +01:00
|
|
|
try {
|
|
|
|
|
const { subscriptionId, payload } = c.req.valid('json');
|
|
|
|
|
const plan = await updateOrgPlan(subscriptionId, payload);
|
|
|
|
|
|
|
|
|
|
return c.json(
|
|
|
|
|
{
|
|
|
|
|
success: true,
|
|
|
|
|
data: plan
|
|
|
|
|
},
|
|
|
|
|
200
|
|
|
|
|
);
|
|
|
|
|
} catch (error) {
|
|
|
|
|
return handleError(c, error, 'Failed to update organization plan');
|
|
|
|
|
}
|
|
|
|
|
})
|
|
|
|
|
/**
|
|
|
|
|
* POST /organization/plan/cancel
|
|
|
|
|
* Cancels an organization plan by subscription ID
|
2025-12-02 09:54:38 +01:00
|
|
|
* Requires authentication (user session or API key)
|
2025-11-24 01:00:50 +01:00
|
|
|
*/
|
2025-12-02 09:54:38 +01:00
|
|
|
.post('/plan/cancel', authOrApiKeyMiddleware, zValidator('json', ZCancelOrgPlan), async (c) => {
|
2025-11-24 01:00:50 +01:00
|
|
|
try {
|
|
|
|
|
const { subscriptionId, payload } = c.req.valid('json');
|
|
|
|
|
const plan = await cancelOrgPlan(subscriptionId, payload);
|
|
|
|
|
|
|
|
|
|
return c.json(
|
|
|
|
|
{
|
|
|
|
|
success: true,
|
|
|
|
|
data: plan
|
|
|
|
|
},
|
|
|
|
|
200
|
|
|
|
|
);
|
|
|
|
|
} catch (error) {
|
|
|
|
|
return handleError(c, error, 'Failed to cancel organization plan');
|
|
|
|
|
}
|
2025-12-02 09:54:38 +01:00
|
|
|
})
|
|
|
|
|
/**
|
|
|
|
|
* POST /organization
|
|
|
|
|
* Creates a new organization with the current user as owner
|
|
|
|
|
* Requires authentication
|
|
|
|
|
*/
|
|
|
|
|
.post('/', authMiddleware, zValidator('json', ZCreateOrganization), async (c) => {
|
|
|
|
|
try {
|
|
|
|
|
const user = c.get('user');
|
|
|
|
|
const data = c.req.valid('json');
|
|
|
|
|
|
|
|
|
|
const result = await createOrg(user.id, data);
|
|
|
|
|
|
|
|
|
|
return c.json(
|
|
|
|
|
{
|
|
|
|
|
success: true,
|
|
|
|
|
data: result
|
|
|
|
|
},
|
|
|
|
|
201
|
|
|
|
|
);
|
|
|
|
|
} catch (error) {
|
|
|
|
|
return handleError(c, error, 'Failed to create organization');
|
|
|
|
|
}
|
|
|
|
|
})
|
|
|
|
|
/**
|
|
|
|
|
* PUT /organization/:orgId
|
|
|
|
|
* Updates an organization (name, avatarUrl)
|
|
|
|
|
* Requires authentication
|
|
|
|
|
*/
|
|
|
|
|
.put(
|
|
|
|
|
'/:orgId',
|
|
|
|
|
authMiddleware,
|
|
|
|
|
zValidator('param', ZUpdateOrganizationParam),
|
|
|
|
|
zValidator('json', ZUpdateOrganization),
|
|
|
|
|
async (c) => {
|
|
|
|
|
try {
|
|
|
|
|
console.log('updateOrg');
|
|
|
|
|
console.log(c.req.valid('param'));
|
|
|
|
|
console.log(c.req.valid('json'));
|
|
|
|
|
const { orgId } = c.req.valid('param');
|
|
|
|
|
|
|
|
|
|
const data = c.req.valid('json') as Partial<TOrganization>;
|
|
|
|
|
|
|
|
|
|
const organization = await updateOrg(orgId, data);
|
|
|
|
|
|
|
|
|
|
return c.json(
|
|
|
|
|
{
|
|
|
|
|
success: true,
|
|
|
|
|
data: organization
|
|
|
|
|
},
|
|
|
|
|
200
|
|
|
|
|
);
|
|
|
|
|
} catch (error) {
|
|
|
|
|
return handleError(c, error, 'Failed to update organization');
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
);
|