Add DemoPage route and integrate appController in server
- Introduced a new DemoPage component and added a corresponding route for it in the client application. - Integrated appController into the server to manage application-related functionalities.
This commit is contained in:
@@ -0,0 +1,64 @@
|
||||
import { Elysia, t } from "elysia";
|
||||
import { eq } from "drizzle-orm";
|
||||
import db from "../db";
|
||||
import { apps } from "../db/schema/apps";
|
||||
import { optionalAuthMiddleware } from "../middlewares/optionalAuth";
|
||||
import { serverSessionService } from "../services/serverSession";
|
||||
import { serverService } from "../services/server";
|
||||
|
||||
export const appController = new Elysia({ prefix: "/apps" })
|
||||
.use(optionalAuthMiddleware)
|
||||
// POST /apps/demo/:name - создать демо-сессию по имени приложения
|
||||
.post(
|
||||
"/demo/:name",
|
||||
async ({ params, currentUser, guestId, status }) => {
|
||||
const { name } = params;
|
||||
|
||||
// Найти приложение по имени
|
||||
const app = await db.query.apps.findFirst({
|
||||
where: eq(apps.name, name),
|
||||
});
|
||||
|
||||
if (!app) {
|
||||
return status(404, "Приложение не найдено");
|
||||
}
|
||||
|
||||
// Проверяем наличие guestId для неавторизованных пользователей
|
||||
if (!currentUser && !guestId) {
|
||||
return status(400, "Для неавторизованных пользователей требуется Guest ID");
|
||||
}
|
||||
|
||||
// Проверяем, что есть доступные demo-серверы
|
||||
const demoServers = await serverService.findAvailableStreamServers("demo");
|
||||
|
||||
if (demoServers.length === 0) {
|
||||
return status(
|
||||
503,
|
||||
"Нет доступных demo серверов. Пожалуйста, попробуйте позже."
|
||||
);
|
||||
}
|
||||
|
||||
// Создаём демо-сессию
|
||||
try {
|
||||
const newSession = await serverSessionService.create({
|
||||
appId: app.id,
|
||||
userId: currentUser?.id,
|
||||
guestId: currentUser ? undefined : guestId || undefined,
|
||||
mode: "stream",
|
||||
tier: "demo",
|
||||
});
|
||||
|
||||
return { session: newSession };
|
||||
} catch (error) {
|
||||
if (error instanceof Error) {
|
||||
return status(503, error.message);
|
||||
}
|
||||
return status(500, "Не удалось создать сессию");
|
||||
}
|
||||
},
|
||||
{
|
||||
params: t.Object({
|
||||
name: t.String(),
|
||||
}),
|
||||
}
|
||||
);
|
||||
@@ -6,6 +6,7 @@ import { companyController } from "./controllers/company";
|
||||
import { branchController } from "./controllers/branch";
|
||||
import { serverController } from "./controllers/server";
|
||||
import { chatController } from "./controllers/chat";
|
||||
import { appController } from "./controllers/app";
|
||||
import { serverSessionService } from "./services/serverSession";
|
||||
import { saveChatMessage } from "./services/chat";
|
||||
import { Server } from "socket.io";
|
||||
@@ -27,6 +28,7 @@ app.use(companyController);
|
||||
app.use(branchController);
|
||||
app.use(serverController);
|
||||
app.use(chatController);
|
||||
app.use(appController);
|
||||
|
||||
app.listen(process.env.PORT || 3000);
|
||||
|
||||
|
||||
Reference in New Issue
Block a user