Changeset e03e6fb for database/drizzle/queries/components.ts
- Timestamp:
- 01/28/26 12:12:43 (5 months ago)
- Branches:
- main
- Children:
- a744c90
- Parents:
- ad211d1
- File:
-
- 1 edited
-
database/drizzle/queries/components.ts (modified) (14 diffs)
Legend:
- Unmodified
- Added
- Removed
-
database/drizzle/queries/components.ts
rad211d1 re03e6fb 196 196 id: componentsTable.id, 197 197 type: componentsTable.type, 198 quantity: buildComponentsTable.numComponents 198 199 }) 199 200 .from(buildComponentsTable) … … 231 232 componentsDetails.push({ 232 233 ...comp, 234 quantity: comp.quantity, 233 235 details: details 234 236 }); … … 256 258 257 259 if(!existingComponents) return null; 258 259 260 260 261 261 const existing = { … … 278 278 ...existing.networkAdapters, 279 279 ...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); 281 284 282 285 const existingTDP = existingComponents.reduce((sum, c) => { 283 286 const tdp = c.details?.tdp ? Number(c.details.tdp) : 0; 284 return sum + tdp;287 return sum + (tdp * (c.quantity || 1)); 285 288 }, 0); 286 289 … … 456 459 if (existing.memory.length > 0) { 457 460 const totalModules = existing.memory.reduce((sum: number, m: any) => 458 sum + Number(m.details.modules), 0461 sum + (Number(m.details.modules) * m.quantity), 0 459 462 ); 460 463 const totalCapacity = existing.memory.reduce((sum: number, m: any) => 461 sum + Number(m.details.capacity), 0464 sum + (Number(m.details.capacity) * m.quantity), 0 462 465 ); 463 466 … … 704 707 if (!caseStorageFormFactor) return false; 705 708 706 const usedSlots = existing.storage .filter(707 (s: any) => s.details.formFactor === stor.formFactor708 ).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); 709 712 710 713 return usedSlots < Number(caseStorageFormFactor.numSlots); … … 821 824 if (!storageFF) return false; 822 825 823 const usedSlots = existing.storage .filter(824 (s: any) => s.details.formFactor === stor.details.formFactor825 ).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);; 826 829 827 830 return usedSlots <= Number(storageFF.numSlots); … … 963 966 .values({ 964 967 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 } 966 976 }); 967 977 … … 969 979 .select({ 970 980 price: componentsTable.price, 981 quantity: buildComponentsTable.numComponents 971 982 }) 972 983 .from(buildComponentsTable) … … 979 990 ); 980 991 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 ); 982 995 983 996 await tx … … 1009 1022 if(!build || build.isApproved) return null; 1010 1023 1011 const result = await tx 1012 .delete(buildComponentsTable) 1024 const [existing] = await tx 1025 .select({ 1026 quantity: buildComponentsTable.numComponents 1027 }) 1028 .from(buildComponentsTable) 1013 1029 .where( 1014 1030 and( … … 1016 1032 eq(buildComponentsTable.componentId, componentId) 1017 1033 ) 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 } 1021 1061 1022 1062 const buildComponents = await tx 1023 1063 .select({ 1024 1064 price: componentsTable.price, 1065 quantity: buildComponentsTable.numComponents 1025 1066 }) 1026 1067 .from(buildComponentsTable) … … 1033 1074 ); 1034 1075 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 ); 1036 1079 1037 1080 await tx … … 1044 1087 ); 1045 1088 1046 return result;1089 return componentId; 1047 1090 }) 1048 1091 }
Note:
See TracChangeset
for help on using the changeset viewer.
