Skip to content

Commit

Permalink
feat: ✨ copyTournamentRecord: new method for tournamentGovernor
Browse files Browse the repository at this point in the history
  • Loading branch information
CourtHive committed Apr 22, 2024
1 parent d70dce7 commit 0bd0e92
Show file tree
Hide file tree
Showing 2 changed files with 54 additions and 0 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
import { checkRequiredParameters } from '@Helpers/parameters/checkRequiredParameters';
import { addDays, extractDate } from '@Tools/dateTime';
import { UUID } from '@Tools/UUID';

// Constants
import { SUCCESS } from '@Constants/resultConstants';
import { Tournament } from '@Types/tournamentTypes';

type CopyTournamentRecordArgs = {
tournamentRecord: Tournament;
copyParticipants?: boolean;
tournamentName: string;
startDate: string;
endDate?: string;
};

export function copyTournamentRecord(params: CopyTournamentRecordArgs) {
const paramsCheck = checkRequiredParameters(params, [
{ tournamentRecord: true, startDate: true, endDate: false, tournamentName: true },
]);
if (paramsCheck.error) return paramsCheck;

const { startDate, endDate } = params.tournamentRecord;
const dayMilliseconds = 24 * 60 * 60 * 1000;
const tournamentDayMilliseconds =
startDate && endDate ? new Date(extractDate(endDate)).getTime() - new Date(extractDate(startDate)).getTime() : 0;
const tournamentDays = tournamentDayMilliseconds / dayMilliseconds;
const newEndDate = params.endDate || addDays(params.startDate, tournamentDays);

const copyParticipant = (participant) => {
const { timeItems, ...rest } = participant;
return { ...rest };
};

const copyEvent = (event) => {
const { drawDefinitions, ...rest } = event;
return { ...rest };
};

const tournamentRecord = {
participants: params.copyParticipants ? params.tournamentRecord.participants?.map(copyParticipant) ?? [] : [],
parentOrganisation: { ...params.tournamentRecord.parentOrganisation },
events: params.tournamentRecord.events?.map(copyEvent) ?? [],
weekdays: { ...params.tournamentRecord.weekdays },
venues: { ...params.tournamentRecord.venues },
tournamentName: params.tournamentName,
startDate: params.startDate,
tournamentId: UUID(),
endDate: newEndDate,
};

return { ...SUCCESS, tournamentRecord };
}
1 change: 1 addition & 0 deletions src/assemblies/governors/tournamentGovernor/generate.ts
Original file line number Diff line number Diff line change
@@ -1 +1,2 @@
export { createTournamentRecord } from '@Generators/tournamentRecords/createTournamentRecord';
export { copyTournamentRecord } from '@Generators/tournamentRecords/copyTournamentRecord';

0 comments on commit 0bd0e92

Please sign in to comment.