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