Compare commits
10
Commits
33abda997f
...
release/66
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
e63fba0286 | ||
|
|
245a096606 | ||
|
|
6e0afe84cd | ||
|
|
6accb4b166 | ||
|
|
f3d05004ce | ||
|
|
eadae789c8 | ||
|
|
d0d578b2b0 | ||
|
|
4ab3c3a135 | ||
|
|
251eb6deb9 | ||
|
|
b7886589cb |
No files matched your search
@@ -0,0 +1 @@
|
|||||||
|
66
|
||||||
+1
-1
@@ -35,7 +35,7 @@ FROM gcr.io/distroless/nodejs24-debian13:nonroot AS runtime
|
|||||||
|
|
||||||
WORKDIR /app
|
WORKDIR /app
|
||||||
|
|
||||||
ENV NODE_ENV=prod
|
# ENV NODE_ENV=prod
|
||||||
|
|
||||||
COPY --from=prod-deps --chown=nonroot:nonroot /app/package.json ./package.json
|
COPY --from=prod-deps --chown=nonroot:nonroot /app/package.json ./package.json
|
||||||
COPY --from=prod-deps --chown=nonroot:nonroot /app/node_modules ./node_modules
|
COPY --from=prod-deps --chown=nonroot:nonroot /app/node_modules ./node_modules
|
||||||
|
|||||||
@@ -86,7 +86,23 @@ pnpm dev:web
|
|||||||
|
|
||||||
```bash
|
```bash
|
||||||
pnpm --filter api test
|
pnpm --filter api test
|
||||||
pnpm --filter web build:test
|
pnpm --filter web build:staging
|
||||||
```
|
```
|
||||||
|
|
||||||
현재 정리된 기준으로 API 테스트와 웹 테스트 빌드를 통과합니다.
|
현재 정리된 기준으로 API 테스트와 웹 테스트 빌드를 통과합니다.
|
||||||
|
|
||||||
|
## Deployment branches
|
||||||
|
|
||||||
|
`main`은 항상 배포 가능한 trunk로 유지하고 dev 배포 기준으로 사용합니다.
|
||||||
|
빌드 번호는 `BUILD_NUMBER` 파일에서 1씩 증가시키며, 현재 시작 번호는 `66`입니다.
|
||||||
|
staging 검증이 필요할 때는 `release/66` 같은 임시 브랜치를 만들고,
|
||||||
|
검증이 끝나면 삭제합니다.
|
||||||
|
|
||||||
|
```bash
|
||||||
|
git push origin main:release/66
|
||||||
|
pnpm deploy:web:staging
|
||||||
|
```
|
||||||
|
|
||||||
|
staging Pages 배포 스크립트는 `CF_PAGES_STAGING_BRANCH`가 있으면 그 값을
|
||||||
|
사용하고, 없으면 최신 `release/*` 브랜치를 자동으로 선택합니다. 최신 기준은
|
||||||
|
브랜치가 가리키는 커밋 시각이며, 동률이면 브랜치명 내림차순입니다.
|
||||||
@@ -11,10 +11,10 @@
|
|||||||
"seed:dev": "ts-node src/seed.ts",
|
"seed:dev": "ts-node src/seed.ts",
|
||||||
"seed:dakao-participant": "ts-node src/seed-dakao-participant.ts",
|
"seed:dakao-participant": "ts-node src/seed-dakao-participant.ts",
|
||||||
"start:dev": "node -e \"process.env.NODE_ENV='dev'; require('./dist/main.js')\"",
|
"start:dev": "node -e \"process.env.NODE_ENV='dev'; require('./dist/main.js')\"",
|
||||||
"start:test": "node -e \"process.env.NODE_ENV='test'; require('./dist/main.js')\"",
|
"start:staging": "node -e \"process.env.NODE_ENV='staging'; require('./dist/main.js')\"",
|
||||||
"start:prod": "node -e \"process.env.NODE_ENV='prod'; require('./dist/main.js')\"",
|
"start:prod": "node -e \"process.env.NODE_ENV='prod'; require('./dist/main.js')\"",
|
||||||
"pm2:dev": "pm2 start npm --name lolympus-tournament-support --update-env -- run start:dev",
|
"pm2:dev": "pm2 start npm --name lolympus-tournament-support --update-env -- run start:dev",
|
||||||
"pm2:test": "pm2 start npm --name lolympus-tournament-support --update-env -- run start:test",
|
"pm2:staging": "pm2 start npm --name lolympus-tournament-support --update-env -- run start:staging",
|
||||||
"pm2:prod": "pm2 start npm --name lolympus-tournament-support --update-env -- run start:prod",
|
"pm2:prod": "pm2 start npm --name lolympus-tournament-support --update-env -- run start:prod",
|
||||||
"start:debug": "nest start --debug --watch",
|
"start:debug": "nest start --debug --watch",
|
||||||
"lint": "eslint \"{src,apps,libs,test}/**/*.ts\" --fix",
|
"lint": "eslint \"{src,apps,libs,test}/**/*.ts\" --fix",
|
||||||
|
|||||||
@@ -0,0 +1,67 @@
|
|||||||
|
import {
|
||||||
|
createCorsOrigin,
|
||||||
|
isAllowedCorsOrigin,
|
||||||
|
resolveWebOrigins,
|
||||||
|
} from './cors-origin';
|
||||||
|
|
||||||
|
describe('CORS origin resolution', () => {
|
||||||
|
it('allows Cloudflare Pages preview origins in dev and staging', () => {
|
||||||
|
const origin = 'https://807e0ea6.lolympus.pages.dev';
|
||||||
|
|
||||||
|
expect(isAllowedCorsOrigin(origin, 'dev')).toBe(true);
|
||||||
|
expect(isAllowedCorsOrigin(origin, 'staging')).toBe(true);
|
||||||
|
});
|
||||||
|
|
||||||
|
it('does not allow Cloudflare Pages preview origins in prod', () => {
|
||||||
|
expect(
|
||||||
|
isAllowedCorsOrigin('https://807e0ea6.lolympus.pages.dev', 'prod'),
|
||||||
|
).toBe(false);
|
||||||
|
});
|
||||||
|
|
||||||
|
it('rejects similar but unsafe Pages origins', () => {
|
||||||
|
expect(
|
||||||
|
isAllowedCorsOrigin('http://807e0ea6.lolympus.pages.dev', 'staging'),
|
||||||
|
).toBe(false);
|
||||||
|
expect(isAllowedCorsOrigin('https://lolympus.pages.dev', 'staging')).toBe(
|
||||||
|
false,
|
||||||
|
);
|
||||||
|
expect(
|
||||||
|
isAllowedCorsOrigin('https://a.b.lolympus.pages.dev', 'staging'),
|
||||||
|
).toBe(false);
|
||||||
|
expect(
|
||||||
|
isAllowedCorsOrigin(
|
||||||
|
'https://807e0ea6.lolympus.pages.dev.evil.com',
|
||||||
|
'staging',
|
||||||
|
),
|
||||||
|
).toBe(false);
|
||||||
|
});
|
||||||
|
|
||||||
|
it('keeps configured web origins allowed', () => {
|
||||||
|
expect(
|
||||||
|
isAllowedCorsOrigin('https://example.com', 'prod', [
|
||||||
|
'https://example.com',
|
||||||
|
]),
|
||||||
|
).toBe(true);
|
||||||
|
});
|
||||||
|
|
||||||
|
it('falls back to default web origins when WEB_ORIGIN is empty', () => {
|
||||||
|
expect(resolveWebOrigins('')).toContain('http://localhost:5173');
|
||||||
|
});
|
||||||
|
|
||||||
|
it('creates a Nest CORS callback from the same rules', (done) => {
|
||||||
|
const corsOrigin = createCorsOrigin('staging');
|
||||||
|
|
||||||
|
expect(corsOrigin).not.toBe(true);
|
||||||
|
if (corsOrigin === true) return;
|
||||||
|
|
||||||
|
corsOrigin('https://807e0ea6.lolympus.pages.dev', (error, allow) => {
|
||||||
|
expect(error).toBeNull();
|
||||||
|
expect(allow).toBe(true);
|
||||||
|
done();
|
||||||
|
});
|
||||||
|
});
|
||||||
|
|
||||||
|
it('keeps dev CORS permissive for local development', () => {
|
||||||
|
expect(createCorsOrigin('dev')).toBe(true);
|
||||||
|
});
|
||||||
|
});
|
||||||
@@ -0,0 +1,101 @@
|
|||||||
|
import type { CustomOrigin } from '@nestjs/common/interfaces/external/cors-options.interface';
|
||||||
|
|
||||||
|
const DEFAULT_WEB_ORIGINS = [
|
||||||
|
'http://localhost:5173',
|
||||||
|
'http://126.dahyeon.us:5173',
|
||||||
|
'https://lolympus.dahyeon.us',
|
||||||
|
];
|
||||||
|
|
||||||
|
const PAGES_DEV_ORIGIN_HOST_PATTERN =
|
||||||
|
/^[a-z0-9](?:[a-z0-9-]{0,61}[a-z0-9])?\.lolympus\.pages\.dev$/i;
|
||||||
|
|
||||||
|
const PAGES_DEV_CORS_ENVIRONMENTS = new Set(['dev', 'staging']);
|
||||||
|
|
||||||
|
function parseOrigin(origin: string, allowPath: boolean): URL | null {
|
||||||
|
const trimmedOrigin = origin.trim();
|
||||||
|
if (trimmedOrigin.length === 0) return null;
|
||||||
|
|
||||||
|
try {
|
||||||
|
const url = new URL(trimmedOrigin);
|
||||||
|
if (
|
||||||
|
!allowPath &&
|
||||||
|
(url.pathname !== '/' || url.search !== '' || url.hash !== '')
|
||||||
|
) {
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
|
||||||
|
return url;
|
||||||
|
} catch {
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
function normalizeOrigin(origin: string, allowPath = false): string | null {
|
||||||
|
const url = parseOrigin(origin, allowPath);
|
||||||
|
if (!url) return null;
|
||||||
|
|
||||||
|
return `${url.protocol}//${url.host}`;
|
||||||
|
}
|
||||||
|
|
||||||
|
export function resolveWebOrigins(
|
||||||
|
webOrigin = process.env.WEB_ORIGIN,
|
||||||
|
): string[] {
|
||||||
|
const configuredOrigins = webOrigin
|
||||||
|
?.split(',')
|
||||||
|
.map((origin) => normalizeOrigin(origin, true))
|
||||||
|
.filter((origin): origin is string => origin !== null);
|
||||||
|
|
||||||
|
if (configuredOrigins && configuredOrigins.length > 0) {
|
||||||
|
return configuredOrigins;
|
||||||
|
}
|
||||||
|
|
||||||
|
return [...DEFAULT_WEB_ORIGINS];
|
||||||
|
}
|
||||||
|
|
||||||
|
export function isLolympusPagesDevOrigin(origin: string): boolean {
|
||||||
|
const url = parseOrigin(origin, false);
|
||||||
|
if (!url) return false;
|
||||||
|
|
||||||
|
return (
|
||||||
|
url.protocol === 'https:' &&
|
||||||
|
url.port === '' &&
|
||||||
|
PAGES_DEV_ORIGIN_HOST_PATTERN.test(url.hostname)
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
|
export function isAllowedCorsOrigin(
|
||||||
|
requestOrigin: string | undefined,
|
||||||
|
nodeEnv = process.env.NODE_ENV,
|
||||||
|
webOrigins = resolveWebOrigins(),
|
||||||
|
): boolean {
|
||||||
|
if (!requestOrigin) return false;
|
||||||
|
|
||||||
|
const normalizedRequestOrigin = normalizeOrigin(requestOrigin);
|
||||||
|
if (!normalizedRequestOrigin) return false;
|
||||||
|
|
||||||
|
const normalizedWebOrigins = webOrigins
|
||||||
|
.map((origin) => normalizeOrigin(origin, true))
|
||||||
|
.filter((origin): origin is string => origin !== null);
|
||||||
|
|
||||||
|
if (normalizedWebOrigins.includes(normalizedRequestOrigin)) {
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
|
||||||
|
return (
|
||||||
|
PAGES_DEV_CORS_ENVIRONMENTS.has(nodeEnv ?? '') &&
|
||||||
|
isLolympusPagesDevOrigin(requestOrigin)
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
|
export function createCorsOrigin(
|
||||||
|
nodeEnv = process.env.NODE_ENV,
|
||||||
|
webOrigin = process.env.WEB_ORIGIN,
|
||||||
|
): true | CustomOrigin {
|
||||||
|
if (nodeEnv === 'dev') return true;
|
||||||
|
|
||||||
|
const webOrigins = resolveWebOrigins(webOrigin);
|
||||||
|
|
||||||
|
return (requestOrigin, callback) => {
|
||||||
|
callback(null, isAllowedCorsOrigin(requestOrigin, nodeEnv, webOrigins));
|
||||||
|
};
|
||||||
|
}
|
||||||
+5
-10
@@ -1,23 +1,18 @@
|
|||||||
import { ValidationPipe } from '@nestjs/common';
|
import { ValidationPipe } from '@nestjs/common';
|
||||||
import { NestFactory } from '@nestjs/core';
|
import { NestFactory } from '@nestjs/core';
|
||||||
import { AppModule } from './app.module';
|
import { AppModule } from './app.module';
|
||||||
|
import { createCorsOrigin } from './cors-origin';
|
||||||
|
|
||||||
async function bootstrap() {
|
async function bootstrap() {
|
||||||
const app = await NestFactory.create(AppModule, {
|
const app = await NestFactory.create(AppModule, {
|
||||||
rawBody: true,
|
rawBody: true,
|
||||||
});
|
});
|
||||||
|
|
||||||
const isDev = process.env.NODE_ENV === 'dev';
|
const nodeEnv = process.env.NODE_ENV;
|
||||||
const webOrigins = process.env.WEB_ORIGIN?.split(',').map((origin) =>
|
const isDev = nodeEnv === 'dev';
|
||||||
origin.trim(),
|
|
||||||
) ?? [
|
|
||||||
'http://localhost:5173',
|
|
||||||
'http://126.dahyeon.us:5173',
|
|
||||||
'https://lolympus.dahyeon.us',
|
|
||||||
];
|
|
||||||
|
|
||||||
app.enableCors({
|
app.enableCors({
|
||||||
origin: isDev ? true : webOrigins,
|
origin: createCorsOrigin(nodeEnv),
|
||||||
credentials: true,
|
credentials: true,
|
||||||
allowedHeaders: ['Authorization', 'Content-Type', 'X-Auth-User-Id'],
|
allowedHeaders: ['Authorization', 'Content-Type', 'X-Auth-User-Id'],
|
||||||
});
|
});
|
||||||
@@ -31,4 +26,4 @@ async function bootstrap() {
|
|||||||
);
|
);
|
||||||
await app.listen(isDev ? 3000 : 10712);
|
await app.listen(isDev ? 3000 : 10712);
|
||||||
}
|
}
|
||||||
bootstrap();
|
void bootstrap();
|
||||||
@@ -0,0 +1,123 @@
|
|||||||
|
import { Injectable } from '@nestjs/common';
|
||||||
|
import { ConnectionService } from '../global/database/connection.service';
|
||||||
|
|
||||||
|
const DEFAULT_LEASE_MS = 5 * 60 * 1000;
|
||||||
|
|
||||||
|
export type DakaoInboxClaimResult =
|
||||||
|
| { status: 'claimed' }
|
||||||
|
| { status: 'duplicate' }
|
||||||
|
| { status: 'inflight' };
|
||||||
|
|
||||||
|
@Injectable()
|
||||||
|
export class DakaoInboxService {
|
||||||
|
constructor(private readonly connectionService: ConnectionService) {}
|
||||||
|
|
||||||
|
public async claim(params: {
|
||||||
|
consumer: string;
|
||||||
|
projectId: string;
|
||||||
|
chatLogLogId: string;
|
||||||
|
leaseMs?: number;
|
||||||
|
}): Promise<DakaoInboxClaimResult> {
|
||||||
|
const Model = await this.connectionService.getModel('dakaoInboundMessage');
|
||||||
|
const now = new Date();
|
||||||
|
const processingExpiresAt = new Date(
|
||||||
|
now.getTime() + (params.leaseMs ?? DEFAULT_LEASE_MS),
|
||||||
|
);
|
||||||
|
|
||||||
|
try {
|
||||||
|
await Model.create({
|
||||||
|
consumer: params.consumer,
|
||||||
|
projectId: params.projectId,
|
||||||
|
chatLogLogId: params.chatLogLogId,
|
||||||
|
state: 'processing',
|
||||||
|
attempts: 1,
|
||||||
|
processingExpiresAt,
|
||||||
|
});
|
||||||
|
return { status: 'claimed' };
|
||||||
|
} catch (err) {
|
||||||
|
if ((err as { code?: number })?.code !== 11000) throw err;
|
||||||
|
}
|
||||||
|
|
||||||
|
const existing = await Model.findOne({
|
||||||
|
consumer: params.consumer,
|
||||||
|
projectId: params.projectId,
|
||||||
|
chatLogLogId: params.chatLogLogId,
|
||||||
|
})
|
||||||
|
.lean()
|
||||||
|
.exec();
|
||||||
|
if (!existing) return { status: 'inflight' };
|
||||||
|
if (existing.state === 'processed') return { status: 'duplicate' };
|
||||||
|
if (
|
||||||
|
existing.state === 'processing' &&
|
||||||
|
existing.processingExpiresAt &&
|
||||||
|
existing.processingExpiresAt > now
|
||||||
|
) {
|
||||||
|
return { status: 'inflight' };
|
||||||
|
}
|
||||||
|
|
||||||
|
const updated = await Model.updateOne(
|
||||||
|
{
|
||||||
|
_id: existing._id,
|
||||||
|
$or: [
|
||||||
|
{ state: 'failed' },
|
||||||
|
{ state: 'processing', processingExpiresAt: { $lte: now } },
|
||||||
|
],
|
||||||
|
},
|
||||||
|
{
|
||||||
|
$set: {
|
||||||
|
state: 'processing',
|
||||||
|
processingExpiresAt,
|
||||||
|
},
|
||||||
|
$inc: { attempts: 1 },
|
||||||
|
},
|
||||||
|
).exec();
|
||||||
|
return updated.modifiedCount > 0
|
||||||
|
? { status: 'claimed' }
|
||||||
|
: { status: 'inflight' };
|
||||||
|
}
|
||||||
|
|
||||||
|
public async markProcessed(params: {
|
||||||
|
consumer: string;
|
||||||
|
projectId: string;
|
||||||
|
chatLogLogId: string;
|
||||||
|
}): Promise<void> {
|
||||||
|
const Model = await this.connectionService.getModel('dakaoInboundMessage');
|
||||||
|
await Model.updateOne(
|
||||||
|
{
|
||||||
|
consumer: params.consumer,
|
||||||
|
projectId: params.projectId,
|
||||||
|
chatLogLogId: params.chatLogLogId,
|
||||||
|
},
|
||||||
|
{
|
||||||
|
$set: {
|
||||||
|
state: 'processed',
|
||||||
|
processedAt: new Date(),
|
||||||
|
},
|
||||||
|
$unset: { processingExpiresAt: '', lastError: '' },
|
||||||
|
},
|
||||||
|
).exec();
|
||||||
|
}
|
||||||
|
|
||||||
|
public async markFailed(params: {
|
||||||
|
consumer: string;
|
||||||
|
projectId: string;
|
||||||
|
chatLogLogId: string;
|
||||||
|
error: unknown;
|
||||||
|
}): Promise<void> {
|
||||||
|
const Model = await this.connectionService.getModel('dakaoInboundMessage');
|
||||||
|
await Model.updateOne(
|
||||||
|
{
|
||||||
|
consumer: params.consumer,
|
||||||
|
projectId: params.projectId,
|
||||||
|
chatLogLogId: params.chatLogLogId,
|
||||||
|
},
|
||||||
|
{
|
||||||
|
$set: {
|
||||||
|
state: 'failed',
|
||||||
|
lastError: (params.error as Error)?.message ?? String(params.error),
|
||||||
|
},
|
||||||
|
$unset: { processingExpiresAt: '' },
|
||||||
|
},
|
||||||
|
).exec();
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -0,0 +1,64 @@
|
|||||||
|
import { Injectable } from '@nestjs/common';
|
||||||
|
import { NatsService } from '../global/nats/nats.service';
|
||||||
|
import { resolveDakaoNatsNames } from './dakao.nats-names';
|
||||||
|
|
||||||
|
@Injectable()
|
||||||
|
export class DakaoJetStreamResourceService {
|
||||||
|
private ensurePromise: Promise<void> | null = null;
|
||||||
|
|
||||||
|
constructor(private readonly natsService: NatsService) {}
|
||||||
|
|
||||||
|
public async ensure(): Promise<void> {
|
||||||
|
this.ensurePromise ??= this.ensureInternal();
|
||||||
|
await this.ensurePromise;
|
||||||
|
}
|
||||||
|
|
||||||
|
private async ensureInternal(): Promise<void> {
|
||||||
|
const names = resolveDakaoNatsNames();
|
||||||
|
await this.natsService.ensureJetStreamStream({
|
||||||
|
name: names.streams.botCommand,
|
||||||
|
subjects: [names.subjects.botCommandSubmitted],
|
||||||
|
retention: 'workqueue',
|
||||||
|
replicas: 1,
|
||||||
|
});
|
||||||
|
await this.natsService.ensureJetStreamConsumer({
|
||||||
|
stream: names.streams.botCommand,
|
||||||
|
durable: names.consumers.botCommand,
|
||||||
|
filterSubject: names.subjects.botCommandSubmitted,
|
||||||
|
description: 'LolyMpus Dakao bot command handler',
|
||||||
|
replicas: 1,
|
||||||
|
});
|
||||||
|
|
||||||
|
await this.natsService.ensureJetStreamStream({
|
||||||
|
name: names.streams.sendRequest,
|
||||||
|
subjects: [names.subjects.chatSendMessage],
|
||||||
|
retention: 'workqueue',
|
||||||
|
replicas: 1,
|
||||||
|
});
|
||||||
|
await this.natsService.ensureJetStreamConsumer({
|
||||||
|
stream: names.streams.sendRequest,
|
||||||
|
durable: names.consumers.sendRequest,
|
||||||
|
filterSubject: names.subjects.chatSendMessage,
|
||||||
|
description: 'Dakao send-message worker',
|
||||||
|
replicas: 1,
|
||||||
|
});
|
||||||
|
|
||||||
|
await this.natsService.ensureJetStreamStream({
|
||||||
|
name: names.streams.sendResult,
|
||||||
|
subjects: [
|
||||||
|
names.subjects.chatSendMessageSucceeded,
|
||||||
|
names.subjects.chatSendMessageFailed,
|
||||||
|
],
|
||||||
|
retention: 'limits',
|
||||||
|
maxAgeDays: 30,
|
||||||
|
replicas: 1,
|
||||||
|
});
|
||||||
|
await this.natsService.ensureJetStreamConsumer({
|
||||||
|
stream: names.streams.sendResult,
|
||||||
|
durable: names.consumers.sendResult,
|
||||||
|
filterSubject: `${names.env}.lolympus.dakao.chat.send-message.*.v1`,
|
||||||
|
description: 'LolyMpus Dakao send-message result handler',
|
||||||
|
replicas: 1,
|
||||||
|
});
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -0,0 +1,47 @@
|
|||||||
|
import {
|
||||||
|
Injectable,
|
||||||
|
Logger,
|
||||||
|
OnModuleDestroy,
|
||||||
|
OnModuleInit,
|
||||||
|
} from '@nestjs/common';
|
||||||
|
import { DakaoOutboxService } from './dakao-outbox.service';
|
||||||
|
|
||||||
|
const SWEEP_INTERVAL_MS = 60 * 1000;
|
||||||
|
const STALE_OUTBOX_MS = 5 * 60 * 1000;
|
||||||
|
|
||||||
|
@Injectable()
|
||||||
|
export class DakaoOutboxSweeperService
|
||||||
|
implements OnModuleInit, OnModuleDestroy
|
||||||
|
{
|
||||||
|
private readonly logger = new Logger(DakaoOutboxSweeperService.name);
|
||||||
|
private timer: NodeJS.Timeout | undefined;
|
||||||
|
|
||||||
|
constructor(private readonly outboxService: DakaoOutboxService) {}
|
||||||
|
|
||||||
|
onModuleInit() {
|
||||||
|
this.timer = setInterval(() => {
|
||||||
|
void this.sweep();
|
||||||
|
}, SWEEP_INTERVAL_MS);
|
||||||
|
this.timer.unref();
|
||||||
|
}
|
||||||
|
|
||||||
|
onModuleDestroy() {
|
||||||
|
if (this.timer) clearInterval(this.timer);
|
||||||
|
}
|
||||||
|
|
||||||
|
private async sweep(): Promise<void> {
|
||||||
|
try {
|
||||||
|
const swept = await this.outboxService.sweepStale({
|
||||||
|
staleMs: STALE_OUTBOX_MS,
|
||||||
|
limit: 20,
|
||||||
|
});
|
||||||
|
if (swept > 0) {
|
||||||
|
this.logger.warn(`Re-published ${swept} stale Dakao outbox messages.`);
|
||||||
|
}
|
||||||
|
} catch (err) {
|
||||||
|
this.logger.error(
|
||||||
|
`Dakao outbox sweep failed: ${(err as Error)?.stack ?? err}`,
|
||||||
|
);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -0,0 +1,171 @@
|
|||||||
|
import { Injectable, Logger } from '@nestjs/common';
|
||||||
|
import { ConnectionService } from '../global/database/connection.service';
|
||||||
|
import { NatsService } from '../global/nats/nats.service';
|
||||||
|
import { DakaoCommandEnvelope, DakaoResultEnvelope } from './dakao.types';
|
||||||
|
|
||||||
|
const TERMINAL_STATES = ['sent', 'failed'] as const;
|
||||||
|
|
||||||
|
@Injectable()
|
||||||
|
export class DakaoOutboxService {
|
||||||
|
private readonly logger = new Logger(DakaoOutboxService.name);
|
||||||
|
|
||||||
|
constructor(
|
||||||
|
private readonly connectionService: ConnectionService,
|
||||||
|
private readonly natsService: NatsService,
|
||||||
|
) {}
|
||||||
|
|
||||||
|
public async enqueueAndPublish<TPayload>(params: {
|
||||||
|
subject: string;
|
||||||
|
eventId: string;
|
||||||
|
envelope: DakaoCommandEnvelope<TPayload>;
|
||||||
|
}): Promise<void> {
|
||||||
|
const OutboxModel =
|
||||||
|
await this.connectionService.getModel('dakaoOutboxMessage');
|
||||||
|
await OutboxModel.findOneAndUpdate(
|
||||||
|
{ eventId: params.eventId },
|
||||||
|
{
|
||||||
|
$setOnInsert: {
|
||||||
|
eventId: params.eventId,
|
||||||
|
eventSource: params.envelope.eventSource,
|
||||||
|
type: params.envelope.type,
|
||||||
|
subject: params.subject,
|
||||||
|
correlationId: params.envelope.correlationId,
|
||||||
|
payload: params.envelope.payload as Record<string, unknown>,
|
||||||
|
state: 'pending',
|
||||||
|
publishAttempts: 0,
|
||||||
|
compensationState: 'none',
|
||||||
|
},
|
||||||
|
},
|
||||||
|
{ upsert: true, returnDocument: 'after', setDefaultsOnInsert: true },
|
||||||
|
).exec();
|
||||||
|
|
||||||
|
try {
|
||||||
|
await this.natsService.publishJetStream(params.subject, params.envelope, {
|
||||||
|
messageId: params.eventId,
|
||||||
|
});
|
||||||
|
await OutboxModel.updateOne(
|
||||||
|
{
|
||||||
|
eventId: params.eventId,
|
||||||
|
state: { $nin: TERMINAL_STATES },
|
||||||
|
},
|
||||||
|
{
|
||||||
|
$set: {
|
||||||
|
state: 'published',
|
||||||
|
publishedAt: new Date(),
|
||||||
|
},
|
||||||
|
$inc: { publishAttempts: 1 },
|
||||||
|
$unset: { lastPublishError: '' },
|
||||||
|
},
|
||||||
|
).exec();
|
||||||
|
} catch (err) {
|
||||||
|
await OutboxModel.updateOne(
|
||||||
|
{
|
||||||
|
eventId: params.eventId,
|
||||||
|
state: { $nin: TERMINAL_STATES },
|
||||||
|
},
|
||||||
|
{
|
||||||
|
$inc: { publishAttempts: 1 },
|
||||||
|
$set: {
|
||||||
|
lastPublishError: (err as Error)?.message ?? String(err),
|
||||||
|
},
|
||||||
|
},
|
||||||
|
).exec();
|
||||||
|
this.logger.error(
|
||||||
|
`Failed to publish Dakao outbox eventId=${params.eventId}: ${
|
||||||
|
(err as Error)?.stack ?? err
|
||||||
|
}`,
|
||||||
|
);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
public async applySendResult(params: {
|
||||||
|
subject: string;
|
||||||
|
envelope: DakaoResultEnvelope;
|
||||||
|
}): Promise<{ duplicate: boolean; terminalState?: 'sent' | 'failed' }> {
|
||||||
|
const ResultModel = await this.connectionService.getModel(
|
||||||
|
'dakaoSendResultEvent',
|
||||||
|
);
|
||||||
|
const OutboxModel =
|
||||||
|
await this.connectionService.getModel('dakaoOutboxMessage');
|
||||||
|
|
||||||
|
try {
|
||||||
|
await ResultModel.create({
|
||||||
|
eventId: params.envelope.eventId,
|
||||||
|
requestEventId: params.envelope.requestEventId,
|
||||||
|
eventSource: params.envelope.eventSource,
|
||||||
|
requestEventSource: params.envelope.requestEventSource,
|
||||||
|
type: params.envelope.type,
|
||||||
|
subject: params.subject,
|
||||||
|
status: params.envelope.status,
|
||||||
|
correlationId: params.envelope.correlationId,
|
||||||
|
handledAt: new Date(params.envelope.handledAt),
|
||||||
|
providerMessageId: params.envelope.providerMessageId,
|
||||||
|
error: params.envelope.error,
|
||||||
|
raw: params.envelope as unknown as Record<string, unknown>,
|
||||||
|
});
|
||||||
|
} catch (err) {
|
||||||
|
if ((err as { code?: number })?.code === 11000) {
|
||||||
|
return { duplicate: true };
|
||||||
|
}
|
||||||
|
throw err;
|
||||||
|
}
|
||||||
|
|
||||||
|
const terminalState =
|
||||||
|
params.envelope.status === 'succeeded' ? 'sent' : 'failed';
|
||||||
|
await OutboxModel.updateOne(
|
||||||
|
{
|
||||||
|
eventId: params.envelope.requestEventId,
|
||||||
|
state: { $nin: TERMINAL_STATES },
|
||||||
|
},
|
||||||
|
{
|
||||||
|
$set: {
|
||||||
|
state: terminalState,
|
||||||
|
terminalAt: new Date(),
|
||||||
|
resultEventId: params.envelope.eventId,
|
||||||
|
terminalError: params.envelope.error,
|
||||||
|
compensationState:
|
||||||
|
params.envelope.status === 'failed' ? 'pending' : 'none',
|
||||||
|
compensationReason: params.envelope.error?.code,
|
||||||
|
},
|
||||||
|
},
|
||||||
|
).exec();
|
||||||
|
|
||||||
|
return { duplicate: false, terminalState };
|
||||||
|
}
|
||||||
|
|
||||||
|
public async sweepStale(params?: {
|
||||||
|
staleMs?: number;
|
||||||
|
limit?: number;
|
||||||
|
}): Promise<number> {
|
||||||
|
const OutboxModel =
|
||||||
|
await this.connectionService.getModel('dakaoOutboxMessage');
|
||||||
|
const staleBefore = new Date(
|
||||||
|
Date.now() - (params?.staleMs ?? 5 * 60 * 1000),
|
||||||
|
);
|
||||||
|
const rows = await OutboxModel.find({
|
||||||
|
state: { $in: ['pending', 'published'] },
|
||||||
|
updatedAt: { $lte: staleBefore },
|
||||||
|
})
|
||||||
|
.sort({ updatedAt: 1 })
|
||||||
|
.limit(params?.limit ?? 20)
|
||||||
|
.lean()
|
||||||
|
.exec();
|
||||||
|
|
||||||
|
for (const row of rows) {
|
||||||
|
await this.enqueueAndPublish({
|
||||||
|
subject: row.subject,
|
||||||
|
eventId: row.eventId,
|
||||||
|
envelope: {
|
||||||
|
schema: 'dakao.command.v1',
|
||||||
|
eventId: row.eventId,
|
||||||
|
type: row.type,
|
||||||
|
eventSource: row.eventSource,
|
||||||
|
correlationId: row.correlationId,
|
||||||
|
payload: row.payload,
|
||||||
|
},
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
|
return rows.length;
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -0,0 +1,81 @@
|
|||||||
|
import { Injectable, Logger, OnModuleInit } from '@nestjs/common';
|
||||||
|
import { NatsService } from '../global/nats/nats.service';
|
||||||
|
import { DakaoJetStreamResourceService } from './dakao-jetstream-resource.service';
|
||||||
|
import { resolveDakaoNatsNames } from './dakao.nats-names';
|
||||||
|
import { DakaoOutboxService } from './dakao-outbox.service';
|
||||||
|
import { DakaoResultEnvelope } from './dakao.types';
|
||||||
|
|
||||||
|
@Injectable()
|
||||||
|
export class DakaoSendResultSubscriber implements OnModuleInit {
|
||||||
|
private readonly logger = new Logger(DakaoSendResultSubscriber.name);
|
||||||
|
|
||||||
|
constructor(
|
||||||
|
private readonly natsService: NatsService,
|
||||||
|
private readonly resources: DakaoJetStreamResourceService,
|
||||||
|
private readonly outboxService: DakaoOutboxService,
|
||||||
|
) {}
|
||||||
|
|
||||||
|
async onModuleInit() {
|
||||||
|
await this.resources.ensure();
|
||||||
|
const names = resolveDakaoNatsNames();
|
||||||
|
void this.natsService.consumeJetStream<DakaoResultEnvelope>(
|
||||||
|
{
|
||||||
|
stream: names.streams.sendResult,
|
||||||
|
durable: names.consumers.sendResult,
|
||||||
|
},
|
||||||
|
(event, msg) => this.handleResult(event, msg.subject),
|
||||||
|
);
|
||||||
|
this.logger.log(
|
||||||
|
`Dakao send-result subscriber ready on stream="${names.streams.sendResult}" durable="${names.consumers.sendResult}"`,
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
|
private async handleResult(
|
||||||
|
event: DakaoResultEnvelope,
|
||||||
|
subject: string,
|
||||||
|
): Promise<void> {
|
||||||
|
if (!this.validate(event)) {
|
||||||
|
this.logger.warn(`Ignoring invalid Dakao send-result on ${subject}`);
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
const result = await this.outboxService.applySendResult({
|
||||||
|
subject,
|
||||||
|
envelope: event,
|
||||||
|
});
|
||||||
|
if (result.duplicate) {
|
||||||
|
this.logger.log(
|
||||||
|
`Duplicate Dakao result ignored eventId=${event.eventId}`,
|
||||||
|
);
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
this.logger.log(
|
||||||
|
`Dakao result applied eventId=${event.eventId} requestEventId=${event.requestEventId} state=${result.terminalState ?? '-'}`,
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
|
private validate(event: unknown): event is DakaoResultEnvelope {
|
||||||
|
if (!event || typeof event !== 'object') return false;
|
||||||
|
const e = event as Partial<DakaoResultEnvelope>;
|
||||||
|
if (e.schema !== 'dakao.result.v1') return false;
|
||||||
|
if (typeof e.eventId !== 'string' || e.eventId.length === 0) return false;
|
||||||
|
if (typeof e.requestEventId !== 'string' || e.requestEventId.length === 0) {
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
if (typeof e.eventSource !== 'string' || e.eventSource.length === 0)
|
||||||
|
return false;
|
||||||
|
if (
|
||||||
|
typeof e.requestEventSource !== 'string' ||
|
||||||
|
e.requestEventSource.length === 0
|
||||||
|
) {
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
if (e.status !== 'succeeded' && e.status !== 'failed') return false;
|
||||||
|
if (
|
||||||
|
typeof e.handledAt !== 'string' ||
|
||||||
|
Number.isNaN(Date.parse(e.handledAt))
|
||||||
|
)
|
||||||
|
return false;
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -1,14 +1,15 @@
|
|||||||
import { Module } from '@nestjs/common';
|
import { Module } from '@nestjs/common';
|
||||||
import { MatchModule } from '../match/match.module';
|
import { MatchModule } from '../match/match.module';
|
||||||
import { RiotModule } from '../riot/riot.module';
|
import { RiotModule } from '../riot/riot.module';
|
||||||
import {
|
import { DAKAO_OPTIONS, createDefaultDakaoOptions } from './dakao.options';
|
||||||
DAKAO_OPTIONS,
|
|
||||||
createDefaultDakaoOptions,
|
|
||||||
} from './dakao.options';
|
|
||||||
import { DakaoPublisher } from './dakao.publisher';
|
import { DakaoPublisher } from './dakao.publisher';
|
||||||
import { DakaoSubscriber } from './dakao.subscriber';
|
import { DakaoSubscriber } from './dakao.subscriber';
|
||||||
|
import { DakaoInboxService } from './dakao-inbox.service';
|
||||||
|
import { DakaoJetStreamResourceService } from './dakao-jetstream-resource.service';
|
||||||
|
import { DakaoOutboxSweeperService } from './dakao-outbox-sweeper.service';
|
||||||
|
import { DakaoOutboxService } from './dakao-outbox.service';
|
||||||
import { DakaoUserService } from './dakao-user.service';
|
import { DakaoUserService } from './dakao-user.service';
|
||||||
import { IdempotencyCache } from './idempotency.cache';
|
import { DakaoSendResultSubscriber } from './dakao-send-result.subscriber';
|
||||||
import { AccountRegisterHandler } from './handlers/account-register.handler';
|
import { AccountRegisterHandler } from './handlers/account-register.handler';
|
||||||
import { MatchCancelHandler } from './handlers/match-cancel.handler';
|
import { MatchCancelHandler } from './handlers/match-cancel.handler';
|
||||||
import { MatchJoinHandler } from './handlers/match-join.handler';
|
import { MatchJoinHandler } from './handlers/match-join.handler';
|
||||||
@@ -22,10 +23,14 @@ import { PreferredPositionRegisterHandler } from './handlers/preferred-position-
|
|||||||
provide: DAKAO_OPTIONS,
|
provide: DAKAO_OPTIONS,
|
||||||
useFactory: createDefaultDakaoOptions,
|
useFactory: createDefaultDakaoOptions,
|
||||||
},
|
},
|
||||||
|
DakaoOutboxService,
|
||||||
|
DakaoOutboxSweeperService,
|
||||||
|
DakaoInboxService,
|
||||||
|
DakaoJetStreamResourceService,
|
||||||
DakaoPublisher,
|
DakaoPublisher,
|
||||||
DakaoSubscriber,
|
DakaoSubscriber,
|
||||||
|
DakaoSendResultSubscriber,
|
||||||
DakaoUserService,
|
DakaoUserService,
|
||||||
IdempotencyCache,
|
|
||||||
AccountRegisterHandler,
|
AccountRegisterHandler,
|
||||||
MatchListHandler,
|
MatchListHandler,
|
||||||
MatchJoinHandler,
|
MatchJoinHandler,
|
||||||
|
|||||||
@@ -0,0 +1,38 @@
|
|||||||
|
import { resolveDakaoNatsNames } from './dakao.nats-names';
|
||||||
|
|
||||||
|
describe('resolveDakaoNatsNames', () => {
|
||||||
|
it('resolves staging subjects and JetStream resources from NODE_ENV=staging', () => {
|
||||||
|
expect(resolveDakaoNatsNames('staging')).toEqual({
|
||||||
|
env: 'staging',
|
||||||
|
shortEnv: 'STG',
|
||||||
|
subjects: {
|
||||||
|
botCommandSubmitted: 'staging.lolympus.dakao.bot-command.submitted.v1',
|
||||||
|
chatSendMessage: 'staging.dakao.chat.send-message.v1',
|
||||||
|
chatSendMessageSucceeded:
|
||||||
|
'staging.lolympus.dakao.chat.send-message.succeeded.v1',
|
||||||
|
chatSendMessageFailed:
|
||||||
|
'staging.lolympus.dakao.chat.send-message.failed.v1',
|
||||||
|
},
|
||||||
|
streams: {
|
||||||
|
botCommand: 'STG_DAKAO_TO_LOLYMPUS_BOTCMD',
|
||||||
|
sendRequest: 'STG_DAKAO_SENDREQ',
|
||||||
|
sendResult: 'STG_DAKAO_TO_LOLYMPUS_SENDRES',
|
||||||
|
},
|
||||||
|
consumers: {
|
||||||
|
botCommand: 'STG_LOLYMPUS_BOTCMD',
|
||||||
|
sendRequest: 'STG_DAKAO_SENDREQ_WORKER',
|
||||||
|
sendResult: 'STG_LOLYMPUS_SENDRES',
|
||||||
|
},
|
||||||
|
});
|
||||||
|
});
|
||||||
|
|
||||||
|
it('keeps JetStream resource names within the 32 character safety target', () => {
|
||||||
|
const names = resolveDakaoNatsNames('prod');
|
||||||
|
const resources = [
|
||||||
|
...Object.values(names.streams),
|
||||||
|
...Object.values(names.consumers),
|
||||||
|
];
|
||||||
|
|
||||||
|
expect(resources.every((name) => name.length <= 32)).toBe(true);
|
||||||
|
});
|
||||||
|
});
|
||||||
@@ -0,0 +1,68 @@
|
|||||||
|
export type DakaoRuntimeEnv = 'dev' | 'staging' | 'prod';
|
||||||
|
export type DakaoShortEnv = 'DEV' | 'STG' | 'PROD';
|
||||||
|
|
||||||
|
export interface DakaoNatsNames {
|
||||||
|
env: DakaoRuntimeEnv;
|
||||||
|
shortEnv: DakaoShortEnv;
|
||||||
|
subjects: {
|
||||||
|
botCommandSubmitted: string;
|
||||||
|
chatSendMessage: string;
|
||||||
|
chatSendMessageSucceeded: string;
|
||||||
|
chatSendMessageFailed: string;
|
||||||
|
};
|
||||||
|
streams: {
|
||||||
|
botCommand: string;
|
||||||
|
sendRequest: string;
|
||||||
|
sendResult: string;
|
||||||
|
};
|
||||||
|
consumers: {
|
||||||
|
botCommand: string;
|
||||||
|
sendRequest: string;
|
||||||
|
sendResult: string;
|
||||||
|
};
|
||||||
|
}
|
||||||
|
|
||||||
|
export function resolveDakaoNatsNames(
|
||||||
|
nodeEnv = process.env.NODE_ENV,
|
||||||
|
): DakaoNatsNames {
|
||||||
|
const env = resolveRuntimeEnv(nodeEnv);
|
||||||
|
const shortEnv = toShortEnv(env);
|
||||||
|
|
||||||
|
return {
|
||||||
|
env,
|
||||||
|
shortEnv,
|
||||||
|
subjects: {
|
||||||
|
botCommandSubmitted: `${env}.lolympus.dakao.bot-command.submitted.v1`,
|
||||||
|
chatSendMessage: `${env}.dakao.chat.send-message.v1`,
|
||||||
|
chatSendMessageSucceeded: `${env}.lolympus.dakao.chat.send-message.succeeded.v1`,
|
||||||
|
chatSendMessageFailed: `${env}.lolympus.dakao.chat.send-message.failed.v1`,
|
||||||
|
},
|
||||||
|
streams: {
|
||||||
|
botCommand: `${shortEnv}_DAKAO_TO_LOLYMPUS_BOTCMD`,
|
||||||
|
sendRequest: `${shortEnv}_DAKAO_SENDREQ`,
|
||||||
|
sendResult: `${shortEnv}_DAKAO_TO_LOLYMPUS_SENDRES`,
|
||||||
|
},
|
||||||
|
consumers: {
|
||||||
|
botCommand: `${shortEnv}_LOLYMPUS_BOTCMD`,
|
||||||
|
sendRequest: `${shortEnv}_DAKAO_SENDREQ_WORKER`,
|
||||||
|
sendResult: `${shortEnv}_LOLYMPUS_SENDRES`,
|
||||||
|
},
|
||||||
|
};
|
||||||
|
}
|
||||||
|
|
||||||
|
function resolveRuntimeEnv(nodeEnv?: string): DakaoRuntimeEnv {
|
||||||
|
if (nodeEnv === 'prod' || nodeEnv === 'production') return 'prod';
|
||||||
|
if (nodeEnv === 'staging') return 'staging';
|
||||||
|
return 'dev';
|
||||||
|
}
|
||||||
|
|
||||||
|
function toShortEnv(env: DakaoRuntimeEnv): DakaoShortEnv {
|
||||||
|
switch (env) {
|
||||||
|
case 'prod':
|
||||||
|
return 'PROD';
|
||||||
|
case 'staging':
|
||||||
|
return 'STG';
|
||||||
|
case 'dev':
|
||||||
|
return 'DEV';
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -1,32 +1,38 @@
|
|||||||
import { SecretService } from '../global/secret/secret.service';
|
import { SecretService } from '../global/secret/secret.service';
|
||||||
import { NatsService } from '../global/nats/nats.service';
|
|
||||||
import { DakaoCommandEnvelope, DakaoSendMessagePayload } from './dakao.types';
|
import { DakaoCommandEnvelope, DakaoSendMessagePayload } from './dakao.types';
|
||||||
import { DakaoPublisher } from './dakao.publisher';
|
import { DakaoPublisher } from './dakao.publisher';
|
||||||
|
import { DakaoOutboxService } from './dakao-outbox.service';
|
||||||
|
|
||||||
function makePublisher() {
|
function makePublisher() {
|
||||||
const natsService = {
|
const outboxService = {
|
||||||
publish: jest.fn().mockResolvedValue(undefined),
|
enqueueAndPublish: jest.fn().mockResolvedValue(undefined),
|
||||||
subscribe: jest.fn(),
|
} as unknown as DakaoOutboxService;
|
||||||
} as unknown as NatsService;
|
|
||||||
const secretService = {} as SecretService;
|
const secretService = {} as SecretService;
|
||||||
const publisher = new DakaoPublisher(natsService, secretService, {
|
const previousNodeEnv = process.env.NODE_ENV;
|
||||||
instanceId: 'test',
|
process.env.NODE_ENV = 'staging';
|
||||||
});
|
|
||||||
|
|
||||||
return { publisher, natsService };
|
try {
|
||||||
|
const publisher = new DakaoPublisher(secretService, outboxService, {
|
||||||
|
instanceId: 'staging-instance',
|
||||||
|
});
|
||||||
|
|
||||||
|
return { publisher, outboxService };
|
||||||
|
} finally {
|
||||||
|
process.env.NODE_ENV = previousNodeEnv;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
function getPublishedPayload(natsService: NatsService): DakaoSendMessagePayload {
|
function getPublishedEnvelope(
|
||||||
const publish = natsService.publish as jest.Mock;
|
outboxService: DakaoOutboxService,
|
||||||
const envelope = publish.mock.calls[0][1] as DakaoCommandEnvelope<
|
): DakaoCommandEnvelope<DakaoSendMessagePayload> {
|
||||||
DakaoSendMessagePayload
|
const enqueue = outboxService.enqueueAndPublish as jest.Mock;
|
||||||
>;
|
return enqueue.mock.calls[0][0]
|
||||||
return envelope.payload;
|
.envelope as DakaoCommandEnvelope<DakaoSendMessagePayload>;
|
||||||
}
|
}
|
||||||
|
|
||||||
describe('DakaoPublisher', () => {
|
describe('DakaoPublisher', () => {
|
||||||
it('keeps valid mention segments intact', async () => {
|
it('keeps valid mention segments intact', async () => {
|
||||||
const { publisher, natsService } = makePublisher();
|
const { publisher, outboxService } = makePublisher();
|
||||||
|
|
||||||
await publisher.sendMessage({
|
await publisher.sendMessage({
|
||||||
chatroomId: '18474390111480678',
|
chatroomId: '18474390111480678',
|
||||||
@@ -40,7 +46,7 @@ describe('DakaoPublisher', () => {
|
|||||||
],
|
],
|
||||||
});
|
});
|
||||||
|
|
||||||
expect(getPublishedPayload(natsService).segments).toEqual([
|
expect(getPublishedEnvelope(outboxService).payload.segments).toEqual([
|
||||||
{
|
{
|
||||||
kind: 'mention',
|
kind: 'mention',
|
||||||
userId: '6215844374579825921',
|
userId: '6215844374579825921',
|
||||||
@@ -51,7 +57,7 @@ describe('DakaoPublisher', () => {
|
|||||||
});
|
});
|
||||||
|
|
||||||
it('downgrades invalid mention segments to text before publishing', async () => {
|
it('downgrades invalid mention segments to text before publishing', async () => {
|
||||||
const { publisher, natsService } = makePublisher();
|
const { publisher, outboxService } = makePublisher();
|
||||||
|
|
||||||
await publisher.sendMessage({
|
await publisher.sendMessage({
|
||||||
chatroomId: '18474390111480678',
|
chatroomId: '18474390111480678',
|
||||||
@@ -65,9 +71,39 @@ describe('DakaoPublisher', () => {
|
|||||||
],
|
],
|
||||||
});
|
});
|
||||||
|
|
||||||
expect(getPublishedPayload(natsService).segments).toEqual([
|
expect(getPublishedEnvelope(outboxService).payload.segments).toEqual([
|
||||||
{ kind: 'text', text: '@소쿠리' },
|
{ kind: 'text', text: '@소쿠리' },
|
||||||
{ kind: 'text', text: ' 신청 완료' },
|
{ kind: 'text', text: ' 신청 완료' },
|
||||||
]);
|
]);
|
||||||
});
|
});
|
||||||
|
|
||||||
|
it('uses deterministic event ids and the shared Dakao send subject', async () => {
|
||||||
|
const { publisher, outboxService } = makePublisher();
|
||||||
|
|
||||||
|
await publisher.sendMessage({
|
||||||
|
chatroomId: '18474390111480678',
|
||||||
|
segments: [{ kind: 'text', text: '신청 완료' }],
|
||||||
|
correlationId: 'chat-log-1',
|
||||||
|
});
|
||||||
|
|
||||||
|
const enqueue = outboxService.enqueueAndPublish as jest.Mock;
|
||||||
|
expect(enqueue.mock.calls[0][0]).toMatchObject({
|
||||||
|
subject: 'staging.dakao.chat.send-message.v1',
|
||||||
|
eventId: 'lolympus:chat.send-message:chat-log-1',
|
||||||
|
});
|
||||||
|
expect(getPublishedEnvelope(outboxService)).toMatchObject({
|
||||||
|
schema: 'dakao.command.v1',
|
||||||
|
eventId: 'lolympus:chat.send-message:chat-log-1',
|
||||||
|
eventSource: 'lolympus',
|
||||||
|
type: 'chat.send-message',
|
||||||
|
correlationId: 'chat-log-1',
|
||||||
|
resultSubjects: {
|
||||||
|
succeeded: 'staging.lolympus.dakao.chat.send-message.succeeded.v1',
|
||||||
|
failed: 'staging.lolympus.dakao.chat.send-message.failed.v1',
|
||||||
|
},
|
||||||
|
});
|
||||||
|
expect(getPublishedEnvelope(outboxService)).not.toHaveProperty(
|
||||||
|
'replySubject',
|
||||||
|
);
|
||||||
|
});
|
||||||
});
|
});
|
||||||
@@ -1,86 +1,86 @@
|
|||||||
import {
|
import { Inject, Injectable, Logger, OnModuleInit } from '@nestjs/common';
|
||||||
Inject,
|
|
||||||
Injectable,
|
|
||||||
Logger,
|
|
||||||
OnModuleDestroy,
|
|
||||||
OnModuleInit,
|
|
||||||
} from '@nestjs/common';
|
|
||||||
import { randomUUID } from 'crypto';
|
import { randomUUID } from 'crypto';
|
||||||
import { SecretService } from '../global/secret/secret.service';
|
import { SecretService } from '../global/secret/secret.service';
|
||||||
import { NatsService } from '../global/nats/nats.service';
|
|
||||||
import {
|
import {
|
||||||
DakaoCommandEnvelope,
|
DakaoCommandEnvelope,
|
||||||
DakaoResultEnvelope,
|
DakaoResultSubjects,
|
||||||
DakaoSegment,
|
DakaoSegment,
|
||||||
DakaoSendMessagePayload,
|
DakaoSendMessagePayload,
|
||||||
} from './dakao.types';
|
} from './dakao.types';
|
||||||
import type { DakaoOptions } from './dakao.options';
|
import type { DakaoOptions } from './dakao.options';
|
||||||
import { DAKAO_OPTIONS } from './dakao.options';
|
import { DAKAO_OPTIONS } from './dakao.options';
|
||||||
|
import { resolveDakaoNatsNames } from './dakao.nats-names';
|
||||||
|
import { DakaoOutboxService } from './dakao-outbox.service';
|
||||||
|
|
||||||
const SEND_MESSAGE_SUBJECT_DEFAULT = 'dakao.command.chat.send-message.v1';
|
const EVENT_SOURCE_DEFAULT = 'lolympus';
|
||||||
const COMMAND_SOURCE_DEFAULT = 'lolympus-tournament-support';
|
|
||||||
|
|
||||||
@Injectable()
|
@Injectable()
|
||||||
export class DakaoPublisher implements OnModuleInit, OnModuleDestroy {
|
export class DakaoPublisher implements OnModuleInit {
|
||||||
private readonly logger = new Logger(DakaoPublisher.name);
|
private readonly logger = new Logger(DakaoPublisher.name);
|
||||||
private replySubject = '';
|
private readonly names = resolveDakaoNatsNames();
|
||||||
private sendMessageSubject = SEND_MESSAGE_SUBJECT_DEFAULT;
|
private sendMessageSubject = this.names.subjects.chatSendMessage;
|
||||||
private source = COMMAND_SOURCE_DEFAULT;
|
private resultSubjects: DakaoResultSubjects = {
|
||||||
|
succeeded: this.names.subjects.chatSendMessageSucceeded,
|
||||||
|
failed: this.names.subjects.chatSendMessageFailed,
|
||||||
|
};
|
||||||
|
private eventSource = EVENT_SOURCE_DEFAULT;
|
||||||
|
|
||||||
constructor(
|
constructor(
|
||||||
private readonly natsService: NatsService,
|
|
||||||
private readonly secretService: SecretService,
|
private readonly secretService: SecretService,
|
||||||
|
private readonly outboxService: DakaoOutboxService,
|
||||||
@Inject(DAKAO_OPTIONS) private readonly options: DakaoOptions,
|
@Inject(DAKAO_OPTIONS) private readonly options: DakaoOptions,
|
||||||
) {}
|
) {}
|
||||||
|
|
||||||
async onModuleInit() {
|
async onModuleInit() {
|
||||||
const replyOverride = await this.secretService
|
|
||||||
.get<string>('DAKAO_REPLY_SUBJECT_PREFIX')
|
|
||||||
.catch(() => undefined);
|
|
||||||
const sendOverride = await this.secretService
|
const sendOverride = await this.secretService
|
||||||
.get<string>('DAKAO_NATS_COMMAND_CHAT_SEND_MESSAGE_SUBJECT')
|
.get<string>('DAKAO_CHAT_SEND_MESSAGE_SUBJECT')
|
||||||
.catch(() => undefined);
|
.catch(() => undefined);
|
||||||
const sourceOverride = await this.secretService
|
const sourceOverride = await this.secretService
|
||||||
.get<string>('DAKAO_COMMAND_SOURCE')
|
.get<string>('DAKAO_EVENT_SOURCE')
|
||||||
.catch(() => undefined);
|
.catch(() => undefined);
|
||||||
|
|
||||||
const replyPrefix = replyOverride || `dakao.result.${COMMAND_SOURCE_DEFAULT}`;
|
this.sendMessageSubject =
|
||||||
this.replySubject = `${replyPrefix}.${this.options.instanceId}`;
|
sendOverride || resolveDakaoNatsNames().subjects.chatSendMessage;
|
||||||
this.sendMessageSubject = sendOverride || SEND_MESSAGE_SUBJECT_DEFAULT;
|
this.resultSubjects = {
|
||||||
this.source = sourceOverride || COMMAND_SOURCE_DEFAULT;
|
succeeded: this.names.subjects.chatSendMessageSucceeded,
|
||||||
|
failed: this.names.subjects.chatSendMessageFailed,
|
||||||
|
};
|
||||||
|
this.eventSource = sourceOverride || EVENT_SOURCE_DEFAULT;
|
||||||
|
|
||||||
this.natsService.subscribe<DakaoResultEnvelope>(
|
|
||||||
this.replySubject,
|
|
||||||
(data) => this.handleReply(data),
|
|
||||||
);
|
|
||||||
this.logger.log(
|
this.logger.log(
|
||||||
`Dakao reply subscriber ready on subject="${this.replySubject}"`,
|
`Dakao send-message publisher ready on subject="${this.sendMessageSubject}" source="${this.eventSource}" instanceId="${this.options.instanceId}"`,
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
async onModuleDestroy() {
|
|
||||||
// NatsService 가 subscription 정리한다.
|
|
||||||
}
|
|
||||||
|
|
||||||
public async sendMessage(params: {
|
public async sendMessage(params: {
|
||||||
chatroomId: string;
|
chatroomId: string;
|
||||||
segments: DakaoSegment[];
|
segments: DakaoSegment[];
|
||||||
correlationId?: string;
|
correlationId?: string;
|
||||||
|
eventId?: string;
|
||||||
}): Promise<void> {
|
}): Promise<void> {
|
||||||
|
const eventId =
|
||||||
|
params.eventId ??
|
||||||
|
(params.correlationId
|
||||||
|
? `${this.eventSource}:chat.send-message:${params.correlationId}`
|
||||||
|
: `${this.eventSource}:chat.send-message:${randomUUID()}`);
|
||||||
const payload: DakaoSendMessagePayload = {
|
const payload: DakaoSendMessagePayload = {
|
||||||
chatroomId: params.chatroomId,
|
chatroomId: params.chatroomId,
|
||||||
segments: this.normalizeSegments(params.segments),
|
segments: this.normalizeSegments(params.segments),
|
||||||
};
|
};
|
||||||
const envelope: DakaoCommandEnvelope<DakaoSendMessagePayload> = {
|
const envelope: DakaoCommandEnvelope<DakaoSendMessagePayload> = {
|
||||||
schema: 'dakao.command.v1',
|
schema: 'dakao.command.v1',
|
||||||
id: randomUUID(),
|
eventId,
|
||||||
type: 'chat.send-message',
|
type: 'chat.send-message',
|
||||||
source: this.source,
|
eventSource: this.eventSource,
|
||||||
correlationId: params.correlationId,
|
correlationId: params.correlationId,
|
||||||
replySubject: this.replySubject,
|
resultSubjects: this.resultSubjects,
|
||||||
payload,
|
payload,
|
||||||
};
|
};
|
||||||
await this.natsService.publish(this.sendMessageSubject, envelope);
|
await this.outboxService.enqueueAndPublish({
|
||||||
|
subject: this.sendMessageSubject,
|
||||||
|
eventId,
|
||||||
|
envelope,
|
||||||
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
private normalizeSegments(segments: DakaoSegment[]): DakaoSegment[] {
|
private normalizeSegments(segments: DakaoSegment[]): DakaoSegment[] {
|
||||||
@@ -111,11 +111,7 @@ export class DakaoPublisher implements OnModuleInit, OnModuleDestroy {
|
|||||||
|
|
||||||
const id = segment.id.trim();
|
const id = segment.id.trim();
|
||||||
const altText = segment.altText.trim();
|
const altText = segment.altText.trim();
|
||||||
if (
|
if (id.length > 0 && altText.length > 0 && !/[()@]/.test(altText)) {
|
||||||
id.length > 0 &&
|
|
||||||
altText.length > 0 &&
|
|
||||||
!/[()@]/.test(altText)
|
|
||||||
) {
|
|
||||||
normalized.push({ kind: 'emoticon', id, altText });
|
normalized.push({ kind: 'emoticon', id, altText });
|
||||||
continue;
|
continue;
|
||||||
}
|
}
|
||||||
@@ -139,16 +135,4 @@ export class DakaoPublisher implements OnModuleInit, OnModuleDestroy {
|
|||||||
const label = displayText || userId || '사용자';
|
const label = displayText || userId || '사용자';
|
||||||
return label.startsWith('@') ? label : `@${label}`;
|
return label.startsWith('@') ? label : `@${label}`;
|
||||||
}
|
}
|
||||||
|
|
||||||
private handleReply(reply: DakaoResultEnvelope): void {
|
|
||||||
if (reply.status === 'succeeded') {
|
|
||||||
this.logger.log(
|
|
||||||
`Dakao reply succeeded id=${reply.id} type=${reply.type} correlationId=${reply.correlationId ?? '-'}`,
|
|
||||||
);
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
this.logger.warn(
|
|
||||||
`Dakao reply failed id=${reply.id} type=${reply.type} code=${reply.error?.code ?? '-'} retryable=${reply.error?.retryable ?? false} message=${reply.error?.message ?? '-'}`,
|
|
||||||
);
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
@@ -6,19 +6,19 @@ import { CommandParseError, parseCommand } from './parsers/command.parser';
|
|||||||
import { DakaoInboundEvent, DakaoLong, DakaoSegment } from './dakao.types';
|
import { DakaoInboundEvent, DakaoLong, DakaoSegment } from './dakao.types';
|
||||||
import { DakaoPublisher } from './dakao.publisher';
|
import { DakaoPublisher } from './dakao.publisher';
|
||||||
import { DakaoUserError, DakaoUserService } from './dakao-user.service';
|
import { DakaoUserError, DakaoUserService } from './dakao-user.service';
|
||||||
import { IdempotencyCache } from './idempotency.cache';
|
|
||||||
import { AccountRegisterHandler } from './handlers/account-register.handler';
|
import { AccountRegisterHandler } from './handlers/account-register.handler';
|
||||||
import { MatchCancelHandler } from './handlers/match-cancel.handler';
|
import { MatchCancelHandler } from './handlers/match-cancel.handler';
|
||||||
import { MatchJoinHandler } from './handlers/match-join.handler';
|
import { MatchJoinHandler } from './handlers/match-join.handler';
|
||||||
import { MatchListHandler } from './handlers/match-list.handler';
|
import { MatchListHandler } from './handlers/match-list.handler';
|
||||||
import { PreferredPositionRegisterHandler } from './handlers/preferred-position-register.handler';
|
import { PreferredPositionRegisterHandler } from './handlers/preferred-position-register.handler';
|
||||||
|
import { resolveDakaoNatsNames } from './dakao.nats-names';
|
||||||
const INBOUND_SUBJECT_DEFAULT = 'lolympus-tournament-support.kakaotalk.command';
|
import { DakaoInboxService } from './dakao-inbox.service';
|
||||||
|
import { DakaoJetStreamResourceService } from './dakao-jetstream-resource.service';
|
||||||
|
|
||||||
@Injectable()
|
@Injectable()
|
||||||
export class DakaoSubscriber implements OnModuleInit {
|
export class DakaoSubscriber implements OnModuleInit {
|
||||||
private readonly logger = new Logger(DakaoSubscriber.name);
|
private readonly logger = new Logger(DakaoSubscriber.name);
|
||||||
private inboundSubject = INBOUND_SUBJECT_DEFAULT;
|
private readonly natsNames = resolveDakaoNatsNames();
|
||||||
private botUserId: string | undefined;
|
private botUserId: string | undefined;
|
||||||
|
|
||||||
constructor(
|
constructor(
|
||||||
@@ -26,7 +26,8 @@ export class DakaoSubscriber implements OnModuleInit {
|
|||||||
private readonly secretService: SecretService,
|
private readonly secretService: SecretService,
|
||||||
private readonly dakaoUserService: DakaoUserService,
|
private readonly dakaoUserService: DakaoUserService,
|
||||||
private readonly publisher: DakaoPublisher,
|
private readonly publisher: DakaoPublisher,
|
||||||
private readonly idempotency: IdempotencyCache,
|
private readonly inbox: DakaoInboxService,
|
||||||
|
private readonly resources: DakaoJetStreamResourceService,
|
||||||
private readonly accountRegisterHandler: AccountRegisterHandler,
|
private readonly accountRegisterHandler: AccountRegisterHandler,
|
||||||
private readonly matchListHandler: MatchListHandler,
|
private readonly matchListHandler: MatchListHandler,
|
||||||
private readonly matchJoinHandler: MatchJoinHandler,
|
private readonly matchJoinHandler: MatchJoinHandler,
|
||||||
@@ -35,21 +36,20 @@ export class DakaoSubscriber implements OnModuleInit {
|
|||||||
) {}
|
) {}
|
||||||
|
|
||||||
async onModuleInit() {
|
async onModuleInit() {
|
||||||
const subjectOverride = await this.secretService
|
|
||||||
.get<string>('DAKAO_INBOUND_SUBJECT')
|
|
||||||
.catch(() => undefined);
|
|
||||||
this.inboundSubject = subjectOverride || INBOUND_SUBJECT_DEFAULT;
|
|
||||||
|
|
||||||
this.botUserId = await this.secretService
|
this.botUserId = await this.secretService
|
||||||
.get<string>('DAKAO_BOT_USER_ID')
|
.get<string>('DAKAO_BOT_USER_ID')
|
||||||
.catch(() => undefined);
|
.catch(() => undefined);
|
||||||
|
|
||||||
this.natsService.subscribe<DakaoInboundEvent>(
|
await this.resources.ensure();
|
||||||
this.inboundSubject,
|
void this.natsService.consumeJetStream<DakaoInboundEvent>(
|
||||||
|
{
|
||||||
|
stream: this.natsNames.streams.botCommand,
|
||||||
|
durable: this.natsNames.consumers.botCommand,
|
||||||
|
},
|
||||||
(event) => this.handleEvent(event),
|
(event) => this.handleEvent(event),
|
||||||
);
|
);
|
||||||
this.logger.log(
|
this.logger.log(
|
||||||
`Dakao inbound subscriber ready on subject="${this.inboundSubject}"`,
|
`Dakao inbound subscriber ready on stream="${this.natsNames.streams.botCommand}" durable="${this.natsNames.consumers.botCommand}"`,
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -60,35 +60,52 @@ export class DakaoSubscriber implements OnModuleInit {
|
|||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
const logIdKey = this.deriveLogIdKey(event);
|
const chatLogLogId = this.serializeDakaoLong(event.origin.chatLog.logId);
|
||||||
if (logIdKey && this.idempotency.has(logIdKey)) {
|
let claimed = false;
|
||||||
return;
|
if (chatLogLogId) {
|
||||||
|
const claim = await this.inbox.claim({
|
||||||
|
consumer: this.natsNames.consumers.botCommand,
|
||||||
|
projectId: event.projectId,
|
||||||
|
chatLogLogId,
|
||||||
|
});
|
||||||
|
if (claim.status === 'duplicate') return;
|
||||||
|
if (claim.status === 'inflight') {
|
||||||
|
throw new Error(
|
||||||
|
`Dakao event is already being processed projectId=${event.projectId} logId=${chatLogLogId}`,
|
||||||
|
);
|
||||||
|
}
|
||||||
|
claimed = true;
|
||||||
}
|
}
|
||||||
if (logIdKey) this.idempotency.remember(logIdKey);
|
|
||||||
|
|
||||||
let communityId: mongoose.Types.ObjectId;
|
let communityId: mongoose.Types.ObjectId;
|
||||||
try {
|
try {
|
||||||
communityId = await this.dakaoUserService.resolveCommunityIdByDakaoProjectId(
|
communityId =
|
||||||
event.projectId,
|
await this.dakaoUserService.resolveCommunityIdByDakaoProjectId(
|
||||||
);
|
event.projectId,
|
||||||
|
);
|
||||||
} catch (err) {
|
} catch (err) {
|
||||||
if (err instanceof DakaoUserError) {
|
if (err instanceof DakaoUserError) {
|
||||||
if (err.code === 'invalid_project_id') {
|
if (err.code === 'invalid_project_id') {
|
||||||
this.logger.warn(`Invalid dakao projectId format: ${event.projectId}`);
|
this.logger.warn(
|
||||||
|
`Invalid dakao projectId format: ${event.projectId}`,
|
||||||
|
);
|
||||||
|
if (claimed) await this.markProcessed(event, chatLogLogId);
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
if (err.code === 'community_not_found') {
|
if (err.code === 'community_not_found') {
|
||||||
// 우리 쪽이 모르는 projectId. 메시지를 보내봤자 컨텍스트가 없으므로 로그만.
|
// 우리 쪽이 모르는 projectId. 메시지를 보내봤자 컨텍스트가 없으므로 로그만.
|
||||||
this.logger.warn(`Unknown dakao projectId: ${event.projectId}`);
|
this.logger.warn(`Unknown dakao projectId: ${event.projectId}`);
|
||||||
|
if (claimed) await this.markProcessed(event, chatLogLogId);
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
if (claimed) await this.markFailed(event, chatLogLogId, err);
|
||||||
throw err;
|
throw err;
|
||||||
}
|
}
|
||||||
|
|
||||||
let segments: DakaoSegment[];
|
let segments: DakaoSegment[];
|
||||||
const senderNickname = this.resolveSenderNickname(event);
|
const senderNickname = this.resolveSenderNickname(event);
|
||||||
const correlationId = this.serializeDakaoLong(event.origin.chatLog.logId);
|
const correlationId = chatLogLogId;
|
||||||
try {
|
try {
|
||||||
const command = parseCommand(event.origin.chatLog.message);
|
const command = parseCommand(event.origin.chatLog.message);
|
||||||
|
|
||||||
@@ -141,27 +158,35 @@ export class DakaoSubscriber implements OnModuleInit {
|
|||||||
segments,
|
segments,
|
||||||
correlationId,
|
correlationId,
|
||||||
});
|
});
|
||||||
|
if (claimed) await this.markProcessed(event, chatLogLogId);
|
||||||
} catch (err) {
|
} catch (err) {
|
||||||
if (err instanceof CommandParseError) {
|
if (err instanceof CommandParseError) {
|
||||||
// not_a_command 는 명령이 아닌 일반 메시지 → 무시.
|
// not_a_command 는 명령이 아닌 일반 메시지 → 무시.
|
||||||
if (err.code === 'not_a_command') return;
|
if (err.code === 'not_a_command') {
|
||||||
|
if (claimed) await this.markProcessed(event, chatLogLogId);
|
||||||
|
return;
|
||||||
|
}
|
||||||
await this.publisher.sendMessage({
|
await this.publisher.sendMessage({
|
||||||
chatroomId: event.chatroomId,
|
chatroomId: event.chatroomId,
|
||||||
segments: this.parseErrorReply(event, senderNickname, err),
|
segments: this.parseErrorReply(event, senderNickname, err),
|
||||||
correlationId,
|
correlationId,
|
||||||
});
|
});
|
||||||
|
if (claimed) await this.markProcessed(event, chatLogLogId);
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
this.logger.error(
|
this.logger.error(
|
||||||
`Unhandled dakao event error: ${(err as Error)?.stack ?? err}`,
|
`Unhandled dakao event error: ${(err as Error)?.stack ?? err}`,
|
||||||
);
|
);
|
||||||
|
if (claimed) await this.markFailed(event, chatLogLogId, err);
|
||||||
|
throw err;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
private validate(event: unknown): event is DakaoInboundEvent {
|
private validate(event: unknown): event is DakaoInboundEvent {
|
||||||
if (!event || typeof event !== 'object') return false;
|
if (!event || typeof event !== 'object') return false;
|
||||||
const e = event as Partial<DakaoInboundEvent>;
|
const e = event as Partial<DakaoInboundEvent>;
|
||||||
if (typeof e.projectId !== 'string' || e.projectId.length === 0) return false;
|
if (typeof e.projectId !== 'string' || e.projectId.length === 0)
|
||||||
|
return false;
|
||||||
if (typeof e.chatroomId !== 'string' || e.chatroomId.length === 0)
|
if (typeof e.chatroomId !== 'string' || e.chatroomId.length === 0)
|
||||||
return false;
|
return false;
|
||||||
if (typeof e.nickname !== 'string') return false;
|
if (typeof e.nickname !== 'string') return false;
|
||||||
@@ -174,11 +199,6 @@ export class DakaoSubscriber implements OnModuleInit {
|
|||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
|
|
||||||
private deriveLogIdKey(event: DakaoInboundEvent): string | null {
|
|
||||||
const serialized = this.serializeDakaoLong(event.origin.chatLog.logId);
|
|
||||||
return serialized ? `${event.projectId}:${serialized}` : null;
|
|
||||||
}
|
|
||||||
|
|
||||||
private serializeDakaoLong(value: DakaoLong | string | undefined): string {
|
private serializeDakaoLong(value: DakaoLong | string | undefined): string {
|
||||||
if (typeof value === 'string') return value;
|
if (typeof value === 'string') return value;
|
||||||
if (!value || typeof value !== 'object') return '';
|
if (!value || typeof value !== 'object') return '';
|
||||||
@@ -197,6 +217,30 @@ export class DakaoSubscriber implements OnModuleInit {
|
|||||||
return fallback ?? '';
|
return fallback ?? '';
|
||||||
}
|
}
|
||||||
|
|
||||||
|
private async markProcessed(
|
||||||
|
event: DakaoInboundEvent,
|
||||||
|
chatLogLogId: string,
|
||||||
|
): Promise<void> {
|
||||||
|
await this.inbox.markProcessed({
|
||||||
|
consumer: this.natsNames.consumers.botCommand,
|
||||||
|
projectId: event.projectId,
|
||||||
|
chatLogLogId,
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
|
private async markFailed(
|
||||||
|
event: DakaoInboundEvent,
|
||||||
|
chatLogLogId: string,
|
||||||
|
error: unknown,
|
||||||
|
): Promise<void> {
|
||||||
|
await this.inbox.markFailed({
|
||||||
|
consumer: this.natsNames.consumers.botCommand,
|
||||||
|
projectId: event.projectId,
|
||||||
|
chatLogLogId,
|
||||||
|
error,
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
private parseErrorReply(
|
private parseErrorReply(
|
||||||
event: DakaoInboundEvent,
|
event: DakaoInboundEvent,
|
||||||
senderNickname: string,
|
senderNickname: string,
|
||||||
|
|||||||
@@ -1,7 +1,7 @@
|
|||||||
// dakao NATS contracts
|
// dakao NATS contracts
|
||||||
// inbound: dakao 가 lolympus-tournament-support.kakaotalk.command 로 발행하는 이벤트
|
// inbound: dakao 가 {env}.lolympus.dakao.bot-command.submitted.v1 로 발행하는 이벤트
|
||||||
// outbound: 우리가 dakao.command.chat.send-message.v1 로 발행하는 명령
|
// outbound: 우리가 {env}.dakao.chat.send-message.v1 로 발행하는 명령
|
||||||
// reply: dakao 가 우리 envelope 의 replySubject 로 publish 하는 결과
|
// result: dakao 가 {env}.lolympus.dakao.chat.send-message.{succeeded|failed}.v1 로 발행하는 결과 이벤트
|
||||||
|
|
||||||
// ===== Inbound =====
|
// ===== Inbound =====
|
||||||
export interface DakaoLong {
|
export interface DakaoLong {
|
||||||
@@ -75,24 +75,33 @@ export interface DakaoSendMessagePayload {
|
|||||||
silence?: boolean;
|
silence?: boolean;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
export interface DakaoResultSubjects {
|
||||||
|
succeeded: string;
|
||||||
|
failed: string;
|
||||||
|
}
|
||||||
|
|
||||||
export interface DakaoCommandEnvelope<TPayload> {
|
export interface DakaoCommandEnvelope<TPayload> {
|
||||||
schema: 'dakao.command.v1';
|
schema: 'dakao.command.v1';
|
||||||
id: string;
|
eventId: string;
|
||||||
type: string;
|
type: string;
|
||||||
source: string;
|
eventSource: string;
|
||||||
correlationId?: string;
|
correlationId?: string;
|
||||||
replySubject?: string;
|
resultSubjects?: DakaoResultSubjects;
|
||||||
payload: TPayload;
|
payload: TPayload;
|
||||||
}
|
}
|
||||||
|
|
||||||
// ===== Reply =====
|
// ===== Result =====
|
||||||
export interface DakaoResultEnvelope {
|
export interface DakaoResultEnvelope {
|
||||||
schema: 'dakao.result.v1';
|
schema: 'dakao.result.v1';
|
||||||
id: string;
|
eventId: string;
|
||||||
|
requestEventId: string;
|
||||||
type: string;
|
type: string;
|
||||||
|
eventSource: string;
|
||||||
|
requestEventSource: string;
|
||||||
status: 'succeeded' | 'failed';
|
status: 'succeeded' | 'failed';
|
||||||
handledAt: string;
|
handledAt: string;
|
||||||
correlationId?: string;
|
correlationId?: string;
|
||||||
|
providerMessageId?: string;
|
||||||
result?: Record<string, unknown>;
|
result?: Record<string, unknown>;
|
||||||
error?: {
|
error?: {
|
||||||
code: string;
|
code: string;
|
||||||
|
|||||||
@@ -7,7 +7,7 @@ describe('resolveMongoDbName', () => {
|
|||||||
|
|
||||||
it('uses isolated databases for non-production environments', () => {
|
it('uses isolated databases for non-production environments', () => {
|
||||||
expect(resolveMongoDbName('dev')).toBe('lolympus-dev');
|
expect(resolveMongoDbName('dev')).toBe('lolympus-dev');
|
||||||
expect(resolveMongoDbName('test')).toBe('lolympus-test');
|
expect(resolveMongoDbName('staging')).toBe('lolympus-staging');
|
||||||
expect(resolveMongoDbName('')).toBe('lolympus-dev');
|
expect(resolveMongoDbName('')).toBe('lolympus-dev');
|
||||||
expect(resolveMongoDbName('local')).toBe('lolympus-dev');
|
expect(resolveMongoDbName('local')).toBe('lolympus-dev');
|
||||||
});
|
});
|
||||||
|
|||||||
@@ -5,7 +5,7 @@ import { tournamentModelDefinitions, TournamentModelTypes } from './schema';
|
|||||||
|
|
||||||
export function resolveMongoDbName(nodeEnv = process.env.NODE_ENV): string {
|
export function resolveMongoDbName(nodeEnv = process.env.NODE_ENV): string {
|
||||||
if (nodeEnv === 'prod') return 'lolympus';
|
if (nodeEnv === 'prod') return 'lolympus';
|
||||||
if (nodeEnv === 'test') return 'lolympus-test';
|
if (nodeEnv === 'staging') return 'lolympus-staging';
|
||||||
return 'lolympus-dev';
|
return 'lolympus-dev';
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -121,7 +121,7 @@ function parseArgs(argv: string[]): CliOptions {
|
|||||||
}
|
}
|
||||||
if (arg === '--env') {
|
if (arg === '--env') {
|
||||||
const value = argv[index + 1];
|
const value = argv[index + 1];
|
||||||
if (!value) throw new Error('--env requires dev, test, or prod.');
|
if (!value) throw new Error('--env requires dev, staging, or prod.');
|
||||||
nodeEnv = value;
|
nodeEnv = value;
|
||||||
index += 1;
|
index += 1;
|
||||||
continue;
|
continue;
|
||||||
@@ -164,8 +164,8 @@ function parseArgs(argv: string[]): CliOptions {
|
|||||||
throw new Error(`Unknown argument: ${arg}`);
|
throw new Error(`Unknown argument: ${arg}`);
|
||||||
}
|
}
|
||||||
|
|
||||||
if (!['dev', 'test', 'prod'].includes(nodeEnv)) {
|
if (!['dev', 'staging', 'prod'].includes(nodeEnv)) {
|
||||||
throw new Error('--env must be dev, test, or prod.');
|
throw new Error('--env must be dev, staging, or prod.');
|
||||||
}
|
}
|
||||||
if (communityId && !mongoose.isValidObjectId(communityId)) {
|
if (communityId && !mongoose.isValidObjectId(communityId)) {
|
||||||
throw new Error('--community-id must be a valid ObjectId.');
|
throw new Error('--community-id must be a valid ObjectId.');
|
||||||
@@ -198,7 +198,7 @@ function printHelp() {
|
|||||||
Options:
|
Options:
|
||||||
--community-id <id> Community ObjectId. Defaults to env or first community.
|
--community-id <id> Community ObjectId. Defaults to env or first community.
|
||||||
--dry-run Print the payload summary without writing.
|
--dry-run Print the payload summary without writing.
|
||||||
--env <env> dev | test | prod. Default: NODE_ENV or dev.
|
--env <env> dev | staging | prod. Default: NODE_ENV or dev.
|
||||||
--replace Delete previous temp matches created by this script first.
|
--replace Delete previous temp matches created by this script first.
|
||||||
--scheduled-at <iso> Scheduled date. Default: yesterday.
|
--scheduled-at <iso> Scheduled date. Default: yesterday.
|
||||||
--title <title> Match title. Default: timestamped temp title.
|
--title <title> Match title. Default: timestamped temp title.
|
||||||
|
|||||||
@@ -71,7 +71,7 @@ function parseArgs(argv: string[]): CliOptions {
|
|||||||
}
|
}
|
||||||
if (arg === '--env') {
|
if (arg === '--env') {
|
||||||
const value = argv[index + 1];
|
const value = argv[index + 1];
|
||||||
if (!value) throw new Error('--env requires dev, test, or prod.');
|
if (!value) throw new Error('--env requires dev, staging, or prod.');
|
||||||
nodeEnv = value;
|
nodeEnv = value;
|
||||||
index += 1;
|
index += 1;
|
||||||
continue;
|
continue;
|
||||||
@@ -104,8 +104,8 @@ function parseArgs(argv: string[]): CliOptions {
|
|||||||
throw new Error(`Unknown argument: ${arg}`);
|
throw new Error(`Unknown argument: ${arg}`);
|
||||||
}
|
}
|
||||||
|
|
||||||
if (!['dev', 'test', 'prod'].includes(nodeEnv)) {
|
if (!['dev', 'staging', 'prod'].includes(nodeEnv)) {
|
||||||
throw new Error('--env must be dev, test, or prod.');
|
throw new Error('--env must be dev, staging, or prod.');
|
||||||
}
|
}
|
||||||
|
|
||||||
return { dryRun, limit, nodeEnv };
|
return { dryRun, limit, nodeEnv };
|
||||||
@@ -119,7 +119,7 @@ function printHelp() {
|
|||||||
Options:
|
Options:
|
||||||
--dry-run Print affected counts without writing. Default.
|
--dry-run Print affected counts without writing. Default.
|
||||||
--apply Apply updates to MongoDB.
|
--apply Apply updates to MongoDB.
|
||||||
--env <env> dev | test | prod. Default: NODE_ENV or dev.
|
--env <env> dev | staging | prod. Default: NODE_ENV or dev.
|
||||||
--limit <n> Only scan first n documents per collection.
|
--limit <n> Only scan first n documents per collection.
|
||||||
`);
|
`);
|
||||||
}
|
}
|
||||||
@@ -130,7 +130,9 @@ async function main() {
|
|||||||
await loadMachineEnvFile();
|
await loadMachineEnvFile();
|
||||||
|
|
||||||
const app = await NestFactory.createApplicationContext(MigrationModule, {
|
const app = await NestFactory.createApplicationContext(MigrationModule, {
|
||||||
logger: options.dryRun ? ['log', 'warn', 'error'] : ['log', 'warn', 'error'],
|
logger: options.dryRun
|
||||||
|
? ['log', 'warn', 'error']
|
||||||
|
: ['log', 'warn', 'error'],
|
||||||
});
|
});
|
||||||
|
|
||||||
try {
|
try {
|
||||||
@@ -145,14 +147,19 @@ async function main() {
|
|||||||
`[team-index migration] mode=${options.dryRun ? 'dry-run' : 'apply'} env=${options.nodeEnv} db=${dbName}`,
|
`[team-index migration] mode=${options.dryRun ? 'dry-run' : 'apply'} env=${options.nodeEnv} db=${dbName}`,
|
||||||
);
|
);
|
||||||
|
|
||||||
const stats = await migrateTeamIndex(connection.db as Parameters<typeof migrateTeamIndex>[0], {
|
const stats = await migrateTeamIndex(
|
||||||
dryRun: options.dryRun,
|
connection.db as Parameters<typeof migrateTeamIndex>[0],
|
||||||
limit: options.limit,
|
{
|
||||||
});
|
dryRun: options.dryRun,
|
||||||
|
limit: options.limit,
|
||||||
|
},
|
||||||
|
);
|
||||||
|
|
||||||
console.table(stats);
|
console.table(stats);
|
||||||
if (options.dryRun) {
|
if (options.dryRun) {
|
||||||
console.log('No writes were made. Re-run with --apply to update documents.');
|
console.log(
|
||||||
|
'No writes were made. Re-run with --apply to update documents.',
|
||||||
|
);
|
||||||
} else {
|
} else {
|
||||||
console.log('Migration applied.');
|
console.log('Migration applied.');
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -91,6 +91,39 @@ export const LEGACY_IMPORT_JOB_STATES: LegacyImportJobState[] = [
|
|||||||
'cancelled',
|
'cancelled',
|
||||||
];
|
];
|
||||||
|
|
||||||
|
export type DakaoInboundMessageState = 'processing' | 'processed' | 'failed';
|
||||||
|
export const DAKAO_INBOUND_MESSAGE_STATES: DakaoInboundMessageState[] = [
|
||||||
|
'processing',
|
||||||
|
'processed',
|
||||||
|
'failed',
|
||||||
|
];
|
||||||
|
|
||||||
|
export type DakaoOutboxMessageState =
|
||||||
|
| 'pending'
|
||||||
|
| 'published'
|
||||||
|
| 'sent'
|
||||||
|
| 'failed';
|
||||||
|
export const DAKAO_OUTBOX_MESSAGE_STATES: DakaoOutboxMessageState[] = [
|
||||||
|
'pending',
|
||||||
|
'published',
|
||||||
|
'sent',
|
||||||
|
'failed',
|
||||||
|
];
|
||||||
|
|
||||||
|
export type DakaoOutboxCompensationState =
|
||||||
|
| 'none'
|
||||||
|
| 'pending'
|
||||||
|
| 'completed'
|
||||||
|
| 'failed';
|
||||||
|
export const DAKAO_OUTBOX_COMPENSATION_STATES: DakaoOutboxCompensationState[] =
|
||||||
|
['none', 'pending', 'completed', 'failed'];
|
||||||
|
|
||||||
|
export type DakaoSendResultStatus = 'succeeded' | 'failed';
|
||||||
|
export const DAKAO_SEND_RESULT_STATUSES: DakaoSendResultStatus[] = [
|
||||||
|
'succeeded',
|
||||||
|
'failed',
|
||||||
|
];
|
||||||
|
|
||||||
export interface PositionPreference {
|
export interface PositionPreference {
|
||||||
position: Position;
|
position: Position;
|
||||||
priority: number; // 1 = 최우선, 숫자가 작을수록 높은 우선순위
|
priority: number; // 1 = 최우선, 숫자가 작을수록 높은 우선순위
|
||||||
@@ -300,9 +333,57 @@ export interface CommunitySeason {
|
|||||||
endedAt?: Date;
|
endedAt?: Date;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
export interface DakaoInboundMessage extends MongooseTimestamps {
|
||||||
|
consumer: string;
|
||||||
|
projectId: string;
|
||||||
|
chatLogLogId: string;
|
||||||
|
state: DakaoInboundMessageState;
|
||||||
|
attempts: number;
|
||||||
|
processingExpiresAt?: Date;
|
||||||
|
processedAt?: Date;
|
||||||
|
lastError?: string;
|
||||||
|
}
|
||||||
|
|
||||||
|
export interface DakaoOutboxMessage extends MongooseTimestamps {
|
||||||
|
eventId: string;
|
||||||
|
eventSource: string;
|
||||||
|
type: string;
|
||||||
|
subject: string;
|
||||||
|
correlationId?: string;
|
||||||
|
payload: Record<string, unknown>;
|
||||||
|
state: DakaoOutboxMessageState;
|
||||||
|
publishAttempts: number;
|
||||||
|
publishedAt?: Date;
|
||||||
|
terminalAt?: Date;
|
||||||
|
resultEventId?: string;
|
||||||
|
lastPublishError?: string;
|
||||||
|
terminalError?: Record<string, unknown>;
|
||||||
|
compensationState: DakaoOutboxCompensationState;
|
||||||
|
compensationReason?: string;
|
||||||
|
}
|
||||||
|
|
||||||
|
export interface DakaoSendResultEvent extends MongooseTimestamps {
|
||||||
|
eventId: string;
|
||||||
|
requestEventId: string;
|
||||||
|
eventSource: string;
|
||||||
|
requestEventSource: string;
|
||||||
|
type: string;
|
||||||
|
subject: string;
|
||||||
|
status: DakaoSendResultStatus;
|
||||||
|
correlationId?: string;
|
||||||
|
handledAt: Date;
|
||||||
|
providerMessageId?: string;
|
||||||
|
error?: Record<string, unknown>;
|
||||||
|
raw: Record<string, unknown>;
|
||||||
|
}
|
||||||
|
|
||||||
export type CommunityDocument = HydratedDocument<Community>;
|
export type CommunityDocument = HydratedDocument<Community>;
|
||||||
export type IdentityDocument = HydratedDocument<Identity>;
|
export type IdentityDocument = HydratedDocument<Identity>;
|
||||||
export type UserDocument = HydratedDocument<User>;
|
export type UserDocument = HydratedDocument<User>;
|
||||||
|
export type DakaoInboundMessageDocument = HydratedDocument<DakaoInboundMessage>;
|
||||||
|
export type DakaoOutboxMessageDocument = HydratedDocument<DakaoOutboxMessage>;
|
||||||
|
export type DakaoSendResultEventDocument =
|
||||||
|
HydratedDocument<DakaoSendResultEvent>;
|
||||||
|
|
||||||
// ===== RiotMatch (Riot 원본 매치 아카이브) =====
|
// ===== RiotMatch (Riot 원본 매치 아카이브) =====
|
||||||
export interface RiotMatch extends MongooseTimestamps {
|
export interface RiotMatch extends MongooseTimestamps {
|
||||||
@@ -844,6 +925,98 @@ CommunitySchema.index({ name: 1 }, { unique: true });
|
|||||||
CommunitySchema.index({ ownerUserId: 1 });
|
CommunitySchema.index({ ownerUserId: 1 });
|
||||||
CommunitySchema.index({ dakaoProjectId: 1 }, { unique: true, sparse: true });
|
CommunitySchema.index({ dakaoProjectId: 1 }, { unique: true, sparse: true });
|
||||||
|
|
||||||
|
export const DakaoInboundMessageSchema = new Schema<DakaoInboundMessage>(
|
||||||
|
{
|
||||||
|
consumer: { type: String, required: true },
|
||||||
|
projectId: { type: String, required: true },
|
||||||
|
chatLogLogId: { type: String, required: true },
|
||||||
|
state: {
|
||||||
|
type: String,
|
||||||
|
enum: DAKAO_INBOUND_MESSAGE_STATES,
|
||||||
|
required: true,
|
||||||
|
default: 'processing',
|
||||||
|
},
|
||||||
|
attempts: { type: Number, required: true, default: 1 },
|
||||||
|
processingExpiresAt: { type: Date },
|
||||||
|
processedAt: { type: Date },
|
||||||
|
lastError: { type: String },
|
||||||
|
},
|
||||||
|
{
|
||||||
|
timestamps: true,
|
||||||
|
collection: 'dakao_inbound_message',
|
||||||
|
},
|
||||||
|
);
|
||||||
|
|
||||||
|
DakaoInboundMessageSchema.index(
|
||||||
|
{ consumer: 1, projectId: 1, chatLogLogId: 1 },
|
||||||
|
{ unique: true },
|
||||||
|
);
|
||||||
|
DakaoInboundMessageSchema.index({ state: 1, processingExpiresAt: 1 });
|
||||||
|
|
||||||
|
export const DakaoOutboxMessageSchema = new Schema<DakaoOutboxMessage>(
|
||||||
|
{
|
||||||
|
eventId: { type: String, required: true, unique: true },
|
||||||
|
eventSource: { type: String, required: true },
|
||||||
|
type: { type: String, required: true },
|
||||||
|
subject: { type: String, required: true },
|
||||||
|
correlationId: { type: String },
|
||||||
|
payload: { type: Schema.Types.Mixed, required: true },
|
||||||
|
state: {
|
||||||
|
type: String,
|
||||||
|
enum: DAKAO_OUTBOX_MESSAGE_STATES,
|
||||||
|
required: true,
|
||||||
|
default: 'pending',
|
||||||
|
},
|
||||||
|
publishAttempts: { type: Number, required: true, default: 0 },
|
||||||
|
publishedAt: { type: Date },
|
||||||
|
terminalAt: { type: Date },
|
||||||
|
resultEventId: { type: String },
|
||||||
|
lastPublishError: { type: String },
|
||||||
|
terminalError: { type: Schema.Types.Mixed },
|
||||||
|
compensationState: {
|
||||||
|
type: String,
|
||||||
|
enum: DAKAO_OUTBOX_COMPENSATION_STATES,
|
||||||
|
required: true,
|
||||||
|
default: 'none',
|
||||||
|
},
|
||||||
|
compensationReason: { type: String },
|
||||||
|
},
|
||||||
|
{
|
||||||
|
timestamps: true,
|
||||||
|
collection: 'dakao_outbox_message',
|
||||||
|
},
|
||||||
|
);
|
||||||
|
|
||||||
|
DakaoOutboxMessageSchema.index({ state: 1, updatedAt: 1 });
|
||||||
|
DakaoOutboxMessageSchema.index({ correlationId: 1 }, { sparse: true });
|
||||||
|
|
||||||
|
export const DakaoSendResultEventSchema = new Schema<DakaoSendResultEvent>(
|
||||||
|
{
|
||||||
|
eventId: { type: String, required: true, unique: true },
|
||||||
|
requestEventId: { type: String, required: true },
|
||||||
|
eventSource: { type: String, required: true },
|
||||||
|
requestEventSource: { type: String, required: true },
|
||||||
|
type: { type: String, required: true },
|
||||||
|
subject: { type: String, required: true },
|
||||||
|
status: {
|
||||||
|
type: String,
|
||||||
|
enum: DAKAO_SEND_RESULT_STATUSES,
|
||||||
|
required: true,
|
||||||
|
},
|
||||||
|
correlationId: { type: String },
|
||||||
|
handledAt: { type: Date, required: true },
|
||||||
|
providerMessageId: { type: String },
|
||||||
|
error: { type: Schema.Types.Mixed },
|
||||||
|
raw: { type: Schema.Types.Mixed, required: true },
|
||||||
|
},
|
||||||
|
{
|
||||||
|
timestamps: true,
|
||||||
|
collection: 'dakao_send_result_event',
|
||||||
|
},
|
||||||
|
);
|
||||||
|
|
||||||
|
DakaoSendResultEventSchema.index({ requestEventId: 1, status: 1 });
|
||||||
|
|
||||||
export const UserSchema = new Schema<User>(
|
export const UserSchema = new Schema<User>(
|
||||||
{
|
{
|
||||||
communityId: {
|
communityId: {
|
||||||
@@ -1519,6 +1692,21 @@ MatchStatsSchema.index({ communityId: 1, championIds: 1 });
|
|||||||
export const tournamentModelDefinitions: ModelDefinition[] = [
|
export const tournamentModelDefinitions: ModelDefinition[] = [
|
||||||
{ name: 'identity', schema: IdentitySchema, schemaName: 'identity' },
|
{ name: 'identity', schema: IdentitySchema, schemaName: 'identity' },
|
||||||
{ name: 'community', schema: CommunitySchema, schemaName: 'community' },
|
{ name: 'community', schema: CommunitySchema, schemaName: 'community' },
|
||||||
|
{
|
||||||
|
name: 'dakaoInboundMessage',
|
||||||
|
schema: DakaoInboundMessageSchema,
|
||||||
|
schemaName: 'dakaoInboundMessage',
|
||||||
|
},
|
||||||
|
{
|
||||||
|
name: 'dakaoOutboxMessage',
|
||||||
|
schema: DakaoOutboxMessageSchema,
|
||||||
|
schemaName: 'dakaoOutboxMessage',
|
||||||
|
},
|
||||||
|
{
|
||||||
|
name: 'dakaoSendResultEvent',
|
||||||
|
schema: DakaoSendResultEventSchema,
|
||||||
|
schemaName: 'dakaoSendResultEvent',
|
||||||
|
},
|
||||||
{ name: 'user', schema: UserSchema, schemaName: 'user' },
|
{ name: 'user', schema: UserSchema, schemaName: 'user' },
|
||||||
{ name: 'riotMatch', schema: RiotMatchSchema, schemaName: 'riotMatch' },
|
{ name: 'riotMatch', schema: RiotMatchSchema, schemaName: 'riotMatch' },
|
||||||
{
|
{
|
||||||
@@ -1548,6 +1736,9 @@ export const tournamentModelDefinitions: ModelDefinition[] = [
|
|||||||
export interface TournamentModelTypes {
|
export interface TournamentModelTypes {
|
||||||
identity: Identity;
|
identity: Identity;
|
||||||
community: Community;
|
community: Community;
|
||||||
|
dakaoInboundMessage: DakaoInboundMessage;
|
||||||
|
dakaoOutboxMessage: DakaoOutboxMessage;
|
||||||
|
dakaoSendResultEvent: DakaoSendResultEvent;
|
||||||
user: User;
|
user: User;
|
||||||
riotMatch: RiotMatch;
|
riotMatch: RiotMatch;
|
||||||
tournamentCode: TournamentCode;
|
tournamentCode: TournamentCode;
|
||||||
|
|||||||
@@ -1,11 +1,21 @@
|
|||||||
|
import { Inject, Injectable, Logger, OnModuleDestroy } from '@nestjs/common';
|
||||||
import {
|
import {
|
||||||
Inject,
|
AckPolicy,
|
||||||
Injectable,
|
DeliverPolicy,
|
||||||
Logger,
|
nanos,
|
||||||
OnModuleDestroy,
|
ReplayPolicy,
|
||||||
} from '@nestjs/common';
|
RetentionPolicy,
|
||||||
import { StringCodec } from 'nats';
|
StorageType,
|
||||||
import type { Msg, NatsConnection, Subscription } from 'nats';
|
StringCodec,
|
||||||
|
} from 'nats';
|
||||||
|
import type {
|
||||||
|
ConsumerMessages,
|
||||||
|
JsMsg,
|
||||||
|
Msg,
|
||||||
|
NatsConnection,
|
||||||
|
PubAck,
|
||||||
|
Subscription,
|
||||||
|
} from 'nats';
|
||||||
import { NATS_CLIENT } from './nats.constants';
|
import { NATS_CLIENT } from './nats.constants';
|
||||||
|
|
||||||
export type NatsMessageHandler<T = unknown> = (
|
export type NatsMessageHandler<T = unknown> = (
|
||||||
@@ -13,17 +23,62 @@ export type NatsMessageHandler<T = unknown> = (
|
|||||||
msg: Msg,
|
msg: Msg,
|
||||||
) => Promise<void> | void;
|
) => Promise<void> | void;
|
||||||
|
|
||||||
|
export type JetStreamRetention = 'workqueue' | 'limits';
|
||||||
|
|
||||||
|
export type JetStreamMessageHandler<T = unknown> = (
|
||||||
|
data: T,
|
||||||
|
msg: JsMsg,
|
||||||
|
) => Promise<void> | void;
|
||||||
|
|
||||||
|
export interface JetStreamStreamOptions {
|
||||||
|
name: string;
|
||||||
|
subjects: string[];
|
||||||
|
retention: JetStreamRetention;
|
||||||
|
maxAgeDays?: number;
|
||||||
|
replicas?: number;
|
||||||
|
}
|
||||||
|
|
||||||
|
export interface JetStreamConsumerOptions {
|
||||||
|
stream: string;
|
||||||
|
durable: string;
|
||||||
|
filterSubject: string;
|
||||||
|
description?: string;
|
||||||
|
ackWaitMs?: number;
|
||||||
|
backoffMs?: number[];
|
||||||
|
maxDeliver?: number;
|
||||||
|
maxAckPending?: number;
|
||||||
|
replicas?: number;
|
||||||
|
}
|
||||||
|
|
||||||
|
export interface JetStreamConsumeOptions {
|
||||||
|
stream: string;
|
||||||
|
durable: string;
|
||||||
|
maxMessages?: number;
|
||||||
|
expiresMs?: number;
|
||||||
|
nakDelayMs?: number;
|
||||||
|
}
|
||||||
|
|
||||||
@Injectable()
|
@Injectable()
|
||||||
export class NatsService implements OnModuleDestroy {
|
export class NatsService implements OnModuleDestroy {
|
||||||
private readonly logger = new Logger(NatsService.name);
|
private readonly logger = new Logger(NatsService.name);
|
||||||
private readonly stringCodec = StringCodec();
|
private readonly stringCodec = StringCodec();
|
||||||
private readonly subscriptions = new Set<Subscription>();
|
private readonly subscriptions = new Set<Subscription>();
|
||||||
|
private readonly jetStreamConsumers = new Set<ConsumerMessages>();
|
||||||
|
|
||||||
constructor(
|
constructor(
|
||||||
@Inject(NATS_CLIENT) private readonly natsClient: NatsConnection,
|
@Inject(NATS_CLIENT) private readonly natsClient: NatsConnection,
|
||||||
) {}
|
) {}
|
||||||
|
|
||||||
async onModuleDestroy() {
|
async onModuleDestroy() {
|
||||||
|
for (const consumer of this.jetStreamConsumers) {
|
||||||
|
try {
|
||||||
|
await consumer.close();
|
||||||
|
} catch {
|
||||||
|
// ignore
|
||||||
|
}
|
||||||
|
}
|
||||||
|
this.jetStreamConsumers.clear();
|
||||||
|
|
||||||
for (const sub of this.subscriptions) {
|
for (const sub of this.subscriptions) {
|
||||||
try {
|
try {
|
||||||
sub.unsubscribe();
|
sub.unsubscribe();
|
||||||
@@ -68,6 +123,108 @@ export class NatsService implements OnModuleDestroy {
|
|||||||
return sub;
|
return sub;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public async publishJetStream<T = unknown>(
|
||||||
|
subject: string,
|
||||||
|
data: T,
|
||||||
|
options?: { messageId?: string; timeoutMs?: number },
|
||||||
|
): Promise<PubAck> {
|
||||||
|
const ack = await this.natsClient
|
||||||
|
.jetstream()
|
||||||
|
.publish(subject, this.stringCodec.encode(JSON.stringify(data)), {
|
||||||
|
msgID: options?.messageId,
|
||||||
|
timeout: options?.timeoutMs ?? 5000,
|
||||||
|
});
|
||||||
|
return ack;
|
||||||
|
}
|
||||||
|
|
||||||
|
public async ensureJetStreamStream(
|
||||||
|
options: JetStreamStreamOptions,
|
||||||
|
): Promise<void> {
|
||||||
|
const manager = await this.natsClient.jetstreamManager();
|
||||||
|
const config = {
|
||||||
|
name: options.name,
|
||||||
|
subjects: options.subjects,
|
||||||
|
retention: this.toRetentionPolicy(options.retention),
|
||||||
|
storage: StorageType.File,
|
||||||
|
num_replicas: options.replicas ?? 1,
|
||||||
|
max_age: options.maxAgeDays
|
||||||
|
? nanos(options.maxAgeDays * 24 * 60 * 60 * 1000)
|
||||||
|
: 0,
|
||||||
|
};
|
||||||
|
|
||||||
|
try {
|
||||||
|
await manager.streams.info(options.name);
|
||||||
|
await manager.streams.update(options.name, {
|
||||||
|
subjects: config.subjects,
|
||||||
|
max_age: config.max_age,
|
||||||
|
num_replicas: config.num_replicas,
|
||||||
|
});
|
||||||
|
return;
|
||||||
|
} catch {
|
||||||
|
await manager.streams.add(config);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
public async ensureJetStreamConsumer(
|
||||||
|
options: JetStreamConsumerOptions,
|
||||||
|
): Promise<void> {
|
||||||
|
const manager = await this.natsClient.jetstreamManager();
|
||||||
|
const config = {
|
||||||
|
durable_name: options.durable,
|
||||||
|
name: options.durable,
|
||||||
|
description: options.description,
|
||||||
|
filter_subject: options.filterSubject,
|
||||||
|
deliver_policy: DeliverPolicy.All,
|
||||||
|
ack_policy: AckPolicy.Explicit,
|
||||||
|
replay_policy: ReplayPolicy.Instant,
|
||||||
|
ack_wait: nanos(options.ackWaitMs ?? 30000),
|
||||||
|
backoff: (options.backoffMs ?? [5000, 30000, 120000, 600000]).map(
|
||||||
|
(entry) => nanos(entry),
|
||||||
|
),
|
||||||
|
max_deliver: options.maxDeliver ?? 10,
|
||||||
|
max_ack_pending: options.maxAckPending ?? 50,
|
||||||
|
num_replicas: options.replicas ?? 1,
|
||||||
|
};
|
||||||
|
|
||||||
|
try {
|
||||||
|
await manager.consumers.info(options.stream, options.durable);
|
||||||
|
await manager.consumers.update(options.stream, options.durable, config);
|
||||||
|
return;
|
||||||
|
} catch {
|
||||||
|
await manager.consumers.add(options.stream, config);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
public async consumeJetStream<T = unknown>(
|
||||||
|
options: JetStreamConsumeOptions,
|
||||||
|
handler: JetStreamMessageHandler<T>,
|
||||||
|
): Promise<void> {
|
||||||
|
try {
|
||||||
|
const consumer = await this.natsClient
|
||||||
|
.jetstream()
|
||||||
|
.consumers.get(options.stream, options.durable);
|
||||||
|
const messages = await consumer.consume({
|
||||||
|
max_messages: options.maxMessages ?? 10,
|
||||||
|
expires: options.expiresMs ?? 30000,
|
||||||
|
});
|
||||||
|
this.jetStreamConsumers.add(messages);
|
||||||
|
|
||||||
|
try {
|
||||||
|
for await (const msg of messages) {
|
||||||
|
await this.dispatchJetStream(msg, handler, options);
|
||||||
|
}
|
||||||
|
} finally {
|
||||||
|
this.jetStreamConsumers.delete(messages);
|
||||||
|
}
|
||||||
|
} catch (err) {
|
||||||
|
this.logger.error(
|
||||||
|
`JetStream consumer stream="${options.stream}" durable="${options.durable}" terminated: ${
|
||||||
|
(err as Error)?.stack ?? err
|
||||||
|
}`,
|
||||||
|
);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
private async consumeSubscription<T>(
|
private async consumeSubscription<T>(
|
||||||
sub: Subscription,
|
sub: Subscription,
|
||||||
handler: NatsMessageHandler<T>,
|
handler: NatsMessageHandler<T>,
|
||||||
@@ -114,4 +271,46 @@ export class NatsService implements OnModuleDestroy {
|
|||||||
);
|
);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
private async dispatchJetStream<T>(
|
||||||
|
msg: JsMsg,
|
||||||
|
handler: JetStreamMessageHandler<T>,
|
||||||
|
options: JetStreamConsumeOptions,
|
||||||
|
): Promise<void> {
|
||||||
|
let data: T;
|
||||||
|
try {
|
||||||
|
data = JSON.parse(msg.string()) as T;
|
||||||
|
} catch (err) {
|
||||||
|
this.logger.warn(
|
||||||
|
`Terminating undecodable JetStream message subject="${msg.subject}" stream="${options.stream}": ${
|
||||||
|
(err as Error)?.message ?? err
|
||||||
|
}`,
|
||||||
|
);
|
||||||
|
msg.term('invalid_json');
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
try {
|
||||||
|
await handler(data, msg);
|
||||||
|
const acked = await msg.ackAck({ timeout: 5000 });
|
||||||
|
if (!acked) {
|
||||||
|
this.logger.warn(
|
||||||
|
`JetStream ack was not confirmed subject="${msg.subject}" stream="${options.stream}" seq=${msg.seq}`,
|
||||||
|
);
|
||||||
|
}
|
||||||
|
} catch (err) {
|
||||||
|
this.logger.error(
|
||||||
|
`JetStream handler error subject="${msg.subject}" stream="${options.stream}" seq=${msg.seq}: ${
|
||||||
|
(err as Error)?.stack ?? err
|
||||||
|
}`,
|
||||||
|
);
|
||||||
|
msg.nak(options.nakDelayMs ?? 5000);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
private toRetentionPolicy(retention: JetStreamRetention): RetentionPolicy {
|
||||||
|
return retention === 'workqueue'
|
||||||
|
? RetentionPolicy.Workqueue
|
||||||
|
: RetentionPolicy.Limits;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
@@ -84,7 +84,7 @@ export class SecretService {
|
|||||||
environment:
|
environment:
|
||||||
process.env.NODE_ENV === 'prod'
|
process.env.NODE_ENV === 'prod'
|
||||||
? 'prod'
|
? 'prod'
|
||||||
: process.env.NODE_ENV === 'test'
|
: process.env.NODE_ENV === 'staging'
|
||||||
? 'staging'
|
? 'staging'
|
||||||
: 'dev',
|
: 'dev',
|
||||||
projectId: bootstrapConfig.projectId,
|
projectId: bootstrapConfig.projectId,
|
||||||
|
|||||||
@@ -1,4 +1,4 @@
|
|||||||
VITE_API_BASE_URL=https://lolympus-test-api.dahyeon.us
|
VITE_API_BASE_URL=https://lolympus-api-staging.dahyeon.us
|
||||||
VITE_DEFAULT_COMMUNITY_ID=69ebe0adf627c11be2f020a1
|
VITE_DEFAULT_COMMUNITY_ID=69ebe0adf627c11be2f020a1
|
||||||
VITE_MSAL_CLIENT_ID=15fb5a20-47e0-44fa-9a41-b50ca9d0e7e3
|
VITE_MSAL_CLIENT_ID=15fb5a20-47e0-44fa-9a41-b50ca9d0e7e3
|
||||||
VITE_MSAL_AUTHORITY=https://login.microsoftonline.com/5ef88004-e953-4b04-93db-64bbf49c6863
|
VITE_MSAL_AUTHORITY=https://login.microsoftonline.com/5ef88004-e953-4b04-93db-64bbf49c6863
|
||||||
@@ -6,9 +6,9 @@
|
|||||||
"scripts": {
|
"scripts": {
|
||||||
"dev": "vite",
|
"dev": "vite",
|
||||||
"build": "tsc -b && vite build",
|
"build": "tsc -b && vite build",
|
||||||
"build:test": "tsc -b && vite build --mode test",
|
"build:staging": "tsc -b && vite build --mode staging",
|
||||||
"build:prod": "tsc -b && vite build --mode production",
|
"build:prod": "tsc -b && vite build --mode production",
|
||||||
"deploy:test": "node ../../scripts/deploy-cloudflare-pages.mjs test",
|
"deploy:staging": "node ../../scripts/deploy-cloudflare-pages.mjs staging",
|
||||||
"deploy:prod": "node ../../scripts/deploy-cloudflare-pages.mjs prod",
|
"deploy:prod": "node ../../scripts/deploy-cloudflare-pages.mjs prod",
|
||||||
"pages:login": "wrangler login",
|
"pages:login": "wrangler login",
|
||||||
"pages:whoami": "wrangler whoami",
|
"pages:whoami": "wrangler whoami",
|
||||||
|
|||||||
+22
-22
@@ -1,22 +1,22 @@
|
|||||||
import { BrowserRouter, Link, Route, Routes } from 'react-router';
|
import { BrowserRouter, Link, Route, Routes } from "react-router";
|
||||||
import { useMsal } from '@azure/msal-react';
|
import { useMsal } from "@azure/msal-react";
|
||||||
import DevAuthSwitcher from './components/DevAuthSwitcher';
|
import DevAuthSwitcher from "./components/DevAuthSwitcher";
|
||||||
import { DEFAULT_COMMUNITY_ID } from './config';
|
import { DEFAULT_COMMUNITY_ID } from "./config";
|
||||||
import { useCommunity } from './api/hooks';
|
import { useCommunity } from "./api/hooks";
|
||||||
import { isMsalConfigured } from './lib/auth/msal.config';
|
import { isMsalConfigured } from "./lib/auth/msal.config";
|
||||||
import AuthCallbackPage from './pages/AuthCallback';
|
import AuthCallbackPage from "./pages/AuthCallback";
|
||||||
import HomePage from './pages/HomePage';
|
import HomePage from "./pages/HomePage";
|
||||||
import LoginPage from './pages/Login';
|
import LoginPage from "./pages/Login";
|
||||||
import MatchDetailPage from './pages/MatchDetailPage';
|
import MatchDetailPage from "./pages/MatchDetailPage";
|
||||||
import MatchTeamProgressPage from './pages/MatchTeamProgressPage';
|
import MatchTeamProgressPage from "./pages/MatchTeamProgressPage";
|
||||||
import AuctionPage from './pages/AuctionPage';
|
import AuctionPage from "./pages/AuctionPage";
|
||||||
import UserHistoryPage from './pages/UserHistoryPage';
|
import UserHistoryPage from "./pages/UserHistoryPage";
|
||||||
import CommunityInternalStatsPage from './pages/CommunityInternalStatsPage';
|
import CommunityInternalStatsPage from "./pages/CommunityInternalStatsPage";
|
||||||
import InternalMatchDetailPage from './pages/InternalMatchDetailPage';
|
import InternalMatchDetailPage from "./pages/InternalMatchDetailPage";
|
||||||
import LegacyImportPage from './pages/admin/LegacyImportPage';
|
import LegacyImportPage from "./pages/admin/LegacyImportPage";
|
||||||
import LegacyMatchesPage from './pages/admin/LegacyMatchesPage';
|
import LegacyMatchesPage from "./pages/admin/LegacyMatchesPage";
|
||||||
import MatchAdminPage from './pages/admin/MatchAdminPage';
|
import MatchAdminPage from "./pages/admin/MatchAdminPage";
|
||||||
import TournamentCodesPage from './pages/admin/TournamentCodesPage';
|
import TournamentCodesPage from "./pages/admin/TournamentCodesPage";
|
||||||
|
|
||||||
function MsalHeaderActions() {
|
function MsalHeaderActions() {
|
||||||
const { accounts, instance } = useMsal();
|
const { accounts, instance } = useMsal();
|
||||||
@@ -43,7 +43,7 @@ function MsalHeaderActions() {
|
|||||||
|
|
||||||
function App() {
|
function App() {
|
||||||
const communityQuery = useCommunity(DEFAULT_COMMUNITY_ID);
|
const communityQuery = useCommunity(DEFAULT_COMMUNITY_ID);
|
||||||
const communityLabel = communityQuery.data?.name ?? 'LOLYMPUS';
|
const communityLabel = communityQuery.data?.name ?? "LOLYMPUS";
|
||||||
|
|
||||||
return (
|
return (
|
||||||
<BrowserRouter>
|
<BrowserRouter>
|
||||||
@@ -57,7 +57,7 @@ function App() {
|
|||||||
{communityLabel}
|
{communityLabel}
|
||||||
</Link>
|
</Link>
|
||||||
<div className="flex items-center gap-3">
|
<div className="flex items-center gap-3">
|
||||||
{DEFAULT_COMMUNITY_ID ? (
|
{/* {DEFAULT_COMMUNITY_ID ? (
|
||||||
<span className="text-[10px] uppercase tracking-wider text-base-content/25">
|
<span className="text-[10px] uppercase tracking-wider text-base-content/25">
|
||||||
API Live
|
API Live
|
||||||
</span>
|
</span>
|
||||||
@@ -65,7 +65,7 @@ function App() {
|
|||||||
<span className="text-[10px] uppercase tracking-wider text-amber-500/70">
|
<span className="text-[10px] uppercase tracking-wider text-amber-500/70">
|
||||||
Set `VITE_DEFAULT_COMMUNITY_ID`
|
Set `VITE_DEFAULT_COMMUNITY_ID`
|
||||||
</span>
|
</span>
|
||||||
)}
|
)} */}
|
||||||
{isMsalConfigured && <MsalHeaderActions />}
|
{isMsalConfigured && <MsalHeaderActions />}
|
||||||
</div>
|
</div>
|
||||||
</div>
|
</div>
|
||||||
|
|||||||
+15
-1
@@ -1,4 +1,5 @@
|
|||||||
import { API_BASE_URL, getAuthUserId } from '../config';
|
import { API_BASE_URL, getAuthUserId } from '../config';
|
||||||
|
import { acquireAccessToken } from './auth/token-provider';
|
||||||
|
|
||||||
export class ApiError extends Error {
|
export class ApiError extends Error {
|
||||||
readonly data: unknown;
|
readonly data: unknown;
|
||||||
@@ -42,6 +43,14 @@ async function readResponseBody(response: Response): Promise<unknown> {
|
|||||||
return text ? text : null;
|
return text ? text : null;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
async function resolveAccessToken(): Promise<string | null> {
|
||||||
|
try {
|
||||||
|
return await acquireAccessToken();
|
||||||
|
} catch {
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
export async function apiFetch<T>(
|
export async function apiFetch<T>(
|
||||||
path: string,
|
path: string,
|
||||||
options: ApiFetchOptions = {},
|
options: ApiFetchOptions = {},
|
||||||
@@ -54,8 +63,13 @@ export async function apiFetch<T>(
|
|||||||
}
|
}
|
||||||
|
|
||||||
if (auth !== 'none' && !requestHeaders.has('Authorization')) {
|
if (auth !== 'none' && !requestHeaders.has('Authorization')) {
|
||||||
|
const accessToken = auth === 'dev' ? null : await resolveAccessToken();
|
||||||
|
if (accessToken) {
|
||||||
|
requestHeaders.set('Authorization', `Bearer ${accessToken}`);
|
||||||
|
}
|
||||||
|
|
||||||
const authUserId = getAuthUserId();
|
const authUserId = getAuthUserId();
|
||||||
if (authUserId) {
|
if (!accessToken && authUserId) {
|
||||||
requestHeaders.set('x-auth-user-id', authUserId);
|
requestHeaders.set('x-auth-user-id', authUserId);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -1073,7 +1073,7 @@ export default function AuctionPage() {
|
|||||||
</div>
|
</div>
|
||||||
|
|
||||||
<aside className="hidden xl:block">
|
<aside className="hidden xl:block">
|
||||||
<ChatPanel matchId={auctionState.matchId} />
|
<ChatPanel />
|
||||||
</aside>
|
</aside>
|
||||||
</div>
|
</div>
|
||||||
</main>
|
</main>
|
||||||
@@ -1231,7 +1231,7 @@ function PoolListItem({
|
|||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
function ChatPanel({ matchId: _matchId }: { matchId: string }) {
|
function ChatPanel() {
|
||||||
return (
|
return (
|
||||||
<div className="border border-base-content/10 bg-base-100 sticky top-4 h-[calc(100vh-2rem)] flex flex-col">
|
<div className="border border-base-content/10 bg-base-100 sticky top-4 h-[calc(100vh-2rem)] flex flex-col">
|
||||||
<div className="border-b border-base-content/10 px-3 py-2.5 flex items-center justify-between">
|
<div className="border-b border-base-content/10 px-3 py-2.5 flex items-center justify-between">
|
||||||
|
|||||||
+3
-3
@@ -5,14 +5,14 @@
|
|||||||
"main": "index.js",
|
"main": "index.js",
|
||||||
"scripts": {
|
"scripts": {
|
||||||
"start:api:dev": "pnpm --filter api start:dev",
|
"start:api:dev": "pnpm --filter api start:dev",
|
||||||
"start:api:test": "pnpm --filter api start:test",
|
"start:api:staging": "pnpm --filter api start:staging",
|
||||||
"start:api:prod": "pnpm --filter api start:prod",
|
"start:api:prod": "pnpm --filter api start:prod",
|
||||||
"start:api": "pnpm --filter api start:prod",
|
"start:api": "pnpm --filter api start:prod",
|
||||||
"build:api": "pnpm --filter api build",
|
"build:api": "pnpm --filter api build",
|
||||||
"dev:web": "pnpm --filter web dev",
|
"dev:web": "pnpm --filter web dev",
|
||||||
"build:web:test": "pnpm --filter web build:test",
|
"build:web:staging": "pnpm --filter web build:staging",
|
||||||
"build:web:prod": "pnpm --filter web build:prod",
|
"build:web:prod": "pnpm --filter web build:prod",
|
||||||
"deploy:web:test": "node scripts/deploy-cloudflare-pages.mjs test",
|
"deploy:web:staging": "node scripts/deploy-cloudflare-pages.mjs staging",
|
||||||
"deploy:web:prod": "node scripts/deploy-cloudflare-pages.mjs prod",
|
"deploy:web:prod": "node scripts/deploy-cloudflare-pages.mjs prod",
|
||||||
"test": "echo \"Error: no test specified\" && exit 1"
|
"test": "echo \"Error: no test specified\" && exit 1"
|
||||||
},
|
},
|
||||||
|
|||||||
@@ -28,7 +28,7 @@ const { MongoClient, ObjectId } = require("mongodb");
|
|||||||
|
|
||||||
const CONFIG = {
|
const CONFIG = {
|
||||||
MONGODB_URI: process.env.MONGODB_URI ?? "mongodb://127.0.0.1:27017",
|
MONGODB_URI: process.env.MONGODB_URI ?? "mongodb://127.0.0.1:27017",
|
||||||
LOLYMPUS_DB_NAME: process.env.LOLYMPUS_DB_NAME ?? "lolympus-test",
|
LOLYMPUS_DB_NAME: process.env.LOLYMPUS_DB_NAME ?? "lolympus-staging",
|
||||||
COMMUNITY_ID: new ObjectId(
|
COMMUNITY_ID: new ObjectId(
|
||||||
process.env.COMMUNITY_ID ?? "69ebe0adf627c11be2f020a1",
|
process.env.COMMUNITY_ID ?? "69ebe0adf627c11be2f020a1",
|
||||||
),
|
),
|
||||||
@@ -137,14 +137,18 @@ async function fetchRiotJson({ host, path, query }) {
|
|||||||
});
|
});
|
||||||
|
|
||||||
if (response.status === 429) {
|
if (response.status === 429) {
|
||||||
const retryAfterSeconds = Number(response.headers.get("retry-after") ?? 1);
|
const retryAfterSeconds = Number(
|
||||||
|
response.headers.get("retry-after") ?? 1,
|
||||||
|
);
|
||||||
await sleep(Math.max(1, retryAfterSeconds) * 1000);
|
await sleep(Math.max(1, retryAfterSeconds) * 1000);
|
||||||
continue;
|
continue;
|
||||||
}
|
}
|
||||||
if (response.status === 404) return null;
|
if (response.status === 404) return null;
|
||||||
if (!response.ok) {
|
if (!response.ok) {
|
||||||
const body = await response.text().catch(() => "");
|
const body = await response.text().catch(() => "");
|
||||||
throw new Error(`Riot API failed ${response.status} ${url.pathname}: ${body}`);
|
throw new Error(
|
||||||
|
`Riot API failed ${response.status} ${url.pathname}: ${body}`,
|
||||||
|
);
|
||||||
}
|
}
|
||||||
return response.json();
|
return response.json();
|
||||||
}
|
}
|
||||||
@@ -190,13 +194,14 @@ async function fetchRecentMatchIdsByPuuid(puuid) {
|
|||||||
|
|
||||||
async function fetchRiotData({ riotId }) {
|
async function fetchRiotData({ riotId }) {
|
||||||
const fallback = splitRiotId(riotId);
|
const fallback = splitRiotId(riotId);
|
||||||
const account =
|
const account = (await fetchAccountByRiotId(fallback)) ?? {
|
||||||
(await fetchAccountByRiotId(fallback)) ?? {
|
...fallback,
|
||||||
...fallback,
|
puuid: normalizeString(INPUT.puuid),
|
||||||
puuid: normalizeString(INPUT.puuid),
|
};
|
||||||
};
|
|
||||||
if (!account?.puuid) {
|
if (!account?.puuid) {
|
||||||
throw new Error(`Riot account not found and PUUID was not provided: ${riotId}`);
|
throw new Error(
|
||||||
|
`Riot account not found and PUUID was not provided: ${riotId}`,
|
||||||
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
const [summoner, leagueEntries, championMasteries, matchIds] =
|
const [summoner, leagueEntries, championMasteries, matchIds] =
|
||||||
@@ -280,7 +285,9 @@ function buildRiotAccount(riotData, now) {
|
|||||||
summonerLevel: riotData.summoner?.summonerLevel ?? 0,
|
summonerLevel: riotData.summoner?.summonerLevel ?? 0,
|
||||||
isPrimary: true,
|
isPrimary: true,
|
||||||
tiers: riotData.leagueEntries.map((entry) => toTierInfo(entry, now)),
|
tiers: riotData.leagueEntries.map((entry) => toTierInfo(entry, now)),
|
||||||
tierHistory: riotData.leagueEntries.map((entry) => toTierHistory(entry, now)),
|
tierHistory: riotData.leagueEntries.map((entry) =>
|
||||||
|
toTierHistory(entry, now),
|
||||||
|
),
|
||||||
matchIds: riotData.matchIds,
|
matchIds: riotData.matchIds,
|
||||||
matchIdsFetchedAt: riotData.matchIds.length > 0 ? now : undefined,
|
matchIdsFetchedAt: riotData.matchIds.length > 0 ? now : undefined,
|
||||||
championMasteries: riotData.championMasteries
|
championMasteries: riotData.championMasteries
|
||||||
|
|||||||
@@ -1,11 +1,11 @@
|
|||||||
/**
|
/**
|
||||||
* Apply already-saved Dakao user bootstrap data into lolympus-test.user.
|
* Apply already-saved Dakao user bootstrap data into lolympus-staging.user.
|
||||||
*
|
*
|
||||||
* This script does NOT call Riot API.
|
* This script does NOT call Riot API.
|
||||||
*
|
*
|
||||||
* It reads saved documents from one of:
|
* It reads saved documents from one of:
|
||||||
* 1. lolympus-test.DakaoUserBootstrapRun
|
* 1. lolympus-staging.DakaoUserBootstrapRun
|
||||||
* 2. lolympus-test.DakaoUserBootstrapCandidate
|
* 2. lolympus-staging.DakaoUserBootstrapCandidate
|
||||||
*
|
*
|
||||||
* It only applies users that already have a valid Riot identity:
|
* It only applies users that already have a valid Riot identity:
|
||||||
* - dryRunUsers[].doc.riotAccounts[0].puuid, or
|
* - dryRunUsers[].doc.riotAccounts[0].puuid, or
|
||||||
@@ -34,7 +34,7 @@ const { MongoClient, ObjectId } = require("mongodb");
|
|||||||
|
|
||||||
const CONFIG = {
|
const CONFIG = {
|
||||||
MONGODB_URI: process.env.MONGODB_URI ?? "mongodb://127.0.0.1:27017",
|
MONGODB_URI: process.env.MONGODB_URI ?? "mongodb://127.0.0.1:27017",
|
||||||
LOLYMPUS_DB_NAME: process.env.LOLYMPUS_DB_NAME ?? "lolympus-test",
|
LOLYMPUS_DB_NAME: process.env.LOLYMPUS_DB_NAME ?? "lolympus-staging",
|
||||||
COMMUNITY_ID: new ObjectId(
|
COMMUNITY_ID: new ObjectId(
|
||||||
process.env.COMMUNITY_ID ?? "69ebe0adf627c11be2f020a1",
|
process.env.COMMUNITY_ID ?? "69ebe0adf627c11be2f020a1",
|
||||||
),
|
),
|
||||||
@@ -89,8 +89,8 @@ function maskConfig() {
|
|||||||
function hasValidRiotAccount(account) {
|
function hasValidRiotAccount(account) {
|
||||||
return Boolean(
|
return Boolean(
|
||||||
normalizeString(account?.puuid) &&
|
normalizeString(account?.puuid) &&
|
||||||
normalizeString(account?.gameName) &&
|
normalizeString(account?.gameName) &&
|
||||||
normalizeString(account?.tagLine),
|
normalizeString(account?.tagLine),
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -151,14 +151,19 @@ function buildPlanFromDryRunUser(planned, now) {
|
|||||||
planned.filter?.communityId ?? doc.communityId ?? CONFIG.COMMUNITY_ID,
|
planned.filter?.communityId ?? doc.communityId ?? CONFIG.COMMUNITY_ID,
|
||||||
"planned.communityId",
|
"planned.communityId",
|
||||||
),
|
),
|
||||||
kakaoTalkId: normalizeString(planned.filter?.kakaoTalkId ?? doc.kakaoTalkId),
|
kakaoTalkId: normalizeString(
|
||||||
|
planned.filter?.kakaoTalkId ?? doc.kakaoTalkId,
|
||||||
|
),
|
||||||
},
|
},
|
||||||
doc: removeUndefined({
|
doc: removeUndefined({
|
||||||
_id:
|
_id:
|
||||||
doc._id && ObjectId.isValid(String(doc._id))
|
doc._id && ObjectId.isValid(String(doc._id))
|
||||||
? toObjectId(doc._id, "planned.doc._id")
|
? toObjectId(doc._id, "planned.doc._id")
|
||||||
: new ObjectId(),
|
: new ObjectId(),
|
||||||
communityId: toObjectId(doc.communityId ?? CONFIG.COMMUNITY_ID, "doc.communityId"),
|
communityId: toObjectId(
|
||||||
|
doc.communityId ?? CONFIG.COMMUNITY_ID,
|
||||||
|
"doc.communityId",
|
||||||
|
),
|
||||||
role: doc.role ?? "member",
|
role: doc.role ?? "member",
|
||||||
kakaoTalkId: normalizeString(doc.kakaoTalkId),
|
kakaoTalkId: normalizeString(doc.kakaoTalkId),
|
||||||
kakaoTalkNickname: normalizeString(doc.kakaoTalkNickname),
|
kakaoTalkNickname: normalizeString(doc.kakaoTalkNickname),
|
||||||
@@ -228,12 +233,18 @@ function buildPlanFromCandidate(candidate, now) {
|
|||||||
return {
|
return {
|
||||||
source: "candidate",
|
source: "candidate",
|
||||||
filter: {
|
filter: {
|
||||||
communityId: toObjectId(candidate.communityId ?? CONFIG.COMMUNITY_ID, "candidate.communityId"),
|
communityId: toObjectId(
|
||||||
|
candidate.communityId ?? CONFIG.COMMUNITY_ID,
|
||||||
|
"candidate.communityId",
|
||||||
|
),
|
||||||
kakaoTalkId: normalizeString(candidate.kakaoTalkId),
|
kakaoTalkId: normalizeString(candidate.kakaoTalkId),
|
||||||
},
|
},
|
||||||
doc: removeUndefined({
|
doc: removeUndefined({
|
||||||
_id: new ObjectId(),
|
_id: new ObjectId(),
|
||||||
communityId: toObjectId(candidate.communityId ?? CONFIG.COMMUNITY_ID, "candidate.communityId"),
|
communityId: toObjectId(
|
||||||
|
candidate.communityId ?? CONFIG.COMMUNITY_ID,
|
||||||
|
"candidate.communityId",
|
||||||
|
),
|
||||||
role: candidate.role ?? "member",
|
role: candidate.role ?? "member",
|
||||||
kakaoTalkId: normalizeString(candidate.kakaoTalkId),
|
kakaoTalkId: normalizeString(candidate.kakaoTalkId),
|
||||||
kakaoTalkNickname: normalizeString(
|
kakaoTalkNickname: normalizeString(
|
||||||
@@ -300,7 +311,9 @@ async function loadPlansFromCandidates(lolympusDb, now) {
|
|||||||
communityId: CONFIG.COMMUNITY_ID,
|
communityId: CONFIG.COMMUNITY_ID,
|
||||||
}).toArray();
|
}).toArray();
|
||||||
const plans = dedupePlans(
|
const plans = dedupePlans(
|
||||||
candidates.map((candidate) => buildPlanFromCandidate(candidate, now)).filter(Boolean),
|
candidates
|
||||||
|
.map((candidate) => buildPlanFromCandidate(candidate, now))
|
||||||
|
.filter(Boolean),
|
||||||
);
|
);
|
||||||
return { sourceRun: null, plans };
|
return { sourceRun: null, plans };
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -19,7 +19,7 @@
|
|||||||
* -> join nickname base with RiotAccount.gameName
|
* -> join nickname base with RiotAccount.gameName
|
||||||
* -> verify/fetch puuid from Riot Account API
|
* -> verify/fetch puuid from Riot Account API
|
||||||
* -> fetch summoner / league / mastery / recent match ids from Riot API
|
* -> fetch summoner / league / mastery / recent match ids from Riot API
|
||||||
* -> upsert lolympus-test.user
|
* -> upsert lolympus-staging.user
|
||||||
*/
|
*/
|
||||||
|
|
||||||
"use strict";
|
"use strict";
|
||||||
@@ -32,7 +32,7 @@ const CONFIG = {
|
|||||||
RIOT_API_KEY: process.env.RIOT_API_KEY ?? "",
|
RIOT_API_KEY: process.env.RIOT_API_KEY ?? "",
|
||||||
|
|
||||||
DAKAO_DB_NAME: "dakao",
|
DAKAO_DB_NAME: "dakao",
|
||||||
LOLYMPUS_DB_NAME: "lolympus-test",
|
LOLYMPUS_DB_NAME: "lolympus-staging",
|
||||||
RIOT_ACCOUNT_DB_NAME: process.env.RIOT_ACCOUNT_DB_NAME ?? "dakao",
|
RIOT_ACCOUNT_DB_NAME: process.env.RIOT_ACCOUNT_DB_NAME ?? "dakao",
|
||||||
RIOT_ACCOUNT_COLLECTION_NAME:
|
RIOT_ACCOUNT_COLLECTION_NAME:
|
||||||
process.env.RIOT_ACCOUNT_COLLECTION_NAME ?? "RiotAccount",
|
process.env.RIOT_ACCOUNT_COLLECTION_NAME ?? "RiotAccount",
|
||||||
@@ -50,7 +50,7 @@ const CONFIG = {
|
|||||||
RECENT_MATCH_COUNT: Number(process.env.RECENT_MATCH_COUNT ?? 20),
|
RECENT_MATCH_COUNT: Number(process.env.RECENT_MATCH_COUNT ?? 20),
|
||||||
FETCH_RECENT_MATCH_IDS: process.env.FETCH_RECENT_MATCH_IDS !== "false",
|
FETCH_RECENT_MATCH_IDS: process.env.FETCH_RECENT_MATCH_IDS !== "false",
|
||||||
|
|
||||||
// Safe default. Set DRY_RUN=false to write into lolympus-test.user.
|
// Safe default. Set DRY_RUN=false to write into lolympus-staging.user.
|
||||||
DRY_RUN: process.env.DRY_RUN !== "false",
|
DRY_RUN: process.env.DRY_RUN !== "false",
|
||||||
SAVE_DRY_RUN_RESULT: process.env.SAVE_DRY_RUN_RESULT !== "false",
|
SAVE_DRY_RUN_RESULT: process.env.SAVE_DRY_RUN_RESULT !== "false",
|
||||||
OUTPUT_ONLY_RIOT_MATCHED: process.env.OUTPUT_ONLY_RIOT_MATCHED !== "false",
|
OUTPUT_ONLY_RIOT_MATCHED: process.env.OUTPUT_ONLY_RIOT_MATCHED !== "false",
|
||||||
@@ -163,10 +163,10 @@ function maskConfigForStorage() {
|
|||||||
function isRiotMatchedCandidate(candidate) {
|
function isRiotMatchedCandidate(candidate) {
|
||||||
return Boolean(
|
return Boolean(
|
||||||
candidate.matchedRiotAccount &&
|
candidate.matchedRiotAccount &&
|
||||||
candidate.ambiguousRiotAccounts?.length === 0 &&
|
candidate.ambiguousRiotAccounts?.length === 0 &&
|
||||||
candidate.riotApiResult &&
|
candidate.riotApiResult &&
|
||||||
!candidate.riotApiError &&
|
!candidate.riotApiError &&
|
||||||
!candidate.riotEnrichmentError,
|
!candidate.riotEnrichmentError,
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -214,7 +214,10 @@ function remember(map, kakaoTalkId, patch) {
|
|||||||
const nickname =
|
const nickname =
|
||||||
normalizeString(patch.kakaoTalkNickname) ||
|
normalizeString(patch.kakaoTalkNickname) ||
|
||||||
normalizeString(current.kakaoTalkNickname);
|
normalizeString(current.kakaoTalkNickname);
|
||||||
const sources = new Set([...(current.sources ?? []), ...(patch.sources ?? [])]);
|
const sources = new Set([
|
||||||
|
...(current.sources ?? []),
|
||||||
|
...(patch.sources ?? []),
|
||||||
|
]);
|
||||||
const lastSeenCandidates = [current.lastSeenAt, patch.lastSeenAt]
|
const lastSeenCandidates = [current.lastSeenAt, patch.lastSeenAt]
|
||||||
.filter(Boolean)
|
.filter(Boolean)
|
||||||
.map((value) => new Date(value));
|
.map((value) => new Date(value));
|
||||||
@@ -229,7 +232,9 @@ function remember(map, kakaoTalkId, patch) {
|
|||||||
sources: [...sources],
|
sources: [...sources],
|
||||||
lastSeenAt:
|
lastSeenAt:
|
||||||
lastSeenCandidates.length > 0
|
lastSeenCandidates.length > 0
|
||||||
? new Date(Math.max(...lastSeenCandidates.map((date) => date.getTime())))
|
? new Date(
|
||||||
|
Math.max(...lastSeenCandidates.map((date) => date.getTime())),
|
||||||
|
)
|
||||||
: null,
|
: null,
|
||||||
recentMessageCount:
|
recentMessageCount:
|
||||||
(current.recentMessageCount ?? 0) + (patch.recentMessageCount ?? 0),
|
(current.recentMessageCount ?? 0) + (patch.recentMessageCount ?? 0),
|
||||||
@@ -361,7 +366,12 @@ function pickRiotAccountForCandidate(candidate, index) {
|
|||||||
|
|
||||||
const nicknameBase = candidate.nicknameBase;
|
const nicknameBase = candidate.nicknameBase;
|
||||||
if (!nicknameBase) {
|
if (!nicknameBase) {
|
||||||
return { source: "no-nickname-base", account: null, override, ambiguous: [] };
|
return {
|
||||||
|
source: "no-nickname-base",
|
||||||
|
account: null,
|
||||||
|
override,
|
||||||
|
ambiguous: [],
|
||||||
|
};
|
||||||
}
|
}
|
||||||
|
|
||||||
const exactMatches = index.exact.get(normalizeJoinKey(nicknameBase)) ?? [];
|
const exactMatches = index.exact.get(normalizeJoinKey(nicknameBase)) ?? [];
|
||||||
@@ -422,7 +432,9 @@ async function fetchRiotJson({ host, path, query }) {
|
|||||||
});
|
});
|
||||||
|
|
||||||
if (response.status === 429) {
|
if (response.status === 429) {
|
||||||
const retryAfterSeconds = Number(response.headers.get("retry-after") ?? 1);
|
const retryAfterSeconds = Number(
|
||||||
|
response.headers.get("retry-after") ?? 1,
|
||||||
|
);
|
||||||
await sleep(Math.max(1, retryAfterSeconds) * 1000);
|
await sleep(Math.max(1, retryAfterSeconds) * 1000);
|
||||||
continue;
|
continue;
|
||||||
}
|
}
|
||||||
@@ -431,7 +443,9 @@ async function fetchRiotJson({ host, path, query }) {
|
|||||||
|
|
||||||
if (!response.ok) {
|
if (!response.ok) {
|
||||||
const body = await response.text().catch(() => "");
|
const body = await response.text().catch(() => "");
|
||||||
throw new Error(`Riot API failed ${response.status} ${url.pathname}: ${body}`);
|
throw new Error(
|
||||||
|
`Riot API failed ${response.status} ${url.pathname}: ${body}`,
|
||||||
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
return response.json();
|
return response.json();
|
||||||
@@ -670,11 +684,14 @@ async function enrichCandidatesWithRiot({ candidates, riotAccountIndex }) {
|
|||||||
riotApiError,
|
riotApiError,
|
||||||
riotEnrichment,
|
riotEnrichment,
|
||||||
riotEnrichmentError,
|
riotEnrichmentError,
|
||||||
preferredPositions: normalizePositions(match.override?.preferredPositions),
|
preferredPositions: normalizePositions(
|
||||||
|
match.override?.preferredPositions,
|
||||||
|
),
|
||||||
role: match.override?.role ?? "member",
|
role: match.override?.role ?? "member",
|
||||||
isActivated: match.override?.isActivated ?? true,
|
isActivated: match.override?.isActivated ?? true,
|
||||||
targetKakaoTalkNickname:
|
targetKakaoTalkNickname:
|
||||||
normalizeString(match.override?.nickname) || candidate.kakaoTalkNickname,
|
normalizeString(match.override?.nickname) ||
|
||||||
|
candidate.kakaoTalkNickname,
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -798,7 +815,7 @@ async function upsertUsers({ lolympusDb, candidates }) {
|
|||||||
preferredPositions:
|
preferredPositions:
|
||||||
candidate.preferredPositions?.length > 0
|
candidate.preferredPositions?.length > 0
|
||||||
? candidate.preferredPositions
|
? candidate.preferredPositions
|
||||||
: existing?.preferredPositions ?? [],
|
: (existing?.preferredPositions ?? []),
|
||||||
isActivated: candidate.isActivated ?? true,
|
isActivated: candidate.isActivated ?? true,
|
||||||
updatedAt: now,
|
updatedAt: now,
|
||||||
};
|
};
|
||||||
|
|||||||
@@ -1,59 +1,177 @@
|
|||||||
import { spawnSync } from 'node:child_process';
|
import { spawnSync } from "node:child_process";
|
||||||
import { existsSync, readFileSync } from 'node:fs';
|
import { existsSync, readFileSync } from "node:fs";
|
||||||
import { dirname, resolve } from 'node:path';
|
import { dirname, resolve } from "node:path";
|
||||||
import { fileURLToPath } from 'node:url';
|
import { fileURLToPath } from "node:url";
|
||||||
|
|
||||||
const __dirname = dirname(fileURLToPath(import.meta.url));
|
const scriptPath = fileURLToPath(import.meta.url);
|
||||||
const repoRoot = resolve(__dirname, '..');
|
const __dirname = dirname(scriptPath);
|
||||||
const webDir = resolve(repoRoot, 'apps/web');
|
const repoRoot = resolve(__dirname, "..");
|
||||||
|
const webDir = resolve(repoRoot, "apps/web");
|
||||||
|
|
||||||
const target = process.argv[2];
|
if (process.argv[1] === scriptPath) {
|
||||||
if (target !== 'test' && target !== 'prod') {
|
main(process.argv.slice(2));
|
||||||
console.error('Usage: node scripts/deploy-cloudflare-pages.mjs <test|prod>');
|
}
|
||||||
|
|
||||||
|
export function main(args = process.argv.slice(2), env = process.env) {
|
||||||
|
const target = args[0];
|
||||||
|
if (target !== "staging" && target !== "prod") {
|
||||||
|
console.error(
|
||||||
|
"Usage: node scripts/deploy-cloudflare-pages.mjs <staging|prod>",
|
||||||
|
);
|
||||||
|
process.exit(1);
|
||||||
|
}
|
||||||
|
|
||||||
|
const viteMode = target === "prod" ? "production" : "staging";
|
||||||
|
const projectName =
|
||||||
|
env.CF_PAGES_PROJECT_NAME ??
|
||||||
|
env.CLOUDFLARE_PAGES_PROJECT_NAME ??
|
||||||
|
"lolympus";
|
||||||
|
const stagingBranch =
|
||||||
|
target === "staging" ? resolveStagingBranch(env) : undefined;
|
||||||
|
|
||||||
|
validateEnv(viteMode, env);
|
||||||
|
run("pnpm", ["--filter", "web", "run", `build:${target}`], repoRoot);
|
||||||
|
|
||||||
|
const deployArgs = [
|
||||||
|
"exec",
|
||||||
|
"wrangler",
|
||||||
|
"pages",
|
||||||
|
"deploy",
|
||||||
|
"dist",
|
||||||
|
"--project-name",
|
||||||
|
projectName,
|
||||||
|
];
|
||||||
|
|
||||||
|
if (target === "staging") {
|
||||||
|
deployArgs.push("--branch", stagingBranch);
|
||||||
|
console.error(
|
||||||
|
`Deploying staging to Cloudflare Pages branch ${stagingBranch}`,
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
|
run("pnpm", deployArgs, webDir);
|
||||||
|
}
|
||||||
|
|
||||||
|
export function resolveStagingBranch(env = process.env) {
|
||||||
|
const configuredBranch = env.CF_PAGES_STAGING_BRANCH?.trim();
|
||||||
|
if (configuredBranch) return configuredBranch;
|
||||||
|
|
||||||
|
refreshRemoteReleaseBranches();
|
||||||
|
|
||||||
|
const latestReleaseBranch = selectLatestReleaseBranch(
|
||||||
|
listReleaseBranchRefs(),
|
||||||
|
);
|
||||||
|
if (latestReleaseBranch) return latestReleaseBranch;
|
||||||
|
|
||||||
|
console.error(
|
||||||
|
[
|
||||||
|
"No release/* branch found for staging deploy.",
|
||||||
|
"Create a release branch, for example: git push origin main:release/66",
|
||||||
|
"Or set CF_PAGES_STAGING_BRANCH explicitly.",
|
||||||
|
].join("\n"),
|
||||||
|
);
|
||||||
process.exit(1);
|
process.exit(1);
|
||||||
}
|
}
|
||||||
|
|
||||||
const viteMode = target === 'prod' ? 'production' : 'test';
|
export function selectLatestReleaseBranch(refRows) {
|
||||||
const projectName =
|
const latestByBranch = new Map();
|
||||||
process.env.CF_PAGES_PROJECT_NAME ??
|
|
||||||
process.env.CLOUDFLARE_PAGES_PROJECT_NAME ??
|
|
||||||
'lolympus';
|
|
||||||
const testBranch = process.env.CF_PAGES_TEST_BRANCH ?? 'test';
|
|
||||||
|
|
||||||
validateEnv(viteMode);
|
for (const row of refRows) {
|
||||||
run('pnpm', ['--filter', 'web', 'run', `build:${target}`], repoRoot);
|
const [timestampText, refName] = row.split("\t");
|
||||||
|
const branch = normalizeReleaseBranch(refName);
|
||||||
|
const timestamp = Number(timestampText);
|
||||||
|
if (!branch || !Number.isFinite(timestamp)) continue;
|
||||||
|
|
||||||
const deployArgs = [
|
const current = latestByBranch.get(branch);
|
||||||
'exec',
|
if (!current || timestamp > current.timestamp) {
|
||||||
'wrangler',
|
latestByBranch.set(branch, { branch, timestamp });
|
||||||
'pages',
|
}
|
||||||
'deploy',
|
}
|
||||||
'dist',
|
|
||||||
'--project-name',
|
|
||||||
projectName,
|
|
||||||
];
|
|
||||||
|
|
||||||
if (target === 'test') {
|
const [latest] = [...latestByBranch.values()].sort((left, right) => {
|
||||||
deployArgs.push('--branch', testBranch);
|
if (left.timestamp !== right.timestamp) {
|
||||||
|
return right.timestamp - left.timestamp;
|
||||||
|
}
|
||||||
|
return right.branch.localeCompare(left.branch, "en", { numeric: true });
|
||||||
|
});
|
||||||
|
|
||||||
|
return latest?.branch;
|
||||||
}
|
}
|
||||||
|
|
||||||
run('pnpm', deployArgs, webDir);
|
export function normalizeReleaseBranch(refName) {
|
||||||
|
const branch = refName?.trim();
|
||||||
|
if (!branch) return undefined;
|
||||||
|
if (branch.startsWith("origin/release/")) {
|
||||||
|
return branch.slice("origin/".length);
|
||||||
|
}
|
||||||
|
if (branch.startsWith("release/")) return branch;
|
||||||
|
return undefined;
|
||||||
|
}
|
||||||
|
|
||||||
|
function listReleaseBranchRefs() {
|
||||||
|
const result = spawnSync(
|
||||||
|
"git",
|
||||||
|
[
|
||||||
|
"for-each-ref",
|
||||||
|
"--sort=-committerdate",
|
||||||
|
"--format=%(committerdate:unix)%09%(refname:short)",
|
||||||
|
"refs/remotes/origin/release",
|
||||||
|
"refs/heads/release",
|
||||||
|
],
|
||||||
|
{
|
||||||
|
cwd: repoRoot,
|
||||||
|
encoding: "utf8",
|
||||||
|
shell: process.platform === "win32",
|
||||||
|
},
|
||||||
|
);
|
||||||
|
|
||||||
|
if (result.status !== 0) {
|
||||||
|
const stderr = result.stderr?.trim();
|
||||||
|
if (stderr) console.error(stderr);
|
||||||
|
return [];
|
||||||
|
}
|
||||||
|
|
||||||
|
return result.stdout.split(/\r?\n/).filter(Boolean);
|
||||||
|
}
|
||||||
|
|
||||||
|
function refreshRemoteReleaseBranches() {
|
||||||
|
spawnSync("git", ["remote", "prune", "origin"], {
|
||||||
|
cwd: repoRoot,
|
||||||
|
stdio: "ignore",
|
||||||
|
shell: process.platform === "win32",
|
||||||
|
});
|
||||||
|
|
||||||
|
spawnSync(
|
||||||
|
"git",
|
||||||
|
[
|
||||||
|
"fetch",
|
||||||
|
"--prune",
|
||||||
|
"origin",
|
||||||
|
"+refs/heads/release/*:refs/remotes/origin/release/*",
|
||||||
|
],
|
||||||
|
{
|
||||||
|
cwd: repoRoot,
|
||||||
|
stdio: "ignore",
|
||||||
|
shell: process.platform === "win32",
|
||||||
|
},
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
function run(command, args, cwd) {
|
function run(command, args, cwd) {
|
||||||
const result = spawnSync(command, args, {
|
const result = spawnSync(command, args, {
|
||||||
cwd,
|
cwd,
|
||||||
stdio: 'inherit',
|
stdio: "inherit",
|
||||||
shell: process.platform === 'win32',
|
shell: process.platform === "win32",
|
||||||
});
|
});
|
||||||
if (result.status !== 0) {
|
if (result.status !== 0) {
|
||||||
process.exit(result.status ?? 1);
|
process.exit(result.status ?? 1);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
function validateEnv(mode) {
|
function validateEnv(mode, env) {
|
||||||
const fileEnv = [
|
const fileEnv = [
|
||||||
'.env',
|
".env",
|
||||||
'.env.local',
|
".env.local",
|
||||||
`.env.${mode}`,
|
`.env.${mode}`,
|
||||||
`.env.${mode}.local`,
|
`.env.${mode}.local`,
|
||||||
].reduce((acc, fileName) => {
|
].reduce((acc, fileName) => {
|
||||||
@@ -62,39 +180,39 @@ function validateEnv(mode) {
|
|||||||
return { ...acc, ...parseEnvFile(envFile) };
|
return { ...acc, ...parseEnvFile(envFile) };
|
||||||
}, {});
|
}, {});
|
||||||
const requiredKeys = [
|
const requiredKeys = [
|
||||||
'VITE_API_BASE_URL',
|
"VITE_API_BASE_URL",
|
||||||
'VITE_DEFAULT_COMMUNITY_ID',
|
"VITE_DEFAULT_COMMUNITY_ID",
|
||||||
'VITE_MSAL_CLIENT_ID',
|
"VITE_MSAL_CLIENT_ID",
|
||||||
'VITE_MSAL_AUTHORITY',
|
"VITE_MSAL_AUTHORITY",
|
||||||
'VITE_MSAL_API_SCOPE',
|
"VITE_MSAL_API_SCOPE",
|
||||||
'VITE_MSAL_REDIRECT_URI',
|
"VITE_MSAL_REDIRECT_URI",
|
||||||
];
|
];
|
||||||
const missing = requiredKeys.filter((key) => {
|
const missing = requiredKeys.filter((key) => {
|
||||||
const value = process.env[key] ?? fileEnv[key];
|
const value = env[key] ?? fileEnv[key];
|
||||||
return !value || value.includes('<');
|
return !value || value.includes("<");
|
||||||
});
|
});
|
||||||
|
|
||||||
if (missing.length === 0) return;
|
if (missing.length === 0) return;
|
||||||
|
|
||||||
console.error(
|
console.error(
|
||||||
[
|
[
|
||||||
`Missing Cloudflare Pages ${mode} build env: ${missing.join(', ')}`,
|
`Missing Cloudflare Pages ${mode} build env: ${missing.join(", ")}`,
|
||||||
`Create apps/web/.env.${mode}, apps/web/.env.${mode}.local, or export these variables before deploy.`,
|
`Create apps/web/.env.${mode}, apps/web/.env.${mode}.local, or export these variables before deploy.`,
|
||||||
`Use apps/web/.env.${mode}.example as a template.`,
|
`Use apps/web/.env.${mode}.example as a template.`,
|
||||||
].join('\n'),
|
].join("\n"),
|
||||||
);
|
);
|
||||||
process.exit(1);
|
process.exit(1);
|
||||||
}
|
}
|
||||||
|
|
||||||
function parseEnvFile(path) {
|
function parseEnvFile(path) {
|
||||||
return Object.fromEntries(
|
return Object.fromEntries(
|
||||||
readFileSync(path, 'utf8')
|
readFileSync(path, "utf8")
|
||||||
.split(/\r?\n/)
|
.split(/\r?\n/)
|
||||||
.map((line) => line.trim())
|
.map((line) => line.trim())
|
||||||
.filter((line) => line && !line.startsWith('#'))
|
.filter((line) => line && !line.startsWith("#"))
|
||||||
.map((line) => {
|
.map((line) => {
|
||||||
const separatorIndex = line.indexOf('=');
|
const separatorIndex = line.indexOf("=");
|
||||||
if (separatorIndex === -1) return [line, ''];
|
if (separatorIndex === -1) return [line, ""];
|
||||||
const key = line.slice(0, separatorIndex).trim();
|
const key = line.slice(0, separatorIndex).trim();
|
||||||
let value = line.slice(separatorIndex + 1).trim();
|
let value = line.slice(separatorIndex + 1).trim();
|
||||||
if (
|
if (
|
||||||
|
|||||||
Reference in new issue
Block a user