fix: 修复模板唯一性及六项运营页面问题
CSS quality / css-quality (push) Waiting to run

This commit is contained in:
hectorzhao
2026-09-21 19:28:50 +08:00
parent 28951b4fc4
commit 3cacb6e8e7
38 changed files with 1122 additions and 194 deletions
+30 -23
View File
@@ -1,12 +1,12 @@
import { BadRequestException } from '@nestjs/common';
import { randomInt, randomUUID } from 'node:crypto';
import { randomUUID } from 'node:crypto';
import type { CreateSmsApplicationDto } from './sms-config.contracts';
/** Pure normalization and report-value helpers shared by the R3 domain services. */
export const APPLICATION_QUEUE_PRIORITIES = ['normal', 'priority'] as const;
export type ApplicationQueuePriority = typeof APPLICATION_QUEUE_PRIORITIES[number];
export type ApplicationQueuePriority = (typeof APPLICATION_QUEUE_PRIORITIES)[number];
export const APPLICATION_INTERFACE_TYPES = ['cmpp20'] as const;
export type ApplicationInterfaceType = typeof APPLICATION_INTERFACE_TYPES[number];
export type ApplicationInterfaceType = (typeof APPLICATION_INTERFACE_TYPES)[number];
export const DEFAULT_DOWNSTREAM_HEARTBEAT_TIMEOUT_MS = 90_000;
export const APPLICATION_DISABLE_GRACE_MS = 72 * 60 * 60 * 1_000;
export const DEFAULT_APPLICATION_DISABLE_SCAN_INTERVAL_MS = 60_000;
@@ -55,16 +55,16 @@ export function validateAndNormalizeTemplateVariables(
const end = content.indexOf('}', start + 2);
if (end < 0) throw new BadRequestException('模板变量未闭合');
const name = content.slice(start + 2, end);
if (!/^[A-Za-z][A-Za-z0-9_]{0,31}$/.test(name)) {
throw new BadRequestException('模板变量名必须以英文字母开头,仅包含英文字母数字和下划线,长度1至32位');
if (!/^[A-Za-z0-9]{1,32}$/.test(name)) {
throw new BadRequestException('模板变量名仅允许英文字母数字,长度1至32位');
}
if (names.includes(name)) throw new BadRequestException(`模板变量 ${name} 重复`);
names.push(name);
cursor = end + 1;
}
if (!supplied) return names.map((name) => ({ name, required: true }));
const suppliedNames = supplied.map((item) => item.name?.trim());
if (suppliedNames.some((name) => !name || !/^[A-Za-z][A-Za-z0-9_]{0,31}$/.test(name))) {
const suppliedNames = supplied.map((item) => item.name);
if (suppliedNames.some((name) => typeof name !== 'string' || !/^[A-Za-z0-9]{1,32}$/.test(name))) {
throw new BadRequestException('变量配置中包含非法变量名');
}
if (new Set(suppliedNames).size !== suppliedNames.length) throw new BadRequestException('变量配置中包含重复变量');
@@ -75,7 +75,10 @@ export function validateAndNormalizeTemplateVariables(
}
export function normalizeSmsSignature(name: string) {
const innerName = name.trim().replace(/^[【\[]+|[】\]]+$/g, '').trim();
const innerName = name
.trim()
.replace(/^[【[]+|[】\]]+$/g, '')
.trim();
return innerName ? `${innerName}` : '';
}
@@ -114,26 +117,27 @@ export function normalizeApplicationInterfaceType(value?: string): ApplicationIn
}
export function normalizeCmppAccessNumberConfig(
data: Pick<CreateSmsApplicationDto, 'cmppApplicationExtension' | 'cmppAccessNumberFillEnabled' | 'cmppAccessNumberFillPrefix'>,
data: Pick<
CreateSmsApplicationDto,
'cmppApplicationExtension' | 'cmppAccessNumberFillEnabled' | 'cmppAccessNumberFillPrefix'
>,
current?: {
cmppApplicationExtension?: string | null;
cmppAccessNumberFillEnabled?: boolean | null;
cmppAccessNumberFillPrefix?: string | null;
},
) {
const applicationExtension = (
data.cmppApplicationExtension === undefined
const applicationExtension =
(data.cmppApplicationExtension === undefined
? current?.cmppApplicationExtension
: data.cmppApplicationExtension
)?.trim() || null;
const fillEnabled = data.cmppAccessNumberFillEnabled
?? current?.cmppAccessNumberFillEnabled
?? false;
const configuredPrefix = (
data.cmppAccessNumberFillPrefix === undefined
)?.trim() || null;
const fillEnabled = data.cmppAccessNumberFillEnabled ?? current?.cmppAccessNumberFillEnabled ?? false;
const configuredPrefix =
(data.cmppAccessNumberFillPrefix === undefined
? current?.cmppAccessNumberFillPrefix
: data.cmppAccessNumberFillPrefix
)?.trim() || null;
)?.trim() || null;
if (applicationExtension && !/^\d+$/.test(applicationExtension)) {
throw new BadRequestException('cmppApplicationExtension must contain digits only');
@@ -152,9 +156,7 @@ export function normalizeCmppAccessNumberConfig(
}
const fillPrefix = fillEnabled ? configuredPrefix : null;
const clientSrcId = applicationExtension
? `${fillPrefix ?? ''}${applicationExtension}`
: null;
const clientSrcId = applicationExtension ? `${fillPrefix ?? ''}${applicationExtension}` : null;
if (clientSrcId && clientSrcId.length > 21) {
throw new BadRequestException('client CMPP Src_Id must not exceed 21 digits');
}
@@ -179,7 +181,9 @@ export function normalizeApplicationCmppStatus(connections: Array<{ status: stri
if (connections.some((connection) => connection.status === 'connected')) {
return 'connected';
}
if (connections.some((connection) => ['auth_failed', 'heartbeat_timeout', 'reconnecting'].includes(connection.status))) {
if (
connections.some((connection) => ['auth_failed', 'heartbeat_timeout', 'reconnecting'].includes(connection.status))
) {
return 'degraded';
}
return 'disconnected';
@@ -202,7 +206,10 @@ export function isRecord(value: unknown): value is Record<string, unknown> {
export function reportValueParts(value: unknown) {
if (isRecord(value) && typeof value.fileObjectId === 'string') {
return { fieldValue: typeof value.fileName === 'string' ? value.fileName : undefined, fileObjectId: value.fileObjectId };
return {
fieldValue: typeof value.fileName === 'string' ? value.fileName : undefined,
fileObjectId: value.fileObjectId,
};
}
return { fieldValue: value === undefined || value === null ? undefined : String(value), fileObjectId: undefined };
}