Skip to content

Commit

Permalink
feat: ✨ generateStatCrew function added to generationGovernor; te…
Browse files Browse the repository at this point in the history
…st coverage
  • Loading branch information
CourtHive committed Apr 2, 2024
1 parent 46d2b71 commit 53cb2bb
Show file tree
Hide file tree
Showing 5 changed files with 3,085 additions and 0 deletions.
80 changes: 80 additions & 0 deletions src/assemblies/generators/tournamentRecords/generateStatCrew.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,80 @@
import { checkRequiredParameters } from '@Helpers/parameters/checkRequiredParameters';
import { allTournamentMatchUps } from '@Query/matchUps/getAllTournamentMatchUps';
import { definedAttributes } from '@Tools/definedAttributes';
import { formatDate } from '@Tools/dateTime';
import { jsonToXml } from './jsonToXml';

// constants
import { TOURNAMENT_RECORD } from '@Constants/attributeConstants';
import { DOUBLES, SINGLES } from '@Constants/matchUpTypes';
import { TEAM } from '@Constants/participantConstants';
import { SUCCESS } from '@Constants/resultConstants';

export function generateStatCrew(params) {
const paramsCheck = checkRequiredParameters(params, [{ [TOURNAMENT_RECORD]: true }]);
if (paramsCheck.error) return paramsCheck;

const { tournamentRecord } = params;
const { tournamentId } = tournamentRecord;

const teamParticipants = tournamentRecord.participants.filter((participant) => participant.participantType === TEAM);

const homeTeam = teamParticipants?.find((team) => team.participantRoleResponsibilities?.includes('Home'));
const awayTeams = teamParticipants?.filter((team) => team.participantId !== homeTeam?.participantId);
const neutralGame = !homeTeam ? 'Y' : 'N';
const date = formatDate(tournamentRecord.startDate, '/', 'MDY');

const visname = awayTeams?.[0]?.participantName;
const visid = awayTeams?.[0]?.participantId;

const homename = homeTeam?.participantName || awayTeams?.[1]?.participantName;
const homeid = homeTeam?.participantId || awayTeams?.[1]?.participantId;

const matchUps = allTournamentMatchUps({ tournamentRecord }).matchUps;
const singles = matchUps?.filter((matchUp) => matchUp.matchUpType === SINGLES);
const doubles = matchUps?.filter((matchUp) => matchUp.matchUpType === DOUBLES);

const getScoreAttributes = ({ sets, sideNumber }) =>
Object.assign(
{},
...(sets?.map((set) => {
const { setNumber, side1Score, side2Score, side1TiebreakScore, side2TiebreakScore } = set;
const setKey = `set_${setNumber}`;
const tbKey = `tie_${setNumber}`;
const setValue = sideNumber === 1 ? side1Score : side2Score;
const tieValue = sideNumber === 1 ? side1TiebreakScore : side2TiebreakScore;
return { [setKey]: setValue, [tbKey]: tieValue };
}) ?? []),
);

const getParticipantAttibutes = ({ matchUpId, participant }) => {
const name = !participant.individualParticipants ? participant.participantName : undefined;
const name_1 = participant.individualParticipants?.[0]?.participantName;
const name_2 = participant.individualParticipants?.[1]?.participantName;
const vh = matchUpId === homeid ? 'H' : 'V';
return definedAttributes({ vh, name, name_1, name_2 });
};

const mapMatchUp = (matchUp) => {
const { collectionPosition: match, orderOfFinish: order, matchUpId, matchUpType, sides } = matchUp;
const childArray = sides?.map((side) => {
const { sideNumber, participant } = side;
const scoreAttributes = getScoreAttributes({ sets: matchUp.score?.sets, sideNumber });
const scoreType = matchUpType === SINGLES ? 'singles_score' : 'doubles_score';
return { [scoreType]: { ...getParticipantAttibutes({ matchUpId, participant }), ...scoreAttributes } };
});

const matchType = matchUpType === SINGLES ? 'singles_match' : 'doubles_match';
return { [matchType]: { match, order, childArray } };
};

const json = {
venue: { gameid: tournamentId, date, neutralGame, homeid, homename, visid, visname, officials: {}, rules: {} },
singles_matches: singles?.map(mapMatchUp),
doubles_matches: doubles?.map(mapMatchUp),
};

const xml = jsonToXml({ json, tagName: 'tngame' });

return { xml, ...SUCCESS };
}
41 changes: 41 additions & 0 deletions src/assemblies/generators/tournamentRecords/jsonToXml.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
export function jsonToXml({ json, tagName }) {
const childArrays = Object.keys(json)
.filter((key) => Array.isArray(json[key]))
.map((key) => ({ key, value: json[key].filter((v) => typeof v === 'object' && Object.keys(v).length === 1) }));
const childObjects = Object.keys(json)
.filter((key) => typeof json[key] === 'object' && !Array.isArray(json[key]))
.map((key) => ({ key, value: json[key] }));
const attributes = Object.keys(json)
.filter((key) => ['string', 'number'].includes(typeof json[key]))
.map((key) => ({ key, value: json[key] }));

return generateTag({ tagName, attributes, childArrays, childObjects });
}

function generateTag(params) {
const { tagName, attributes = [], childArrays = [], childObjects } = params;
const carriageReturn = '\r\n';

const arrayChild = (v) => {
const key = Object.keys(v)[0];
const value = v[key];
return { key, value };
};
const arrayValues = childArrays
?.map(({ key, value }) => generateTag({ tagName: key, childObjects: value.map(arrayChild) }))
.join('\r\n');

const childValues = childObjects
?.map(({ key, value }) => jsonToXml({ tagName: key, json: value ?? [] }))
.join('\r\n');

const attributeString = attributes?.map(({ key, value }) => `${key}="${value}"`).join(' ');
const attributeValue = attributeString ? ` ${attributeString}` : '';

if (tagName === 'childArray') return childValues;

const objectsTag = childValues?.length ? `${carriageReturn}${childValues}${carriageReturn}` : '';
const arraysTag = arrayValues?.length ? `${carriageReturn}${arrayValues}${carriageReturn}` : '';

return `<${tagName}${attributeValue}>${objectsTag}${arraysTag}</${tagName}>`;
}
1 change: 1 addition & 0 deletions src/assemblies/governors/generationGovernor/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ export { roundRobinGroups } from '../../generators/drawDefinitions/drawTypes/rou
export { generateSeedingScaleItems } from '../../generators/drawDefinitions/generateSeedingScaleItems';
export { drawMatic } from '../../generators/drawDefinitions/drawTypes/adHoc/drawMatic/drawMatic';
export { generateFlightProfile } from '../../generators/drawDefinitions/generateFlightProfile';
export { generateStatCrew } from '@Generators/tournamentRecords/generateStatCrew';
export { generateLineUps } from '../../generators/participants/generateLineUps';
export { generateCourts } from '../../generators/venues/generateCourts';
export * as garman from '../../generators/scheduling/garman/garman';
Loading

0 comments on commit 53cb2bb

Please sign in to comment.