Files
classroomio/apps/api/src/routes/account/account.ts
T

50 lines
1.3 KiB
TypeScript
Raw Normal View History

import { getAccountData, updateUser } from '@api/services/account';
import { Hono } from '@api/utils/hono';
import { ZUpdateProfile } from '@cio/utils/validation/account';
import { authMiddleware } from '@api/middlewares/auth';
import { handleError } from '@api/utils/errors';
import { zValidator } from '@hono/zod-validator';
export const accountRouter = new Hono()
.get('/', authMiddleware, async (c) => {
const user = c.get('user');
try {
console.time('accountRouter');
const accountData = await getAccountData(user.id);
console.timeEnd('accountRouter');
return c.json(
{
success: true,
user,
profile: accountData.profile,
organizations: accountData.organizations
},
200
);
} catch (error) {
return handleError(c, error, 'Failed to fetch account data');
}
})
.put('/user', authMiddleware, zValidator('json', ZUpdateProfile), async (c) => {
const user = c.get('user');
try {
const validatedData = c.req.valid('json');
const updatedProfile = await updateUser(user.id, validatedData);
return c.json(
{
success: true,
profile: updatedProfile
},
200
);
} catch (error) {
return handleError(c, error, 'Failed to update profile');
}
});