refactor: harden competition persistence type boundaries
This commit is contained in:
1 parent
ec92494211
commit
7ea2002975
7 files changed
+127
-62
No files matched your search
@@ -12,10 +12,11 @@ import {
|
||||
} from '@nestjs/common';
|
||||
import mongoose from 'mongoose';
|
||||
import { ConnectionService } from '../global/database/connection.service';
|
||||
import type { Competition } from './competition.schema';
|
||||
import type { Competition, CompetitionFact } from './competition.schema';
|
||||
import { CompetitionBootstrapService } from './competition-bootstrap.service';
|
||||
|
||||
type PersistenceId = string | mongoose.Types.ObjectId;
|
||||
type CompetitionRecord = Competition & { _id: mongoose.Types.ObjectId };
|
||||
|
||||
export interface CompetitionScope {
|
||||
communityId: mongoose.Types.ObjectId;
|
||||
@@ -35,7 +36,9 @@ export class CompetitionQueryService {
|
||||
) {}
|
||||
|
||||
/** Resolve the one-to-one aggregate, lazily freezing a completed team build. */
|
||||
public async getCompetition(scope: CompetitionScope): Promise<any> {
|
||||
public async getCompetition(
|
||||
scope: CompetitionScope,
|
||||
): Promise<CompetitionRecord> {
|
||||
const CompetitionModel =
|
||||
await this.connectionService.getModel('competition');
|
||||
const existing = await CompetitionModel.findOne({
|
||||
@@ -44,7 +47,7 @@ export class CompetitionQueryService {
|
||||
})
|
||||
.lean()
|
||||
.exec();
|
||||
if (existing) return existing;
|
||||
if (existing) return existing as CompetitionRecord;
|
||||
|
||||
const MatchModel = await this.connectionService.getModel('match');
|
||||
const match = await MatchModel.findOne({
|
||||
@@ -203,37 +206,47 @@ export class CompetitionQueryService {
|
||||
nextCursor:
|
||||
rows.length > limit && last
|
||||
? encodeCursor({
|
||||
revision: last.revision,
|
||||
ordinalWithinRevision: last.ordinalWithinRevision,
|
||||
id: idString(last._id),
|
||||
revision: requiredNumber(last.revision, 'revision'),
|
||||
ordinalWithinRevision: requiredNumber(
|
||||
last.ordinalWithinRevision,
|
||||
'ordinalWithinRevision',
|
||||
),
|
||||
id: requiredId(last._id, '_id'),
|
||||
})
|
||||
: null,
|
||||
};
|
||||
}
|
||||
}
|
||||
|
||||
function presentFact(source: Record<string, any>): CompetitionFactView {
|
||||
function presentFact(
|
||||
source: CompetitionFact & { _id: mongoose.Types.ObjectId },
|
||||
): CompetitionFactView {
|
||||
const common = {
|
||||
id: idString(source._id) as never,
|
||||
competitionId: idString(source.competitionId) as never,
|
||||
revision: source.revision,
|
||||
ordinalWithinRevision: source.ordinalWithinRevision,
|
||||
commandId: String(source.commandId) as never,
|
||||
schemaVersion: source.schemaVersion,
|
||||
id: requiredId(source._id, '_id') as never,
|
||||
competitionId: requiredId(source.competitionId, 'competitionId') as never,
|
||||
revision: requiredNumber(source.revision, 'revision'),
|
||||
ordinalWithinRevision: requiredNumber(
|
||||
source.ordinalWithinRevision,
|
||||
'ordinalWithinRevision',
|
||||
),
|
||||
commandId: requiredString(source.commandId, 'commandId') as never,
|
||||
schemaVersion: requiredNumber(source.schemaVersion, 'schemaVersion'),
|
||||
actor: toWire(source.actor) as never,
|
||||
occurredAt: isoDate(source.occurredAt),
|
||||
recordedAt: isoDate(source.recordedAt),
|
||||
...(source.causationId ? { causationId: String(source.causationId) } : {}),
|
||||
...(typeof source.causationId === 'string' && source.causationId.length > 0
|
||||
? { causationId: source.causationId }
|
||||
: {}),
|
||||
};
|
||||
switch (source.type) {
|
||||
case 'game-accepted':
|
||||
return {
|
||||
...common,
|
||||
type: 'game-accepted',
|
||||
gameId: idString(source.gameId) as never,
|
||||
seriesId: idString(source.seriesId) as never,
|
||||
gameSlotId: idString(source.gameSlotId) as never,
|
||||
ordinal: source.ordinal,
|
||||
gameId: requiredId(source.gameId, 'gameId') as never,
|
||||
seriesId: requiredId(source.seriesId, 'seriesId') as never,
|
||||
gameSlotId: requiredId(source.gameSlotId, 'gameSlotId') as never,
|
||||
ordinal: requiredNumber(source.ordinal, 'ordinal'),
|
||||
startedAt: isoDate(source.startedAt ?? source.occurredAt),
|
||||
durationSeconds: numberValue(source.durationSeconds ?? 0),
|
||||
participants: toWire(source.participants ?? []) as never,
|
||||
@@ -244,26 +257,32 @@ function presentFact(source: Record<string, any>): CompetitionFactView {
|
||||
return {
|
||||
...common,
|
||||
type: 'game-voided',
|
||||
gameAcceptedFactId: idString(source.gameAcceptedFactId) as never,
|
||||
reason: String(source.reason),
|
||||
gameAcceptedFactId: requiredId(
|
||||
source.gameAcceptedFactId,
|
||||
'gameAcceptedFactId',
|
||||
) as never,
|
||||
reason: requiredString(source.reason, 'reason'),
|
||||
};
|
||||
case 'series-adjudicated':
|
||||
return {
|
||||
...common,
|
||||
type: 'series-adjudicated',
|
||||
seriesId: idString(source.seriesId) as never,
|
||||
seriesId: requiredId(source.seriesId, 'seriesId') as never,
|
||||
decision: toWire(source.decision) as never,
|
||||
reason: String(source.reason),
|
||||
reason: requiredString(source.reason, 'reason'),
|
||||
};
|
||||
case 'adjudication-reversed':
|
||||
return {
|
||||
...common,
|
||||
type: 'adjudication-reversed',
|
||||
adjudicationFactId: idString(source.adjudicationFactId) as never,
|
||||
reason: String(source.reason),
|
||||
adjudicationFactId: requiredId(
|
||||
source.adjudicationFactId,
|
||||
'adjudicationFactId',
|
||||
) as never,
|
||||
reason: requiredString(source.reason, 'reason'),
|
||||
};
|
||||
default:
|
||||
throw new Error(`Unsupported Competition fact type: ${source.type}`);
|
||||
throw new Error('Unsupported Competition fact type.');
|
||||
}
|
||||
}
|
||||
|
||||
@@ -279,7 +298,9 @@ function encodeCursor(cursor: FactCursor): string {
|
||||
|
||||
function decodeCursor(value: string): FactCursor {
|
||||
try {
|
||||
const parsed = JSON.parse(Buffer.from(value, 'base64url').toString('utf8'));
|
||||
const parsed: unknown = JSON.parse(
|
||||
Buffer.from(value, 'base64url').toString('utf8'),
|
||||
);
|
||||
if (
|
||||
!isRecord(parsed) ||
|
||||
!Number.isInteger(parsed.revision) ||
|
||||
@@ -315,8 +336,10 @@ function idString(value: PersistenceId): string {
|
||||
: String(value);
|
||||
}
|
||||
|
||||
function isoDate(value: Date | string): string {
|
||||
return value instanceof Date ? value.toISOString() : String(value);
|
||||
function isoDate(value: unknown): string {
|
||||
if (value instanceof Date) return value.toISOString();
|
||||
if (typeof value === 'string') return value;
|
||||
throw new Error('Competition fact contains an invalid date.');
|
||||
}
|
||||
|
||||
function numberValue(value: unknown): number {
|
||||
@@ -338,3 +361,18 @@ function toWire(value: unknown): unknown {
|
||||
function isRecord(value: unknown): value is Record<string, unknown> {
|
||||
return value !== null && typeof value === 'object' && !Array.isArray(value);
|
||||
}
|
||||
|
||||
function requiredId(value: unknown, field: string): string {
|
||||
if (value instanceof mongoose.Types.ObjectId) return value.toHexString();
|
||||
return requiredString(value, field);
|
||||
}
|
||||
|
||||
function requiredString(value: unknown, field: string): string {
|
||||
if (typeof value === 'string' && value.length > 0) return value;
|
||||
throw new Error(`Competition fact contains an invalid ${field}.`);
|
||||
}
|
||||
|
||||
function requiredNumber(value: unknown, field: string): number {
|
||||
if (typeof value === 'number' && Number.isFinite(value)) return value;
|
||||
throw new Error(`Competition fact contains an invalid ${field}.`);
|
||||
}
|
||||
Reference in new issue
Block a user