Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix/duplicates #2

Merged
merged 3 commits into from
Mar 26, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions docker-compose.yml
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ volumes:

services:
pocketbase:
image: ghcr.io/bennetrr/lego-pocketbase:v1.0.0
image: ghcr.io/bennetrr/lego-pocketbase:v1.0.1
container_name: lego-pocketBase
restart: always
ports:
Expand All @@ -16,7 +16,7 @@ services:
target: /pb/

app:
image: ghcr.io/bennetrr/lego-app:v1.0.0
image: ghcr.io/bennetrr/lego-app:v1.0.1
container_name: lego-app
restart: always
ports:
Expand Down
19 changes: 18 additions & 1 deletion src/connectors/RebrickableAPI.ts
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ export default class RebrickableApi {
const minifigsResponse = await this.ax.get<RebrickableSetMinifigs>(`sets/${setNumber}/minifigs/?page_size=10000`);
const minifigsResponseData = minifigsResponse.data.results;

const partsData: LegoPartsRecord[] = partsResponseData.map(part => ({
const partsDataUnCleaned: LegoPartsRecord[] = partsResponseData.map(part => ({
part_number: part.part.part_num,
part_name: part.part.name,
color_name: part.color.name,
Expand All @@ -54,6 +54,23 @@ export default class RebrickableApi {
set: undefined
}));

// Rebrickable has multiple entries for the same part when there are spare parts
// Combine these entries in one single entry
let partsData: LegoPartsRecord[] = [];

// Sets only allow unique items
const partIdentifier = new Set(partsDataUnCleaned.map(part => `${part.part_number}###${part.color_name}`));

partIdentifier.forEach(identifier => {
// Get all parts with the same product number
const partEntries = partsDataUnCleaned.filter(part => identifier == `${part.part_number}###${part.color_name}`);
const newPartEntry = partEntries[0];

// Add up the total counts of every entry
newPartEntry.total_count = partEntries.map(part => part.total_count).reduce((a, b) => a + b);
partsData = [...partsData, newPartEntry];
});

const minifigsData: LegoPartsRecord[] = minifigsResponseData.map(minifig => ({
part_number: minifig.set_num,
part_name: minifig.set_name,
Expand Down