diff --git a/.env b/.env index 667f50e..3c9117c 100644 --- a/.env +++ b/.env @@ -1,7 +1,5 @@ PORT=3001 DB_URL=postgres://postgres:v1sq3vD5faXL@194.26.138.94:5432/mate -JWT_SECRET=722fdcd1d98ae1a56b285db89d4df8a608fba313910210183c0a2caf558cce62 -JWT_EXPIRATION=30d HMAC_SECRET=9a9ccda5db157ccfd04b4094264c55f58097f85d1fd6c22fbab0a86680dd3efd -API_URL=http://192.168.1.183:3000 +API_URL=http://localhost:3000 COMPANY_ID=56e69b37-15e6-4de4-b953-e77435a09a06 \ No newline at end of file diff --git a/src/db/schema/clients.ts b/src/db/schema/clients.ts index d473bda..98662a9 100644 --- a/src/db/schema/clients.ts +++ b/src/db/schema/clients.ts @@ -2,12 +2,16 @@ import { pgTable, timestamp, uuid, varchar } from "drizzle-orm/pg-core"; import { companiesTable } from "./companies"; import { relations } from "drizzle-orm"; import { sessionsTable } from "./sessions"; +import { usersTable } from "./users"; export const clientsTable = pgTable("clients", { id: uuid("id").defaultRandom().primaryKey(), - fullname: varchar("fullname").notNull(), - email: varchar("email").notNull(), - phone: varchar("phone", { length: 15 }), + name: varchar("name").notNull(), + phone: varchar("phone", { length: 11 }).notNull(), + email: varchar("email"), + ownerId: uuid("owner_id") + .notNull() + .references(() => usersTable.id), companyId: uuid("company_id") .notNull() .references(() => companiesTable.id), diff --git a/src/db/schema/servers.ts b/src/db/schema/servers.ts index 170ab04..c314297 100644 --- a/src/db/schema/servers.ts +++ b/src/db/schema/servers.ts @@ -1,12 +1,12 @@ import { pgTable, timestamp, uuid, varchar } from "drizzle-orm/pg-core"; import { companiesTable } from "./companies"; -import { actionsTable } from "./actions"; import { relations } from "drizzle-orm"; import { sessionsTable } from "./sessions"; +import { appsTable } from "./apps"; export const serversTable = pgTable("servers", { id: uuid("id").primaryKey().defaultRandom(), - hostname: varchar("hostname", { length: 15 }).notNull(), + hostname: varchar("hostname", { length: 15 }).unique().notNull(), name: varchar("name").notNull(), location: varchar("location").notNull(), companyId: uuid("company_id") @@ -27,5 +27,5 @@ export const serversRelations = relations(serversTable, ({ one, many }) => ({ references: [companiesTable.id], }), sessions: many(sessionsTable), - actions: many(actionsTable), + apps: many(appsTable), })); diff --git a/src/db/schema/sessions.ts b/src/db/schema/sessions.ts index 005430c..04e151b 100644 --- a/src/db/schema/sessions.ts +++ b/src/db/schema/sessions.ts @@ -1,39 +1,56 @@ -import { pgTable, timestamp, uuid, varchar } from "drizzle-orm/pg-core"; +import { + pgTable, + timestamp, + uuid, + varchar, + uniqueIndex, +} from "drizzle-orm/pg-core"; import { companiesTable } from "./companies"; import { usersTable } from "./users"; import { relations } from "drizzle-orm"; import { serversTable } from "./servers"; import { clientsTable } from "./clients"; import { appsTable } from "./apps"; +import { sql } from "drizzle-orm"; -export const sessionsTable = pgTable("sessions", { - id: uuid("id").defaultRandom().primaryKey(), - status: varchar("status", { - enum: ["starting", "started", "restarting", "ending", "ended"], - }), - appId: uuid("app_id") - .notNull() - .references(() => appsTable.id), - ownerId: uuid("owner_id") - .notNull() - .references(() => usersTable.id), - serverId: uuid("server_id") - .notNull() - .references(() => serversTable.id), - clientId: uuid("client_id") - .notNull() - .references(() => clientsTable.id), - companyId: uuid("company_id") - .notNull() - .references(() => companiesTable.id), - createdAt: timestamp("created_at", { withTimezone: true }) - .notNull() - .defaultNow(), - updatedAt: timestamp("updated_at", { withTimezone: true }) - .notNull() - .defaultNow() - .$onUpdate(() => new Date()), -}); +export const sessionsTable = pgTable( + "sessions", + { + id: uuid("id").defaultRandom().primaryKey(), + status: varchar("status", { + enum: ["starting", "started", "restarting", "ending", "ended"], + }) + .notNull() + .default("starting"), + appId: uuid("app_id") + .notNull() + .references(() => appsTable.id), + ownerId: uuid("owner_id") + .notNull() + .references(() => usersTable.id), + serverId: uuid("server_id") + .notNull() + .references(() => serversTable.id), + clientId: uuid("client_id") + .notNull() + .references(() => clientsTable.id), + companyId: uuid("company_id") + .notNull() + .references(() => companiesTable.id), + createdAt: timestamp("created_at", { withTimezone: true }) + .notNull() + .defaultNow(), + updatedAt: timestamp("updated_at", { withTimezone: true }) + .notNull() + .defaultNow() + .$onUpdate(() => new Date()), + }, + (table) => ({ + activeServerSessionIdx: uniqueIndex("active_server_session_idx") + .on(table.serverId, table.status) + .where(sql`${table.status} IN ('starting', 'started')`), + }) +); export const sessionsRelations = relations(sessionsTable, ({ one }) => ({ owner: one(usersTable, { diff --git a/src/index.ts b/src/index.ts index c382506..035fc19 100644 --- a/src/index.ts +++ b/src/index.ts @@ -6,13 +6,18 @@ import os from "os"; import cron from "node-cron"; import { exec, execFile } from "child_process"; import { createHmac } from "crypto"; -import got, { HTTPError } from "got"; +import got from "got"; import { readdirSync } from "fs"; type Session = typeof sessionsTable.$inferSelect & { app: { fileName: string; }; + client: { + name: string; + phone: string; + email?: string; + }; }; const hostname = os.hostname(); @@ -122,6 +127,13 @@ async function getSessions() { fileName: true, }, }, + client: { + columns: { + name: true, + phone: true, + email: true, + }, + }, }, orderBy: desc(sessionsTable.createdAt), }); @@ -133,7 +145,16 @@ async function startSession(session: Session) { const filePath = `C:/apps/${session.app.fileName}/${session.app.fileName}.exe`; try { - execFile(filePath, []); + const clientName = session.client.name; + const clientPhone = session.client.phone; + const clientEmail = session.client.email || ""; + + execFile(filePath, [ + `-ClientName=${clientName}`, + `-ClientPhone=${clientPhone}`, + `-ClientEmail=${clientEmail}`, + `-ManagerFullName=Твой любимый манагер`, + ]); await db .update(sessionsTable)