Skip to content

Commit

Permalink
feat: support setting of clubs
Browse files Browse the repository at this point in the history
  • Loading branch information
cskiwi committed Dec 31, 2024
1 parent db992de commit fecc7ed
Show file tree
Hide file tree
Showing 13 changed files with 716 additions and 306 deletions.
9 changes: 9 additions & 0 deletions .vscode/launch.json
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,15 @@
// For more information, visit: https://go.microsoft.com/fwlink/?linkid=830387
"version": "0.2.0",
"configurations": [
{
"name": "Attach by Process ID",
"processId": "${command:PickProcess}",
"request": "attach",
"skipFiles": [
"<node_internals>/**"
],
"type": "node"
},
{
"name": "Server",
"type": "node",
Expand Down
28 changes: 28 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -97,3 +97,31 @@ https://github.com/rakyll/hey

- `docker build -t badman . -f apps/api/Dockerfile`
- `docker run badman --env-file .env -p 5001:5001`

### todo

- [ ] Rename club: plumpjes ternat -> Carpe Pluma Ternat
- [ ] Rename club: psv Brugge -> Koninklijke Badmintonclub PSV Brugge
- [ ] Rename club: De valkaart -> DE VALKAART BC
- [ ] Rename club: Hamse BC (L) -> Hamse BC (L)
- [ ] Rename club: Very Bad'Lobbes -> Lob'Bad
- [ ] create new club: Badminton Club Moorsel
- [ ] create new club: Asbl Les Rollingchairs
- [ ] create new club: The power to smash
- [ ] create new club: Shuttle Badminton
- [ ] create new club: Badmintonclub Aalter
- [ ] create new club: Fun Team Leuven
- [ ] remove memberships:

```sql

DELETE FROM "ClubPlayerMemberships"
WHERE
"playerId" IN (
'3028b2fc-68df-445d-a699-f6388f76303e',
'38386ba2-34fb-45ad-ab8a-f0774bcb19da',
'85e9bc2e-6a42-455a-b91f-958ca3bc4a16'
)
AND "end" = NULL

```
2 changes: 2 additions & 0 deletions libs/backend/database/src/database.module.ts
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,8 @@ export class DatabaseModule implements OnModuleInit {
await loadTest();
}

this.sequelize.options.logging = false;

this.logger.debug('initialize addons');
slugifyModel(Player as unknown as Model, {
source: ['firstName', 'lastName', 'memberId'],
Expand Down
2 changes: 2 additions & 0 deletions libs/backend/database/src/provider/config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,8 @@ export class SequelizeConfigProvider implements SequelizeOptionsFactory {

// log the options when in development
if (env !== 'production') {
console.log(this.configService.get<boolean>('DB_LOGGING'));

this.logger.debug({
logging: this.configService.get<boolean>('DB_LOGGING'),
dialect: options.dialect,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -224,7 +224,7 @@ export class EncounterChangeCompetitionResolver {
],
},
],
logging: (msg) => this.logger.debug(msg),
// logging: (msg) => this.logger.debug(msg),
});

// can request new dates in timezone europe/brussels
Expand Down
98 changes: 66 additions & 32 deletions libs/backend/ranking/src/controllers/upload.controller.ts
Original file line number Diff line number Diff line change
@@ -1,18 +1,22 @@
import { File, MultipartFile, UploadGuard } from '@badman/backend-utils';
import { MultipartValue } from '@fastify/multipart';
import { Controller, Logger, Post, Res, UseGuards } from '@nestjs/common';
import { FastifyReply } from 'fastify';
import moment from 'moment';
import { Worker } from 'worker_threads';
import * as XLSX from 'xlsx';
import { MembersRolePerGroupData, UpdateRankingService } from '../services';
import { UploadGuard, MultipartFile, File } from '@badman/backend-utils';
import workerThreadFilePath from '../worker/config';
import { Worker } from 'worker_threads';
import { ConfigService } from '@nestjs/config';

@Controller('ranking/upload')
export class UploadRankingController {
private readonly _logger = new Logger(UploadRankingController.name);

constructor(private _updateRankingService: UpdateRankingService) {}
constructor(
private _updateRankingService: UpdateRankingService,
private _config: ConfigService,
) {}

@Post('preview')
@UseGuards(UploadGuard)
Expand All @@ -36,12 +40,7 @@ export class UploadRankingController {
(file.fields['updateCompStatus'] as MultipartValue)?.value === 'true';
const updateRanking = (file.fields['updateRanking'] as MultipartValue)?.value === 'true';
const rankingDate = moment((file.fields['rankingDate'] as MultipartValue)?.value as string);
const clubMembershipStartDate = moment(
(file.fields['clubMembershipStartDate'] as MultipartValue)?.value as string,
);
const clubMembershipEndDate = moment(
(file.fields['clubMembershipEndDate'] as MultipartValue)?.value as string,
);

const removeAllRanking =
(file.fields['removeAllRanking'] as MultipartValue)?.value === 'true';
const updatePossible = (file.fields['updatePossible'] as MultipartValue)?.value === 'true';
Expand All @@ -57,29 +56,43 @@ export class UploadRankingController {

res.send({ message: true });

const worker = new Worker(workerThreadFilePath, {
workerData: JSON.stringify({
updateCompStatus,
updateRanking,
updatePossible,
updateClubs,
rankingDate,
clubMembershipStartDate,
clubMembershipEndDate,
removeAllRanking,
rankingSystemId,
createNewPlayers,
mappedData,
}),
});

worker.on('message', () => {
this._logger.verbose('Done');
});
worker.on('error', (e) => {
return this._logger.error('on error', e);
});
worker.on('exit', (code) => this._logger.log('on exit', code));
if (this._config.get('NODE_ENV') === 'development') {
try {
this._updateRankingService.processFileUpload(mappedData, {
updateCompStatus,
updateRanking,
updatePossible,
updateClubs,
rankingDate: rankingDate.toDate(),
removeAllRanking,
rankingSystemId,
createNewPlayers,
});
} catch (e) {
this._logger.error('Error processing file', e);
}
} else {
const worker = new Worker(workerThreadFilePath, {
workerData: JSON.stringify({
updateCompStatus,
updateRanking,
updatePossible,
updateClubs,
rankingDate,
removeAllRanking,
rankingSystemId,
createNewPlayers,
mappedData,
}),
});

worker.on('error', (e) => {
return this._logger.error('on error', e);
});
worker.on('exit', (code) => {
this._logger.log('Processing existed', code);
});
}
});
}

Expand Down Expand Up @@ -154,12 +167,17 @@ export class UploadRankingController {
row['lastname2']?.trim(),
]?.filter((name) => !!name);

const startdate = this.parseExcelDate(row['startdate']);
const enddate = this.parseExcelDate(row['endate']);

return {
memberId: row['memberid'],
firstName: row['firstname'],
lastName: names?.join(' '),
clubName: row['groupname'],
gender: row['gender'],
startdate: new Date(startdate.year, startdate.month - 1, startdate.day),
enddate: new Date(enddate.year, enddate.month - 1, enddate.day),
single: row['PlayerLevelSingle'],
doubles: row['PlayerLevelDouble'],
mixed: row['PlayerLevelMixed'],
Expand Down Expand Up @@ -197,6 +215,20 @@ export class UploadRankingController {

return [...players.values()];
}

private parseExcelDate(excelDate: number) {
if (excelDate < 61) excelDate += 1; // lotus123 bugfix
const utc = new Date((excelDate - 25569) * 8.64e7);
return {
year: utc.getUTCFullYear(),
month: utc.getUTCMonth() + 1,
day: utc.getUTCDate(),
hours: utc.getUTCHours(),
minutes: utc.getUTCMinutes(),
seconds: utc.getUTCSeconds(),
milliseconds: utc.getUTCMilliseconds(),
};
}
}

interface bbfRating {
Expand All @@ -217,6 +249,8 @@ interface exportMembers {
lastname2: string;
gender: 'V' | 'M';
groupname: string;
startdate: number;
endate: number;
PlayerLevelSingle: number;
PlayerLevelDouble: number;
PlayerLevelMixed: number;
Expand Down
9 changes: 8 additions & 1 deletion libs/backend/ranking/src/ranking.module.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,17 @@ import { RankingController, UploadRankingController } from './controllers';
import { CalculationService, PlaceService, PointsService, UpdateRankingService } from './services';
import { BelgiumFlandersPlacesModule } from '@badman/belgium-flanders-places';
import { BelgiumFlandersPointsModule } from '@badman/belgium-flanders-points';
import { ConfigModule } from '@nestjs/config';

@Module({
controllers: [UploadRankingController, RankingController],
imports: [DatabaseModule, QueueModule, BelgiumFlandersPlacesModule, BelgiumFlandersPointsModule],
imports: [
DatabaseModule,
QueueModule,
BelgiumFlandersPlacesModule,
BelgiumFlandersPointsModule,
ConfigModule,
],
providers: [PointsService, CalculationService, PlaceService, UpdateRankingService],
exports: [CalculationService, PointsService, PlaceService],
})
Expand Down
Loading

0 comments on commit fecc7ed

Please sign in to comment.