@@ -8,7 +8,7 @@ const { Op } = require('sequelize');
8
8
9
9
const validator = require ( '@byba/express-validator' ) ;
10
10
11
- const { helpers : { SlugHelper : { formatSlug } } } = require ( __lib ) ;
11
+ const { helpers : { StringHelper : { formatSlug } , SortHelper } } = require ( __lib ) ;
12
12
13
13
const router = express . Router ( ) ;
14
14
@@ -91,43 +91,6 @@ async function recalculateEntryFees(db, season, typeID, org) {
91
91
}
92
92
}
93
93
94
- function shuffleArray ( array ) {
95
- for ( let i = array . length - 1 ; i > 0 ; i -- ) {
96
- const j = Math . floor ( Math . random ( ) * ( i + 1 ) ) ;
97
- [ array [ i ] , array [ j ] ] = [ array [ j ] , array [ i ] ] ;
98
- }
99
-
100
- return array ;
101
- }
102
-
103
- function leagueSort ( entries ) {
104
- entries . sort ( ( a , b ) => {
105
- const aMember = a . Organisation . OrganisationMemberships ;
106
- const aScore = aMember . length == 0 ? 0 : aMember [ 0 ] . LeagueScore ;
107
-
108
- const bMember = b . Organisation . OrganisationMemberships ;
109
- const bScore = bMember . length == 0 ? 0 : bMember [ 0 ] . LeagueScore ;
110
-
111
- return aScore - bScore ;
112
- } ) ;
113
-
114
- return entries ;
115
- }
116
-
117
- function entrySort ( array , direction ) {
118
- array . sort ( ( a , b ) => {
119
- const d1 = new Date ( a . EntryDate ) , d2 = new Date ( b . EntryDate ) ;
120
-
121
- if ( direction === 'asc' ) {
122
- return d1 - d2 ;
123
- }
124
-
125
- return d2 - d1 ;
126
- } ) ;
127
-
128
- return array ;
129
- }
130
-
131
94
function splitEntries ( entries , cutoff ) {
132
95
const late = [ ] , regular = [ ] ;
133
96
@@ -218,15 +181,15 @@ async function generateSchedule(req, next, eventID, config, divisionOrder) {
218
181
let late = [ ] , regular = div ;
219
182
if ( config . LateOnFirst ) {
220
183
[ late , regular ] = splitEntries ( div , cutoff ) ;
221
- entrySort ( late , 'desc' ) ;
184
+ late = late . sort ( ( a , b ) => SortHelper . dateProp ( 'EntryDate' , a , b , false ) ) ;
222
185
}
223
186
224
187
if ( config . Type . indexOf ( 'entry' ) > - 1 ) {
225
- entrySort ( regular , config . Type . split ( '-' ) [ 1 ] ) ;
188
+ regular = regular . sort ( ( a , b ) => SortHelper . dateProp ( 'EntryDate' , a , b , config . Type . split ( '-' ) [ 1 ] == 'asc' ) ) ;
226
189
} else if ( config . Type == 'league' ) {
227
- leagueSort ( regular ) ;
190
+ regular = regular . sort ( ( a , b ) => SortHelper . compare ( a . Organisation . OrganisationMemberships [ 0 ] ?. LeagueScore , b . Organisation . OrganisationMemberships [ 0 ] ?. LeagueScore ) ) ;
228
191
} else {
229
- shuffleArray ( regular ) ;
192
+ regular = SortHelper . shuffleArray ( regular ) ;
230
193
}
231
194
232
195
div = late . concat ( regular ) ;
@@ -288,7 +251,10 @@ router.get('/', async (req, res, next) => {
288
251
] ,
289
252
where : {
290
253
SeasonId : season . id
291
- }
254
+ } ,
255
+ order : [
256
+ [ 'Start' ]
257
+ ]
292
258
} ) ,
293
259
req . db . EventType . findAll ( )
294
260
] ) ;
0 commit comments