Ignore:
File:
1 edited

Legend:

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

    re03e6fb rb0685a8  
    196196            id: componentsTable.id,
    197197            type: componentsTable.type,
    198             quantity: buildComponentsTable.numComponents
    199198        })
    200199        .from(buildComponentsTable)
     
    232231        componentsDetails.push({
    233232            ...comp,
    234             quantity: comp.quantity,
    235233            details: details
    236234        });
     
    258256
    259257    if(!existingComponents) return null;
     258
     259
    260260
    261261    const existing = {
     
    278278        ...existing.networkAdapters,
    279279        ...existing.soundCards
    280     ].reduce((sum: number, c: any) => {
    281         if (!c) return sum;
    282         return sum + (c.quantity || 1);
    283     }, 0);
     280    ].filter(Boolean).length;
    284281
    285282    const existingTDP = existingComponents.reduce((sum, c) => {
    286283        const tdp = c.details?.tdp ? Number(c.details.tdp) : 0;
    287         return sum + (tdp * (c.quantity || 1));
     284        return sum + tdp;
    288285    }, 0);
    289286
     
    459456        if (existing.memory.length > 0) {
    460457            const totalModules = existing.memory.reduce((sum: number, m: any) =>
    461                 sum + (Number(m.details.modules) * m.quantity), 0
     458                sum + Number(m.details.modules), 0
    462459            );
    463460            const totalCapacity = existing.memory.reduce((sum: number, m: any) =>
    464                 sum + (Number(m.details.capacity) * m.quantity), 0
     461                sum + Number(m.details.capacity), 0
    465462            );
    466463
     
    707704            if (!caseStorageFormFactor) return false;
    708705
    709             const usedSlots = existing.storage
    710                 .filter((s: any) => s.details.formFactor === stor.formFactor)
    711                 .reduce((sum: number, s: any) => sum + s.quantity, 0);
     706            const usedSlots = existing.storage.filter(
     707                (s: any) => s.details.formFactor === stor.formFactor
     708            ).length;
    712709
    713710            return usedSlots < Number(caseStorageFormFactor.numSlots);
     
    824821                if (!storageFF) return false;
    825822
    826                 const usedSlots = existing.storage
    827                     .filter((s: any) => s.details.formFactor === stor.details.formFactor)
    828                     .reduce((sum: number, s: any) => sum + s.quantity, 0);;
     823                const usedSlots = existing.storage.filter(
     824                    (s: any) => s.details.formFactor === stor.details.formFactor
     825                ).length;
    829826
    830827                return usedSlots <= Number(storageFF.numSlots);
     
    966963            .values({
    967964                buildId,
    968                 componentId,
    969                 numComponents: 1
    970             })
    971             .onConflictDoUpdate({
    972                 target: [buildComponentsTable.buildId, buildComponentsTable.componentId],
    973                 set: {
    974                     numComponents: sql`${buildComponentsTable.numComponents} + 1`
    975                 }
     965                componentId
    976966            });
    977967
     
    979969            .select({
    980970                price:  componentsTable.price,
    981                 quantity: buildComponentsTable.numComponents
    982971            })
    983972            .from(buildComponentsTable)
     
    990979            );
    991980
    992         const totalPrice = buildComponents.reduce((sum, c) =>
    993             sum + (Number(c.price) * c.quantity), 0
    994         );
     981        const totalPrice = buildComponents.reduce((sum, c) => sum + Number(c.price), 0);
    995982
    996983        await tx
     
    10221009        if(!build || build.isApproved) return null;
    10231010
    1024         const [existing] = await tx
    1025             .select({
    1026                 quantity: buildComponentsTable.numComponents
    1027             })
    1028             .from(buildComponentsTable)
     1011        const result = await tx
     1012            .delete(buildComponentsTable)
    10291013            .where(
    10301014                and(
     
    10321016                    eq(buildComponentsTable.componentId, componentId)
    10331017                )
    1034             )
    1035             .limit(1);
    1036 
    1037         if (!existing) return null;
    1038 
    1039         if (existing.quantity > 1) {
    1040             await tx
    1041                 .update(buildComponentsTable)
    1042                 .set({
    1043                     numComponents: sql`${buildComponentsTable.numComponents} - 1`
    1044                 })
    1045                 .where(
    1046                     and(
    1047                         eq(buildComponentsTable.buildId, buildId),
    1048                         eq(buildComponentsTable.componentId, componentId)
    1049                     )
    1050                 );
    1051         } else {
    1052             await tx
    1053                 .delete(buildComponentsTable)
    1054                 .where(
    1055                     and(
    1056                         eq(buildComponentsTable.buildId, buildId),
    1057                         eq(buildComponentsTable.componentId, componentId)
    1058                     )
    1059                 );
    1060         }
     1018            );
     1019
     1020        if(result.rowCount === 0) return null;
    10611021
    10621022        const buildComponents = await tx
    10631023            .select({
    10641024                price:  componentsTable.price,
    1065                 quantity: buildComponentsTable.numComponents
    10661025            })
    10671026            .from(buildComponentsTable)
     
    10741033            );
    10751034
    1076         const totalPrice = buildComponents.reduce((sum, c) =>
    1077             sum + (Number(c.price) * c.quantity), 0
    1078         );
     1035        const totalPrice = buildComponents.reduce((sum, c) => sum + Number(c.price), 0);
    10791036
    10801037        await tx
     
    10871044            );
    10881045
    1089         return componentId;
     1046        return result;
    10901047    })
    10911048}
Note: See TracChangeset for help on using the changeset viewer.