-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
chore(merge): merge develop into main
- Loading branch information
Showing
7 changed files
with
257 additions
and
11 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
export * from './twizzit-to-player-db'; |
239 changes: 239 additions & 0 deletions
239
apps/scripts/src/app/scripts/twizzit-to-player-db/twizzit-to-player-db.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,239 @@ | ||
import { Player } from '@badman/backend-database'; | ||
import { Injectable, Logger } from '@nestjs/common'; | ||
import moment from 'moment'; | ||
import { Op, Transaction } from 'sequelize'; | ||
import { Sequelize } from 'sequelize-typescript'; | ||
import * as XLSX from 'xlsx'; | ||
|
||
@Injectable() | ||
export class TwizzitToPlayerDbService { | ||
private readonly logger = new Logger(TwizzitToPlayerDbService.name); | ||
|
||
constructor(private _sequelize: Sequelize) {} | ||
|
||
async process() { | ||
const transaction = await this._sequelize.transaction(); | ||
|
||
try { | ||
this.logger.log('Starting resync'); | ||
await this.setPlayerIndexes(transaction); | ||
await transaction.commit(); | ||
this.logger.log('Done'); | ||
} catch (e) { | ||
await transaction.rollback(); | ||
throw e; | ||
} | ||
} | ||
|
||
private async setPlayerIndexes(transaction?: Transaction) { | ||
const workbook = XLSX.readFile( | ||
'./apps/scripts/src/app/scripts/twizzit-to-player-db/2024-12-18_12_51_42.xlsx', | ||
); | ||
const rows = XLSX.utils.sheet_to_json<InputExcel>(workbook.Sheets[workbook.SheetNames[0]]); | ||
|
||
// Find in Player table the player with the same naam and voornaam | ||
|
||
const inputPlayers: [lastName: string, firstName: string][] = []; | ||
|
||
for (const row of rows) { | ||
inputPlayers.push([row.Achternaam, row.Voornaam]); | ||
inputPlayers.push([row['Dubbelpartner Achternaam'], row.Dubbelpartner]); | ||
inputPlayers.push([row['Mixpartner Achternaam'], row.Mixpartner]); | ||
} | ||
|
||
const players = await Player.findAll({ | ||
transaction, | ||
where: { | ||
[Op.or]: inputPlayers | ||
.filter(([lastName, firstName]) => lastName && firstName) | ||
.map(([lastName, firstName]) => ({ | ||
lastName, | ||
firstName, | ||
})), | ||
}, | ||
}); | ||
|
||
this.logger.log('Found ' + players.length + ' players'); | ||
|
||
const glenn = players.find((player) => player.slug == 'glenn-latomme'); | ||
|
||
if (glenn) { | ||
this.logger.log('Found glenn'); | ||
} | ||
const playerPerLevel = new Map<string, [Player, Player][]>(); | ||
|
||
rows.forEach((row) => { | ||
let playerDb = players.filter( | ||
(player) => player.lastName == row.Achternaam && player.firstName == row.Voornaam, | ||
); | ||
|
||
let plyerDubbelpartnerDb = players.filter( | ||
(player) => | ||
player.lastName == row['Dubbelpartner Achternaam'] && | ||
player.firstName == row.Dubbelpartner, | ||
); | ||
|
||
let plyerMixpartnerDb = players.filter( | ||
(player) => | ||
player.lastName == row['Mixpartner Achternaam'] && player.firstName == row.Mixpartner, | ||
); | ||
|
||
if (!playerPerLevel.has('All')) { | ||
playerPerLevel.set('All', []); | ||
} | ||
|
||
// take the first player with a starting 5 memberid, else with any gender filled in, else with the memberid not null | ||
let player = | ||
playerDb.find((player) => player.memberId?.startsWith('5')) ?? | ||
playerDb.find((player) => player.gender != null) ?? | ||
playerDb.find((player) => player.memberId != null) ?? | ||
playerDb[0]; | ||
let plyerDubbelpartner = | ||
plyerDubbelpartnerDb.find((player) => player.memberId?.startsWith('5')) ?? | ||
plyerDubbelpartnerDb.find((player) => player.gender != null) ?? | ||
plyerDubbelpartnerDb.find((player) => player.memberId != null) ?? | ||
plyerDubbelpartnerDb[0]; | ||
let plyerMixpartner = | ||
plyerMixpartnerDb.find((player) => player.memberId?.startsWith('5')) ?? | ||
plyerMixpartnerDb.find((player) => player.gender != null) ?? | ||
plyerMixpartnerDb.find((player) => player.memberId != null) ?? | ||
plyerMixpartnerDb[0]; | ||
|
||
if (!player && row.Achternaam && row.Voornaam) { | ||
player = new Player({ | ||
lastName: row.Achternaam, | ||
firstName: row.Voornaam, | ||
}); | ||
} | ||
|
||
if (!plyerDubbelpartner && row['Dubbelpartner Achternaam'] && row.Dubbelpartner) { | ||
plyerDubbelpartner = new Player({ | ||
lastName: row['Dubbelpartner Achternaam'], | ||
firstName: row.Dubbelpartner, | ||
}); | ||
} | ||
|
||
if (!plyerMixpartner && row['Mixpartner Achternaam'] && row.Mixpartner) { | ||
plyerMixpartner = new Player({ | ||
lastName: row['Mixpartner Achternaam'], | ||
firstName: row.Mixpartner, | ||
}); | ||
} | ||
// add all players to the first tab | ||
if (player) { | ||
playerPerLevel.get('All')?.push([player, null]); | ||
} | ||
|
||
if (plyerDubbelpartner) { | ||
playerPerLevel.get('All')?.push([plyerDubbelpartner, null]); | ||
} | ||
|
||
if (plyerMixpartner) { | ||
playerPerLevel.get('All')?.push([plyerMixpartner, null]); | ||
} | ||
|
||
// if player and dubble player add to map | ||
if (row['Dubbel op zondag'] != 'Geen dubbel') { | ||
// add to map based on value of Dubbel on zondag's value | ||
let key = `${player.gender}-${row['Dubbel op zondag']}`; | ||
if ( | ||
row['Dubbel op zondag'] == 'Zeer sterk (sterke/zeer sterke recreant of klassement 11-12)' | ||
) { | ||
key = 'GD-Zeer sterk'; | ||
} | ||
|
||
if (!playerPerLevel.has(key)) { | ||
playerPerLevel.set(key, []); | ||
} | ||
|
||
playerPerLevel.get(key)?.push([player, plyerDubbelpartner]); | ||
} | ||
|
||
// distinct 'all' players on firstname and lastname | ||
playerPerLevel.set('All', this.makeUniqueByFullName(playerPerLevel.get('All'))); | ||
|
||
// if player and mix player add to map | ||
if (row['Mix op zaterdag'] != 'Geen mix') { | ||
// add to map based on value of Mix op zaterdag's value | ||
let key = `GD-${row['Mix op zaterdag']}`; | ||
if ( | ||
row['Mix op zaterdag'] == 'Zeer sterk (sterke/zeer sterke recreant of klassement 11-12)' | ||
) { | ||
key = 'GD-Zeer sterk'; | ||
} | ||
if (!playerPerLevel.has(key)) { | ||
playerPerLevel.set(key, []); | ||
} | ||
|
||
playerPerLevel.get(key)?.push([player, plyerMixpartner]); | ||
} | ||
}); | ||
|
||
// create a sheet per key with all players | ||
const wb = XLSX.utils.book_new(); | ||
|
||
for (const [key, players] of playerPerLevel) { | ||
const ws = XLSX.utils.json_to_sheet( | ||
players.map(([p, partner]) => ({ | ||
Lidnummer: p.memberId, | ||
Naam: p.lastName, | ||
Voornaam: p.firstName, | ||
Geslacht: p.gender, | ||
PartnerLidnummer: partner?.memberId, | ||
PartnerNaam: partner?.lastName, | ||
PartnerVoornaam: partner?.firstName, | ||
PartnerGeslacht: partner?.gender, | ||
})), | ||
); | ||
|
||
XLSX.utils.book_append_sheet(wb, ws, key); | ||
} | ||
XLSX.writeFile( | ||
wb, | ||
'./apps/scripts/src/app/scripts/twizzit-to-player-db/player-export' + | ||
moment().format('YYYY-MM-DD_HH-mm-ss') + | ||
'.xlsx', | ||
); | ||
} | ||
|
||
makeUniqueByFullName(array: [Player, Player][]): [Player, Player][] { | ||
const seen = new Set<string>(); | ||
return array.filter(([person]) => { | ||
const identifier = `${person.firstName}-${person.lastName}`; | ||
if (seen.has(identifier)) { | ||
return false; | ||
} | ||
seen.add(identifier); | ||
return true; | ||
}); | ||
} | ||
} | ||
|
||
interface InputExcel { | ||
Id: string; | ||
'Created On': string; | ||
'Created By': string; | ||
'Dubbel op zondag': string; | ||
'Mix op zaterdag': string; | ||
Voornaam: string; | ||
Achternaam: string; | ||
Telefoon: string; | ||
Email: string; | ||
'Klassement dubbel': string; | ||
'Klassement mix': string; | ||
'Ik schrijf in': string; | ||
Dubbelpartner: string; | ||
'Dubbelpartner Achternaam': string; | ||
'Klassement partner dubbel': string; | ||
Mixpartner: string; | ||
'Mixpartner Achternaam': string; | ||
'Klassement partner mix': string; | ||
'Extra opmerkingen/telefoonnummers:': string; | ||
'Linked contact IDs': string; | ||
} | ||
|
||
interface OutputExcel { | ||
Lidnummer: string; | ||
Naam: string; | ||
Voornaam: string; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters