56 lines
1.4 KiB
TypeScript
56 lines
1.4 KiB
TypeScript
import { status } from "elysia";
|
|
import db from "../../db";
|
|
import { appsTable, appsToServers } from "../../db/schema";
|
|
import { sql } from "drizzle-orm";
|
|
import type { AuthContext } from "./../../middlewares/auth";
|
|
|
|
export default async function createApp(
|
|
auth: AuthContext,
|
|
body: {
|
|
apps: string[];
|
|
serverId: string;
|
|
companyId: string;
|
|
}
|
|
) {
|
|
if (auth.managerId !== "hmac-manager") {
|
|
return status(403, {
|
|
error: "Forbidden",
|
|
});
|
|
}
|
|
|
|
try {
|
|
const apps = await db
|
|
.insert(appsTable)
|
|
.values(
|
|
body.apps.map((app) => ({
|
|
name: app,
|
|
fileName: app,
|
|
companyId: body.companyId,
|
|
}))
|
|
)
|
|
.returning()
|
|
.onConflictDoUpdate({
|
|
target: [appsTable.fileName],
|
|
set: {
|
|
name: sql`excluded.name`,
|
|
fileName: sql`excluded.fileName`,
|
|
companyId: sql`excluded.company_id`,
|
|
},
|
|
});
|
|
|
|
const appsOfServer = await db
|
|
.insert(appsToServers)
|
|
.values(apps.map(({ id }) => ({ appId: id, serverId: body.serverId })))
|
|
.onConflictDoUpdate({
|
|
target: [appsToServers.appId, appsToServers.serverId],
|
|
set: { appId: sql`excluded.app_id`, serverId: body.serverId },
|
|
})
|
|
.returning();
|
|
|
|
return appsOfServer;
|
|
} catch (error) {
|
|
console.log((error as Error).message);
|
|
return status(500, "Internal Server Error");
|
|
}
|
|
}
|