Changeset e03e6fb


Ignore:
Timestamp:
01/28/26 12:12:43 (5 months ago)
Author:
Tome <gjorgievtome@…>
Branches:
main
Children:
a744c90
Parents:
ad211d1
Message:

Fix missing attribute in buildComponents relation

Location:
database
Files:
5 edited
1 moved

Legend:

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

    rad211d1 re03e6fb  
    200200            .select({
    201201                componentId: buildComponentsTable.componentId,
    202                 component: componentsTable
     202                component: componentsTable,
     203                quantity: buildComponentsTable.numComponents
    203204            })
    204205            .from(buildComponentsTable)
     
    292293        return {
    293294            ...buildDetails,
    294             components: components.map(c => c.component),
     295            components: components.map(c => ({
     296                    ...c.component,
     297                    quantity: c.quantity
     298            })),
    295299            reviews: reviews.map(r => ({
    296300                username: r.username,
     
    419423
    420424        const existing = await tx
    421             .select({ componentId: buildComponentsTable.componentId })
     425            .select({
     426                componentId: buildComponentsTable.componentId,
     427                numComponents: buildComponentsTable.numComponents
     428            })
    422429            .from(buildComponentsTable)
    423430            .where(eq(buildComponentsTable.buildId, buildId));
     
    428435                    buildId: newBuild.id,
    429436                    componentId: r.componentId,
     437                    numComponents: r.numComponents
    430438                })),
    431439            );
     
    545553        const components = await tx
    546554            .select({
    547                 componentId: buildComponentsTable.componentId
     555                componentId: buildComponentsTable.componentId,
     556                quantity: buildComponentsTable.numComponents
    548557            })
    549558            .from(buildComponentsTable)
     
    554563        return {
    555564            build,
    556             componentIds: components.map(c => c.componentId)
     565            components: components.map(c => ({
     566                id: c.componentId,
     567                quantity: c.quantity
     568            }))
    557569        };
    558570    });
  • database/drizzle/queries/components.ts

    rad211d1 re03e6fb  
    196196            id: componentsTable.id,
    197197            type: componentsTable.type,
     198            quantity: buildComponentsTable.numComponents
    198199        })
    199200        .from(buildComponentsTable)
     
    231232        componentsDetails.push({
    232233            ...comp,
     234            quantity: comp.quantity,
    233235            details: details
    234236        });
     
    256258
    257259    if(!existingComponents) return null;
    258 
    259 
    260260
    261261    const existing = {
     
    278278        ...existing.networkAdapters,
    279279        ...existing.soundCards
    280     ].filter(Boolean).length;
     280    ].reduce((sum: number, c: any) => {
     281        if (!c) return sum;
     282        return sum + (c.quantity || 1);
     283    }, 0);
    281284
    282285    const existingTDP = existingComponents.reduce((sum, c) => {
    283286        const tdp = c.details?.tdp ? Number(c.details.tdp) : 0;
    284         return sum + tdp;
     287        return sum + (tdp * (c.quantity || 1));
    285288    }, 0);
    286289
     
    456459        if (existing.memory.length > 0) {
    457460            const totalModules = existing.memory.reduce((sum: number, m: any) =>
    458                 sum + Number(m.details.modules), 0
     461                sum + (Number(m.details.modules) * m.quantity), 0
    459462            );
    460463            const totalCapacity = existing.memory.reduce((sum: number, m: any) =>
    461                 sum + Number(m.details.capacity), 0
     464                sum + (Number(m.details.capacity) * m.quantity), 0
    462465            );
    463466
     
    704707            if (!caseStorageFormFactor) return false;
    705708
    706             const usedSlots = existing.storage.filter(
    707                 (s: any) => s.details.formFactor === stor.formFactor
    708             ).length;
     709            const usedSlots = existing.storage
     710                .filter((s: any) => s.details.formFactor === stor.formFactor)
     711                .reduce((sum: number, s: any) => sum + s.quantity, 0);
    709712
    710713            return usedSlots < Number(caseStorageFormFactor.numSlots);
     
    821824                if (!storageFF) return false;
    822825
    823                 const usedSlots = existing.storage.filter(
    824                     (s: any) => s.details.formFactor === stor.details.formFactor
    825                 ).length;
     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);;
    826829
    827830                return usedSlots <= Number(storageFF.numSlots);
     
    963966            .values({
    964967                buildId,
    965                 componentId
     968                componentId,
     969                numComponents: 1
     970            })
     971            .onConflictDoUpdate({
     972                target: [buildComponentsTable.buildId, buildComponentsTable.componentId],
     973                set: {
     974                    numComponents: sql`${buildComponentsTable.numComponents} + 1`
     975                }
    966976            });
    967977
     
    969979            .select({
    970980                price:  componentsTable.price,
     981                quantity: buildComponentsTable.numComponents
    971982            })
    972983            .from(buildComponentsTable)
     
    979990            );
    980991
    981         const totalPrice = buildComponents.reduce((sum, c) => sum + Number(c.price), 0);
     992        const totalPrice = buildComponents.reduce((sum, c) =>
     993            sum + (Number(c.price) * c.quantity), 0
     994        );
    982995
    983996        await tx
     
    10091022        if(!build || build.isApproved) return null;
    10101023
    1011         const result = await tx
    1012             .delete(buildComponentsTable)
     1024        const [existing] = await tx
     1025            .select({
     1026                quantity: buildComponentsTable.numComponents
     1027            })
     1028            .from(buildComponentsTable)
    10131029            .where(
    10141030                and(
     
    10161032                    eq(buildComponentsTable.componentId, componentId)
    10171033                )
    1018             );
    1019 
    1020         if(result.rowCount === 0) return null;
     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        }
    10211061
    10221062        const buildComponents = await tx
    10231063            .select({
    10241064                price:  componentsTable.price,
     1065                quantity: buildComponentsTable.numComponents
    10251066            })
    10261067            .from(buildComponentsTable)
     
    10331074            );
    10341075
    1035         const totalPrice = buildComponents.reduce((sum, c) => sum + Number(c.price), 0);
     1076        const totalPrice = buildComponents.reduce((sum, c) =>
     1077            sum + (Number(c.price) * c.quantity), 0
     1078        );
    10361079
    10371080        await tx
     
    10441087            );
    10451088
    1046         return result;
     1089        return componentId;
    10471090    })
    10481091}
  • database/drizzle/schema/builds.ts

    rad211d1 re03e6fb  
    1717
    1818export const buildComponentsTable = pgTable("build_component", {
    19         id: serial("id").primaryKey(),
    2019        buildId: integer("build_id")
    2120            .notNull()
     
    2423            .notNull()
    2524            .references(() => componentsTable.id, { onDelete: "cascade", onUpdate: "cascade" }),
     25        numComponents: integer("num_components")
     26            .notNull()
     27            .default(1),
    2628    },
     29    (t) => ({
     30        pk: primaryKey({ columns: [t.buildId, t.componentId] }),
     31    }),
    2732);
    2833
  • database/migrations/0000_shallow_darkhawk.sql

    rad211d1 re03e6fb  
    11CREATE TABLE "build_component" (
    2         "id" serial PRIMARY KEY NOT NULL,
    3         "build_id" integer NOT NULL,
    4         "component_id" integer NOT NULL
     2        "build_id" integer NOT NULL,
     3        "component_id" integer NOT NULL,
     4        "num_components" integer DEFAULT 1 NOT NULL,
     5        CONSTRAINT "build_component_build_id_component_id_pk" PRIMARY KEY("build_id","component_id")
    56);
    67--> statement-breakpoint
  • database/migrations/meta/0000_snapshot.json

    rad211d1 re03e6fb  
    11{
    2   "id": "779a3882-651b-446d-8967-3728aeea2888",
     2  "id": "d1ab4ce8-e387-4d74-8d5b-511fdbe0a032",
    33  "prevId": "00000000-0000-0000-0000-000000000000",
    44  "version": "7",
     
    99      "schema": "",
    1010      "columns": {
    11         "id": {
    12           "name": "id",
    13           "type": "serial",
    14           "primaryKey": true,
    15           "notNull": true
    16         },
    1711        "build_id": {
    1812          "name": "build_id",
     
    2620          "primaryKey": false,
    2721          "notNull": true
     22        },
     23        "num_components": {
     24          "name": "num_components",
     25          "type": "integer",
     26          "primaryKey": false,
     27          "notNull": true,
     28          "default": 1
    2829        }
    2930      },
     
    5758        }
    5859      },
    59       "compositePrimaryKeys": {},
     60      "compositePrimaryKeys": {
     61        "build_component_build_id_component_id_pk": {
     62          "name": "build_component_build_id_component_id_pk",
     63          "columns": [
     64            "build_id",
     65            "component_id"
     66          ]
     67        }
     68      },
    6069      "uniqueConstraints": {},
    6170      "policies": {},
  • database/migrations/meta/_journal.json

    rad211d1 re03e6fb  
    66      "idx": 0,
    77      "version": "7",
    8       "when": 1769188258136,
    9       "tag": "0000_regular_kulan_gath",
     8      "when": 1769598141213,
     9      "tag": "0000_shallow_darkhawk",
    1010      "breakpoints": true
    1111    }
Note: See TracChangeset for help on using the changeset viewer.