feat: redesign auction engine with redis sot
This commit is contained in:
1 parent
737702d78c
commit
e8bb20d988
75 files changed
+12737
-542
No files matched your search
@@ -0,0 +1,94 @@
|
||||
import {
|
||||
AuctionHistoryEntrySchema,
|
||||
AuctionSchema,
|
||||
AuctionSideEffectApplicationSchema,
|
||||
tournamentModelDefinitions,
|
||||
} from './schema';
|
||||
|
||||
function indexWithKeys(
|
||||
schemaIndexes: ReturnType<typeof AuctionSchema.indexes>,
|
||||
expectedKeys: Record<string, 1 | -1>,
|
||||
) {
|
||||
return schemaIndexes.find(([keys]) => {
|
||||
return JSON.stringify(keys) === JSON.stringify(expectedKeys);
|
||||
});
|
||||
}
|
||||
|
||||
describe('auction Redis schema foundation', () => {
|
||||
it('defines Redis v1 projection metadata on auction documents', () => {
|
||||
expect(AuctionSchema.path('revision')).toBeDefined();
|
||||
expect(AuctionSchema.path('auctionEngineVersion')).toBeDefined();
|
||||
expect(AuctionSchema.path('auctionLifecycleId')).toBeDefined();
|
||||
expect(AuctionSchema.path('lastAppliedAuctionStateTransitionId')).toBeDefined();
|
||||
expect(AuctionSchema.path('currentAuctionHistorySequence')).toBeDefined();
|
||||
expect(AuctionSchema.path('startAuctionRequestId')).toBeDefined();
|
||||
expect(AuctionSchema.path('startAuctionStateTransitionId')).toBeDefined();
|
||||
expect(AuctionSchema.path('matchSideEffectStatus')).toBeDefined();
|
||||
expect(AuctionSchema.path('matchSideEffectRevision')).toBeDefined();
|
||||
});
|
||||
|
||||
it('defines the new auction state field names for migration targets', () => {
|
||||
expect(AuctionSchema.path('auctionTeams')).toBeDefined();
|
||||
expect(AuctionSchema.path('auctionPlayerOrder')).toBeDefined();
|
||||
expect(AuctionSchema.path('passedAuctionPlayerQueue')).toBeDefined();
|
||||
expect(AuctionSchema.path('currentAuctionRound')).toBeDefined();
|
||||
expect(AuctionSchema.path('currentAuctionPlayerOrderIndex')).toBeDefined();
|
||||
expect(AuctionSchema.path('auctionBidHistory')).toBeDefined();
|
||||
expect(AuctionSchema.path('currentHighestBidPointAmount')).toBeDefined();
|
||||
expect(AuctionSchema.path('currentHighestBidderTeamIndex')).toBeDefined();
|
||||
expect(AuctionSchema.path('currentBidRoundDeadlineTimestampMS')).toBeDefined();
|
||||
expect(AuctionSchema.path('maximumBidRoundDurationSeconds')).toBeDefined();
|
||||
expect(AuctionSchema.path('bidRoundExtensionSeconds')).toBeDefined();
|
||||
expect(AuctionSchema.path('initialTeamBidPointAmount')).toBeDefined();
|
||||
expect(AuctionSchema.path('topTierAuctionPriorityThreshold')).toBeDefined();
|
||||
expect(AuctionSchema.path('manualTopTierAuctionPriorityUserIds')).toBeDefined();
|
||||
expect(AuctionSchema.path('auctionStartedAtTimestampMS')).toBeDefined();
|
||||
expect(AuctionSchema.path('auctionCompletedAtTimestampMS')).toBeDefined();
|
||||
});
|
||||
|
||||
it('adds history transition fields and reset/undo action enum values', () => {
|
||||
expect(AuctionHistoryEntrySchema.path('auctionStateTransitionId')).toBeDefined();
|
||||
expect(AuctionHistoryEntrySchema.path('auctionLifecycleId')).toBeDefined();
|
||||
expect(AuctionHistoryEntrySchema.path('revision')).toBeDefined();
|
||||
expect(AuctionHistoryEntrySchema.path('historyMutation')).toBeDefined();
|
||||
expect(AuctionHistoryEntrySchema.path('matchSideEffectMutation')).toBeDefined();
|
||||
expect(
|
||||
AuctionHistoryEntrySchema.path<string>('action').options.enum,
|
||||
).toEqual(
|
||||
expect.arrayContaining(['start', 'open-bidding', 'bid', 'sold', 'passed', 'next', 'complete', 'reset', 'undo']),
|
||||
);
|
||||
});
|
||||
|
||||
it('adds unique history indexes for sequence and transition idempotency', () => {
|
||||
const indexes = AuctionHistoryEntrySchema.indexes();
|
||||
|
||||
expect(indexWithKeys(indexes, { auctionId: 1, sequence: 1 })?.[1]).toMatchObject({
|
||||
unique: true,
|
||||
});
|
||||
expect(
|
||||
indexWithKeys(indexes, { auctionId: 1, auctionStateTransitionId: 1 })?.[1],
|
||||
).toMatchObject({ unique: true, sparse: true });
|
||||
});
|
||||
|
||||
it('registers auction side effect application model with lease fields and unique idempotency index', () => {
|
||||
expect(AuctionSideEffectApplicationSchema.path('processingOwnerToken')).toBeDefined();
|
||||
expect(
|
||||
AuctionSideEffectApplicationSchema.path('processingLeaseExpiresAtTimestampMS'),
|
||||
).toBeDefined();
|
||||
expect(AuctionSideEffectApplicationSchema.path('payloadChecksum')).toBeDefined();
|
||||
|
||||
const indexes = AuctionSideEffectApplicationSchema.indexes();
|
||||
expect(
|
||||
indexWithKeys(indexes, {
|
||||
auctionId: 1,
|
||||
auctionStateTransitionId: 1,
|
||||
sideEffectType: 1,
|
||||
})?.[1],
|
||||
).toMatchObject({ unique: true });
|
||||
expect(
|
||||
tournamentModelDefinitions.some(
|
||||
(definition) => definition.name === 'auctionSideEffectApplication',
|
||||
),
|
||||
).toBe(true);
|
||||
});
|
||||
});
|
||||
@@ -95,6 +95,6 @@ export class ConnectionService implements OnModuleInit {
|
||||
}
|
||||
|
||||
public async onModuleDestroy() {
|
||||
this.connection?.close();
|
||||
await this.connection?.close();
|
||||
}
|
||||
}
|
||||
+219
@@ -0,0 +1,219 @@
|
||||
import { migrateAuctionRedisFieldRename } from './20260622-auction-redis-field-rename';
|
||||
|
||||
type Doc = Record<string, unknown> & { _id: string };
|
||||
|
||||
function createDb(params: { auctions?: Doc[]; history?: Doc[] }) {
|
||||
const collections = {
|
||||
auction: params.auctions ?? [],
|
||||
auctionHistory: params.history ?? [],
|
||||
};
|
||||
|
||||
return {
|
||||
collections,
|
||||
db: {
|
||||
collection(name: string) {
|
||||
const docs =
|
||||
name === 'auction'
|
||||
? collections.auction
|
||||
: name === 'auctionHistory'
|
||||
? collections.auctionHistory
|
||||
: [];
|
||||
return {
|
||||
find() {
|
||||
return {
|
||||
limit() {
|
||||
return {
|
||||
async *[Symbol.asyncIterator]() {
|
||||
for (const doc of docs) yield doc;
|
||||
},
|
||||
};
|
||||
},
|
||||
};
|
||||
},
|
||||
async bulkWrite(writes: Array<{
|
||||
updateOne: {
|
||||
filter: { _id: unknown };
|
||||
update: {
|
||||
$set?: Record<string, unknown>;
|
||||
$unset?: Record<string, ''>;
|
||||
};
|
||||
};
|
||||
}>) {
|
||||
for (const write of writes) {
|
||||
const doc = docs.find((item) => item._id === write.updateOne.filter._id);
|
||||
if (!doc) continue;
|
||||
Object.assign(doc, write.updateOne.update.$set ?? {});
|
||||
for (const key of Object.keys(write.updateOne.update.$unset ?? {})) {
|
||||
delete doc[key];
|
||||
}
|
||||
}
|
||||
},
|
||||
};
|
||||
},
|
||||
},
|
||||
};
|
||||
}
|
||||
|
||||
describe('migrateAuctionRedisFieldRename', () => {
|
||||
it('renames auction fields and nested bid/team fields', async () => {
|
||||
const deadline = new Date('2026-06-22T01:02:03.004Z');
|
||||
const { db, collections } = createDb({
|
||||
auctions: [
|
||||
{
|
||||
_id: 'auction-1',
|
||||
teams: [
|
||||
{
|
||||
teamIndex: 1,
|
||||
captainUserId: 'u1',
|
||||
points: 900,
|
||||
members: [{ userId: 'u1', assignedPosition: 'TOP', cost: 100 }],
|
||||
},
|
||||
],
|
||||
order: [{ userId: 'p1', passCount: 0 }],
|
||||
passedQueue: [{ userId: 'p2', passCount: 1 }],
|
||||
auctionRound: 2,
|
||||
currentIndex: 3,
|
||||
bids: [
|
||||
{
|
||||
teamIndex: 1,
|
||||
amount: 120,
|
||||
bidderUserId: 'u1',
|
||||
timestamp: deadline,
|
||||
},
|
||||
],
|
||||
currentBid: 120,
|
||||
currentBidderTeamIndex: 1,
|
||||
currentDeadline: deadline,
|
||||
maxTimerSeconds: 30,
|
||||
bidExtensionSeconds: 5,
|
||||
initialPoints: 1000,
|
||||
topTierThreshold: 'DIAMOND',
|
||||
manualTopTierUserIds: ['u2'],
|
||||
startedAt: deadline,
|
||||
completedAt: deadline,
|
||||
},
|
||||
],
|
||||
});
|
||||
|
||||
const dryRunStats = await migrateAuctionRedisFieldRename(db, {
|
||||
dryRun: true,
|
||||
});
|
||||
expect(dryRunStats.changedAuctions).toBe(1);
|
||||
expect(collections.auction[0]).toHaveProperty('teams');
|
||||
|
||||
const stats = await migrateAuctionRedisFieldRename(db, { dryRun: false });
|
||||
|
||||
expect(stats.changedAuctions).toBe(1);
|
||||
expect(collections.auction[0]).toMatchObject({
|
||||
auctionEngineVersion: 'legacy-renamed',
|
||||
auctionLifecycleId: 'legacy-auction:auction-1',
|
||||
revision: 0,
|
||||
currentAuctionHistorySequence: 0,
|
||||
matchSideEffectStatus: 'none',
|
||||
matchSideEffectRevision: 0,
|
||||
auctionTeams: [
|
||||
{
|
||||
teamIndex: 1,
|
||||
captainUserId: 'u1',
|
||||
remainingBidPointAmount: 900,
|
||||
members: [
|
||||
{
|
||||
userId: 'u1',
|
||||
assignedPosition: 'TOP',
|
||||
memberAcquisitionBidPointAmount: 100,
|
||||
},
|
||||
],
|
||||
},
|
||||
],
|
||||
auctionPlayerOrder: [{ userId: 'p1', passCount: 0 }],
|
||||
passedAuctionPlayerQueue: [{ userId: 'p2', passCount: 1 }],
|
||||
currentAuctionRound: 2,
|
||||
currentAuctionPlayerOrderIndex: 3,
|
||||
auctionBidHistory: [
|
||||
{
|
||||
bidderTeamIndex: 1,
|
||||
bidPointAmount: 120,
|
||||
bidderUserId: 'u1',
|
||||
bidPlacedAtTimestampMS: deadline.getTime(),
|
||||
},
|
||||
],
|
||||
currentHighestBidPointAmount: 120,
|
||||
currentHighestBidderTeamIndex: 1,
|
||||
currentBidRoundDeadlineTimestampMS: deadline.getTime(),
|
||||
maximumBidRoundDurationSeconds: 30,
|
||||
bidRoundExtensionSeconds: 5,
|
||||
initialTeamBidPointAmount: 1000,
|
||||
topTierAuctionPriorityThreshold: 'DIAMOND',
|
||||
manualTopTierAuctionPriorityUserIds: ['u2'],
|
||||
auctionStartedAtTimestampMS: deadline.getTime(),
|
||||
auctionCompletedAtTimestampMS: deadline.getTime(),
|
||||
});
|
||||
expect(collections.auction[0]).not.toHaveProperty('teams');
|
||||
expect(collections.auction[0]).not.toHaveProperty('bids');
|
||||
expect(collections.auction[0]).not.toHaveProperty('currentDeadline');
|
||||
});
|
||||
|
||||
it('renames history snapshots and adds transition metadata', async () => {
|
||||
const { db, collections } = createDb({
|
||||
history: [
|
||||
{
|
||||
_id: 'history-1',
|
||||
auctionId: 'auction-1',
|
||||
matchId: 'match-1',
|
||||
sequence: 2,
|
||||
action: 'sold',
|
||||
snapshotBefore: {
|
||||
currentBid: 100,
|
||||
bids: [{ teamIndex: 1, amount: 100, timestamp: 123 }],
|
||||
},
|
||||
},
|
||||
],
|
||||
});
|
||||
|
||||
await migrateAuctionRedisFieldRename(db, { dryRun: false });
|
||||
|
||||
expect(collections.auctionHistory[0]).toMatchObject({
|
||||
auctionLifecycleId: 'legacy-auction:auction-1',
|
||||
auctionStateTransitionId: 'legacy-history:auction-1:2',
|
||||
revision: 2,
|
||||
historyMutation: { type: 'append' },
|
||||
matchSideEffectMutation: { type: 'none' },
|
||||
snapshotBefore: {
|
||||
currentHighestBidPointAmount: 100,
|
||||
auctionBidHistory: [
|
||||
{
|
||||
bidderTeamIndex: 1,
|
||||
bidPointAmount: 100,
|
||||
bidPlacedAtTimestampMS: 123,
|
||||
},
|
||||
],
|
||||
},
|
||||
});
|
||||
});
|
||||
|
||||
it('is idempotent after applying updates', async () => {
|
||||
const { db } = createDb({
|
||||
auctions: [{ _id: 'auction-1', teams: [] }],
|
||||
history: [{ _id: 'history-1', auctionId: 'auction-1', sequence: 1 }],
|
||||
});
|
||||
|
||||
await migrateAuctionRedisFieldRename(db, { dryRun: false });
|
||||
const stats = await migrateAuctionRedisFieldRename(db, { dryRun: false });
|
||||
|
||||
expect(stats.changedAuctions).toBe(0);
|
||||
expect(stats.changedHistoryEntries).toBe(0);
|
||||
});
|
||||
|
||||
it('fails apply when duplicate history sequences would break unique indexes', async () => {
|
||||
const { db } = createDb({
|
||||
history: [
|
||||
{ _id: 'h1', auctionId: 'auction-1', sequence: 1 },
|
||||
{ _id: 'h2', auctionId: 'auction-1', sequence: 1 },
|
||||
],
|
||||
});
|
||||
|
||||
await expect(
|
||||
migrateAuctionRedisFieldRename(db, { dryRun: false }),
|
||||
).rejects.toThrow('Duplicate auctionHistory sequence');
|
||||
});
|
||||
});
|
||||
+336
@@ -0,0 +1,336 @@
|
||||
interface MigrationCollection {
|
||||
find(filter: Record<string, unknown>): {
|
||||
limit(limit: number): AsyncIterable<AnyDoc>;
|
||||
};
|
||||
bulkWrite(
|
||||
writes: Array<{
|
||||
updateOne: {
|
||||
filter: { _id: unknown };
|
||||
update: { $set?: Record<string, unknown>; $unset?: Record<string, ''> };
|
||||
};
|
||||
}>,
|
||||
options: { ordered: boolean },
|
||||
): Promise<unknown>;
|
||||
}
|
||||
|
||||
interface MigrationDb {
|
||||
collection(name: string): MigrationCollection;
|
||||
}
|
||||
|
||||
interface MigrationOptions {
|
||||
dryRun?: boolean;
|
||||
limit?: number;
|
||||
}
|
||||
|
||||
interface MigrationStats {
|
||||
changedAuctions: number;
|
||||
changedHistoryEntries: number;
|
||||
duplicateHistorySequences: number;
|
||||
scannedAuctions: number;
|
||||
scannedHistoryEntries: number;
|
||||
}
|
||||
|
||||
type AnyDoc = Record<string, unknown> & { _id: unknown };
|
||||
type UpdateDoc = { $set?: Record<string, unknown>; $unset?: Record<string, ''> };
|
||||
|
||||
const AUCTION_FIELD_RENAMES: Array<{
|
||||
from: string;
|
||||
to: string;
|
||||
transform?: (value: unknown) => unknown;
|
||||
}> = [
|
||||
{ from: 'teams', to: 'auctionTeams', transform: transformAuctionTeams },
|
||||
{ from: 'order', to: 'auctionPlayerOrder' },
|
||||
{ from: 'passedQueue', to: 'passedAuctionPlayerQueue' },
|
||||
{ from: 'auctionRound', to: 'currentAuctionRound' },
|
||||
{ from: 'currentIndex', to: 'currentAuctionPlayerOrderIndex' },
|
||||
{ from: 'bids', to: 'auctionBidHistory', transform: transformAuctionBidHistory },
|
||||
{ from: 'currentBid', to: 'currentHighestBidPointAmount' },
|
||||
{ from: 'currentBidderTeamIndex', to: 'currentHighestBidderTeamIndex' },
|
||||
{
|
||||
from: 'currentDeadline',
|
||||
to: 'currentBidRoundDeadlineTimestampMS',
|
||||
transform: toTimestampMS,
|
||||
},
|
||||
{ from: 'maxTimerSeconds', to: 'maximumBidRoundDurationSeconds' },
|
||||
{ from: 'bidExtensionSeconds', to: 'bidRoundExtensionSeconds' },
|
||||
{ from: 'initialPoints', to: 'initialTeamBidPointAmount' },
|
||||
{ from: 'topTierThreshold', to: 'topTierAuctionPriorityThreshold' },
|
||||
{
|
||||
from: 'manualTopTierUserIds',
|
||||
to: 'manualTopTierAuctionPriorityUserIds',
|
||||
},
|
||||
{
|
||||
from: 'startedAt',
|
||||
to: 'auctionStartedAtTimestampMS',
|
||||
transform: toTimestampMS,
|
||||
},
|
||||
{
|
||||
from: 'completedAt',
|
||||
to: 'auctionCompletedAtTimestampMS',
|
||||
transform: toTimestampMS,
|
||||
},
|
||||
];
|
||||
|
||||
export async function migrateAuctionRedisFieldRename(
|
||||
db: MigrationDb,
|
||||
options: MigrationOptions = {},
|
||||
): Promise<MigrationStats> {
|
||||
const dryRun = options.dryRun ?? true;
|
||||
const stats: MigrationStats = {
|
||||
changedAuctions: 0,
|
||||
changedHistoryEntries: 0,
|
||||
duplicateHistorySequences: 0,
|
||||
scannedAuctions: 0,
|
||||
scannedHistoryEntries: 0,
|
||||
};
|
||||
|
||||
stats.duplicateHistorySequences = await countDuplicateHistorySequences(
|
||||
db.collection('auctionHistory'),
|
||||
options.limit,
|
||||
);
|
||||
if (!dryRun && stats.duplicateHistorySequences > 0) {
|
||||
throw new Error(
|
||||
`Duplicate auctionHistory sequence entries found: ${stats.duplicateHistorySequences}`,
|
||||
);
|
||||
}
|
||||
|
||||
await migrateCollection(
|
||||
db.collection('auction'),
|
||||
options.limit,
|
||||
dryRun,
|
||||
stats,
|
||||
'auction',
|
||||
);
|
||||
await migrateCollection(
|
||||
db.collection('auctionHistory'),
|
||||
options.limit,
|
||||
dryRun,
|
||||
stats,
|
||||
'auctionHistory',
|
||||
);
|
||||
|
||||
return stats;
|
||||
}
|
||||
|
||||
async function migrateCollection(
|
||||
collection: MigrationCollection,
|
||||
limit: number | undefined,
|
||||
dryRun: boolean,
|
||||
stats: MigrationStats,
|
||||
kind: 'auction' | 'auctionHistory',
|
||||
) {
|
||||
const writes: Array<{
|
||||
updateOne: {
|
||||
filter: { _id: unknown };
|
||||
update: UpdateDoc;
|
||||
};
|
||||
}> = [];
|
||||
|
||||
for await (const doc of collection.find({}).limit(limit ?? 0)) {
|
||||
if (kind === 'auction') stats.scannedAuctions += 1;
|
||||
if (kind === 'auctionHistory') stats.scannedHistoryEntries += 1;
|
||||
|
||||
const update =
|
||||
kind === 'auction' ? migrateAuctionDoc(doc) : migrateHistoryDoc(doc);
|
||||
if (!update) continue;
|
||||
|
||||
if (kind === 'auction') stats.changedAuctions += 1;
|
||||
if (kind === 'auctionHistory') stats.changedHistoryEntries += 1;
|
||||
writes.push({
|
||||
updateOne: {
|
||||
filter: { _id: doc._id },
|
||||
update,
|
||||
},
|
||||
});
|
||||
}
|
||||
|
||||
if (!dryRun && writes.length > 0) {
|
||||
await collection.bulkWrite(writes, { ordered: false });
|
||||
}
|
||||
}
|
||||
|
||||
async function countDuplicateHistorySequences(
|
||||
collection: MigrationCollection,
|
||||
limit: number | undefined,
|
||||
): Promise<number> {
|
||||
const seen = new Set<string>();
|
||||
const duplicates = new Set<string>();
|
||||
for await (const doc of collection.find({}).limit(limit ?? 0)) {
|
||||
const auctionId = stringifyId(doc.auctionId);
|
||||
const sequence = typeof doc.sequence === 'number' ? doc.sequence : undefined;
|
||||
if (!auctionId || sequence === undefined) continue;
|
||||
|
||||
const key = `${auctionId}:${sequence}`;
|
||||
if (seen.has(key)) duplicates.add(key);
|
||||
seen.add(key);
|
||||
}
|
||||
return duplicates.size;
|
||||
}
|
||||
|
||||
function migrateAuctionDoc(doc: AnyDoc): UpdateDoc | null {
|
||||
const set: Record<string, unknown> = {};
|
||||
const unset: Record<string, ''> = {};
|
||||
|
||||
for (const rename of AUCTION_FIELD_RENAMES) {
|
||||
renameField(doc, rename.from, rename.to, set, unset, rename.transform);
|
||||
}
|
||||
|
||||
setDefault(doc, set, 'revision', 0);
|
||||
setDefault(doc, set, 'currentAuctionHistorySequence', 0);
|
||||
setDefault(doc, set, 'auctionEngineVersion', 'legacy-renamed');
|
||||
setDefault(doc, set, 'auctionLifecycleId', legacyAuctionLifecycleId(doc._id));
|
||||
setDefault(doc, set, 'matchSideEffectStatus', 'none');
|
||||
setDefault(doc, set, 'matchSideEffectRevision', 0);
|
||||
|
||||
return buildUpdate(set, unset);
|
||||
}
|
||||
|
||||
function migrateHistoryDoc(doc: AnyDoc): UpdateDoc | null {
|
||||
const set: Record<string, unknown> = {};
|
||||
const unset: Record<string, ''> = {};
|
||||
const auctionId = stringifyId(doc.auctionId);
|
||||
const sequence = typeof doc.sequence === 'number' ? doc.sequence : 0;
|
||||
|
||||
setDefault(
|
||||
doc,
|
||||
set,
|
||||
'auctionLifecycleId',
|
||||
auctionId ? legacyAuctionLifecycleId(auctionId) : undefined,
|
||||
);
|
||||
setDefault(
|
||||
doc,
|
||||
set,
|
||||
'auctionStateTransitionId',
|
||||
auctionId ? `legacy-history:${auctionId}:${sequence}` : undefined,
|
||||
);
|
||||
setDefault(doc, set, 'revision', sequence);
|
||||
setDefault(doc, set, 'historyMutation', { type: 'append' });
|
||||
setDefault(doc, set, 'matchSideEffectMutation', { type: 'none' });
|
||||
|
||||
const snapshotBefore = isRecord(doc.snapshotBefore)
|
||||
? migrateAuctionSnapshot(doc.snapshotBefore)
|
||||
: undefined;
|
||||
if (snapshotBefore?.changed) {
|
||||
set.snapshotBefore = snapshotBefore.value;
|
||||
}
|
||||
|
||||
return buildUpdate(set, unset);
|
||||
}
|
||||
|
||||
function migrateAuctionSnapshot(snapshot: Record<string, unknown>): {
|
||||
changed: boolean;
|
||||
value: Record<string, unknown>;
|
||||
} {
|
||||
const value = { ...snapshot };
|
||||
let changed = false;
|
||||
for (const rename of AUCTION_FIELD_RENAMES) {
|
||||
if (!(rename.from in value)) continue;
|
||||
if (!(rename.to in value)) {
|
||||
value[rename.to] = rename.transform
|
||||
? rename.transform(value[rename.from])
|
||||
: value[rename.from];
|
||||
}
|
||||
delete value[rename.from];
|
||||
changed = true;
|
||||
}
|
||||
return { changed, value };
|
||||
}
|
||||
|
||||
function renameField(
|
||||
doc: Record<string, unknown>,
|
||||
from: string,
|
||||
to: string,
|
||||
set: Record<string, unknown>,
|
||||
unset: Record<string, ''>,
|
||||
transform?: (value: unknown) => unknown,
|
||||
) {
|
||||
if (!(from in doc)) return;
|
||||
if (!(to in doc)) {
|
||||
set[to] = transform ? transform(doc[from]) : doc[from];
|
||||
}
|
||||
unset[from] = '';
|
||||
}
|
||||
|
||||
function setDefault(
|
||||
doc: Record<string, unknown>,
|
||||
set: Record<string, unknown>,
|
||||
key: string,
|
||||
value: unknown,
|
||||
) {
|
||||
if (value === undefined || key in doc || key in set) return;
|
||||
set[key] = value;
|
||||
}
|
||||
|
||||
function buildUpdate(
|
||||
set: Record<string, unknown>,
|
||||
unset: Record<string, ''>,
|
||||
): UpdateDoc | null {
|
||||
const update: UpdateDoc = {};
|
||||
if (Object.keys(set).length > 0) update.$set = set;
|
||||
if (Object.keys(unset).length > 0) update.$unset = unset;
|
||||
return update.$set || update.$unset ? update : null;
|
||||
}
|
||||
|
||||
function transformAuctionTeams(value: unknown): unknown[] {
|
||||
return asArray(value).map((team) => {
|
||||
const next = { ...team };
|
||||
if ('points' in next && !('remainingBidPointAmount' in next)) {
|
||||
next.remainingBidPointAmount = next.points;
|
||||
}
|
||||
delete next.points;
|
||||
next.members = asArray(next.members).map((member) => {
|
||||
const migratedMember = { ...member };
|
||||
if (
|
||||
'cost' in migratedMember &&
|
||||
!('memberAcquisitionBidPointAmount' in migratedMember)
|
||||
) {
|
||||
migratedMember.memberAcquisitionBidPointAmount = migratedMember.cost;
|
||||
}
|
||||
delete migratedMember.cost;
|
||||
return migratedMember;
|
||||
});
|
||||
return next;
|
||||
});
|
||||
}
|
||||
|
||||
function transformAuctionBidHistory(value: unknown): unknown[] {
|
||||
return asArray(value).map((bid) => {
|
||||
const next: Record<string, unknown> = {};
|
||||
if ('teamIndex' in bid) next.bidderTeamIndex = bid.teamIndex;
|
||||
if ('amount' in bid) next.bidPointAmount = bid.amount;
|
||||
if ('bidderUserId' in bid) next.bidderUserId = bid.bidderUserId;
|
||||
if ('timestamp' in bid) {
|
||||
next.bidPlacedAtTimestampMS = toTimestampMS(bid.timestamp);
|
||||
}
|
||||
return next;
|
||||
});
|
||||
}
|
||||
|
||||
function toTimestampMS(value: unknown): unknown {
|
||||
if (value instanceof Date) return value.getTime();
|
||||
if (typeof value === 'number') return value;
|
||||
if (typeof value === 'string') {
|
||||
const parsed = Date.parse(value);
|
||||
return Number.isNaN(parsed) ? value : parsed;
|
||||
}
|
||||
return value;
|
||||
}
|
||||
|
||||
function legacyAuctionLifecycleId(value: unknown): string {
|
||||
return `legacy-auction:${stringifyId(value) ?? 'unknown'}`;
|
||||
}
|
||||
|
||||
function stringifyId(value: unknown): string | undefined {
|
||||
if (value === undefined || value === null) return undefined;
|
||||
return typeof value === 'string' ? value : String(value);
|
||||
}
|
||||
|
||||
function asArray(value: unknown): Record<string, unknown>[] {
|
||||
return Array.isArray(value)
|
||||
? value.filter(isRecord).map((item) => ({ ...item }))
|
||||
: [];
|
||||
}
|
||||
|
||||
function isRecord(value: unknown): value is Record<string, unknown> {
|
||||
return Boolean(value && typeof value === 'object' && !Array.isArray(value));
|
||||
}
|
||||
@@ -0,0 +1,363 @@
|
||||
import 'reflect-metadata';
|
||||
import { Module } from '@nestjs/common';
|
||||
import { ConfigModule } from '@nestjs/config';
|
||||
import { NestFactory } from '@nestjs/core';
|
||||
import mongoose from 'mongoose';
|
||||
import { readFile } from 'node:fs/promises';
|
||||
import { resolve } from 'node:path';
|
||||
import { ConnectionService, resolveMongoDbName } from '../connection.service';
|
||||
import { SecretModule } from '../../secret/secret.module';
|
||||
import { migrateAuctionRedisFieldRename } from './20260622-auction-redis-field-rename';
|
||||
|
||||
interface CliOptions {
|
||||
duplicateSampleLimit: number;
|
||||
dryRun: boolean;
|
||||
limit?: number;
|
||||
listDuplicates: boolean;
|
||||
nodeEnv: string;
|
||||
repairDuplicateSequences: boolean;
|
||||
}
|
||||
|
||||
interface DuplicateSequenceRepairStats {
|
||||
affectedAuctions: number;
|
||||
changedAuctionSequenceFields: number;
|
||||
changedHistoryEntries: number;
|
||||
scannedHistoryEntries: number;
|
||||
}
|
||||
|
||||
@Module({
|
||||
imports: [
|
||||
ConfigModule.forRoot({
|
||||
isGlobal: true,
|
||||
envFilePath: [
|
||||
resolve(__dirname, '../../../../../../project.env'),
|
||||
resolve(process.cwd(), '../project.env'),
|
||||
resolve(process.cwd(), 'apps/project.env'),
|
||||
resolve(process.cwd(), 'project.env'),
|
||||
],
|
||||
}),
|
||||
SecretModule,
|
||||
],
|
||||
providers: [ConnectionService],
|
||||
})
|
||||
class MigrationModule {}
|
||||
|
||||
async function loadMachineEnvFile() {
|
||||
const homeDir = process.env.HOME ?? process.env.USERPROFILE;
|
||||
if (!homeDir) return;
|
||||
|
||||
const machineEnvFile = resolve(homeDir, 'source/global-env-vars.txt');
|
||||
|
||||
try {
|
||||
const file = await readFile(machineEnvFile, 'utf8');
|
||||
for (const rawLine of file.split(/\r?\n/)) {
|
||||
const line = rawLine.trim();
|
||||
if (!line || line.startsWith('#')) continue;
|
||||
|
||||
const matched = line.match(/^([A-Za-z_][A-Za-z0-9_]*)\s*=\s*(.*)$/);
|
||||
if (!matched) continue;
|
||||
|
||||
const [, key, rawValue] = matched;
|
||||
process.env[key] = rawValue.replace(/^["']|["']$/g, '');
|
||||
}
|
||||
} catch {
|
||||
// 로컬 machine env 파일이 없으면 그냥 지나간다.
|
||||
}
|
||||
}
|
||||
|
||||
function parseArgs(argv: string[]): CliOptions {
|
||||
let dryRun = true;
|
||||
let duplicateSampleLimit = 20;
|
||||
let limit: number | undefined;
|
||||
let listDuplicates = false;
|
||||
let nodeEnv = process.env.NODE_ENV ?? 'dev';
|
||||
let repairDuplicateSequences = false;
|
||||
|
||||
for (let index = 0; index < argv.length; index += 1) {
|
||||
const arg = argv[index];
|
||||
if (arg === '--apply') {
|
||||
dryRun = false;
|
||||
continue;
|
||||
}
|
||||
if (arg === '--dry-run') {
|
||||
dryRun = true;
|
||||
continue;
|
||||
}
|
||||
if (arg === '--list-duplicates') {
|
||||
listDuplicates = true;
|
||||
continue;
|
||||
}
|
||||
if (arg === '--repair-duplicate-sequences') {
|
||||
repairDuplicateSequences = true;
|
||||
continue;
|
||||
}
|
||||
if (arg === '--duplicate-sample-limit') {
|
||||
duplicateSampleLimit = parsePositiveInteger(
|
||||
argv[index + 1],
|
||||
'--duplicate-sample-limit',
|
||||
);
|
||||
index += 1;
|
||||
continue;
|
||||
}
|
||||
if (arg.startsWith('--duplicate-sample-limit=')) {
|
||||
duplicateSampleLimit = parsePositiveInteger(
|
||||
arg.slice('--duplicate-sample-limit='.length),
|
||||
'--duplicate-sample-limit',
|
||||
);
|
||||
continue;
|
||||
}
|
||||
if (arg === '--env') {
|
||||
const value = argv[index + 1];
|
||||
if (!value) throw new Error('--env requires dev, staging, or prod.');
|
||||
nodeEnv = value;
|
||||
index += 1;
|
||||
continue;
|
||||
}
|
||||
if (arg.startsWith('--env=')) {
|
||||
nodeEnv = arg.slice('--env='.length);
|
||||
continue;
|
||||
}
|
||||
if (arg === '--limit') {
|
||||
limit = parsePositiveInteger(argv[index + 1], '--limit');
|
||||
index += 1;
|
||||
continue;
|
||||
}
|
||||
if (arg.startsWith('--limit=')) {
|
||||
limit = parsePositiveInteger(arg.slice('--limit='.length), '--limit');
|
||||
continue;
|
||||
}
|
||||
if (arg === '--help' || arg === '-h') {
|
||||
printHelp();
|
||||
process.exit(0);
|
||||
}
|
||||
throw new Error(`Unknown argument: ${arg}`);
|
||||
}
|
||||
|
||||
if (!['dev', 'staging', 'prod'].includes(nodeEnv)) {
|
||||
throw new Error('--env must be dev, staging, or prod.');
|
||||
}
|
||||
|
||||
return {
|
||||
duplicateSampleLimit,
|
||||
dryRun,
|
||||
limit,
|
||||
listDuplicates,
|
||||
nodeEnv,
|
||||
repairDuplicateSequences,
|
||||
};
|
||||
}
|
||||
|
||||
function parsePositiveInteger(value: string | undefined, label: string) {
|
||||
const parsed = Number(value);
|
||||
if (!Number.isInteger(parsed) || parsed < 1) {
|
||||
throw new Error(`${label} requires a positive integer.`);
|
||||
}
|
||||
return parsed;
|
||||
}
|
||||
|
||||
function printHelp() {
|
||||
console.log(`Usage:
|
||||
pnpm --filter api exec ts-node src/modules/global/database/migrations/run-auction-redis-field-rename.ts [--dry-run]
|
||||
pnpm --filter api exec ts-node src/modules/global/database/migrations/run-auction-redis-field-rename.ts --env staging --apply
|
||||
|
||||
Options:
|
||||
--dry-run Print affected counts without writing. Default.
|
||||
--apply Apply auction and auctionHistory field updates.
|
||||
--env <env> dev | staging | prod. Default: NODE_ENV or dev.
|
||||
--limit <n> Only scan first n documents per collection.
|
||||
--list-duplicates
|
||||
Print duplicate auctionHistory auctionId+sequence samples.
|
||||
--duplicate-sample-limit <n>
|
||||
Max duplicate groups to print. Default: 20.
|
||||
--repair-duplicate-sequences
|
||||
Resequence affected auctionHistory rows by takenAt/createdAt/_id before migration.
|
||||
`);
|
||||
}
|
||||
|
||||
async function findAuctionIdsWithDuplicateHistorySequences(
|
||||
db: NonNullable<mongoose.Connection['db']>,
|
||||
): Promise<unknown[]> {
|
||||
const groups = await db
|
||||
.collection('auctionHistory')
|
||||
.aggregate([
|
||||
{
|
||||
$group: {
|
||||
_id: { auctionId: '$auctionId', sequence: '$sequence' },
|
||||
count: { $sum: 1 },
|
||||
},
|
||||
},
|
||||
{ $match: { count: { $gt: 1 } } },
|
||||
{ $group: { _id: '$_id.auctionId' } },
|
||||
])
|
||||
.toArray();
|
||||
|
||||
return groups.map((group) => group._id).filter(Boolean);
|
||||
}
|
||||
|
||||
async function listDuplicateHistorySequences(
|
||||
db: NonNullable<mongoose.Connection['db']>,
|
||||
sampleLimit: number,
|
||||
) {
|
||||
return db
|
||||
.collection('auctionHistory')
|
||||
.aggregate([
|
||||
{
|
||||
$group: {
|
||||
_id: { auctionId: '$auctionId', sequence: '$sequence' },
|
||||
actions: { $push: '$action' },
|
||||
count: { $sum: 1 },
|
||||
createdAtValues: { $push: '$createdAt' },
|
||||
historyIds: { $push: '$_id' },
|
||||
matchIds: { $addToSet: '$matchId' },
|
||||
takenAtValues: { $push: '$takenAt' },
|
||||
},
|
||||
},
|
||||
{ $match: { count: { $gt: 1 } } },
|
||||
{ $sort: { count: -1, '_id.sequence': 1 } },
|
||||
{ $limit: sampleLimit },
|
||||
])
|
||||
.toArray();
|
||||
}
|
||||
|
||||
function printDuplicateHistorySequences(groups: Awaited<
|
||||
ReturnType<typeof listDuplicateHistorySequences>
|
||||
>) {
|
||||
if (groups.length === 0) {
|
||||
console.log('No duplicate auctionHistory auctionId+sequence groups found.');
|
||||
return;
|
||||
}
|
||||
|
||||
console.log('Duplicate auctionHistory auctionId+sequence groups:');
|
||||
console.table(
|
||||
groups.map((group) => ({
|
||||
actions: (group.actions as unknown[]).map(String).join(', '),
|
||||
auctionId: String(group._id?.auctionId ?? ''),
|
||||
count: group.count,
|
||||
createdAt: (group.createdAtValues as unknown[]).map(String).join(', '),
|
||||
historyIds: (group.historyIds as unknown[]).map(String).join(', '),
|
||||
matchIds: (group.matchIds as unknown[]).map(String).join(', '),
|
||||
sequence: group._id?.sequence,
|
||||
takenAt: (group.takenAtValues as unknown[]).map(String).join(', '),
|
||||
})),
|
||||
);
|
||||
}
|
||||
|
||||
async function repairDuplicateHistorySequences(
|
||||
db: NonNullable<mongoose.Connection['db']>,
|
||||
dryRun: boolean,
|
||||
): Promise<DuplicateSequenceRepairStats> {
|
||||
const historyCollection = db.collection('auctionHistory');
|
||||
const auctionCollection = db.collection('auction');
|
||||
const affectedAuctionIds = await findAuctionIdsWithDuplicateHistorySequences(db);
|
||||
const stats: DuplicateSequenceRepairStats = {
|
||||
affectedAuctions: affectedAuctionIds.length,
|
||||
changedAuctionSequenceFields: 0,
|
||||
changedHistoryEntries: 0,
|
||||
scannedHistoryEntries: 0,
|
||||
};
|
||||
|
||||
for (const auctionId of affectedAuctionIds) {
|
||||
const histories = await historyCollection
|
||||
.find({ auctionId })
|
||||
.project({ _id: 1, createdAt: 1, sequence: 1, takenAt: 1 })
|
||||
.sort({ takenAt: 1, createdAt: 1, _id: 1 })
|
||||
.toArray();
|
||||
stats.scannedHistoryEntries += histories.length;
|
||||
|
||||
const writes = histories
|
||||
.map((history, index) => ({
|
||||
history,
|
||||
nextSequence: index + 1,
|
||||
}))
|
||||
.filter(({ history, nextSequence }) => history.sequence !== nextSequence);
|
||||
|
||||
stats.changedHistoryEntries += writes.length;
|
||||
stats.changedAuctionSequenceFields += 1;
|
||||
|
||||
if (dryRun) continue;
|
||||
if (writes.length > 0) {
|
||||
await historyCollection.bulkWrite(
|
||||
writes.map(({ history, nextSequence }) => ({
|
||||
updateOne: {
|
||||
filter: { _id: history._id },
|
||||
update: { $set: { sequence: nextSequence } },
|
||||
},
|
||||
})),
|
||||
{ ordered: true },
|
||||
);
|
||||
}
|
||||
await auctionCollection.updateOne(
|
||||
{ _id: auctionId } as Record<string, unknown>,
|
||||
{ $set: { currentAuctionHistorySequence: histories.length } },
|
||||
);
|
||||
}
|
||||
|
||||
return stats;
|
||||
}
|
||||
|
||||
async function main() {
|
||||
const options = parseArgs(process.argv.slice(2));
|
||||
process.env.NODE_ENV = options.nodeEnv;
|
||||
process.env.SECRET_SOURCE = process.env.SECRET_SOURCE || 'infisical';
|
||||
await loadMachineEnvFile();
|
||||
|
||||
const app = await NestFactory.createApplicationContext(MigrationModule, {
|
||||
logger: ['log', 'warn', 'error'],
|
||||
});
|
||||
|
||||
try {
|
||||
const connectionService = app.get(ConnectionService);
|
||||
const connection = await connectionService.getConnection();
|
||||
if (!connection.db) {
|
||||
throw new Error('MongoDB connection has no db handle.');
|
||||
}
|
||||
|
||||
const dbName = resolveMongoDbName(options.nodeEnv);
|
||||
console.log(
|
||||
`[auction redis field rename migration] mode=${
|
||||
options.dryRun ? 'dry-run' : 'apply'
|
||||
} env=${options.nodeEnv} db=${dbName}`,
|
||||
);
|
||||
|
||||
if (options.repairDuplicateSequences) {
|
||||
const repairStats = await repairDuplicateHistorySequences(
|
||||
connection.db,
|
||||
options.dryRun,
|
||||
);
|
||||
console.log('Duplicate auctionHistory sequence repair:');
|
||||
console.table(repairStats);
|
||||
}
|
||||
|
||||
const stats = await migrateAuctionRedisFieldRename(
|
||||
connection.db as Parameters<typeof migrateAuctionRedisFieldRename>[0],
|
||||
{
|
||||
dryRun: options.dryRun,
|
||||
limit: options.limit,
|
||||
},
|
||||
);
|
||||
|
||||
console.table(stats);
|
||||
if (options.listDuplicates && stats.duplicateHistorySequences > 0) {
|
||||
const duplicateGroups = await listDuplicateHistorySequences(
|
||||
connection.db,
|
||||
options.duplicateSampleLimit,
|
||||
);
|
||||
printDuplicateHistorySequences(duplicateGroups);
|
||||
}
|
||||
if (options.dryRun) {
|
||||
console.log(
|
||||
'No writes were made. Re-run with --apply to update documents.',
|
||||
);
|
||||
} else {
|
||||
console.log('Migration applied.');
|
||||
}
|
||||
} finally {
|
||||
await app.close();
|
||||
await mongoose.disconnect();
|
||||
}
|
||||
}
|
||||
|
||||
void main().catch((error: unknown) => {
|
||||
console.error(error instanceof Error ? error.message : error);
|
||||
process.exitCode = 1;
|
||||
});
|
||||
@@ -153,14 +153,42 @@ export const AUCTION_TOP_TIER_THRESHOLDS = [
|
||||
] as const;
|
||||
export type AuctionTopTierThreshold =
|
||||
(typeof AUCTION_TOP_TIER_THRESHOLDS)[number];
|
||||
export type AuctionActionType =
|
||||
| 'start'
|
||||
| 'open-bidding'
|
||||
| 'bid'
|
||||
| 'sold'
|
||||
| 'passed'
|
||||
| 'next'
|
||||
| 'complete';
|
||||
export const AUCTION_ACTION_TYPES = [
|
||||
'start',
|
||||
'open-bidding',
|
||||
'bid',
|
||||
'sold',
|
||||
'passed',
|
||||
'next',
|
||||
'complete',
|
||||
'reset',
|
||||
'undo',
|
||||
] as const;
|
||||
export type AuctionActionType = (typeof AUCTION_ACTION_TYPES)[number];
|
||||
export const AUCTION_ENGINE_VERSIONS = ['redis-v1', 'legacy-renamed'] as const;
|
||||
export type AuctionEngineVersion = (typeof AUCTION_ENGINE_VERSIONS)[number];
|
||||
export const AUCTION_MATCH_SIDE_EFFECT_STATUSES = [
|
||||
'none',
|
||||
'pending',
|
||||
'processing',
|
||||
'completed',
|
||||
'failed',
|
||||
] as const;
|
||||
export type AuctionMatchSideEffectStatus =
|
||||
(typeof AUCTION_MATCH_SIDE_EFFECT_STATUSES)[number];
|
||||
export const AUCTION_SIDE_EFFECT_APPLICATION_STATES = [
|
||||
'pending',
|
||||
'processing',
|
||||
'completed',
|
||||
'failed',
|
||||
] as const;
|
||||
export type AuctionSideEffectApplicationState =
|
||||
(typeof AUCTION_SIDE_EFFECT_APPLICATION_STATES)[number];
|
||||
|
||||
export type AuctionHistoryMutation =
|
||||
| { type: 'append' }
|
||||
| { targetHistorySequence: number; type: 'deleteAfterSequenceAndAppendUndo' }
|
||||
| { resetMode: 'clear-history' | 'preserve-audit'; type: 'reset' };
|
||||
|
||||
export interface MongooseTimestamps {
|
||||
createdAt: Date;
|
||||
@@ -616,6 +644,20 @@ export interface AuctionTeam {
|
||||
members: AuctionTeamMember[];
|
||||
}
|
||||
|
||||
export interface RedisAuctionTeamMember {
|
||||
userId: mongoose.Types.ObjectId;
|
||||
assignedPosition: Position;
|
||||
memberAcquisitionBidPointAmount: number;
|
||||
}
|
||||
|
||||
export interface RedisAuctionTeam {
|
||||
teamIndex: number;
|
||||
name?: string;
|
||||
captainUserId: mongoose.Types.ObjectId;
|
||||
remainingBidPointAmount: number;
|
||||
members: RedisAuctionTeamMember[];
|
||||
}
|
||||
|
||||
export interface AuctionBid {
|
||||
teamIndex: number;
|
||||
amount: number;
|
||||
@@ -623,6 +665,13 @@ export interface AuctionBid {
|
||||
timestamp: Date;
|
||||
}
|
||||
|
||||
export interface AuctionBidHistoryEntry {
|
||||
bidderTeamIndex: number;
|
||||
bidPointAmount: number;
|
||||
bidderUserId: mongoose.Types.ObjectId;
|
||||
bidPlacedAtTimestampMS: number;
|
||||
}
|
||||
|
||||
export interface AuctionChatMessage {
|
||||
id: string;
|
||||
userId?: mongoose.Types.ObjectId;
|
||||
@@ -634,6 +683,17 @@ export interface AuctionChatMessage {
|
||||
export interface Auction extends MongooseTimestamps {
|
||||
matchId: mongoose.Types.ObjectId;
|
||||
phase: AuctionPhase;
|
||||
revision: number;
|
||||
auctionEngineVersion?: AuctionEngineVersion;
|
||||
auctionLifecycleId?: string;
|
||||
lastAppliedAuctionStateTransitionId?: string;
|
||||
currentAuctionHistorySequence: number;
|
||||
startAuctionRequestId?: string;
|
||||
startAuctionStateTransitionId?: string;
|
||||
auctionStartedByUserId?: mongoose.Types.ObjectId;
|
||||
matchSideEffectStatus: AuctionMatchSideEffectStatus;
|
||||
matchSideEffectRevision: number;
|
||||
matchSideEffectError?: string;
|
||||
teams: AuctionTeam[];
|
||||
order: AuctionPlayerSnapshot[];
|
||||
passedQueue: AuctionPlayerSnapshot[];
|
||||
@@ -651,6 +711,22 @@ export interface Auction extends MongooseTimestamps {
|
||||
chatMessages: AuctionChatMessage[];
|
||||
startedAt?: Date;
|
||||
completedAt?: Date;
|
||||
auctionTeams?: RedisAuctionTeam[];
|
||||
auctionPlayerOrder?: AuctionPlayerSnapshot[];
|
||||
passedAuctionPlayerQueue?: AuctionPlayerSnapshot[];
|
||||
currentAuctionRound?: number;
|
||||
currentAuctionPlayerOrderIndex?: number;
|
||||
auctionBidHistory?: AuctionBidHistoryEntry[];
|
||||
currentHighestBidPointAmount?: number;
|
||||
currentHighestBidderTeamIndex?: number;
|
||||
currentBidRoundDeadlineTimestampMS?: number;
|
||||
maximumBidRoundDurationSeconds?: number;
|
||||
bidRoundExtensionSeconds?: number;
|
||||
initialTeamBidPointAmount?: number;
|
||||
topTierAuctionPriorityThreshold?: AuctionTopTierThreshold;
|
||||
manualTopTierAuctionPriorityUserIds?: mongoose.Types.ObjectId[];
|
||||
auctionStartedAtTimestampMS?: number;
|
||||
auctionCompletedAtTimestampMS?: number;
|
||||
}
|
||||
|
||||
export type AuctionDocument = HydratedDocument<Auction>;
|
||||
@@ -660,13 +736,38 @@ export interface AuctionHistoryEntry extends MongooseTimestamps {
|
||||
matchId: mongoose.Types.ObjectId;
|
||||
sequence: number;
|
||||
action: AuctionActionType;
|
||||
auctionStateTransitionId?: string;
|
||||
auctionLifecycleId?: string;
|
||||
revision?: number;
|
||||
actorUserId?: mongoose.Types.ObjectId;
|
||||
snapshotBefore: Record<string, unknown>;
|
||||
historyMutation?: AuctionHistoryMutation;
|
||||
matchSideEffectMutation?: Record<string, unknown>;
|
||||
takenAt: Date;
|
||||
}
|
||||
|
||||
export type AuctionHistoryEntryDocument = HydratedDocument<AuctionHistoryEntry>;
|
||||
|
||||
export interface AuctionSideEffectApplication extends MongooseTimestamps {
|
||||
auctionId: mongoose.Types.ObjectId;
|
||||
matchId: mongoose.Types.ObjectId;
|
||||
auctionStateTransitionId: string;
|
||||
auctionLifecycleId: string;
|
||||
sideEffectType: string;
|
||||
state: AuctionSideEffectApplicationState;
|
||||
payload: Record<string, unknown>;
|
||||
payloadChecksum: string;
|
||||
attempts: number;
|
||||
processingStartedAtTimestampMS?: number;
|
||||
processingLeaseExpiresAtTimestampMS?: number;
|
||||
processingOwnerToken?: string;
|
||||
completedAtTimestampMS?: number;
|
||||
lastError?: string;
|
||||
}
|
||||
|
||||
export type AuctionSideEffectApplicationDocument =
|
||||
HydratedDocument<AuctionSideEffectApplication>;
|
||||
|
||||
// ===== MatchStats (precomputed internal stats fragment) =====
|
||||
export interface MatchStatsRuneStyleSelection {
|
||||
styleId: number;
|
||||
@@ -1646,6 +1747,34 @@ const AuctionTeamSubSchema = new Schema<AuctionTeam>(
|
||||
{ _id: false },
|
||||
);
|
||||
|
||||
const RedisAuctionTeamMemberSubSchema = new Schema<RedisAuctionTeamMember>(
|
||||
{
|
||||
userId: { type: Schema.Types.ObjectId, ref: 'user', required: true },
|
||||
assignedPosition: { type: String, enum: POSITIONS, required: true },
|
||||
memberAcquisitionBidPointAmount: {
|
||||
type: Number,
|
||||
required: true,
|
||||
default: 0,
|
||||
},
|
||||
},
|
||||
{ _id: false },
|
||||
);
|
||||
|
||||
const RedisAuctionTeamSubSchema = new Schema<RedisAuctionTeam>(
|
||||
{
|
||||
teamIndex: { type: Number, required: true, min: 1 },
|
||||
name: { type: String, required: false },
|
||||
captainUserId: { type: Schema.Types.ObjectId, ref: 'user', required: true },
|
||||
remainingBidPointAmount: { type: Number, required: true, default: 0 },
|
||||
members: {
|
||||
type: [RedisAuctionTeamMemberSubSchema],
|
||||
required: true,
|
||||
default: [],
|
||||
},
|
||||
},
|
||||
{ _id: false },
|
||||
);
|
||||
|
||||
const AuctionBidSubSchema = new Schema<AuctionBid>(
|
||||
{
|
||||
teamIndex: { type: Number, required: true, min: 1 },
|
||||
@@ -1656,6 +1785,16 @@ const AuctionBidSubSchema = new Schema<AuctionBid>(
|
||||
{ _id: false },
|
||||
);
|
||||
|
||||
const AuctionBidHistoryEntrySubSchema = new Schema<AuctionBidHistoryEntry>(
|
||||
{
|
||||
bidderTeamIndex: { type: Number, required: true, min: 1 },
|
||||
bidPointAmount: { type: Number, required: true },
|
||||
bidderUserId: { type: Schema.Types.ObjectId, ref: 'user', required: true },
|
||||
bidPlacedAtTimestampMS: { type: Number, required: true },
|
||||
},
|
||||
{ _id: false },
|
||||
);
|
||||
|
||||
const AuctionChatMessageSubSchema = new Schema<AuctionChatMessage>(
|
||||
{
|
||||
id: { type: String, required: true },
|
||||
@@ -1681,6 +1820,40 @@ export const AuctionSchema = new Schema<Auction>(
|
||||
required: true,
|
||||
default: 'waiting',
|
||||
},
|
||||
revision: { type: Number, required: true, default: 0, min: 0 },
|
||||
auctionEngineVersion: {
|
||||
type: String,
|
||||
enum: AUCTION_ENGINE_VERSIONS,
|
||||
required: false,
|
||||
},
|
||||
auctionLifecycleId: { type: String, required: false },
|
||||
lastAppliedAuctionStateTransitionId: { type: String, required: false },
|
||||
currentAuctionHistorySequence: {
|
||||
type: Number,
|
||||
required: true,
|
||||
default: 0,
|
||||
min: 0,
|
||||
},
|
||||
startAuctionRequestId: { type: String, required: false },
|
||||
startAuctionStateTransitionId: { type: String, required: false },
|
||||
auctionStartedByUserId: {
|
||||
type: Schema.Types.ObjectId,
|
||||
ref: 'user',
|
||||
required: false,
|
||||
},
|
||||
matchSideEffectStatus: {
|
||||
type: String,
|
||||
enum: AUCTION_MATCH_SIDE_EFFECT_STATUSES,
|
||||
required: true,
|
||||
default: 'none',
|
||||
},
|
||||
matchSideEffectRevision: {
|
||||
type: Number,
|
||||
required: true,
|
||||
default: 0,
|
||||
min: 0,
|
||||
},
|
||||
matchSideEffectError: { type: String, required: false },
|
||||
teams: { type: [AuctionTeamSubSchema], required: true, default: [] },
|
||||
order: {
|
||||
type: [AuctionPlayerSnapshotSubSchema],
|
||||
@@ -1720,6 +1893,55 @@ export const AuctionSchema = new Schema<Auction>(
|
||||
},
|
||||
startedAt: { type: Date, required: false },
|
||||
completedAt: { type: Date, required: false },
|
||||
auctionTeams: {
|
||||
type: [RedisAuctionTeamSubSchema],
|
||||
required: false,
|
||||
default: undefined,
|
||||
},
|
||||
auctionPlayerOrder: {
|
||||
type: [AuctionPlayerSnapshotSubSchema],
|
||||
required: false,
|
||||
default: undefined,
|
||||
},
|
||||
passedAuctionPlayerQueue: {
|
||||
type: [AuctionPlayerSnapshotSubSchema],
|
||||
required: false,
|
||||
default: undefined,
|
||||
},
|
||||
currentAuctionRound: { type: Number, required: false, min: 1 },
|
||||
currentAuctionPlayerOrderIndex: { type: Number, required: false, min: 0 },
|
||||
auctionBidHistory: {
|
||||
type: [AuctionBidHistoryEntrySubSchema],
|
||||
required: false,
|
||||
default: undefined,
|
||||
},
|
||||
currentHighestBidPointAmount: { type: Number, required: false, min: 0 },
|
||||
currentHighestBidderTeamIndex: { type: Number, required: false, min: 1 },
|
||||
currentBidRoundDeadlineTimestampMS: {
|
||||
type: Number,
|
||||
required: false,
|
||||
min: 0,
|
||||
},
|
||||
maximumBidRoundDurationSeconds: {
|
||||
type: Number,
|
||||
required: false,
|
||||
min: 1,
|
||||
},
|
||||
bidRoundExtensionSeconds: { type: Number, required: false, min: 0 },
|
||||
initialTeamBidPointAmount: { type: Number, required: false, min: 0 },
|
||||
topTierAuctionPriorityThreshold: {
|
||||
type: String,
|
||||
enum: AUCTION_TOP_TIER_THRESHOLDS,
|
||||
required: false,
|
||||
},
|
||||
manualTopTierAuctionPriorityUserIds: {
|
||||
type: [Schema.Types.ObjectId],
|
||||
ref: 'user',
|
||||
required: false,
|
||||
default: undefined,
|
||||
},
|
||||
auctionStartedAtTimestampMS: { type: Number, required: false, min: 0 },
|
||||
auctionCompletedAtTimestampMS: { type: Number, required: false, min: 0 },
|
||||
},
|
||||
{
|
||||
timestamps: true,
|
||||
@@ -1727,7 +1949,7 @@ export const AuctionSchema = new Schema<Auction>(
|
||||
},
|
||||
);
|
||||
|
||||
const AuctionHistoryEntrySchema = new Schema<AuctionHistoryEntry>(
|
||||
export const AuctionHistoryEntrySchema = new Schema<AuctionHistoryEntry>(
|
||||
{
|
||||
auctionId: {
|
||||
type: Schema.Types.ObjectId,
|
||||
@@ -1744,19 +1966,16 @@ const AuctionHistoryEntrySchema = new Schema<AuctionHistoryEntry>(
|
||||
sequence: { type: Number, required: true, min: 1 },
|
||||
action: {
|
||||
type: String,
|
||||
enum: [
|
||||
'start',
|
||||
'open-bidding',
|
||||
'bid',
|
||||
'sold',
|
||||
'passed',
|
||||
'next',
|
||||
'complete',
|
||||
],
|
||||
enum: AUCTION_ACTION_TYPES,
|
||||
required: true,
|
||||
},
|
||||
auctionStateTransitionId: { type: String, required: false },
|
||||
auctionLifecycleId: { type: String, required: false },
|
||||
revision: { type: Number, required: false, min: 0 },
|
||||
actorUserId: { type: Schema.Types.ObjectId, ref: 'user', required: false },
|
||||
snapshotBefore: { type: Schema.Types.Mixed, required: true },
|
||||
historyMutation: { type: Schema.Types.Mixed, required: false },
|
||||
matchSideEffectMutation: { type: Schema.Types.Mixed, required: false },
|
||||
takenAt: { type: Date, required: true, default: Date.now },
|
||||
},
|
||||
{
|
||||
@@ -1766,6 +1985,74 @@ const AuctionHistoryEntrySchema = new Schema<AuctionHistoryEntry>(
|
||||
);
|
||||
|
||||
AuctionHistoryEntrySchema.index({ auctionId: 1, sequence: -1 });
|
||||
AuctionHistoryEntrySchema.index(
|
||||
{ auctionId: 1, sequence: 1 },
|
||||
{ unique: true },
|
||||
);
|
||||
AuctionHistoryEntrySchema.index(
|
||||
{ auctionId: 1, auctionStateTransitionId: 1 },
|
||||
{ sparse: true, unique: true },
|
||||
);
|
||||
|
||||
export const AuctionSideEffectApplicationSchema =
|
||||
new Schema<AuctionSideEffectApplication>(
|
||||
{
|
||||
auctionId: {
|
||||
type: Schema.Types.ObjectId,
|
||||
ref: 'auction',
|
||||
required: true,
|
||||
index: true,
|
||||
},
|
||||
matchId: {
|
||||
type: Schema.Types.ObjectId,
|
||||
ref: 'match',
|
||||
required: true,
|
||||
index: true,
|
||||
},
|
||||
auctionStateTransitionId: { type: String, required: true },
|
||||
auctionLifecycleId: { type: String, required: true },
|
||||
sideEffectType: { type: String, required: true },
|
||||
state: {
|
||||
type: String,
|
||||
enum: AUCTION_SIDE_EFFECT_APPLICATION_STATES,
|
||||
required: true,
|
||||
default: 'pending',
|
||||
},
|
||||
payload: { type: Schema.Types.Mixed, required: true, default: {} },
|
||||
payloadChecksum: { type: String, required: true },
|
||||
attempts: { type: Number, required: true, default: 0, min: 0 },
|
||||
processingStartedAtTimestampMS: {
|
||||
type: Number,
|
||||
required: false,
|
||||
min: 0,
|
||||
},
|
||||
processingLeaseExpiresAtTimestampMS: {
|
||||
type: Number,
|
||||
required: false,
|
||||
min: 0,
|
||||
},
|
||||
processingOwnerToken: { type: String, required: false },
|
||||
completedAtTimestampMS: { type: Number, required: false, min: 0 },
|
||||
lastError: { type: String, required: false },
|
||||
},
|
||||
{
|
||||
timestamps: true,
|
||||
collection: 'auctionSideEffectApplications',
|
||||
},
|
||||
);
|
||||
|
||||
AuctionSideEffectApplicationSchema.index(
|
||||
{
|
||||
auctionId: 1,
|
||||
auctionStateTransitionId: 1,
|
||||
sideEffectType: 1,
|
||||
},
|
||||
{ unique: true },
|
||||
);
|
||||
AuctionSideEffectApplicationSchema.index({
|
||||
state: 1,
|
||||
processingLeaseExpiresAtTimestampMS: 1,
|
||||
});
|
||||
|
||||
export const MatchStatsSchema: mongoose.Schema<MatchStats> = new Schema(
|
||||
{
|
||||
@@ -1840,6 +2127,11 @@ export const tournamentModelDefinitions: ModelDefinition[] = [
|
||||
schema: AuctionHistoryEntrySchema,
|
||||
schemaName: 'auctionHistory',
|
||||
},
|
||||
{
|
||||
name: 'auctionSideEffectApplication',
|
||||
schema: AuctionSideEffectApplicationSchema,
|
||||
schemaName: 'auctionSideEffectApplication',
|
||||
},
|
||||
{
|
||||
name: 'matchStats',
|
||||
schema: MatchStatsSchema,
|
||||
@@ -1860,5 +2152,6 @@ export interface TournamentModelTypes {
|
||||
match: Match;
|
||||
auction: Auction;
|
||||
auctionHistory: AuctionHistoryEntry;
|
||||
auctionSideEffectApplication: AuctionSideEffectApplication;
|
||||
matchStats: MatchStats;
|
||||
}
|
||||
@@ -1 +0,0 @@
|
||||
export const GARNET_CLIENT = Symbol('GARNET_CLIENT');
|
||||
@@ -1,57 +0,0 @@
|
||||
import { Logger, Module } from '@nestjs/common';
|
||||
import { createClient } from 'redis';
|
||||
import { SecretModule } from '../secret/secret.module';
|
||||
import { SecretService } from '../secret/secret.service';
|
||||
import { GARNET_CLIENT } from './garnet.constants';
|
||||
import { GarnetService, type GarnetRedisClient } from './garnet.service';
|
||||
|
||||
function normalizeGarnetUrl(endpoint: string): string {
|
||||
const trimmed = endpoint.trim();
|
||||
if (!trimmed) {
|
||||
throw new Error('GARNET_ENDPOINT cannot be empty');
|
||||
}
|
||||
if (/^redis(s)?:\/\//iu.test(trimmed)) {
|
||||
return trimmed;
|
||||
}
|
||||
return `redis://${trimmed}`;
|
||||
}
|
||||
|
||||
@Module({
|
||||
imports: [SecretModule],
|
||||
providers: [
|
||||
{
|
||||
provide: GARNET_CLIENT,
|
||||
inject: [SecretService],
|
||||
useFactory: async (
|
||||
secretService: SecretService,
|
||||
): Promise<GarnetRedisClient> => {
|
||||
await secretService.ready();
|
||||
|
||||
const [endpoint, username, password] = await Promise.all([
|
||||
secretService.get<string>('GARNET_ENDPOINT'),
|
||||
secretService.get<string>('GARNET_USERID'),
|
||||
secretService.get<string>('GARNET_PASSWORD'),
|
||||
]);
|
||||
|
||||
const logger = new Logger('GarnetClient');
|
||||
const client = createClient({
|
||||
url: normalizeGarnetUrl(endpoint),
|
||||
username,
|
||||
password,
|
||||
});
|
||||
|
||||
client.on('error', (error) => {
|
||||
logger.error(`Garnet client error: ${error?.message ?? error}`);
|
||||
});
|
||||
|
||||
await client.connect();
|
||||
logger.log('Garnet connected');
|
||||
|
||||
return client;
|
||||
},
|
||||
},
|
||||
GarnetService,
|
||||
],
|
||||
exports: [GarnetService, GARNET_CLIENT],
|
||||
})
|
||||
export class GarnetModule {}
|
||||
@@ -2,12 +2,12 @@ import { Module, Global } from '@nestjs/common';
|
||||
import { SecretModule } from './secret/secret.module';
|
||||
import { NatsModule } from './nats/nats.module';
|
||||
import { DatabaseModule } from './database/database.module';
|
||||
import { GarnetModule } from './garnet/garnet.module';
|
||||
import { RedisModule } from './redis/redis.module';
|
||||
|
||||
@Global()
|
||||
@Module({
|
||||
imports: [SecretModule, NatsModule, DatabaseModule, GarnetModule],
|
||||
imports: [SecretModule, NatsModule, DatabaseModule, RedisModule],
|
||||
providers: [],
|
||||
exports: [SecretModule, NatsModule, DatabaseModule, GarnetModule],
|
||||
exports: [SecretModule, NatsModule, DatabaseModule, RedisModule],
|
||||
})
|
||||
export class GlobalModule {}
|
||||
@@ -0,0 +1,59 @@
|
||||
import {
|
||||
assertDistinctRedisInstances,
|
||||
normalizeRedisUrl,
|
||||
resolveRedisClientOptions,
|
||||
} from './redis.config';
|
||||
|
||||
describe('redis.config', () => {
|
||||
it('normalizes host:port endpoints into redis URLs', () => {
|
||||
expect(normalizeRedisUrl('127.0.0.1:6379', 'REDIS_ENDPOINT')).toBe(
|
||||
'redis://127.0.0.1:6379',
|
||||
);
|
||||
expect(normalizeRedisUrl('rediss://cache.example.com:6380')).toBe(
|
||||
'rediss://cache.example.com:6380',
|
||||
);
|
||||
});
|
||||
|
||||
it('throws a clear error for empty endpoints', () => {
|
||||
expect(() => normalizeRedisUrl(' ', 'REDIS_ENDPOINT')).toThrow(
|
||||
'REDIS_ENDPOINT cannot be empty',
|
||||
);
|
||||
});
|
||||
|
||||
it('keeps credentials optional when resolving client options', () => {
|
||||
expect(
|
||||
resolveRedisClientOptions({
|
||||
endpoint: 'redis.internal:6379',
|
||||
username: '',
|
||||
password: undefined,
|
||||
endpointEnvName: 'REDIS_ENDPOINT',
|
||||
}),
|
||||
).toEqual({
|
||||
url: 'redis://redis.internal:6379',
|
||||
});
|
||||
});
|
||||
|
||||
it('rejects auction Redis when it points at the same physical Redis instance', () => {
|
||||
expect(() =>
|
||||
assertDistinctRedisInstances(
|
||||
'redis://cache.internal:6379/0',
|
||||
'redis://cache.internal:6379/1',
|
||||
),
|
||||
).toThrow('AUCTION_REDIS must use a separate Redis instance');
|
||||
});
|
||||
|
||||
it('allows different Redis hosts or ports', () => {
|
||||
expect(() =>
|
||||
assertDistinctRedisInstances(
|
||||
'redis://cache.internal:6379',
|
||||
'redis://auction.internal:6379',
|
||||
),
|
||||
).not.toThrow();
|
||||
expect(() =>
|
||||
assertDistinctRedisInstances(
|
||||
'redis://cache.internal:6379',
|
||||
'redis://cache.internal:6380',
|
||||
),
|
||||
).not.toThrow();
|
||||
});
|
||||
});
|
||||
@@ -0,0 +1,77 @@
|
||||
export interface RedisClientOptions {
|
||||
password?: string;
|
||||
url: string;
|
||||
username?: string;
|
||||
}
|
||||
|
||||
export interface ResolveRedisClientOptionsParams {
|
||||
endpoint?: string;
|
||||
endpointEnvName?: string;
|
||||
password?: string;
|
||||
url?: string;
|
||||
urlEnvName?: string;
|
||||
username?: string;
|
||||
}
|
||||
|
||||
function trimOptional(value: string | undefined): string | undefined {
|
||||
const trimmed = value?.trim();
|
||||
return trimmed ? trimmed : undefined;
|
||||
}
|
||||
|
||||
export function normalizeRedisUrl(
|
||||
endpointOrUrl: string,
|
||||
envName = 'REDIS_URL',
|
||||
): string {
|
||||
const trimmed = endpointOrUrl.trim();
|
||||
if (!trimmed) {
|
||||
throw new Error(`${envName} cannot be empty`);
|
||||
}
|
||||
if (/^redis(s)?:\/\//iu.test(trimmed)) {
|
||||
return trimmed;
|
||||
}
|
||||
return `redis://${trimmed}`;
|
||||
}
|
||||
|
||||
export function resolveRedisClientOptions(
|
||||
params: ResolveRedisClientOptionsParams,
|
||||
): RedisClientOptions {
|
||||
const url = trimOptional(params.url);
|
||||
const endpoint = trimOptional(params.endpoint);
|
||||
const selectedEndpoint = url ?? endpoint;
|
||||
const selectedEnvName = url
|
||||
? (params.urlEnvName ?? 'REDIS_URL')
|
||||
: (params.endpointEnvName ?? 'REDIS_ENDPOINT');
|
||||
|
||||
if (!selectedEndpoint) {
|
||||
throw new Error(`${selectedEnvName} cannot be empty`);
|
||||
}
|
||||
|
||||
const options: RedisClientOptions = {
|
||||
url: normalizeRedisUrl(selectedEndpoint, selectedEnvName),
|
||||
};
|
||||
const username = trimOptional(params.username);
|
||||
const password = trimOptional(params.password);
|
||||
if (username) options.username = username;
|
||||
if (password) options.password = password;
|
||||
return options;
|
||||
}
|
||||
|
||||
function parseRedisInstanceIdentity(redisUrl: string): string {
|
||||
const url = new URL(normalizeRedisUrl(redisUrl));
|
||||
const hostname = url.hostname.toLowerCase();
|
||||
const port = url.port || '6379';
|
||||
return `${hostname}:${port}`;
|
||||
}
|
||||
|
||||
export function assertDistinctRedisInstances(
|
||||
redisUrl: string,
|
||||
auctionRedisUrl: string,
|
||||
): void {
|
||||
const redisIdentity = parseRedisInstanceIdentity(redisUrl);
|
||||
const auctionRedisIdentity = parseRedisInstanceIdentity(auctionRedisUrl);
|
||||
if (redisIdentity === auctionRedisIdentity) {
|
||||
throw new Error(
|
||||
'AUCTION_REDIS must use a separate Redis instance from REDIS because auction Redis is a live state source of truth.',
|
||||
);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1 @@
|
||||
export const REDIS_CLIENT = Symbol('REDIS_CLIENT');
|
||||
@@ -0,0 +1,68 @@
|
||||
import { Logger, Module } from '@nestjs/common';
|
||||
import { createClient } from 'redis';
|
||||
import { SecretModule } from '../secret/secret.module';
|
||||
import { SecretService } from '../secret/secret.service';
|
||||
import { REDIS_CLIENT } from './redis.constants';
|
||||
import { resolveRedisClientOptions } from './redis.config';
|
||||
import { RedisService, type RedisClient } from './redis.service';
|
||||
|
||||
async function readFirstSecret(
|
||||
secretService: SecretService,
|
||||
keys: string[],
|
||||
): Promise<string | undefined> {
|
||||
for (const key of keys) {
|
||||
const value = await secretService.get<string>(key, '');
|
||||
const trimmed = value.trim();
|
||||
if (trimmed) return trimmed;
|
||||
}
|
||||
return undefined;
|
||||
}
|
||||
|
||||
@Module({
|
||||
imports: [SecretModule],
|
||||
providers: [
|
||||
{
|
||||
provide: REDIS_CLIENT,
|
||||
inject: [SecretService],
|
||||
useFactory: async (secretService: SecretService): Promise<RedisClient> => {
|
||||
await secretService.ready();
|
||||
|
||||
const [url, endpoint, username, password] = await Promise.all([
|
||||
readFirstSecret(secretService, ['REDIS_URL', 'GARNET_URL']),
|
||||
readFirstSecret(secretService, ['REDIS_ENDPOINT', 'GARNET_ENDPOINT']),
|
||||
readFirstSecret(secretService, [
|
||||
'REDIS_USERNAME',
|
||||
'REDIS_USERID',
|
||||
'GARNET_USERNAME',
|
||||
'GARNET_USERID',
|
||||
]),
|
||||
readFirstSecret(secretService, ['REDIS_PASSWORD', 'GARNET_PASSWORD']),
|
||||
]);
|
||||
|
||||
const logger = new Logger('RedisClient');
|
||||
const client = createClient(
|
||||
resolveRedisClientOptions({
|
||||
endpoint,
|
||||
endpointEnvName: 'REDIS_ENDPOINT',
|
||||
password,
|
||||
url,
|
||||
urlEnvName: 'REDIS_URL',
|
||||
username,
|
||||
}),
|
||||
);
|
||||
|
||||
client.on('error', (error) => {
|
||||
logger.error(`Redis client error: ${error?.message ?? error}`);
|
||||
});
|
||||
|
||||
await client.connect();
|
||||
logger.log('Redis connected');
|
||||
|
||||
return client;
|
||||
},
|
||||
},
|
||||
RedisService,
|
||||
],
|
||||
exports: [RedisService, REDIS_CLIENT],
|
||||
})
|
||||
export class RedisModule {}
|
||||
+6
-8
@@ -1,8 +1,8 @@
|
||||
import { Inject, Injectable, Logger, OnModuleDestroy } from '@nestjs/common';
|
||||
import type { createClient } from 'redis';
|
||||
import { GARNET_CLIENT } from './garnet.constants';
|
||||
import { REDIS_CLIENT } from './redis.constants';
|
||||
|
||||
export type GarnetRedisClient = ReturnType<typeof createClient>;
|
||||
export type RedisClient = ReturnType<typeof createClient>;
|
||||
|
||||
export interface StreamEntry {
|
||||
deliveriesCounter?: number;
|
||||
@@ -48,12 +48,10 @@ function normalizeStreamFields(fields: unknown): Record<string, string> {
|
||||
}
|
||||
|
||||
@Injectable()
|
||||
export class GarnetService implements OnModuleDestroy {
|
||||
private readonly logger = new Logger(GarnetService.name);
|
||||
export class RedisService implements OnModuleDestroy {
|
||||
private readonly logger = new Logger(RedisService.name);
|
||||
|
||||
constructor(
|
||||
@Inject(GARNET_CLIENT) private readonly client: GarnetRedisClient,
|
||||
) {}
|
||||
constructor(@Inject(REDIS_CLIENT) private readonly client: RedisClient) {}
|
||||
|
||||
async onModuleDestroy() {
|
||||
try {
|
||||
@@ -63,7 +61,7 @@ export class GarnetService implements OnModuleDestroy {
|
||||
}
|
||||
}
|
||||
|
||||
public raw(): GarnetRedisClient {
|
||||
public raw(): RedisClient {
|
||||
return this.client;
|
||||
}
|
||||
|
||||
Reference in new issue
Block a user