|
1 | 1 | import { db } from '../../config/database'; |
2 | | -import { Recruit, TeamRecruitingRanking } from './types'; |
| 2 | +import { TransferEligibility } from '../enums'; |
| 3 | +import { Recruit, TeamRecruitingRanking, Transfer } from './types'; |
3 | 4 |
|
4 | 5 | export const getRecruits = async ( |
5 | 6 | year?: number, |
@@ -180,3 +181,143 @@ export const getTeamRankings = async ( |
180 | 181 | rating: Number(r.points), |
181 | 182 | })); |
182 | 183 | }; |
| 184 | + |
| 185 | +export const getTransfers = async ( |
| 186 | + season?: number, |
| 187 | + sourceTeam?: string, |
| 188 | + destinationTeam?: string, |
| 189 | + sourceConference?: string, |
| 190 | + destinationConference?: string, |
| 191 | + position?: string, |
| 192 | +): Promise<Transfer[]> => { |
| 193 | + let query = db |
| 194 | + .selectFrom('transfer') |
| 195 | + .innerJoin('recruitPosition', 'recruitPosition.id', 'transfer.positionId') |
| 196 | + .leftJoin('team as fromTeam', 'fromTeam.id', 'transfer.fromTeamId') |
| 197 | + .leftJoin('conferenceTeam as fromConferenceTeam', (join) => |
| 198 | + join |
| 199 | + .onRef('fromConferenceTeam.teamId', '=', 'fromTeam.id') |
| 200 | + .onRef('fromConferenceTeam.startYear', '<=', 'transfer.season') |
| 201 | + .on((eb) => |
| 202 | + eb.or([ |
| 203 | + eb('fromConferenceTeam.endYear', '>=', eb.ref('transfer.season')), |
| 204 | + eb('fromConferenceTeam.endYear', 'is', null), |
| 205 | + ]), |
| 206 | + ), |
| 207 | + ) |
| 208 | + .leftJoin( |
| 209 | + 'conference as fromConference', |
| 210 | + 'fromConference.id', |
| 211 | + 'fromConferenceTeam.conferenceId', |
| 212 | + ) |
| 213 | + .leftJoin('team as toTeam', 'toTeam.id', 'transfer.toTeamId') |
| 214 | + .leftJoin('conferenceTeam as toConferenceTeam', (join) => |
| 215 | + join |
| 216 | + .onRef('toConferenceTeam.teamId', '=', 'toTeam.id') |
| 217 | + .onRef('toConferenceTeam.startYear', '<=', 'transfer.season') |
| 218 | + .on((eb) => |
| 219 | + eb.or([ |
| 220 | + eb('toConferenceTeam.endYear', '>=', eb.ref('transfer.season')), |
| 221 | + eb('toConferenceTeam.endYear', 'is', null), |
| 222 | + ]), |
| 223 | + ), |
| 224 | + ) |
| 225 | + .leftJoin( |
| 226 | + 'conference as toConference', |
| 227 | + 'toConference.id', |
| 228 | + 'toConferenceTeam.conferenceId', |
| 229 | + ) |
| 230 | + .select([ |
| 231 | + 'transfer.id', |
| 232 | + 'transfer.sourceId', |
| 233 | + 'transfer.season', |
| 234 | + 'transfer.firstName', |
| 235 | + 'transfer.lastName', |
| 236 | + 'recruitPosition.position as position', |
| 237 | + 'fromTeam.id as sourceTeamId', |
| 238 | + 'fromTeam.school as sourceTeam', |
| 239 | + 'fromConference.abbreviation as sourceConference', |
| 240 | + 'toTeam.id as destinationTeamId', |
| 241 | + 'toTeam.school as destinationTeam', |
| 242 | + 'toConference.abbreviation as destinationConference', |
| 243 | + 'transfer.stars', |
| 244 | + 'transfer.rating', |
| 245 | + 'transfer.eligibility', |
| 246 | + 'transfer.yearsRemaining', |
| 247 | + ]) |
| 248 | + .orderBy('transfer.season', 'desc') |
| 249 | + .orderBy('transfer.lastName') |
| 250 | + .orderBy('transfer.firstName'); |
| 251 | + |
| 252 | + if (season) { |
| 253 | + query = query.where('transfer.season', '=', season); |
| 254 | + } |
| 255 | + |
| 256 | + if (sourceTeam) { |
| 257 | + query = query.where( |
| 258 | + (eb) => eb.fn('lower', [eb.ref('fromTeam.school')]), |
| 259 | + '=', |
| 260 | + sourceTeam.toLowerCase(), |
| 261 | + ); |
| 262 | + } |
| 263 | + |
| 264 | + if (destinationTeam) { |
| 265 | + query = query.where( |
| 266 | + (eb) => eb.fn('lower', [eb.ref('toTeam.school')]), |
| 267 | + '=', |
| 268 | + destinationTeam.toLowerCase(), |
| 269 | + ); |
| 270 | + } |
| 271 | + |
| 272 | + if (sourceConference) { |
| 273 | + query = query.where( |
| 274 | + (eb) => eb.fn('lower', [eb.ref('fromConference.abbreviation')]), |
| 275 | + '=', |
| 276 | + sourceConference.toLowerCase(), |
| 277 | + ); |
| 278 | + } |
| 279 | + |
| 280 | + if (destinationConference) { |
| 281 | + query = query.where( |
| 282 | + (eb) => eb.fn('lower', [eb.ref('toConference.abbreviation')]), |
| 283 | + '=', |
| 284 | + destinationConference.toLowerCase(), |
| 285 | + ); |
| 286 | + } |
| 287 | + |
| 288 | + if (position) { |
| 289 | + query = query.where( |
| 290 | + (eb) => eb.fn('lower', [eb.ref('recruitPosition.position')]), |
| 291 | + '=', |
| 292 | + position.toLowerCase(), |
| 293 | + ); |
| 294 | + } |
| 295 | + |
| 296 | + const transfers = await query.execute(); |
| 297 | + return transfers.map((t) => ({ |
| 298 | + id: t.id, |
| 299 | + sourceId: t.sourceId, |
| 300 | + year: t.season, |
| 301 | + firstName: t.firstName, |
| 302 | + lastName: t.lastName, |
| 303 | + position: t.position, |
| 304 | + origin: t.sourceTeamId |
| 305 | + ? { |
| 306 | + id: t.sourceTeamId, |
| 307 | + name: t.sourceTeam, |
| 308 | + conference: t.sourceConference, |
| 309 | + } |
| 310 | + : null, |
| 311 | + destination: t.destinationTeamId |
| 312 | + ? { |
| 313 | + id: t.destinationTeamId, |
| 314 | + name: t.destinationTeam, |
| 315 | + conference: t.destinationConference, |
| 316 | + } |
| 317 | + : null, |
| 318 | + stars: t.stars, |
| 319 | + rating: t.rating ? Number(t.rating) : null, |
| 320 | + eligibility: t.eligibility ? (t.eligibility as TransferEligibility) : null, |
| 321 | + yearsRemaining: t.yearsRemaining, |
| 322 | + })); |
| 323 | +}; |
0 commit comments