upd: upsert apps

This commit is contained in:
2025-06-05 14:24:22 +05:00
parent 2b1e2fedd4
commit f408328771
2 changed files with 47 additions and 32 deletions
+13 -13
View File
@@ -1,7 +1,7 @@
import { error } from "elysia";
import db from "../../db";
import { appsTable, serversTable } from "../../db/schema";
import { eq, inArray } from "drizzle-orm";
import { appsTable } from "../../db/schema";
import { sql } from "drizzle-orm";
import type { AuthContext } from "./../../middlewares/auth";
export default async function createApp(
@@ -12,6 +12,7 @@ export default async function createApp(
companyId: string;
}
) {
console.log(auth);
if (auth.userId !== "hmac-user") {
return error(403, {
error: "Forbidden",
@@ -19,16 +20,6 @@ export default async function createApp(
}
try {
const existingApp = await db.query.appsTable.findMany({
where: inArray(appsTable.name, body.apps),
});
if (existingApp.length) {
return error(400, {
error: "App with this name already exists",
});
}
const apps = await db
.insert(appsTable)
.values(
@@ -39,7 +30,16 @@ export default async function createApp(
serverId: body.serverId,
}))
)
.returning();
.returning()
.onConflictDoUpdate({
target: [appsTable.fileName, appsTable.serverId],
set: {
name: sql`excluded.name`,
fileName: sql`excluded.fileName`,
companyId: sql`excluded.company_id`,
serverId: sql`excluded.server_id`,
},
});
return apps;
} catch (err) {