Ignore:
Timestamp:
12/25/25 01:49:51 (7 months ago)
Author:
Tome <gjorgievtome@…>
Branches:
main
Children:
e7f1bfa
Parents:
d4842f4
Message:

fix returns and add checks

File:
1 edited

Legend:

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

    rd4842f4 r83fb5e2  
    4646
    4747export async function setBuildApprovalStatus(db: Database, buildId: number, isApproved: boolean){
    48     const result = await db
     48    const [result] = await db
    4949        .update(buildsTable)
    5050        .set({
     
    6161        })
    6262
    63     return result.length;
     63    return result?.id ?? null;
    6464}
    6565
     
    135135            created_at: buildsTable.createdAt,
    136136            total_price: buildsTable.totalPrice,
    137             avgRating: sql<number>`AVG(${ratingBuildsTable.value}::float)`
     137            avgRating: sql<number>`COALESCE(AVG(${ratingBuildsTable.value}::float),0)`
    138138        })
    139139        .from(buildsTable)
     
    160160
    161161    return approvedBuildsList;
    162 }
    163 
    164 export async function getHighestRankedBuilds(db: Database, limit? : number) {
    165     const highestRankedBuildsList = await db
    166         .select({
    167             id: buildsTable.id,
    168             user_id: buildsTable.userId,
    169             name: buildsTable.name,
    170             created_at: buildsTable.createdAt,
    171             total_price: buildsTable.totalPrice,
    172             avgRating: sql<number>`AVG(${ratingBuildsTable.value}::float)`
    173         })
    174         .from(buildsTable)
    175         .innerJoin(
    176             ratingBuildsTable,
    177             eq(buildsTable.id, ratingBuildsTable.buildId)
    178         )
    179         .where(
    180             eq(buildsTable.isApproved, true)
    181         )
    182         .groupBy(buildsTable.id)
    183         .orderBy(
    184             desc(sql<number>`AVG(${ratingBuildsTable.value}::float)`)
    185         )
    186         .limit(limit || 100); // 100 placeholder
    187 
    188     return highestRankedBuildsList;
    189162}
    190163
     
    335308        .limit(1);
    336309
    337     if (existing.length) {
     310    if (existing.length > 0) {
    338311        await db
    339312            .delete(favoriteBuildsTable)
     
    345318            );
    346319
    347         return { favorite: false };
     320        return null;
    348321    }
    349322
     
    355328        });
    356329
    357     return { favorite: true };
     330    return true;
    358331}
    359332
     
    378351        })
    379352
    380     return result;
     353    return result ?? null;
    381354}
    382355
     
    404377        })
    405378
    406     return result;
     379    return result ?? null;
    407380}
    408381
     
    498471
    499472export async function deleteBuild(db: Database, userId: number, buildId: number) {
    500     const result = await db
     473    const [result] = await db
    501474        .delete(buildsTable)
    502475        .where(
     
    510483        })
    511484
    512     return result.length;
     485    return result?.id ?? null;
    513486}
    514487
     
    550523        }
    551524
    552         return newBuild.id;
     525        return newBuild?.id ?? null;
    553526    });
    554527}
Note: See TracChangeset for help on using the changeset viewer.