@@ -54,9 +54,11 @@ export class StorageLocationService {
54
54
const maxRows = 19 ;
55
55
const shelvesPerRow = 6 ;
56
56
57
+ const result : StorageLocationDto [ ] = [ ] ;
58
+
57
59
const occupiedStorageLocations : StorageLocationDto [ ] = await this . getOccupiedStorageLocations ( ) ;
58
60
59
- for ( let row = 1 ; row < maxRows ; row ++ ) {
61
+ for ( let row = 1 ; row <= maxRows ; row ++ ) {
60
62
const occupiedStorageLocationsInRow : StorageLocationDto [ ] =
61
63
occupiedStorageLocations . filter ( ( storageLocation : StorageLocationDto ) => storageLocation . row === row ) ;
62
64
@@ -65,7 +67,7 @@ export class StorageLocationService {
65
67
continue ;
66
68
}
67
69
68
- for ( let shelf = 1 ; shelf < shelvesPerRow ; shelf ++ ) {
70
+ for ( let shelf = 1 ; shelf <= shelvesPerRow ; shelf ++ ) {
69
71
if ( occupiedStorageLocationsInRow . some ( ( storageLocation : StorageLocationDto ) => storageLocation . shelf === shelf ) ) {
70
72
continue ;
71
73
}
@@ -84,10 +86,26 @@ export class StorageLocationService {
84
86
private async getOccupiedStorageLocations ( ) : Promise < StorageLocationDto [ ] > {
85
87
const wines : WineDto [ ] = await this . wineService . getAllWines ( )
86
88
87
- return wines . flatMap ( ( wine : WineDto ) =>
89
+ const storageLocations : StorageLocationDto [ ] = wines . flatMap ( ( wine : WineDto ) =>
88
90
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 ;
92
110
}
93
111
}
0 commit comments