From 0bd0e92419b2c225f155dde4bd58b94961471d5e Mon Sep 17 00:00:00 2001 From: courthive Date: Sun, 21 Apr 2024 21:41:19 -0400 Subject: [PATCH] feat: :sparkles: `copyTournamentRecord`: new method for tournamentGovernor --- .../tournamentRecords/copyTournamentRecord.ts | 53 +++++++++++++++++++ .../governors/tournamentGovernor/generate.ts | 1 + 2 files changed, 54 insertions(+) create mode 100644 src/assemblies/generators/tournamentRecords/copyTournamentRecord.ts diff --git a/src/assemblies/generators/tournamentRecords/copyTournamentRecord.ts b/src/assemblies/generators/tournamentRecords/copyTournamentRecord.ts new file mode 100644 index 0000000000..b84d731c99 --- /dev/null +++ b/src/assemblies/generators/tournamentRecords/copyTournamentRecord.ts @@ -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 }; +} diff --git a/src/assemblies/governors/tournamentGovernor/generate.ts b/src/assemblies/governors/tournamentGovernor/generate.ts index 55738d3c3f..9b9f154a01 100644 --- a/src/assemblies/governors/tournamentGovernor/generate.ts +++ b/src/assemblies/governors/tournamentGovernor/generate.ts @@ -1 +1,2 @@ export { createTournamentRecord } from '@Generators/tournamentRecords/createTournamentRecord'; +export { copyTournamentRecord } from '@Generators/tournamentRecords/copyTournamentRecord';