Index: database/drizzle/queries/builds.ts
===================================================================
--- database/drizzle/queries/builds.ts	(revision e09fe6ff3ab0ade0c931236df5aa4b711ed8e058)
+++ database/drizzle/queries/builds.ts	(revision 1ac9ee4c292344780bd23707d9be69481891c46a)
@@ -10,4 +10,5 @@
 import {eq, desc, and, sql, ilike, asc} from "drizzle-orm";
 import {inArray} from "drizzle-orm/sql/expressions/conditions";
+import {addComponentToBuild} from "./components";
 
 export async function getPendingBuilds(db: Database) {
@@ -392,11 +393,5 @@
     return db.transaction(async (tx) => {
         const [buildToClone] = await tx
-            .select({
-                id: buildsTable.id,
-                userId: buildsTable.userId,
-                name: buildsTable.name,
-                description: buildsTable.description,
-                totalPrice: buildsTable.totalPrice,
-            })
+            .select()
             .from(buildsTable)
             .where(
@@ -421,59 +416,21 @@
             });
 
-        const components = await tx
-            .select()
+        if(!newBuild) return null;
+
+        const existing = await tx
+            .select({ componentId: buildComponentsTable.componentId })
             .from(buildComponentsTable)
-            .where(
-                eq(buildComponentsTable.buildId, buildId)
+            .where(eq(buildComponentsTable.buildId, buildId));
+
+        if (existing.length > 0) {
+            await tx.insert(buildComponentsTable).values(
+                existing.map((r) => ({
+                    buildId: newBuild.id,
+                    componentId: r.componentId,
+                })),
             );
-
-        if(components.length) {
-            await tx
-                .insert(buildComponentsTable)
-                .values(
-                    components.map(component => ({
-                        buildId: newBuild.id,
-                        componentId: component.componentId
-                    }))
-                );
         }
 
-        const [clonedBuild] = await tx
-            .select({
-                id: buildsTable.id,
-                userId: buildsTable.userId,
-                name: buildsTable.name,
-                createdAt: buildsTable.createdAt,
-                description: buildsTable.description,
-                totalPrice: buildsTable.totalPrice
-            })
-            .from(buildsTable)
-            .innerJoin(
-                usersTable,
-                eq(buildsTable.userId, usersTable.id)
-            )
-            .where(
-                eq(buildsTable.id, newBuild.id)
-            )
-            .limit(1);
-
-        const clonedComponents = await tx
-            .select({
-                componentId: buildComponentsTable.componentId,
-                component: componentsTable
-            })
-            .from(buildComponentsTable)
-            .innerJoin(
-                componentsTable,
-                eq(buildComponentsTable.componentId, componentsTable.id)
-            )
-            .where(
-                eq(buildComponentsTable.buildId, newBuild.id)
-            );
-
-        return {
-            ...clonedBuild,
-            components: clonedComponents.map(c => c.component)
-        };
+        return newBuild.id;
     });
 }
@@ -513,67 +470,83 @@
 }
 
-export async function editBuild(db: Database, userId: number, buildId: number, name: string, description: string, componentIds: number[]) {
-    return db.transaction(async (tx) => {
-        const [build] = await tx
-            .select({
-                id: buildsTable.id,
-                userId: buildsTable.userId,
-                isApproved: buildsTable.isApproved
+export async function editBuild(db: Database, userId: number, buildId: number) {
+    const [buildToEdit] = await db
+        .select()
+        .from(buildsTable)
+        .where(
+            and(
+                eq(buildsTable.id, buildId),
+                eq(buildsTable.userId, userId)
+            )
+        )
+        .limit(1);
+
+    if (!buildToEdit || buildToEdit.isApproved) return null;
+
+
+    return buildToEdit?.id ?? null;
+}
+
+export async function saveBuildState(db: Database, userId: number, buildId: number, name: string, description: string ) {
+    const [build] = await db
+        .select({
+            id: buildsTable.id,
+            isApproved: buildsTable.isApproved
+        })
+        .from(buildsTable)
+        .where(
+            and(
+                eq(buildsTable.id, buildId),
+                eq(buildsTable.userId, userId)
+            )
+        )
+        .limit(1);
+
+    if (!build || build.isApproved) return null;
+
+    const [updated] = await db
+        .update(buildsTable)
+        .set({
+            name,
+            description
+        })
+        .where(eq(buildsTable.id, buildId))
+        .returning({ id: buildsTable.id });
+
+    return updated?.id ?? null;
+}
+
+export async function getBuildState(db: Database, userId: number, buildId: number) {
+    const [build] = await db
+        .select({
+            id: buildsTable.id,
+            userId: buildsTable.userId,
+            isApproved: buildsTable.isApproved,
+            name: buildsTable.name,
+            description: buildsTable.description,
+            totalPrice: buildsTable.totalPrice,
+        })
+        .from(buildsTable)
+        .where(
+            and(
+                eq(buildsTable.id, buildId),
+                eq(buildsTable.userId, userId)
+            )
+        )
+        .limit(1);
+
+    if (!build || build.isApproved) return null;
+
+    const components = await db
+        .select({ componentId: buildComponentsTable.componentId
             })
-            .from(buildsTable)
-            .where(
-                and(
-                    eq(buildsTable.id, buildId),
-                    eq(buildsTable.userId, userId)
-                )
-            )
-            .limit(1);
-
-        if (!build) return null;
-        if (build.isApproved) return null;
-
-        const components = await tx
-            .select({
-                id: componentsTable.id,
-                price: componentsTable.price
-            })
-            .from(componentsTable)
-            .where(
-                inArray(componentsTable.id, componentIds)
-            );
-
-        const totalPrice = components.reduce((sum, c) => sum + Number(c.price), 0);
-
-        await tx
-            .update(buildsTable)
-            .set({
-                name: name,
-                description: description,
-                totalPrice: totalPrice.toFixed(2),
-            })
-            .where(
-                and(
-                    eq(buildsTable.id, buildId),
-                    eq(buildsTable.userId, userId)
-                )
-            );
-
-        await tx
-            .delete(buildComponentsTable)
-            .where(
-                eq(buildComponentsTable.buildId, buildId)
-            );
-
-        if(components.length) {
-            await tx.insert(buildComponentsTable)
-                .values(
-                    componentIds.map(componentId => ({
-                        buildId: buildId,
-                        componentId: componentId
-                    }))
-                );
-        }
-
-        return build.id;
-    });
-}
+        .from(buildComponentsTable)
+        .where(
+            eq(buildComponentsTable.buildId, buildId)
+        );
+
+    return {
+        build,
+        componentIds: components.map(c => c.componentId)
+    };
+}
