Skip to content

Commit 5eb8a52

Browse files
committedAug 18, 2024
Next free storage location calculation corrected
1 parent 5040c98 commit 5eb8a52

File tree

1 file changed

+24
-6
lines changed

1 file changed

+24
-6
lines changed
 

‎backend/src/wines/storage-location.service.ts

+24-6
Original file line numberDiff line numberDiff line change
@@ -54,9 +54,11 @@ export class StorageLocationService {
5454
const maxRows = 19;
5555
const shelvesPerRow = 6;
5656

57+
const result: StorageLocationDto[] = [];
58+
5759
const occupiedStorageLocations: StorageLocationDto[] = await this.getOccupiedStorageLocations();
5860

59-
for (let row = 1; row < maxRows; row++) {
61+
for (let row = 1; row <= maxRows; row++) {
6062
const occupiedStorageLocationsInRow: StorageLocationDto[] =
6163
occupiedStorageLocations.filter((storageLocation: StorageLocationDto) => storageLocation.row === row);
6264

@@ -65,7 +67,7 @@ export class StorageLocationService {
6567
continue;
6668
}
6769

68-
for (let shelf = 1; shelf < shelvesPerRow; shelf++) {
70+
for (let shelf = 1; shelf <= shelvesPerRow; shelf++) {
6971
if (occupiedStorageLocationsInRow.some((storageLocation: StorageLocationDto) => storageLocation.shelf === shelf)) {
7072
continue;
7173
}
@@ -84,10 +86,26 @@ export class StorageLocationService {
8486
private async getOccupiedStorageLocations(): Promise<StorageLocationDto[]> {
8587
const wines: WineDto[] = await this.wineService.getAllWines()
8688

87-
return wines.flatMap((wine: WineDto) =>
89+
const storageLocations: StorageLocationDto[] = wines.flatMap((wine: WineDto) =>
8890
wine.vintageInfos.flatMap(
89-
(vintageInfo: VintageInfoDto) => vintageInfo.storageLocations,
90-
),
91-
);
91+
(vintageInfo: VintageInfoDto) => vintageInfo.storageLocations));
92+
93+
const storageLocationsSorted: StorageLocationDto[] =
94+
storageLocations.sort((a: StorageLocationDto, b: StorageLocationDto) =>
95+
this.sortStorageLocations(a, b));
96+
97+
return storageLocationsSorted;
98+
}
99+
100+
private sortStorageLocations(a: StorageLocationDto, b: StorageLocationDto): number {
101+
if (a.row < b.row) {
102+
return -1;
103+
}
104+
105+
if (a.row > b.row) {
106+
return 1;
107+
}
108+
109+
return (a.shelf < b.shelf) ? -1 : 1;
92110
}
93111
}

0 commit comments

Comments
 (0)