import { createRoute, OpenAPIHono } from '@hono/zod-openapi' import { z } from 'zod' import { AppDataSource } from '../data-source' import { HTTPException } from 'hono/http-exception'; import { authMiddleware } from '../middleware/auth.middleware'; import { AuthContext } from '../types/context'; interface Migration { name: string; timestamp: number; } const app = new OpenAPIHono() const MigrationResponseSchema = z.object({ success: z.boolean(), message: z.string(), migrations: z.array(z.string()).optional() }) const runMigrationRoute = createRoute({ method: 'post', path: '/migrations/run', middleware: authMiddleware, responses: { 200: { content: { 'application/json': { schema: MigrationResponseSchema } }, description: 'Migrations executed successfully' }, 500: { description: 'Migration failed' } } }) const revertMigrationRoute = createRoute({ method: 'post', path: '/migrations/revert', middleware: authMiddleware, responses: { 200: { content: { 'application/json': { schema: MigrationResponseSchema } }, description: 'Migration reverted successfully' }, 500: { description: 'Revert failed' } } }) app.openapi(runMigrationRoute, async (c) => { try { const migrations = await AppDataSource.runMigrations() return c.json({ success: true, message: 'Migrations executed successfully', migrations: migrations.map((m: Migration) => m.name) }) } catch (error) { throw error } }) app.openapi(revertMigrationRoute, async (c) => { try { await AppDataSource.undoLastMigration() const migrations = await AppDataSource.showMigrations() return c.json({ success: true, message: 'Migration reverted successfully', migrations: migrations }) } catch (error) { throw error } }) export default app export type AppType = typeof app