Skip to content

Commit 6ec1a4d

Browse files
committed
refactor: extract shared terminal match status constants
Replace duplicated terminal status strings across 4 locations in matches.controller.ts with shared static class constants.
1 parent 7c5ae53 commit 6ec1a4d

1 file changed

Lines changed: 18 additions & 32 deletions

File tree

src/matches/matches.controller.ts

Lines changed: 18 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,20 @@ import { MatchRelayService } from "./match-relay/match-relay.service";
3737
export class MatchesController {
3838
private readonly appConfig: AppConfig;
3939

40+
private static readonly TERMINAL_STATUSES: string[] = [
41+
"Finished",
42+
"Canceled",
43+
"Forfeit",
44+
"Tie",
45+
"Surrendered",
46+
];
47+
48+
private static readonly TERMINAL_OR_PRE_START_STATUSES: string[] = [
49+
...MatchesController.TERMINAL_STATUSES,
50+
"Scheduled",
51+
"PickingPlayers",
52+
];
53+
4054
constructor(
4155
private readonly logger: Logger,
4256
private readonly hasura: HasuraService,
@@ -187,13 +201,7 @@ export class MatchesController {
187201
throw Error("unable to find match");
188202
}
189203

190-
if (
191-
matches_by_pk.status === "Tie" ||
192-
matches_by_pk.status === "Canceled" ||
193-
matches_by_pk.status === "Forfeit" ||
194-
matches_by_pk.status === "Finished" ||
195-
matches_by_pk.status === "Surrendered"
196-
) {
204+
if (MatchesController.TERMINAL_STATUSES.includes(matches_by_pk.status)) {
197205
response.status(204).end();
198206
return;
199207
}
@@ -330,11 +338,7 @@ export class MatchesController {
330338
*/
331339
if (
332340
data.op === "DELETE" ||
333-
status === "Tie" ||
334-
status === "Forfeit" ||
335-
status === "Canceled" ||
336-
status === "Finished" ||
337-
status === "Surrendered"
341+
MatchesController.TERMINAL_STATUSES.includes(status)
338342
) {
339343
this.matchRelayService.removeBroadcast(matchId);
340344
await this.removeDiscordIntegration(matchId);
@@ -623,17 +627,7 @@ export class MatchesController {
623627
throw Error("match not found");
624628
}
625629

626-
const terminalOrPreStartStatuses: string[] = [
627-
"Finished",
628-
"Canceled",
629-
"Forfeit",
630-
"Tie",
631-
"Surrendered",
632-
"Scheduled",
633-
"PickingPlayers",
634-
];
635-
636-
if (terminalOrPreStartStatuses.includes(matchToSetWinner.status)) {
630+
if (MatchesController.TERMINAL_OR_PRE_START_STATUSES.includes(matchToSetWinner.status)) {
637631
throw Error("cannot set winner for a match in this state");
638632
}
639633

@@ -685,15 +679,7 @@ export class MatchesController {
685679
throw Error("match not found");
686680
}
687681

688-
const terminalStatuses: string[] = [
689-
"Finished",
690-
"Canceled",
691-
"Forfeit",
692-
"Tie",
693-
"Surrendered",
694-
];
695-
696-
if (terminalStatuses.includes(matchToForfeit.status)) {
682+
if (MatchesController.TERMINAL_STATUSES.includes(matchToForfeit.status)) {
697683
throw Error("cannot forfeit a match that has already ended");
698684
}
699685

0 commit comments

Comments
 (0)