458 lines
13 KiB
TypeScript
458 lines
13 KiB
TypeScript
import mongoose, { HydratedDocument, Schema } from 'mongoose';
|
|
|
|
export const COMPETITION_PHASES = [
|
|
'ready',
|
|
'running',
|
|
'completed',
|
|
'cancelled',
|
|
] as const;
|
|
export type CompetitionPhase = (typeof COMPETITION_PHASES)[number];
|
|
|
|
export const COMPETITION_FACT_TYPES = [
|
|
'game-accepted',
|
|
'game-voided',
|
|
'series-adjudicated',
|
|
'adjudication-reversed',
|
|
] as const;
|
|
export type CompetitionFactType = (typeof COMPETITION_FACT_TYPES)[number];
|
|
|
|
export const COMPETITION_OUTBOX_STATES = [
|
|
'pending',
|
|
'published',
|
|
'failed',
|
|
'dead-letter',
|
|
] as const;
|
|
export type CompetitionOutboxState = (typeof COMPETITION_OUTBOX_STATES)[number];
|
|
|
|
export const TOURNAMENT_CODE_ASSIGNMENT_STATES = [
|
|
'reserved',
|
|
'used',
|
|
'released',
|
|
'voided',
|
|
] as const;
|
|
export type TournamentCodeAssignmentState =
|
|
(typeof TOURNAMENT_CODE_ASSIGNMENT_STATES)[number];
|
|
|
|
export interface CompetitionDomainInvariantIndex {
|
|
collection:
|
|
| 'match'
|
|
| 'competition'
|
|
| 'competition_fact'
|
|
| 'competition_outbox'
|
|
| 'tournament_code_assignment';
|
|
key: mongoose.IndexDefinition;
|
|
options: mongoose.IndexOptions & { name: string; unique: true };
|
|
}
|
|
|
|
/**
|
|
* Database-enforced Competition identities. Runtime schemas and the one-off
|
|
* migration consume this same list so production cannot silently run with a
|
|
* weaker set of uniqueness guarantees than command handling expects.
|
|
*/
|
|
export const COMPETITION_DOMAIN_INVARIANT_INDEXES: CompetitionDomainInvariantIndex[] =
|
|
[
|
|
{
|
|
collection: 'match',
|
|
key: { competitionId: 1 },
|
|
options: {
|
|
name: 'competitionId_1',
|
|
unique: true,
|
|
partialFilterExpression: {
|
|
competitionId: { $type: 'objectId' },
|
|
},
|
|
},
|
|
},
|
|
{
|
|
collection: 'competition',
|
|
key: { matchId: 1 },
|
|
options: { name: 'matchId_1', unique: true },
|
|
},
|
|
{
|
|
collection: 'competition_fact',
|
|
key: { competitionId: 1, revision: 1, ordinalWithinRevision: 1 },
|
|
options: {
|
|
name: 'competitionId_1_revision_1_ordinalWithinRevision_1',
|
|
unique: true,
|
|
},
|
|
},
|
|
{
|
|
collection: 'competition_fact',
|
|
key: { competitionId: 1, commandId: 1, ordinalWithinRevision: 1 },
|
|
options: {
|
|
name: 'competitionId_1_commandId_1_ordinalWithinRevision_1',
|
|
unique: true,
|
|
},
|
|
},
|
|
{
|
|
collection: 'competition_fact',
|
|
key: { 'evidence.riotMatchId': 1 },
|
|
options: {
|
|
name: 'evidence.riotMatchId_1',
|
|
unique: true,
|
|
partialFilterExpression: {
|
|
type: 'game-accepted',
|
|
'evidence.riotMatchId': { $type: 'string' },
|
|
},
|
|
},
|
|
},
|
|
{
|
|
collection: 'competition_outbox',
|
|
key: { eventId: 1 },
|
|
options: { name: 'eventId_1', unique: true },
|
|
},
|
|
{
|
|
collection: 'competition_outbox',
|
|
key: { competitionId: 1, idempotencyKey: 1 },
|
|
options: {
|
|
name: 'competitionId_1_idempotencyKey_1',
|
|
unique: true,
|
|
},
|
|
},
|
|
{
|
|
collection: 'tournament_code_assignment',
|
|
key: { gameSlotId: 1 },
|
|
options: {
|
|
name: 'gameSlotId_1',
|
|
unique: true,
|
|
partialFilterExpression: { state: { $in: ['reserved', 'used'] } },
|
|
},
|
|
},
|
|
{
|
|
collection: 'tournament_code_assignment',
|
|
key: { tournamentCodeId: 1 },
|
|
options: {
|
|
name: 'tournamentCodeId_1',
|
|
unique: true,
|
|
partialFilterExpression: { state: { $in: ['reserved', 'used'] } },
|
|
},
|
|
},
|
|
{
|
|
collection: 'tournament_code_assignment',
|
|
key: { riotMatchId: 1 },
|
|
options: {
|
|
name: 'riotMatchId_1',
|
|
unique: true,
|
|
partialFilterExpression: { riotMatchId: { $type: 'string' } },
|
|
},
|
|
},
|
|
];
|
|
|
|
export interface CompetitionTeamMemberSnapshot {
|
|
userId: mongoose.Types.ObjectId;
|
|
displayName: string;
|
|
gameName?: string;
|
|
tagLine?: string;
|
|
profileIconId?: number;
|
|
assignedPosition?: string;
|
|
cost?: number;
|
|
}
|
|
|
|
export interface CompetitionTeamSnapshot {
|
|
teamId: mongoose.Types.ObjectId;
|
|
legacyTeamIndex?: number;
|
|
displayOrder: number;
|
|
seedNumber: number;
|
|
name: string;
|
|
captainUserId?: mongoose.Types.ObjectId;
|
|
members: CompetitionTeamMemberSnapshot[];
|
|
}
|
|
|
|
export interface Competition {
|
|
matchId: mongoose.Types.ObjectId;
|
|
communityId: mongoose.Types.ObjectId;
|
|
schemaVersion: number;
|
|
projectorVersion: number;
|
|
revision: number;
|
|
phase: CompetitionPhase;
|
|
teams: CompetitionTeamSnapshot[];
|
|
format: Record<string, unknown>;
|
|
seriesGraph: Array<Record<string, unknown>>;
|
|
projection: Record<string, unknown>;
|
|
createdAt: Date;
|
|
updatedAt: Date;
|
|
}
|
|
|
|
export type CompetitionDocument = HydratedDocument<Competition>;
|
|
|
|
export interface CompetitionFact {
|
|
competitionId: mongoose.Types.ObjectId;
|
|
type: CompetitionFactType;
|
|
revision: number;
|
|
ordinalWithinRevision: number;
|
|
commandId: string;
|
|
idempotencyKey: string;
|
|
requestHash: string;
|
|
schemaVersion: number;
|
|
actor: Record<string, unknown>;
|
|
occurredAt: Date;
|
|
recordedAt: Date;
|
|
causationId?: string;
|
|
gameId?: mongoose.Types.ObjectId;
|
|
seriesId?: mongoose.Types.ObjectId;
|
|
gameSlotId?: mongoose.Types.ObjectId;
|
|
ordinal?: number;
|
|
startedAt?: Date;
|
|
durationSeconds?: number;
|
|
participants?: Array<Record<string, unknown>>;
|
|
evidence?: Record<string, unknown>;
|
|
mapping?: Record<string, unknown>;
|
|
gameAcceptedFactId?: mongoose.Types.ObjectId;
|
|
adjudicationFactId?: mongoose.Types.ObjectId;
|
|
decision?: Record<string, unknown>;
|
|
reason?: string;
|
|
createdAt: Date;
|
|
updatedAt: Date;
|
|
}
|
|
|
|
export type CompetitionFactDocument = HydratedDocument<CompetitionFact>;
|
|
|
|
export interface CompetitionOutbox {
|
|
eventId: string;
|
|
competitionId: mongoose.Types.ObjectId;
|
|
revision: number;
|
|
commandId: string;
|
|
idempotencyKey: string;
|
|
requestHash: string;
|
|
type: string;
|
|
payload: Record<string, unknown>;
|
|
state: CompetitionOutboxState;
|
|
attemptCount: number;
|
|
availableAt: Date;
|
|
publishedAt?: Date;
|
|
lastError?: string;
|
|
createdAt: Date;
|
|
updatedAt: Date;
|
|
}
|
|
|
|
export type CompetitionOutboxDocument = HydratedDocument<CompetitionOutbox>;
|
|
|
|
export interface TournamentCodeAssignment {
|
|
competitionId: mongoose.Types.ObjectId;
|
|
seriesId: mongoose.Types.ObjectId;
|
|
gameSlotId: mongoose.Types.ObjectId;
|
|
tournamentCodeId: mongoose.Types.ObjectId;
|
|
state: TournamentCodeAssignmentState;
|
|
reservedAt: Date;
|
|
usedAt?: Date;
|
|
releasedAt?: Date;
|
|
voidedAt?: Date;
|
|
riotMatchId?: string;
|
|
commandId: string;
|
|
createdAt: Date;
|
|
updatedAt: Date;
|
|
}
|
|
|
|
export type TournamentCodeAssignmentDocument =
|
|
HydratedDocument<TournamentCodeAssignment>;
|
|
|
|
const CompetitionTeamMemberSchema = new Schema<CompetitionTeamMemberSnapshot>(
|
|
{
|
|
userId: { type: Schema.Types.ObjectId, ref: 'user', required: true },
|
|
displayName: { type: String, required: true },
|
|
gameName: { type: String },
|
|
tagLine: { type: String },
|
|
profileIconId: { type: Number },
|
|
assignedPosition: { type: String },
|
|
cost: { type: Number },
|
|
},
|
|
{ _id: false },
|
|
);
|
|
|
|
const CompetitionTeamSchema = new Schema<CompetitionTeamSnapshot>(
|
|
{
|
|
teamId: { type: Schema.Types.ObjectId, required: true },
|
|
legacyTeamIndex: { type: Number, min: 1 },
|
|
displayOrder: { type: Number, required: true, min: 1 },
|
|
seedNumber: { type: Number, required: true, min: 1 },
|
|
name: { type: String, required: true },
|
|
captainUserId: { type: Schema.Types.ObjectId, ref: 'user' },
|
|
members: {
|
|
type: [CompetitionTeamMemberSchema],
|
|
required: true,
|
|
default: [],
|
|
},
|
|
},
|
|
{ _id: false },
|
|
);
|
|
|
|
export const CompetitionSchema: mongoose.Schema<Competition> =
|
|
new Schema<Competition>(
|
|
{
|
|
matchId: {
|
|
type: Schema.Types.ObjectId,
|
|
ref: 'match',
|
|
required: true,
|
|
},
|
|
communityId: {
|
|
type: Schema.Types.ObjectId,
|
|
ref: 'community',
|
|
required: true,
|
|
},
|
|
schemaVersion: { type: Number, required: true, min: 1 },
|
|
projectorVersion: { type: Number, required: true, min: 1 },
|
|
revision: { type: Number, required: true, min: 0, default: 0 },
|
|
phase: {
|
|
type: String,
|
|
enum: COMPETITION_PHASES,
|
|
required: true,
|
|
default: 'ready',
|
|
},
|
|
teams: { type: [CompetitionTeamSchema], required: true },
|
|
format: { type: Schema.Types.Mixed, required: true },
|
|
seriesGraph: { type: Schema.Types.Mixed, required: true },
|
|
projection: { type: Schema.Types.Mixed, required: true },
|
|
},
|
|
{ timestamps: true, collection: 'competition' },
|
|
);
|
|
|
|
applyCompetitionDomainInvariantIndexes(CompetitionSchema, 'competition');
|
|
CompetitionSchema.index({ communityId: 1, phase: 1, updatedAt: -1 });
|
|
|
|
export const CompetitionFactSchema = new Schema<CompetitionFact>(
|
|
{
|
|
competitionId: {
|
|
type: Schema.Types.ObjectId,
|
|
ref: 'competition',
|
|
required: true,
|
|
},
|
|
type: { type: String, enum: COMPETITION_FACT_TYPES, required: true },
|
|
revision: { type: Number, required: true, min: 1 },
|
|
ordinalWithinRevision: { type: Number, required: true, min: 1 },
|
|
commandId: { type: String, required: true },
|
|
idempotencyKey: { type: String, required: true },
|
|
requestHash: { type: String, required: true },
|
|
schemaVersion: { type: Number, required: true, min: 1 },
|
|
actor: { type: Schema.Types.Mixed, required: true },
|
|
occurredAt: { type: Date, required: true },
|
|
recordedAt: { type: Date, required: true },
|
|
causationId: { type: String },
|
|
gameId: { type: Schema.Types.ObjectId },
|
|
seriesId: { type: Schema.Types.ObjectId },
|
|
gameSlotId: { type: Schema.Types.ObjectId },
|
|
ordinal: { type: Number, min: 1 },
|
|
startedAt: { type: Date },
|
|
durationSeconds: { type: Number, min: 0 },
|
|
participants: { type: Schema.Types.Mixed },
|
|
evidence: { type: Schema.Types.Mixed },
|
|
mapping: { type: Schema.Types.Mixed },
|
|
gameAcceptedFactId: { type: Schema.Types.ObjectId },
|
|
adjudicationFactId: { type: Schema.Types.ObjectId },
|
|
decision: { type: Schema.Types.Mixed },
|
|
reason: { type: String },
|
|
},
|
|
{ timestamps: true, collection: 'competition_fact' },
|
|
);
|
|
|
|
applyCompetitionDomainInvariantIndexes(
|
|
CompetitionFactSchema,
|
|
'competition_fact',
|
|
);
|
|
CompetitionFactSchema.index({ competitionId: 1, seriesId: 1, recordedAt: 1 });
|
|
|
|
export const CompetitionOutboxSchema = new Schema<CompetitionOutbox>(
|
|
{
|
|
eventId: { type: String, required: true },
|
|
competitionId: {
|
|
type: Schema.Types.ObjectId,
|
|
ref: 'competition',
|
|
required: true,
|
|
},
|
|
revision: { type: Number, required: true, min: 1 },
|
|
commandId: { type: String, required: true },
|
|
idempotencyKey: { type: String, required: true },
|
|
requestHash: { type: String, required: true },
|
|
type: { type: String, required: true },
|
|
payload: { type: Schema.Types.Mixed, required: true },
|
|
state: {
|
|
type: String,
|
|
enum: COMPETITION_OUTBOX_STATES,
|
|
required: true,
|
|
default: 'pending',
|
|
},
|
|
attemptCount: { type: Number, required: true, min: 0, default: 0 },
|
|
availableAt: { type: Date, required: true },
|
|
publishedAt: { type: Date },
|
|
lastError: { type: String },
|
|
},
|
|
{ timestamps: true, collection: 'competition_outbox' },
|
|
);
|
|
|
|
applyCompetitionDomainInvariantIndexes(
|
|
CompetitionOutboxSchema,
|
|
'competition_outbox',
|
|
);
|
|
CompetitionOutboxSchema.index({ state: 1, availableAt: 1 });
|
|
|
|
export const TournamentCodeAssignmentSchema =
|
|
new Schema<TournamentCodeAssignment>(
|
|
{
|
|
competitionId: {
|
|
type: Schema.Types.ObjectId,
|
|
ref: 'competition',
|
|
required: true,
|
|
},
|
|
seriesId: { type: Schema.Types.ObjectId, required: true },
|
|
gameSlotId: { type: Schema.Types.ObjectId, required: true },
|
|
tournamentCodeId: {
|
|
type: Schema.Types.ObjectId,
|
|
ref: 'tournamentCode',
|
|
required: true,
|
|
},
|
|
state: {
|
|
type: String,
|
|
enum: TOURNAMENT_CODE_ASSIGNMENT_STATES,
|
|
required: true,
|
|
},
|
|
reservedAt: { type: Date, required: true },
|
|
usedAt: { type: Date },
|
|
releasedAt: { type: Date },
|
|
voidedAt: { type: Date },
|
|
riotMatchId: { type: String },
|
|
commandId: { type: String, required: true },
|
|
},
|
|
{ timestamps: true, collection: 'tournament_code_assignment' },
|
|
);
|
|
|
|
applyCompetitionDomainInvariantIndexes(
|
|
TournamentCodeAssignmentSchema,
|
|
'tournament_code_assignment',
|
|
);
|
|
TournamentCodeAssignmentSchema.index({ competitionId: 1, state: 1 });
|
|
|
|
export function applyCompetitionDomainInvariantIndexes(
|
|
schema: mongoose.Schema,
|
|
collection: CompetitionDomainInvariantIndex['collection'],
|
|
): void {
|
|
for (const index of COMPETITION_DOMAIN_INVARIANT_INDEXES) {
|
|
if (index.collection !== collection) continue;
|
|
schema.index(index.key, index.options);
|
|
}
|
|
}
|
|
|
|
export const competitionModelDefinitions: Array<{
|
|
name: string;
|
|
schema: mongoose.Schema;
|
|
schemaName: string;
|
|
}> = [
|
|
{
|
|
name: 'competition',
|
|
schema: CompetitionSchema,
|
|
schemaName: 'competition',
|
|
},
|
|
{
|
|
name: 'competitionFact',
|
|
schema: CompetitionFactSchema,
|
|
schemaName: 'competition_fact',
|
|
},
|
|
{
|
|
name: 'competitionOutbox',
|
|
schema: CompetitionOutboxSchema,
|
|
schemaName: 'competition_outbox',
|
|
},
|
|
{
|
|
name: 'tournamentCodeAssignment',
|
|
schema: TournamentCodeAssignmentSchema,
|
|
schemaName: 'tournament_code_assignment',
|
|
},
|
|
];
|