feat: add competition persistence schemas
This commit is contained in:
1 parent
05a83db886
commit
2135af7107
3 files changed
+540
No files matched your search
@@ -0,0 +1,370 @@
|
||||
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',
|
||||
] 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 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;
|
||||
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' },
|
||||
);
|
||||
|
||||
CompetitionSchema.index({ matchId: 1 }, { unique: true });
|
||||
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 },
|
||||
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' },
|
||||
);
|
||||
|
||||
CompetitionFactSchema.index(
|
||||
{ competitionId: 1, revision: 1, ordinalWithinRevision: 1 },
|
||||
{ unique: true },
|
||||
);
|
||||
CompetitionFactSchema.index(
|
||||
{ competitionId: 1, commandId: 1, ordinalWithinRevision: 1 },
|
||||
{ unique: true },
|
||||
);
|
||||
CompetitionFactSchema.index(
|
||||
{ 'evidence.riotMatchId': 1 },
|
||||
{
|
||||
unique: true,
|
||||
partialFilterExpression: {
|
||||
type: 'game-accepted',
|
||||
'evidence.riotMatchId': { $type: 'string' },
|
||||
},
|
||||
},
|
||||
);
|
||||
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' },
|
||||
);
|
||||
|
||||
CompetitionOutboxSchema.index({ eventId: 1 }, { unique: true });
|
||||
CompetitionOutboxSchema.index({ state: 1, availableAt: 1 });
|
||||
CompetitionOutboxSchema.index(
|
||||
{ competitionId: 1, idempotencyKey: 1 },
|
||||
{ unique: true },
|
||||
);
|
||||
|
||||
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' },
|
||||
);
|
||||
|
||||
const activeAssignmentFilter = { state: { $in: ['reserved', 'used'] } };
|
||||
TournamentCodeAssignmentSchema.index(
|
||||
{ gameSlotId: 1 },
|
||||
{
|
||||
unique: true,
|
||||
partialFilterExpression: activeAssignmentFilter,
|
||||
},
|
||||
);
|
||||
TournamentCodeAssignmentSchema.index(
|
||||
{ tournamentCodeId: 1 },
|
||||
{
|
||||
unique: true,
|
||||
partialFilterExpression: activeAssignmentFilter,
|
||||
},
|
||||
);
|
||||
TournamentCodeAssignmentSchema.index(
|
||||
{ riotMatchId: 1 },
|
||||
{
|
||||
unique: true,
|
||||
partialFilterExpression: { riotMatchId: { $type: 'string' } },
|
||||
},
|
||||
);
|
||||
TournamentCodeAssignmentSchema.index({ competitionId: 1, state: 1 });
|
||||
|
||||
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',
|
||||
},
|
||||
];
|
||||
Reference in new issue
Block a user