Ignore:
Timestamp:
12/28/25 17:27:19 (6 months ago)
Author:
Tome <gjorgievtome@…>
Branches:
main
Children:
1ac9ee4
Parents:
67186d2
Message:

Refactor queries and telefunctions

File:
1 edited

Legend:

Unmodified
Added
Removed
  • database/drizzle/queries/builds.ts

    r67186d2 r8fe7f1a  
    1010import {eq, desc, and, sql, ilike, asc} from "drizzle-orm";
    1111import {inArray} from "drizzle-orm/sql/expressions/conditions";
     12import {addComponentToBuild} from "./components";
    1213
    1314export async function getPendingBuilds(db: Database) {
     
    392393    return db.transaction(async (tx) => {
    393394        const [buildToClone] = await tx
    394             .select({
    395                 id: buildsTable.id,
    396                 userId: buildsTable.userId,
    397                 name: buildsTable.name,
    398                 description: buildsTable.description,
    399                 totalPrice: buildsTable.totalPrice,
    400             })
     395            .select()
    401396            .from(buildsTable)
    402397            .where(
     
    421416            });
    422417
    423         const components = await tx
    424             .select()
     418        if(!newBuild) return null;
     419
     420        const existing = await tx
     421            .select({ componentId: buildComponentsTable.componentId })
    425422            .from(buildComponentsTable)
    426             .where(
    427                 eq(buildComponentsTable.buildId, buildId)
     423            .where(eq(buildComponentsTable.buildId, buildId));
     424
     425        if (existing.length > 0) {
     426            await tx.insert(buildComponentsTable).values(
     427                existing.map((r) => ({
     428                    buildId: newBuild.id,
     429                    componentId: r.componentId,
     430                })),
    428431            );
    429 
    430         if(components.length) {
    431             await tx
    432                 .insert(buildComponentsTable)
    433                 .values(
    434                     components.map(component => ({
    435                         buildId: newBuild.id,
    436                         componentId: component.componentId
    437                     }))
    438                 );
    439432        }
    440433
    441         const [clonedBuild] = await tx
    442             .select({
    443                 id: buildsTable.id,
    444                 userId: buildsTable.userId,
    445                 name: buildsTable.name,
    446                 createdAt: buildsTable.createdAt,
    447                 description: buildsTable.description,
    448                 totalPrice: buildsTable.totalPrice
    449             })
    450             .from(buildsTable)
    451             .innerJoin(
    452                 usersTable,
    453                 eq(buildsTable.userId, usersTable.id)
    454             )
    455             .where(
    456                 eq(buildsTable.id, newBuild.id)
    457             )
    458             .limit(1);
    459 
    460         const clonedComponents = await tx
    461             .select({
    462                 componentId: buildComponentsTable.componentId,
    463                 component: componentsTable
    464             })
    465             .from(buildComponentsTable)
    466             .innerJoin(
    467                 componentsTable,
    468                 eq(buildComponentsTable.componentId, componentsTable.id)
    469             )
    470             .where(
    471                 eq(buildComponentsTable.buildId, newBuild.id)
    472             );
    473 
    474         return {
    475             ...clonedBuild,
    476             components: clonedComponents.map(c => c.component)
    477         };
     434        return newBuild.id;
    478435    });
    479436}
     
    513470}
    514471
    515 export async function editBuild(db: Database, userId: number, buildId: number, name: string, description: string, componentIds: number[]) {
    516     return db.transaction(async (tx) => {
    517         const [build] = await tx
    518             .select({
    519                 id: buildsTable.id,
    520                 userId: buildsTable.userId,
    521                 isApproved: buildsTable.isApproved
     472export async function editBuild(db: Database, userId: number, buildId: number) {
     473    const [buildToEdit] = await db
     474        .select()
     475        .from(buildsTable)
     476        .where(
     477            and(
     478                eq(buildsTable.id, buildId),
     479                eq(buildsTable.userId, userId)
     480            )
     481        )
     482        .limit(1);
     483
     484    if (!buildToEdit || buildToEdit.isApproved) return null;
     485
     486
     487    return buildToEdit?.id ?? null;
     488}
     489
     490export async function saveBuildState(db: Database, userId: number, buildId: number, name: string, description: string ) {
     491    const [build] = await db
     492        .select({
     493            id: buildsTable.id,
     494            isApproved: buildsTable.isApproved
     495        })
     496        .from(buildsTable)
     497        .where(
     498            and(
     499                eq(buildsTable.id, buildId),
     500                eq(buildsTable.userId, userId)
     501            )
     502        )
     503        .limit(1);
     504
     505    if (!build || build.isApproved) return null;
     506
     507    const [updated] = await db
     508        .update(buildsTable)
     509        .set({
     510            name,
     511            description
     512        })
     513        .where(eq(buildsTable.id, buildId))
     514        .returning({ id: buildsTable.id });
     515
     516    return updated?.id ?? null;
     517}
     518
     519export async function getBuildState(db: Database, userId: number, buildId: number) {
     520    const [build] = await db
     521        .select({
     522            id: buildsTable.id,
     523            userId: buildsTable.userId,
     524            isApproved: buildsTable.isApproved,
     525            name: buildsTable.name,
     526            description: buildsTable.description,
     527            totalPrice: buildsTable.totalPrice,
     528        })
     529        .from(buildsTable)
     530        .where(
     531            and(
     532                eq(buildsTable.id, buildId),
     533                eq(buildsTable.userId, userId)
     534            )
     535        )
     536        .limit(1);
     537
     538    if (!build || build.isApproved) return null;
     539
     540    const components = await db
     541        .select({ componentId: buildComponentsTable.componentId
    522542            })
    523             .from(buildsTable)
    524             .where(
    525                 and(
    526                     eq(buildsTable.id, buildId),
    527                     eq(buildsTable.userId, userId)
    528                 )
    529             )
    530             .limit(1);
    531 
    532         if (!build) return null;
    533         if (build.isApproved) return null;
    534 
    535         const components = await tx
    536             .select({
    537                 id: componentsTable.id,
    538                 price: componentsTable.price
    539             })
    540             .from(componentsTable)
    541             .where(
    542                 inArray(componentsTable.id, componentIds)
    543             );
    544 
    545         const totalPrice = components.reduce((sum, c) => sum + Number(c.price), 0);
    546 
    547         await tx
    548             .update(buildsTable)
    549             .set({
    550                 name: name,
    551                 description: description,
    552                 totalPrice: totalPrice.toFixed(2),
    553             })
    554             .where(
    555                 and(
    556                     eq(buildsTable.id, buildId),
    557                     eq(buildsTable.userId, userId)
    558                 )
    559             );
    560 
    561         await tx
    562             .delete(buildComponentsTable)
    563             .where(
    564                 eq(buildComponentsTable.buildId, buildId)
    565             );
    566 
    567         if(components.length) {
    568             await tx.insert(buildComponentsTable)
    569                 .values(
    570                     componentIds.map(componentId => ({
    571                         buildId: buildId,
    572                         componentId: componentId
    573                     }))
    574                 );
    575         }
    576 
    577         return build.id;
    578     });
    579 }
     543        .from(buildComponentsTable)
     544        .where(
     545            eq(buildComponentsTable.buildId, buildId)
     546        );
     547
     548    return {
     549        build,
     550        componentIds: components.map(c => c.componentId)
     551    };
     552}
Note: See TracChangeset for help on using the changeset viewer.