upd
This commit is contained in:
@@ -1,6 +1,13 @@
|
||||
// import Elysia from "elysia";
|
||||
// import authMiddleware from "../middlewares/auth";
|
||||
import Elysia, { t } from "elysia";
|
||||
import authMiddleware from "../middlewares/auth";
|
||||
import createClient from "../services/clients/create";
|
||||
|
||||
// export const clientsController = new Elysia({ prefix: "/clients" })
|
||||
// .use(authMiddleware)
|
||||
// .get("/", async ({auth:{companyId}}) => {}, {});
|
||||
export const clientsController = new Elysia({ prefix: "/clients" })
|
||||
.use(authMiddleware)
|
||||
.post("/", async ({ auth, body }) => createClient(auth, body), {
|
||||
body: t.Object({
|
||||
name: t.String(),
|
||||
phone: t.String(),
|
||||
email: t.Optional(t.String()),
|
||||
}),
|
||||
});
|
||||
|
||||
@@ -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),
|
||||
|
||||
@@ -1,8 +1,8 @@
|
||||
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(),
|
||||
@@ -27,5 +27,5 @@ export const serversRelations = relations(serversTable, ({ one, many }) => ({
|
||||
references: [companiesTable.id],
|
||||
}),
|
||||
sessions: many(sessionsTable),
|
||||
actions: many(actionsTable),
|
||||
apps: many(appsTable),
|
||||
}));
|
||||
|
||||
@@ -19,7 +19,9 @@ export const sessionsTable = pgTable(
|
||||
id: uuid("id").defaultRandom().primaryKey(),
|
||||
status: varchar("status", {
|
||||
enum: ["starting", "started", "restarting", "ending", "ended"],
|
||||
}).notNull(),
|
||||
})
|
||||
.notNull()
|
||||
.default("starting"),
|
||||
appId: uuid("app_id")
|
||||
.notNull()
|
||||
.references(() => appsTable.id),
|
||||
|
||||
@@ -5,6 +5,7 @@ import usersController from "./controllers/usersController";
|
||||
import sessionsController from "./controllers/sessionsController";
|
||||
import serversController from "./controllers/serversController";
|
||||
import appsController from "./controllers/appsController";
|
||||
import { clientsController } from "./controllers/clientsController";
|
||||
|
||||
const app = new Elysia();
|
||||
|
||||
@@ -13,11 +14,13 @@ app.use(
|
||||
origin: "*",
|
||||
})
|
||||
);
|
||||
|
||||
app.use(authController);
|
||||
app.use(usersController);
|
||||
app.use(serversController);
|
||||
app.use(sessionsController);
|
||||
app.use(appsController);
|
||||
app.use(clientsController);
|
||||
|
||||
app.listen(3000);
|
||||
|
||||
|
||||
@@ -0,0 +1,44 @@
|
||||
import { eq } from "drizzle-orm";
|
||||
import db from "../../db";
|
||||
import { clientsTable } from "../../db/schema";
|
||||
import { error } from "elysia";
|
||||
|
||||
async function createClient(
|
||||
auth: {
|
||||
userId: string;
|
||||
companyId: string;
|
||||
},
|
||||
body: {
|
||||
name: string;
|
||||
phone: string;
|
||||
email?: string;
|
||||
}
|
||||
) {
|
||||
try {
|
||||
// Check for existing client
|
||||
const [existingClient] = await db
|
||||
.select()
|
||||
.from(clientsTable)
|
||||
.where(eq(clientsTable.name, body.name));
|
||||
|
||||
if (existingClient) {
|
||||
return existingClient;
|
||||
}
|
||||
|
||||
// Create new client
|
||||
const [newClient] = await db
|
||||
.insert(clientsTable)
|
||||
.values({
|
||||
...body,
|
||||
ownerId: auth.userId,
|
||||
companyId: auth.companyId,
|
||||
})
|
||||
.returning();
|
||||
|
||||
return newClient;
|
||||
} catch (err) {
|
||||
return error(500, "Internal Server Error");
|
||||
}
|
||||
}
|
||||
|
||||
export default createClient;
|
||||
@@ -32,7 +32,9 @@ export default async function getServers(
|
||||
with: {
|
||||
client: {
|
||||
columns: {
|
||||
fullname: true,
|
||||
name: true,
|
||||
phone: true,
|
||||
email: true,
|
||||
},
|
||||
},
|
||||
app: {
|
||||
@@ -42,6 +44,7 @@ export default async function getServers(
|
||||
},
|
||||
},
|
||||
},
|
||||
apps: true,
|
||||
}
|
||||
: undefined),
|
||||
},
|
||||
|
||||
@@ -40,7 +40,6 @@ async function createSession(
|
||||
...body,
|
||||
ownerId: auth.userId,
|
||||
companyId: auth.companyId,
|
||||
status: "starting",
|
||||
})
|
||||
.returning();
|
||||
|
||||
|
||||
Reference in New Issue
Block a user