-
Notifications
You must be signed in to change notification settings - Fork 6
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: ✨
copyTournamentRecord
: new method for tournamentGovernor
- Loading branch information
Showing
2 changed files
with
54 additions
and
0 deletions.
There are no files selected for viewing
53 changes: 53 additions & 0 deletions
53
src/assemblies/generators/tournamentRecords/copyTournamentRecord.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,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 }; | ||
} |
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 |
---|---|---|
@@ -1 +1,2 @@ | ||
export { createTournamentRecord } from '@Generators/tournamentRecords/createTournamentRecord'; | ||
export { copyTournamentRecord } from '@Generators/tournamentRecords/copyTournamentRecord'; |