feat: complete cmpp gateway delivery recovery workflows

This commit is contained in:
hectorzhao
2026-07-08 16:30:06 +08:00
parent cc628d0214
commit 8144f08652
60 changed files with 8901 additions and 94 deletions
+28 -2
View File
@@ -20,6 +20,8 @@ export interface CreateChannelDto {
rateLimitPerSecond?: number;
unitPrice?: number;
status?: string;
desiredConnections?: number;
windowSize?: number;
config?: Record<string, unknown>;
}
@@ -196,6 +198,7 @@ export class ChannelsService implements OnModuleInit, OnModuleDestroy {
if (!Number.isInteger(gatewayPort) || gatewayPort <= 0 || gatewayPort > 65535) {
throw new BadRequestException('gatewayPort must be an integer between 1 and 65535');
}
const config = normalizeChannelRuntimeConfig(data.config, data.desiredConnections, data.windowSize);
const channel = await this.prisma.smsChannel.create({
data: {
code: data.code,
@@ -213,7 +216,7 @@ export class ChannelsService implements OnModuleInit, OnModuleDestroy {
rateLimitPerSecond: data.rateLimitPerSecond ?? 100,
unitPrice: data.unitPrice ?? 0,
status: data.status ?? 'active',
config: data.config as Prisma.InputJsonValue | undefined,
config: config as Prisma.InputJsonValue,
},
});
if (channel.status === 'active') {
@@ -231,6 +234,9 @@ export class ChannelsService implements OnModuleInit, OnModuleDestroy {
if (gatewayPort !== undefined && (!Number.isInteger(gatewayPort) || gatewayPort <= 0 || gatewayPort > 65535)) {
throw new BadRequestException('gatewayPort must be an integer between 1 and 65535');
}
const config = data.config !== undefined || data.desiredConnections !== undefined || data.windowSize !== undefined
? normalizeChannelRuntimeConfig(channel.config, data.desiredConnections, data.windowSize)
: undefined;
const updated = await this.prisma.smsChannel.update({
where: { id: channelId },
data: {
@@ -249,7 +255,7 @@ export class ChannelsService implements OnModuleInit, OnModuleDestroy {
rateLimitPerSecond: data.rateLimitPerSecond,
unitPrice: data.unitPrice,
status: data.status,
config: data.config as Prisma.InputJsonValue | undefined,
config: config as Prisma.InputJsonValue | undefined,
},
});
await this.prisma.operationLog.create({
@@ -1105,6 +1111,26 @@ function getDesiredConnections(config?: Prisma.JsonValue | null) {
return 1;
}
function normalizeChannelRuntimeConfig(config?: Prisma.JsonValue | Record<string, unknown> | null, desiredConnections?: number, windowSize?: number) {
const base = config && typeof config === 'object' && !Array.isArray(config)
? { ...(config as Record<string, unknown>) }
: {};
base.desiredConnections = getPositiveRuntimeInteger(desiredConnections ?? base.desiredConnections, 1, 'desiredConnections');
base.windowSize = getPositiveRuntimeInteger(windowSize ?? base.windowSize, 16, 'windowSize');
return base;
}
function getPositiveRuntimeInteger(value: unknown, fallback: number, fieldName: string) {
if (value === undefined || value === null || value === '') {
return fallback;
}
const normalized = Number(value);
if (!Number.isInteger(normalized) || normalized <= 0) {
throw new BadRequestException(`${fieldName} must be a positive integer`);
}
return normalized;
}
function bullmqConnection() {
const redisUrl = new URL(process.env.REDIS_URL ?? 'redis://127.0.0.1:6379');
return {