feat: mock exercise template api (#613)

* feat: move mock exercise template to api

* feat: add template migration file

* chore:
- remove exercise templates form api
- rework api to pull exercise templates from db
- add exercise template seed to seed file
- declutter seed file

* chore:
- refactor template fetch to be dependent on the selected tag
- add tag to schema, remote db and template's seed file
- clear remote db's template table and replace data and their respective tags

* chore: add number of questions and center loading screen on the frontend

* chore: pr fixes

* chore: type fixes

* chore:
- move seed functionality to /seed folder
- add typing for individual seed functions
- pr fixes

* chore: pr fixes v2

* update output css

* refactor: update seeding script and schema definitions

- Renamed `seedTypes` to `seedQuestionTypes` for clarity in the seeding process.
- Enhanced the seeding script to include a command-line interface for better usability, allowing specific seed functions to be executed based on flags.
- Removed redundant unique constraints in the schema definitions for `courseNewsfeedComment` and `exerciseTemplate` to streamline database structure.

* feat: add email verification error handling page and update session management

- Introduced a new page for handling email verification errors, providing user-friendly messages based on error types.
- Updated session management logic to improve handling of public routes and session states.
- Enhanced security policies in the Svelte configuration to include additional sources for styles and scripts.
- Removed the old email verification error page to streamline the codebase.

* move the exercise template to cloudflare cdn

---------

Co-authored-by: Rotimi Best <[email protected]>
This commit is contained in:
Adeyemi Adetunji
2025-12-25 05:09:27 +01:00
committed by GitHub
co-authored by Rotimi Best
parent ac3f35a97f
commit 50d6ee3111
263 changed files with 1568 additions and 37195 deletions
+2
View File
@@ -17,6 +17,7 @@ import { dashAnalyticsRouter } from '@api/routes/dash';
import { mediaRouter } from '@api/routes/media';
import { onboardingRouter } from '@api/routes/onboarding';
import { organizationRouter } from '@api/routes/organization';
import { exerciseRouter } from '@api/routes/exercise';
import { communityRouter } from '@api/routes/community';
// Create Hono app with chaining for RPC support
@@ -78,6 +79,7 @@ export const app = new Hono()
.route('/media', mediaRouter)
.route('/organization', organizationRouter)
.route('/dash', dashAnalyticsRouter)
.route('/exercise', exerciseRouter)
.route('/community', communityRouter)
// Error handling
+1
View File
@@ -0,0 +1 @@
export * from './template';
+38
View File
@@ -0,0 +1,38 @@
import { Hono } from '@api/utils/hono';
import { authMiddleware } from '@api/middlewares/auth';
import { ZTemplateById, ZTemplateByTag } from '@cio/utils/validation/mocks';
import { handleError } from '@api/utils/errors';
import { zValidator } from '@hono/zod-validator';
import { fetchAllTemplatesMetadata, fetchTemplateById, fetchTemplatesByTag } from '@api/services/exercise/template';
export const exerciseRouter = new Hono()
.get('/template', authMiddleware, async (c) => {
try {
const result = await fetchAllTemplatesMetadata();
return c.json({ success: true, data: result }, 200);
} catch (error) {
return handleError(c, error, 'Failed to load template metadata');
}
})
.get('/template/:id', authMiddleware, zValidator('param', ZTemplateById), async (c) => {
try {
const { id } = c.req.valid('param');
const result = await fetchTemplateById(id);
return c.json({ success: true, data: result }, 200);
} catch (error) {
return handleError(c, error, 'Failed to load template');
}
})
.get('/template/tag/:tag', authMiddleware, zValidator('param', ZTemplateByTag), async (c) => {
try {
const { tag } = c.req.valid('param');
const result = await fetchTemplatesByTag(tag);
return c.json({ success: true, data: result }, 200);
} catch (error) {
return handleError(c, error, 'Failed to load template');
}
});
@@ -0,0 +1,54 @@
import { calculateTotalPoints } from '@api/utils/template';
import { getAllTemplates, getTemplateById, getTemplateByTag } from '@db/queries/template/template';
import { TNewExerciseTemplate, TExerciseTemplate } from '@db/types';
function mapTemplateToMetadata(templates: TNewExerciseTemplate[]) {
return templates.map((template) => {
const questionnaire = template.questionnaire;
return {
id: template.id,
title: template.title,
description: template.description,
questions: questionnaire.questions.length,
points: calculateTotalPoints(template),
tag: template.tag
};
});
}
export async function fetchAllTemplatesMetadata() {
let templates: TExerciseTemplate[];
try {
templates = await getAllTemplates();
} catch (error) {
console.error('Failed to fetch templates from DB:', error);
throw error;
}
return mapTemplateToMetadata(templates);
}
export async function fetchTemplateById(id: number): Promise<TExerciseTemplate> {
let template: TExerciseTemplate;
try {
template = await getTemplateById(id);
} catch (error) {
console.error('Failed to fetch template from DB:', error);
throw error;
}
return template;
}
export async function fetchTemplatesByTag(tag: string) {
let templates: TNewExerciseTemplate[];
try {
templates = await getTemplateByTag(tag);
} catch (error) {
console.error('Failed to fetch templates from DB:', error);
throw error;
}
return mapTemplateToMetadata(templates);
}
+11
View File
@@ -0,0 +1,11 @@
import { TNewExerciseTemplate } from '@db/types';
export function calculateTotalPoints(template: TNewExerciseTemplate): number {
let totalPoints = 0;
template.questionnaire.questions.forEach((question) => {
totalPoints += question.points;
});
return totalPoints;
}