Skip to content

Commit

Permalink
refactor: ♻️ extend use of checkRequiredParameters; cleanup;
Browse files Browse the repository at this point in the history
  • Loading branch information
CourtHive committed Mar 25, 2024
1 parent a4d1d9b commit 930271d
Show file tree
Hide file tree
Showing 3 changed files with 24 additions and 26 deletions.
3 changes: 2 additions & 1 deletion src/acquire/findDrawDefinition.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
import { decorateResult } from '@Functions/global/decorateResult';
import { makeDeepCopy } from '@Tools/makeDeepCopy';
import { findTournamentId } from './findTournamentId';
import { makeDeepCopy } from '@Tools/makeDeepCopy';
import { findEvent } from './findEvent';

// constants and types
import { DrawDefinition, Event, Tournament } from '@Types/tournamentTypes';
import { SUCCESS } from '@Constants/resultConstants';
import {
Expand Down
43 changes: 20 additions & 23 deletions src/acquire/findDrawMatchUp.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import { checkRequiredParameters } from '@Helpers/parameters/checkRequiredParameters';
import { getAllStructureMatchUps } from '@Query/matchUps/getAllStructureMatchUps';
import { getContextContent } from '@Query/hierarchical/getContextContent';
import { getMatchUp } from '@Query/matchUps/getMatchUpFromMatchUps';
Expand All @@ -6,15 +7,10 @@ import { getDrawStructures } from './findStructure';

// constants and types
import { DrawDefinition, Event, Participant, Structure } from '@Types/tournamentTypes';
import { ErrorType, MATCHUP_NOT_FOUND } from '@Constants/errorConditionConstants';
import { ContextContent, ContextProfile, MatchUpsMap } from '@Types/factoryTypes';
import { DRAW_DEFINITION, MATCHUP_ID } from '@Constants/attributeConstants';
import { HydratedMatchUp } from '@Types/hydrated';
import {
ErrorType,
INVALID_VALUES,
MATCHUP_NOT_FOUND,
MISSING_DRAW_DEFINITION,
MISSING_MATCHUP_ID,
} from '@Constants/errorConditionConstants';

/*
public version of findMatchUp
Expand All @@ -39,29 +35,30 @@ type FindDrawMatchUpArgs = {
event?: Event;
};

export function findDrawMatchUp({
tournamentParticipants,
afterRecoveryTimes,
contextContent,
contextProfile,
drawDefinition,
matchUpsMap,
matchUpId,
inContext,
context,
event,
}: FindDrawMatchUpArgs): {
export function findDrawMatchUp(params: FindDrawMatchUpArgs): {
matchUp?: HydratedMatchUp;
structure?: Structure;
error?: ErrorType;
} {
if (!drawDefinition) return { error: MISSING_DRAW_DEFINITION };
if (!matchUpId) return { error: MISSING_MATCHUP_ID };
if (typeof matchUpId !== 'string') return { error: INVALID_VALUES };
const paramsCheck = checkRequiredParameters(params, [{ [DRAW_DEFINITION]: true, [MATCHUP_ID]: true }]);
if (paramsCheck.error) return paramsCheck;

const {
tournamentParticipants,
afterRecoveryTimes,
contextProfile,
drawDefinition,
matchUpsMap,
matchUpId,
inContext,
context,
event,
} = params;

const { structures = [] } = getDrawStructures({ drawDefinition });

if (contextProfile && !contextContent) contextContent = getContextContent({ contextProfile, drawDefinition });
const contextContent =
params.contextContent || (contextProfile && getContextContent({ contextProfile, drawDefinition }));

for (const structure of structures) {
const { matchUps } = getAllStructureMatchUps({
Expand Down
4 changes: 2 additions & 2 deletions src/tools/objects.ts
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ export const hav = hasAttributeValues;
// useful in notifications where back end does not recognize undefined for updates
export function undefinedToNull(obj: object, shallow?: boolean) {
if (obj === undefined) return null;
if (typeof obj !== 'object' || obj === null) return obj;
if (!isObject(obj) || obj === null) return obj;

const definedKeys = Object.keys(obj);
const notNull = (value) => (value === undefined ? null : value);
Expand All @@ -73,7 +73,7 @@ export function undefinedToNull(obj: object, shallow?: boolean) {
function countKeys(o) {
if (Array.isArray(o)) {
return o.length + o.map(countKeys).reduce((a, b) => a + b, 0);
} else if (typeof o === 'object' && o !== null) {
} else if (isObject(o) && o !== null) {
return (
Object.keys(o).length +
Object.keys(o)
Expand Down

0 comments on commit 930271d

Please sign in to comment.