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
@@ -0,0 +1,4 @@
-- Deleted records remain available for audit but do not reserve an application template name.
-- Fail on conflicting legacy rows; never rename or delete business data during migration.
CREATE UNIQUE INDEX "SmsTemplate_application_name_active_key"
ON "SmsTemplate" ("applicationId", btrim(name)) WHERE "auditStatus" <> 'deleted';
@@ -0,0 +1,21 @@
-- Serialize member writes against channel capability changes; validate whole carrier strings.
CREATE FUNCTION cmpp_check_group_channel_carrier() RETURNS trigger LANGUAGE plpgsql AS $$
DECLARE
capabilities text[];
legacy text;
target_carrier text;
BEGIN
SELECT carriers, carrier INTO capabilities, legacy FROM "SmsChannel" WHERE id=NEW."channelId" FOR SHARE;
SELECT carrier INTO target_carrier FROM "SmsChannelGroup" WHERE id=NEW."groupId";
IF cardinality(capabilities) = 0 THEN
capabilities := CASE WHEN legacy='all' THEN ARRAY['mobile','unicom','telecom'] ELSE ARRAY[legacy] END;
END IF;
IF target_carrier IS NOT NULL AND NOT (target_carrier=ANY(capabilities)) THEN
RAISE EXCEPTION 'Channel carrier is not compatible with the channel group carrier' USING ERRCODE='23514';
END IF;
RETURN NEW;
END;
$$;
CREATE TRIGGER "SmsChannelGroupItem_carrier_guard"
BEFORE INSERT OR UPDATE OF "groupId", "channelId" ON "SmsChannelGroupItem"
FOR EACH ROW EXECUTE FUNCTION cmpp_check_group_channel_carrier();
@@ -1,7 +1,7 @@
import { ChannelConfigurationService } from './channel-configuration.service';
import { selectChannelCandidate } from '../send-chain/send-chain.helpers';
describe('carrier capability reduction', () => {
it('preserves group references and avoids reconnecting for a capability-only change', async () => {
it('removes incompatible group members in a transaction without reconnecting', async () => {
const channel = {
id: 'c',
carrier: 'all',
@@ -10,16 +10,18 @@ describe('carrier capability reduction', () => {
config: {},
};
const prisma = {
$transaction: jest.fn(),
smsChannel: {
findUnique: jest.fn().mockResolvedValue(channel),
update: jest.fn().mockImplementation(({ data }) => ({ ...channel, ...data })),
},
operationLog: { create: jest.fn() },
smsChannelGroupItem: {
findMany: jest.fn().mockResolvedValue([{ group: { name: 'existing' } }]),
findMany: jest.fn().mockResolvedValue([{ id: 'member', group: { name: 'existing', carrier: 'telecom' } }]),
deleteMany: jest.fn(),
},
};
prisma.$transaction.mockImplementation((callback) => callback(prisma));
const connection = { requestChannelConnection: jest.fn(), requestChannelDisconnection: jest.fn() };
await new ChannelConfigurationService(prisma as never, connection as never).updateChannel('c', {
carriers: ['mobile', 'unicom'],
@@ -27,7 +29,7 @@ describe('carrier capability reduction', () => {
expect(prisma.smsChannel.update).toHaveBeenCalledWith(
expect.objectContaining({ data: expect.objectContaining({ carriers: ['mobile', 'unicom'] }) }),
);
expect(prisma.smsChannelGroupItem.deleteMany).not.toHaveBeenCalled();
expect(prisma.smsChannelGroupItem.deleteMany).toHaveBeenCalledWith({ where: { id: { in: ['member'] } } });
expect(connection.requestChannelConnection).not.toHaveBeenCalled();
const candidate = {
channelId: 'c',
@@ -173,53 +173,72 @@ export class ChannelConfigurationService {
cmppVersion: cmppVersion ?? channel.cmppVersion,
config: config ?? channel.config,
});
const updated = await this.prisma.smsChannel.update({
where: { id: channelId },
data: {
code: data.code,
name: data.name,
carrier:
data.carriers !== undefined || data.carrier !== undefined
? legacyCarrierFromCapabilities(carriers)
: undefined,
carriers: data.carriers !== undefined || data.carrier !== undefined ? carriers : undefined,
sendRegion: data.sendRegion,
protocol: 'CMPP',
gatewayHost: data.gatewayHost,
gatewayPort,
enterpriseCode: data.enterpriseCode,
account: data.account,
passwordCipher: data.passwordCipher,
srcId: data.srcId,
cmppVersion,
rateLimitPerSecond,
unitPrice: data.unitPrice,
status: data.status,
config: config as Prisma.InputJsonValue | undefined,
},
});
await this.prisma.operationLog.create({
data: {
action: 'sms_channel.update',
resource: 'sms_channel',
resourceId: channelId,
detail: {
before: {
code: channel.code,
name: channel.name,
carrier: channel.carrier,
carriers: channel.carriers,
sendRegion: channel.sendRegion,
gatewayHost: channel.gatewayHost,
gatewayPort: channel.gatewayPort,
enterpriseCode: channel.enterpriseCode,
account: channel.account,
srcId: channel.srcId,
unitPrice: moneyToNumber(channel.unitPrice),
},
after: data,
} as Prisma.InputJsonValue,
},
const updated = await this.prisma.$transaction(async (tx) => {
const updated = await tx.smsChannel.update({
where: { id: channelId },
data: {
code: data.code,
name: data.name,
carrier:
data.carriers !== undefined || data.carrier !== undefined
? legacyCarrierFromCapabilities(carriers)
: undefined,
carriers: data.carriers !== undefined || data.carrier !== undefined ? carriers : undefined,
sendRegion: data.sendRegion,
protocol: 'CMPP',
gatewayHost: data.gatewayHost,
gatewayPort,
enterpriseCode: data.enterpriseCode,
account: data.account,
passwordCipher: data.passwordCipher,
srcId: data.srcId,
cmppVersion,
rateLimitPerSecond,
unitPrice: data.unitPrice,
status: data.status,
config: config as Prisma.InputJsonValue | undefined,
},
});
const removedGroupItems =
data.carriers !== undefined || data.carrier !== undefined
? await tx.smsChannelGroupItem.findMany({
where: { channelId, group: { carrier: { notIn: carriers } } },
select: {
id: true,
groupId: true,
carrier: true,
province: true,
group: { select: { name: true, carrier: true } },
},
})
: [];
if (removedGroupItems.length)
await tx.smsChannelGroupItem.deleteMany({ where: { id: { in: removedGroupItems.map((item) => item.id) } } });
await tx.operationLog.create({
data: {
action: 'sms_channel.update',
resource: 'sms_channel',
resourceId: channelId,
detail: {
before: {
code: channel.code,
name: channel.name,
carrier: channel.carrier,
carriers: channel.carriers,
sendRegion: channel.sendRegion,
gatewayHost: channel.gatewayHost,
gatewayPort: channel.gatewayPort,
enterpriseCode: channel.enterpriseCode,
account: channel.account,
srcId: channel.srcId,
unitPrice: moneyToNumber(channel.unitPrice),
},
after: { ...data, passwordCipher: data.passwordCipher ? '[updated]' : undefined },
removedGroupItems,
} as Prisma.InputJsonValue,
},
});
return updated;
});
const updatedStatus = data.status ?? channel.status;
if (updatedStatus === 'active' && (connectionConfigChanged || channel.status !== 'active')) {
@@ -363,6 +363,10 @@ export class ChannelReportingService {
status?: string;
reportType?: string;
keyword?: string;
enterpriseKeyword?: string;
applicationKeyword?: string;
channelKeyword?: string;
objectKeyword?: string;
createdAtFrom?: string;
createdAtTo?: string;
page?: number;
@@ -482,6 +486,16 @@ export class ChannelReportingService {
const changedAt = new Date(task.updatedAt);
if (query.createdAtFrom && changedAt < new Date(`${query.createdAtFrom}T00:00:00+08:00`)) return false;
if (query.createdAtTo && changedAt > new Date(`${query.createdAtTo}T23:59:59.999+08:00`)) return false;
const matches = (value: string | null | undefined, filter?: string) =>
!filter?.trim() || (value ?? '').includes(filter.trim());
if (!matches(task.signature.tenant.name, query.enterpriseKeyword)) return false;
if (!matches(task.signature.application?.name, query.applicationKeyword)) return false;
if (!matches(task.channel.name, query.channelKeyword)) return false;
const objects =
task.reportType === 'drainage'
? [task.drainageInfo?.siteName, task.drainageInfo?.url]
: [task.signature.name];
if (query.objectKeyword?.trim() && !objects.some((value) => matches(value, query.objectKeyword))) return false;
if (!query.keyword?.trim()) return true;
const keyword = query.keyword.trim();
return [
+8
View File
@@ -246,6 +246,10 @@ export class ChannelsController {
@Query('status') status?: string,
@Query('reportType') reportType?: string,
@Query('keyword') keyword?: string,
@Query('enterpriseKeyword') enterpriseKeyword?: string,
@Query('applicationKeyword') applicationKeyword?: string,
@Query('channelKeyword') channelKeyword?: string,
@Query('objectKeyword') objectKeyword?: string,
@Query('createdAtFrom') createdAtFrom?: string,
@Query('createdAtTo') createdAtTo?: string,
@Query('page') page?: string,
@@ -260,6 +264,10 @@ export class ChannelsController {
status,
reportType,
keyword,
enterpriseKeyword,
applicationKeyword,
channelKeyword,
objectKeyword,
createdAtFrom,
createdAtTo,
page: Number(page),
+7 -3
View File
@@ -72,11 +72,14 @@ function createPrismaMock() {
},
],
};
const channelUpdate = jest.fn().mockImplementation(({ data }) => Promise.resolve({ ...channel, ...data }));
const operationLogCreate = jest.fn();
return {
$queryRaw: jest.fn().mockResolvedValue([]),
$transaction: jest.fn((callback) =>
callback({
smsChannel: {
update: channelUpdate,
create: jest.fn().mockImplementation(({ data }) => Promise.resolve({ id: 'channel-copy', ...data })),
},
smsChannelGroup: {
@@ -86,6 +89,7 @@ function createPrismaMock() {
.mockResolvedValue({ id: 'group-1', code: 'G-MOBILE', name: '移动组', carrier: 'mobile', items: [] }),
},
smsChannelGroupItem: {
findMany: jest.fn().mockResolvedValue([]),
deleteMany: jest.fn(),
createMany: jest.fn(),
},
@@ -98,7 +102,7 @@ function createPrismaMock() {
createMany: jest.fn(),
},
operationLog: {
create: jest.fn(),
create: operationLogCreate,
},
}),
),
@@ -106,7 +110,7 @@ function createPrismaMock() {
findMany: jest.fn(),
create: jest.fn().mockImplementation(({ data }) => Promise.resolve({ id: 'channel-1', ...data })),
findUnique: jest.fn().mockResolvedValue(channel),
update: jest.fn().mockImplementation(({ data }) => Promise.resolve({ ...channel, ...data })),
update: channelUpdate,
},
channelHealthMetric: { findMany: jest.fn() },
smsChannelGroup: {
@@ -228,7 +232,7 @@ function createPrismaMock() {
updateMany: jest.fn().mockResolvedValue({ count: 1 }),
},
operationLog: {
create: jest.fn(),
create: operationLogCreate,
findMany: jest.fn().mockResolvedValue([
{
id: 'log-1',
+4
View File
@@ -202,6 +202,10 @@ export class ChannelsService implements OnModuleInit, OnModuleDestroy {
status?: string;
reportType?: string;
keyword?: string;
enterpriseKeyword?: string;
applicationKeyword?: string;
channelKeyword?: string;
objectKeyword?: string;
createdAtFrom?: string;
createdAtTo?: string;
page?: number;
+16
View File
@@ -103,3 +103,19 @@ export async function operationStatus(tx: Prisma.TransactionClient) {
downstreamDeliverySummary: { alertCount: stalled + ack + failed },
};
}
/** Restore the original hourly business-message series, bounded by today's queuedAt index. */
export async function hourlySendTrend(tx: Prisma.TransactionClient, date: string) {
const rows = await tx.$queryRaw<Array<{ hour: number; submittedCount: bigint; successCount: bigint }>>(Prisma.sql`
SELECT EXTRACT(HOUR FROM ("queuedAt" AT TIME ZONE 'UTC') AT TIME ZONE 'Asia/Shanghai')::integer AS hour,
COUNT(*)::bigint AS "submittedCount",COUNT(*) FILTER (WHERE status='delivered')::bigint AS "successCount"
FROM "SmsMessageRecord" WHERE "queuedAt">=${startOfDay(date)} AND "queuedAt"<${startOfDay(addDays(date, 1))}
GROUP BY hour ORDER BY hour`);
const byHour = new Map(rows.map((row) => [row.hour, row]));
return Array.from({ length: 24 }, (_, hour) => ({
hour,
label: String(hour).padStart(2, '0') + ':00',
submittedCount: Number(byHour.get(hour)?.submittedCount ?? 0),
successCount: Number(byHour.get(hour)?.successCount ?? 0),
}));
}
+4 -2
View File
@@ -10,7 +10,7 @@ import { randomUUID } from 'node:crypto';
import { PrismaService } from '../prisma/prisma.service';
import type { SessionRequest } from '../auth/session-validation.middleware';
import { addDays, startOfDay, todayKey } from '../signature-analytics/analytics-date';
import { aggregateHome, enterpriseRanks, metrics, operationStatus } from './home-read';
import { aggregateHome, enterpriseRanks, hourlySendTrend, metrics, operationStatus } from './home-read';
@Injectable()
export class HomeService {
@@ -40,7 +40,7 @@ export class HomeService {
const state = await tx.homeProjectionState.findUniqueOrThrow({ where: { id: 'home' } });
if (!state.initialized || state.seededDay !== date)
throw new ServiceUnavailableException('今日统计正在初始化,请稍后刷新');
const [rows, ranks, status, pending, unresolved] = await Promise.all([
const [rows, ranks, status, pending, unresolved, hourlyTrend] = await Promise.all([
aggregateHome(tx, date, state.version),
enterpriseRanks(tx, date),
operationStatus(tx),
@@ -51,6 +51,7 @@ export class HomeService {
receivedAt: { gte: startOfDay(addDays(date, -3)), lt: startOfDay(addDays(date, 1)) },
},
}),
hourlySendTrend(tx, date),
]);
const values = metrics(rows[0]);
const summary = {
@@ -66,6 +67,7 @@ export class HomeService {
timeSourceCoverage: { approximate: values.approximate, incomplete: values.incomplete },
today: values,
enterpriseSpendRanks: ranks,
hourlySendTrend: hourlyTrend,
...status,
};
const snapshot = await tx.homeSnapshot.create({
+9 -7
View File
@@ -10,13 +10,15 @@ import type {
// Pure query builders and response mappers shared by the R2 query domains.
export function messageWhere(query: MessageQuery): Prisma.SmsMessageRecordWhereInput {
const statusWhere =
query.status === 'submit_failed'
? { OR: [{ status: 'submit_failed' }, { submitStatus: { in: ['rejected', 'timeout'] } }] }
: query.status === 'failed'
? { status: 'failed', submitStatus: 'accepted' }
: query.status
? { status: query.status }
: {};
query.status === 'unknown'
? { status: { in: ['submitted', 'unknown'] } }
: query.status === 'submit_failed'
? { OR: [{ status: 'submit_failed' }, { submitStatus: { in: ['rejected', 'timeout'] } }] }
: query.status === 'failed'
? { status: 'failed', submitStatus: 'accepted' }
: query.status
? { status: query.status }
: {};
return {
tenantId: query.tenantId,
applicationId: query.applicationId,
+13 -8
View File
@@ -5,6 +5,7 @@ import type { ReviewDto, StatusChangeDto } from './sms-config.contracts';
import { SmsApplicationLifecycleService } from './application-lifecycle.service';
import { SmsReportValidationService } from './report-validation.service';
import { writeUniqueSignature } from './signature-uniqueness';
import { templateWriteError } from './template-uniqueness';
/** R3 domain service. Kept framework-agnostic and composed behind SmsConfigService. */
export class SmsAuditService {
@@ -72,7 +73,9 @@ export class SmsAuditService {
throw new NotFoundException('Template not found');
}
const status = data.status ?? 'deleted';
const updated = await this.prisma.smsTemplate.update({ where: { id: templateId }, data: { auditStatus: status } });
const updated = await this.prisma.smsTemplate
.update({ where: { id: templateId }, data: { auditStatus: status } })
.catch(templateWriteError);
await this.lifecycle.writeOperationLog(
template.tenantId,
data.operatorId,
@@ -158,13 +161,15 @@ export class SmsAuditService {
}
const reviewerId = await this.resolveReviewerId(data.reviewerId);
const updated = await this.prisma.smsTemplate.update({
where: { id: templateId },
data: {
auditStatus: statusAfter,
rejectReason: statusAfter === 'rejected' ? data.reason : null,
},
});
const updated = await this.prisma.smsTemplate
.update({
where: { id: templateId },
data: {
auditStatus: statusAfter,
rejectReason: statusAfter === 'rejected' ? data.reason : null,
},
})
.catch(templateWriteError);
await this.createAuditRecord({
tenantId: template.tenantId,
targetType: 'sms_template',
+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 };
}
@@ -0,0 +1,8 @@
import { BadRequestException } from '@nestjs/common';
import { Prisma } from '@prisma/client';
export function templateWriteError(error: unknown): never {
if (error instanceof Prisma.PrismaClientKnownRequestError && error.code === 'P2002')
throw new BadRequestException('同一企业应用下已存在相同名称的模板,请修改模板名称');
throw error;
}
@@ -0,0 +1,19 @@
import { validateAndNormalizeTemplateVariables } from './sms-config.helpers';
describe('template variable names', () => {
it.each(['code', 'Code123', '123', 'A'.repeat(32)])('accepts ASCII alphanumeric name %s', (name) => {
expect(validateAndNormalizeTemplateVariables(`正文\${${name}}`, [{ name, example: '中文示例' }])).toEqual([
{ name, example: '中文示例' },
]);
});
it.each(['中文', 'code_1', 'code-1', ' name', '', 'é', '', 'A'.repeat(33)])('rejects invalid name %s', (name) => {
expect(() => validateAndNormalizeTemplateVariables(`正文\${${name}}`)).toThrow('变量名仅允许');
});
it('rejects unclosed, repeated and mismatched variables', () => {
expect(() => validateAndNormalizeTemplateVariables('${code')).toThrow('未闭合');
expect(() => validateAndNormalizeTemplateVariables('${code}${code}')).toThrow('重复');
expect(() => validateAndNormalizeTemplateVariables('${code}', [{ name: 'other' }])).toThrow('完全一致');
expect(() => validateAndNormalizeTemplateVariables('${code}', [{ name: 'code_' }])).toThrow('非法');
expect(() => validateAndNormalizeTemplateVariables('${code}', [{ name: 'code ' }])).toThrow('非法');
});
});
+59 -48
View File
@@ -3,6 +3,7 @@ import { Prisma } from '@prisma/client';
import { shanghaiDateRange } from '../common/shanghai-date-range';
import { PrismaService } from '../prisma/prisma.service';
import { SmsAuditService } from './audit.service';
import { templateWriteError } from './template-uniqueness';
import type {
CreateSmsTemplateDto,
CreateSmsTemplateOptions,
@@ -87,6 +88,7 @@ export class SmsTemplateService {
}
async createTemplate(data: CreateSmsTemplateDto, options: CreateSmsTemplateOptions = {}) {
const name = this.templateName(data.name);
const variables = validateAndNormalizeTemplateVariables(data.content, data.variables);
const application = await this.prisma.smsApplication.findUnique({
where: { id: data.applicationId },
@@ -96,26 +98,28 @@ export class SmsTemplateService {
throw new BadRequestException('applicationId does not belong to the template tenant');
}
await this.validateTemplateSignature(data.signatureId, data.tenantId, data.applicationId, data.content);
return this.prisma.smsTemplate.create({
data: {
tenantId: data.tenantId,
applicationId: data.applicationId,
signatureId: data.signatureId,
name: data.name,
content: data.content,
category: data.category,
auditStatus: options.initialAuditStatus,
billingUnits: estimateBillingUnits(data.content),
variables: {
create: variables.map((variable: TemplateVariableInput) => ({
name: variable.name,
example: variable.example,
required: variable.required ?? true,
})),
return this.prisma.smsTemplate
.create({
data: {
tenantId: data.tenantId,
applicationId: data.applicationId,
signatureId: data.signatureId,
name,
content: data.content,
category: data.category,
auditStatus: options.initialAuditStatus,
billingUnits: estimateBillingUnits(data.content),
variables: {
create: variables.map((variable: TemplateVariableInput) => ({
name: variable.name,
example: variable.example,
required: variable.required ?? true,
})),
},
},
},
include: { variables: true, application: true, tenant: true, signature: true },
});
include: { variables: true, application: true, tenant: true, signature: true },
})
.catch(templateWriteError);
}
async updateTemplate(templateId: string, data: UpdateSmsTemplateDto, tenantId?: string) {
@@ -151,35 +155,42 @@ export class SmsTemplateService {
(data.category !== undefined && data.category !== template.category) ||
data.variables !== undefined;
const auditStatus = materialChanged && template.auditStatus === 'approved' ? 'pending' : data.auditStatus;
return this.prisma.$transaction(async (tx) => {
if (variables) {
await tx.templateVariable.deleteMany({ where: { templateId } });
}
return tx.smsTemplate.update({
where: { id: templateId },
data: {
applicationId: data.applicationId,
optOutRules: data.applicationId && data.applicationId !== template.applicationId ? [] : undefined,
signatureId: data.signatureId,
name: data.name,
content: data.content,
category: data.category,
auditStatus,
rejectReason: auditStatus === 'pending' ? null : undefined,
billingUnits: data.content ? estimateBillingUnits(data.content) : undefined,
variables: variables
? {
create: variables.map((variable) => ({
name: variable.name,
example: variable.example,
required: variable.required ?? true,
})),
}
: undefined,
},
include: { variables: true, application: true, tenant: true, signature: true },
});
});
return this.prisma
.$transaction(async (tx) => {
if (variables) {
await tx.templateVariable.deleteMany({ where: { templateId } });
}
return tx.smsTemplate.update({
where: { id: templateId },
data: {
applicationId: data.applicationId,
optOutRules: data.applicationId && data.applicationId !== template.applicationId ? [] : undefined,
signatureId: data.signatureId,
name: data.name === undefined ? undefined : this.templateName(data.name),
content: data.content,
category: data.category,
auditStatus,
rejectReason: auditStatus === 'pending' ? null : undefined,
billingUnits: data.content ? estimateBillingUnits(data.content) : undefined,
variables: variables
? {
create: variables.map((variable) => ({
name: variable.name,
example: variable.example,
required: variable.required ?? true,
})),
}
: undefined,
},
include: { variables: true, application: true, tenant: true, signature: true },
});
})
.catch(templateWriteError);
}
private templateName(value: string) {
if (typeof value !== 'string' || !value.trim()) throw new BadRequestException('模板名称不能为空');
return value.trim();
}
async updateClientTemplate(templateId: string, data: UpdateSmsTemplateDto, tenantId?: string) {
@@ -2397,3 +2397,8 @@ Webhook需在当前受支持Node运行时通过真实HTTPS投递;SSRF校验后
## 2026-09-21 未报备签名统计编码兼容修正
保持既有未报备签名定义:规范正文开头签名在当前企业应用有效签名库不存在才计入。实时统计和日报聚合须同时兼容现有SQL_ASCII及UTF8数据库;不能因汉字共享编码字节漏统计,也不能放宽空签名、嵌套括号或非规范开头限制。不更改分页、权限、归属、数据库编码或已冻结历史报表。根因和本地验收见[排查及修复记录](unreported-signature-diagnosis-20260921.md)。
## 2026-09-21 六项运营功能更正
模板签名使用通用可搜索下拉框;变量名仅ASCII英文字母和数字1~32位,示例内容可中文。按用户最终更正,同一企业应用下模板名称唯一,与签名无关;名称去首尾空格,非deleted状态占用名称,跨应用同名允许,创建/编辑/恢复/并发均受约束。首页恢复北京时间今日小时发送曲线,置于企业消费排行上方;未知筛选包含submitted与历史unknownsubmitted显示提交成功;签名通道质量列表删除通道提交列,详情保留;报备明细企业/应用/通道/对象四条件独立且AND组合。通道移除运营商须先提示并确认,然后事务性移除该运营商全部省网/全国组成员并审计,保留其他组成员及路由,禁止并发重新挂回不兼容成员。此项替代2026-09-20仅跳过不移除的规则。设计、迁移兼容与验收见[六项整改](operations-six-fixes-20260921.md)。授权代码提交推送,未授权环境部署。
+34
View File
@@ -0,0 +1,34 @@
# 2026-09-21 六项运营功能整改
状态:实现及本机真实接口、页面验收完成,提交推送收尾。授权:本地修改、测试、提交、推送;不部署、不发送短信、不修改线上业务配置。基线main 28951b42026-09-21 19:25前回读实际远端main 001d5f2;推送将包含前一轮未报备统计修复及本次提交,其他会话未提交修改保留。
## 规则与实现范围
1. 模板:运营端和客户端复用Select的searchable;变量名仅ASCII英文字母和数字,保留1~32字符上限,不接受下划线、空格、汉字或其他符号,数字可以开头。自定义输入、正文占位符、API变量配置均校验;变量示例值仍可使用中文。已有模板不自动改写,编辑涉及变量/正文时按新规则校验。按用户随后更正,以“企业应用×模板名称”唯一,签名不同也不能同名;名称去首尾空格,非deleted状态均占用名称,已删除模板不占用,新建/编辑/恢复和并发保存由数据库部分唯一索引兜底,API返回明确中文冲突提示。不同应用同名允许。2026-09-21预生产只读检查有效模板按applicationId+btrim(name)重名组0;迁移不自动重命名或删除任何模板,目标环境若有新冲突则阻断迁移。
2. 首页:恢复今日24小时发送曲线,位于企业消费排行上方。沿用原曲线口径,按北京时间queuedAt小时汇总号码级业务短信数及其中最终delivered数,不把供应商补发计为新业务短信;来自真实PG当日范围查询,空小时补0,接口失败显示不可用/旧数据提示。通过现有首页summary加载,不重新调用旧的全量dashboard接口;返回hourlySendTrend,指标卡及其他快照逻辑保留。
3. 短信记录:“未知”筛选包含提交成功尚无最终送达结果的submitted以及历史unknown;不包含成功/失败/排队/拒绝,既有其他条件取交集。submitted显示“提交成功”,不变更真实业务状态或回执。
4. 签名质量:仅删除“签名通道发送质量”列表的通道提交列,调整该表统计说明;保留详情的提交尝试数及后端指标,避免改变其他统计口径。
5. 通道报备明细:拆分企业、应用、通道、报备对象四个独立文本搜索框,AND组合且各自仅匹配对应字段。报备对象按当前类型匹配签名或引流站点/URL。保留旧keyword接口兼容、时间/状态/运营商/类型条件、虚拟未报备行、详情/导出及分页,查询/重置回第一页,翻页使用已应用条件。
6. 通道运营商缩减:保存前列出移除的运营商并要求确认影响,提示将从这些运营商的全部通道组移除(含省网/全国),可能导致该组无可用通道;重新勾选/取消不写库。后端同一事务更新通道、按通道组carrier移除成员、记录移除关系审计,其他运营商组/组本体/企业应用路由/报备历史保持。能力变更不触发无关重连。成员写入的数据库约束锁定通道并检查组运营商兼容性,阻止并发通道组保存重新挂回已移除能力。事务失败整体回滚。替代此前“缩减能力仅跳过、不移除引用”的规则。
## 数据、权限与兼容
新增迁移仅增加模板名称部分唯一索引和通道组成员写入兼容性约束,无历史重写、余额/收费调整。迁移失败不自动resolve或清数据。应用回退不移除索引/约束,旧代码写入不兼容关系会被数据库拒绝。沿用既有运营端/客户端登录、权限及租户限定;前端提示不代替服务端校验。模板名称冲突返回400而非500。
## 验收
真实本机隔离PG全迁移、API与必要Redis;模板并发创建/更新、自编辑、跨应用、软删除重用、非法变量与中文示例;小时边界/零点/重复Submit;未知筛选的命中与排除;四搜索项组合及分页虚拟行;运营商移除、取消、回滚、并发成员写入、审计和保留项。页面1600×1000、1366×768、390×844首次进入/刷新/跨路由和相关交互,检查控制台及截图。Browser插件/对应browser技能不在清单,按前端测试技能使用已有Playwright,不安装依赖。定向+前后端全量、覆盖率、TypeScript/生产构建、格式/lint/样式/包体及diff门禁;精确提交候选与工作区结果区分。实际结果收尾追加,不将方案当已完成。
## 2026-09-21 本轮结果
- 根因:签名Select未启用通用搜索;变量校验允许下划线且前端缺少输入拦截;模板无应用名称唯一约束;新首页未接回旧小时序列;未知条件直接查unknown遗漏submitted;签名质量列表保留了不需要的列;报备查询只提供合并keyword;通道更新此前明确保留失配的通道组引用。
- 真实验收脚本:`tools/testing/verify-operations-six-fixes.mjs`,仅允许localhost专用数据库名。PG16.14隔离新库115项迁移通过(113项基线和本轮2项,不含其他会话未提交迁移);Redis专用16452,真实Nest全局认证、PG、HTTP、前端production构建。无需文件上传/发送,未启动MinIO和Gateway,无任何网络短信发送。Redis5为既有本机版本,BullMQ版本建议提示保留;本轮只验证会话和配置查询,不作为队列/Gateway验收。
- 模板:运营与客户端均拒绝同应用跨签名重名;两个并发创建只有一个成功;首尾空格、自编辑、编辑重名、跨应用、删除后重用及恢复冲突通过;非法变量、数字开头和中文示例通过;未登录和客户端访问运营接口被拒绝。数据库唯一索引覆盖写竞争,迁移不自动处理历史重名。
- 小时与短信:真实24小时数据,北京零点包含、前日末排除、空桶为0;同业务多条供应商提交记录不增加小时业务量;真实首页summary一致。未知筛选只含submitted/unknown,浏览器真实请求参数为unknown且展示提交成功。
- 报备:企业/应用/通道/对象同时匹配;逐项不匹配均空;虚拟未报备记录保留,分页总数和第二页不同记录验证通过。签名质量真实页面三种尺寸均无通道提交表头。
- 通道:删除联通时全国与湖北成员均移除,移动/电信及4条应用路由保留,审计记录2条删除关系;强制审计失败触发整体回滚;真实第二数据库连接在通道更新期间写成员等待,提交后被兼容约束拒绝。页面不确认禁止保存、确认后可保存、关闭取消不写库。提示样式由同目录ChannelFormModal.css直接归属并登记所有权,不改历史CSS基线。
- 页面:1600×1000、1366×768、390×844覆盖首页刷新/图表位置、四个搜索框、模板签名搜索及粘贴非法变量、通道警示与取消、质量列表、未知筛选、跨路由;无pageerror。18张截图在`C:/Users/hectorzhao/AppData/Local/Temp/cmpp-operations-six-20260921-final/`,人工查看桌面首页与移动通道警示。截图为本机真实测试数据,不是预生产页面。
- 从暂存区导出精确候选`C:/cmpp-platform-local/operations-six-candidate-20260921`:前端38组179项、覆盖率语句88.48/分支85.09/函数84/行88.19;后端87组935项、覆盖率68.02/53.71/68.98/70.76,均达到现有门禁;前后端TypeScript与生产构建通过。lint无错误,保留10项既有any/Hook依赖警告;格式、样式、CSS所有权与15项治理测试、包体和staged diff检查通过,入口107.46KiB gzip低于250KiB预算。Gateway未改,不执行Go或线上链路测试。
- 原始日志:工作区`.local-data/operations-six-20260921/``C:/cmpp-platform-local/operations-six-*.log`;最终真实验收`operations-six-real-verified.log`。最初后端事务mock、浏览器控件定位/手机隐藏表头等待错误已修正;工作区并发覆盖率曾15项超时,精确候选maxWorkers=2、testTimeout=15000全通过。Windows导出CRLF造成格式检查失败,改用本次checkout-index进程级core.autocrlf=false导出提交内LF后通过,未改全局配置。旧拒收脚本中通道缩减断言同步新规则,未执行其发送命令生成流程。
- 边界:本次交付代码和两项迁移,未部署测试或预生产;目标环境真实页面、真实历史数据量下首页查询耗时以及迁移时新增重名检查仍需在获授权的发布流程核验。应用回退不会移除已执行的数据库约束。
- 增量覆盖率门禁另行执行87组935项通过,语句87.98/分支77.03/函数95.91/行91.22;专用PG和Redis已停止,隔离数据、产物和失败/成功日志保留。
+15
View File
@@ -5742,3 +5742,18 @@ TC-SQA-0114:真实隔离PG覆盖核心日期/日报/长短信/事务/分页
| TC-UNREPORTED-ENC-004 | 已冻结日报在源记录改变后普通生成跳过、补建覆盖拒绝,原内容保持;隔离测试不创建任何供应商Submit。 |
执行:本机PostgreSQL16.14两编码各113项提交内迁移,verify-unreported-signature-encoding.mjs全部通过;真实Nest控制器/服务/PG,不含全局登录鉴权、浏览器及在线发送。完整结果和证据见[本轮记录](unreported-signature-diagnosis-20260921.md)。
## 2026-09-21 六项运营整改用例
| 编号 | 验收要求 |
|---|---|
| TC-OPS6-001 | 运营端/客户端签名可搜索;变量仅英文数字,汉字/符号/空格/未闭合/重复均拒绝,数字开头和中文示例可保存。 |
| TC-OPS6-002 | 同应用跨签名同名拒绝;trim名称后比较;自编辑正常,跨应用允许;并发只一个成功;软删除可重用,恢复冲突拒绝。 |
| TC-OPS6-003 | 首页真实24小时曲线在消费排行上方;北京时间日边界和零桶正确;重复供应商提交不增加业务短信量,刷新仍真实读取。 |
| TC-OPS6-004 | 未知筛选匹配submitted/unknown,排除delivered/failed/queued/rejected,真实页面请求unknown且显示提交成功。 |
| TC-OPS6-005 | 签名通道质量表头无通道提交列,详情指标不删;报备四个搜索框独立字段AND组合,过滤先于分页且保留虚拟未报备行。 |
| TC-OPS6-006 | 移除运营商前明确提示并确认;取消不修改。全国/省网成员均移除,其他组/路由保留且审计完整;事务故障整体回滚,并发成员写入等待后拒绝失配。 |
| TC-OPS6-007 | 三种尺寸1600×1000、1366×768、390×844覆盖相关页面、刷新、路由、搜索/校验/确认/取消,无pageerror。 |
本轮真实本机PG115项迁移、Redis、全Nest认证HTTP和production前端通过;脚本verify-operations-six-fixes.mjs,精确候选前端179/后端935项通过。目标环境未部署未验收,迁移冲突和历史数据量性能仍须发布时核验。详见[执行证据](operations-six-fixes-20260921.md)。
+9
View File
@@ -5221,3 +5221,12 @@ API全量81套/880项通过并达覆盖率门禁(语句67.73%、分支52.89%
代码与需求口径一致,相关需求和TC-UNREPORTED-ENC-001004同步。原始证据.local-data/unreported-signature-20260921/,详情见[诊断及修复记录](unreported-signature-diagnosis-20260921.md)。本轮仅本地修改和提交,无推送、部署或历史重算;不包含其他会话草稿。真实HTTP为最小控制器验收,未注册应用全局登录鉴权,未做浏览器或线上页面验收;前端/Go未变不重跑。预生产仍需单独授权部署后验收。
提交前从暂存区导出的精确候选另行通过API86套922项、覆盖率68.05/53.73/69.02/70.77和生产构建;候选产物在两种编码新库重新执行完整真实PG/HTTP/日报脚本通过。前述990项含他轮未提交测试,不作为本次精确提交测试数。最终candidate-api-coverage.log、candidate-build.log及real-*-candidate.log留档;专用PG已停止,数据保留。
## 2026-09-21 六项运营整改:实现、隔离真实验收与提交推送
用户授权本轮修改、提交、推送,并明确模板唯一规则改为企业应用×模板名称。六项代码完成,设计先行并同步需求及TC-OPS6-001~007;其中通道移除组成员替代此前仅跳过规则。新增应用模板名部分唯一索引和通道组成员兼容约束,历史数据不重写;签名搜索/变量校验、首页小时曲线、未知映射、质量列删除、报备四条件、通道移除提示/事务均已实现。
本机PG16.14独立数据库115项迁移、真实Redis、全Nest认证API、production前端通过;并发模板只一个成功,跨签名/跨应用/软删除/恢复符合最终口径;通道省网/全国移除、故障回滚、第二数据库连接并发失配拒绝通过。三尺寸18张真实页面截图和刷新/路由/相关交互通过,无pageerror,未发送短信、未启动Gateway。精确暂存候选前端38组179项、后端87组935项,覆盖率/构建/类型/lint/格式/样式/包体/diff门禁通过;遗留10条原有lint警告。并发覆盖率超时和浏览器脚本定位失败保留,降低并发及修正定位后复验通过。原工作区后端1003项含他轮未提交测试,不作为提交内数量。
证据及未验收项见[六项整改记录](operations-six-fixes-20260921.md)。本轮Git将包含28951b4未报备签名修复及本次提交,保护其他会话所有未提交内容;推送结果以最终远端精确SHA回读为准。本轮未部署测试或预生产,线上页面、目标历史量下曲线查询性能与迁移时是否新出现重名不冒充已验收。
+4
View File
@@ -323,6 +323,10 @@ export const adminChannelsReportsApi = {
status?: string;
reportType?: 'signature' | 'drainage';
keyword?: string;
enterpriseKeyword?: string;
applicationKeyword?: string;
channelKeyword?: string;
objectKeyword?: string;
createdAtFrom?: string;
createdAtTo?: string;
page: number;
+1
View File
@@ -16,6 +16,7 @@ export type HomeSummary = {
profitCents: number;
profitRate: number;
};
hourlySendTrend: Array<{ hour: number; label: string; submittedCount: number; successCount: number }>;
taskCount: number;
pendingAudits: {
enterpriseCertifications: number;
+1 -8
View File
@@ -190,13 +190,6 @@ function AnalyticsPanel({ kind }: { kind: string }) {
align: 'right',
render: (record) => record.total.toLocaleString('zh-CN'),
},
{
key: 'channelSubmitTotal',
title: '通道提交',
width: '110px',
align: 'right',
render: (record) => record.channelSubmitTotal.toLocaleString('zh-CN'),
},
{
key: 'successCount',
title: '送达成功',
@@ -339,7 +332,7 @@ function AnalyticsPanel({ kind }: { kind: string }) {
</div>
<div className="signature-quality-card__note">
<strong></strong>
</div>
<Table
columns={signatureColumns}
@@ -19,6 +19,11 @@ import {
} from '@/components/ui';
import { formatDateTime } from '@/utils/dateTime';
import { replaceLeadingSmsSignature } from '@/utils/smsSignature';
import {
templateVariableError,
templateVariableNameHint,
templateVariableNamePattern,
} from '@/utils/templateVariables';
import { Edit3, Eye, Plus, Search } from 'lucide-react';
import { useEffect, useRef, useState } from 'react';
import { TemplateOptOutModal } from './TemplateOptOutModal';
@@ -102,6 +107,7 @@ function TemplateFormModal({
tenants: TenantOption[];
}) {
const [customVariable, setCustomVariable] = useState('');
const [customVariableError, setCustomVariableError] = useState('');
const [variablesOpen, setVariablesOpen] = useState(false);
const contentRef = useRef<HTMLTextAreaElement>(null);
const initialSignature = signatures.find((signature) => signature.id === item?.signatureId);
@@ -124,6 +130,7 @@ function TemplateFormModal({
});
const [initialForm] = useState(form);
const dirty = JSON.stringify(form) !== JSON.stringify(initialForm);
const variableError = templateVariableError(form.content);
const tenantApplications = applications.filter(
(application) => application.tenantId === form.tenantId && application.status !== 'deleted',
);
@@ -152,7 +159,11 @@ function TemplateFormModal({
}
function insertVariable(name: string) {
const normalized = name.trim();
const normalized = name;
if (!templateVariableNamePattern.test(normalized)) {
setCustomVariableError(templateVariableNameHint);
return;
}
if (!normalized) {
return;
}
@@ -183,7 +194,14 @@ function TemplateFormModal({
</Button>
<Button
disabled={!form.tenantId || !form.applicationId || !form.signatureId || !form.name || !form.content.trim()}
disabled={
!form.tenantId ||
!form.applicationId ||
!form.signatureId ||
!form.name.trim() ||
!form.content.trim() ||
Boolean(variableError)
}
onClick={() => onSubmit({ ...form, variables: currentVariables })}
>
@@ -224,6 +242,8 @@ function TemplateFormModal({
/>
<Select
label="签名"
searchable
searchPlaceholder="搜索短信签名"
onChange={(event) => selectSignature(event.target.value)}
options={[
{ label: '请选择签名', value: '' },
@@ -247,6 +267,7 @@ function TemplateFormModal({
/>
<Textarea
hint="模板内容必须以所选签名开头;选择或切换签名时系统会自动填入或替换完整签名,例如:【XX公司】验证码为${code}。"
error={variableError}
label="模板内容"
onChange={(event) => setContent(event.target.value)}
placeholder="请选择签名后填写正文,例如:尊敬的${name},您的验证码为${code}。"
@@ -276,12 +297,26 @@ function TemplateFormModal({
<h3></h3>
<div className="template-custom-variable">
<Input
onChange={(event) => setCustomVariable(event.target.value)}
aria-label="自定义变量名"
error={customVariableError}
maxLength={32}
onChange={(event) => {
if (/[^A-Za-z0-9]/.test(event.target.value)) {
setCustomVariableError(templateVariableNameHint);
return;
}
setCustomVariableError('');
setCustomVariable(event.target.value);
}}
placeholder="英文字符或数字"
value={customVariable}
/>
<Button
onClick={() => {
if (!templateVariableNamePattern.test(customVariable)) {
setCustomVariableError(templateVariableNameHint);
return;
}
insertVariable(customVariable);
setCustomVariable('');
}}
+2
View File
@@ -4,6 +4,7 @@ import { useNavigate } from 'react-router-dom';
import { Breadcrumb, Button, Modal, MoneyText, Table, Tag, type TableColumn } from '@/components/ui';
import './AdminHome.css';
import { HomeMetrics } from './home/HomeMetrics';
import { HourlySendChart } from './home/HourlySendChart';
import { homeApi } from '@/api/admin/home.api';
import type { HomeSummary } from '@/api/types/home';
import { formatAmount, moneyUnitsToYuan } from '@/utils/currency';
@@ -216,6 +217,7 @@ export function AdminHome() {
</div>
)}
<HomeMetrics key={dashboard?.snapshotToken ?? 'empty'} data={dashboard} />
<HourlySendChart data={dashboard?.hourlySendTrend} loading={loading && !dashboard} />
<div className="surface section-stack">
<div className="section-heading">
+36 -12
View File
@@ -176,7 +176,12 @@ export function AdminReportTasksPage() {
const [searchParams] = useSearchParams();
const initialStatus = searchParams.get('scope') === 'pending' ? 'pending' : 'all';
const [tasks, setTasks] = useState<ReportTask[]>([]);
const [keyword, setKeyword] = useState('');
const [search, setSearch] = useState({
enterpriseKeyword: '',
applicationKeyword: '',
channelKeyword: '',
objectKeyword: '',
});
const [dateRange, setDateRange] = useState<DateRangeValue>({});
const [reportType, setReportType] = useState('all');
const [status, setStatus] = useState(initialStatus);
@@ -196,7 +201,10 @@ export function AdminReportTasksPage() {
const requestId = useRef(0);
const [total, setTotal] = useState(0);
const [appliedFilters, setAppliedFilters] = useState({
keyword: '',
enterpriseKeyword: '',
applicationKeyword: '',
channelKeyword: '',
objectKeyword: '',
dateRange: {} as DateRangeValue,
reportType: 'all',
status: initialStatus,
@@ -210,7 +218,10 @@ export function AdminReportTasksPage() {
reportType: filters.reportType === 'all' ? undefined : (filters.reportType as 'signature' | 'drainage'),
status: filters.status === 'all' ? undefined : filters.status,
carrier: filters.carrier === 'all' ? undefined : filters.carrier,
keyword: filters.keyword || undefined,
enterpriseKeyword: filters.enterpriseKeyword || undefined,
applicationKeyword: filters.applicationKeyword || undefined,
channelKeyword: filters.channelKeyword || undefined,
objectKeyword: filters.objectKeyword || undefined,
createdAtFrom: filters.dateRange.start || undefined,
createdAtTo: filters.dateRange.end || undefined,
page: targetPage,
@@ -444,12 +455,22 @@ export function AdminReportTasksPage() {
</div>
{error ? <p className="form-error">{error}</p> : null}
<div className="surface admin-task-filter">
<Input
label="企业/应用/通道/报备对象"
onChange={(event) => setKeyword(event.target.value)}
placeholder="搜索报备明细"
value={keyword}
/>
{(
[
['enterpriseKeyword', '企业'],
['applicationKeyword', '应用'],
['channelKeyword', '通道'],
['objectKeyword', '报备对象'],
] as const
).map(([key, label]) => (
<Input
key={key}
label={label}
placeholder={`搜索${label}`}
value={search[key]}
onChange={(event) => setSearch((current) => ({ ...current, [key]: event.target.value }))}
/>
))}
<Select
label="报备类型"
onChange={(event) => setReportType(event.target.value)}
@@ -487,7 +508,7 @@ export function AdminReportTasksPage() {
<Button
icon={<Search size={16} />}
onClick={() => {
const filters = { keyword: keyword.trim(), dateRange, reportType, status, carrier };
const filters = { ...search, dateRange, reportType, status, carrier };
setAppliedFilters(filters);
if (page !== 1) setPage(1);
else loadData(1, filters);
@@ -497,13 +518,16 @@ export function AdminReportTasksPage() {
</Button>
<Button
onClick={() => {
setKeyword('');
setSearch({ enterpriseKeyword: '', applicationKeyword: '', channelKeyword: '', objectKeyword: '' });
setDateRange({});
setReportType('all');
setCarrier('all');
setStatus('all');
const filters = {
keyword: '',
enterpriseKeyword: '',
applicationKeyword: '',
channelKeyword: '',
objectKeyword: '',
dateRange: {} as DateRangeValue,
reportType: 'all',
status: 'all',
+3 -3
View File
@@ -105,7 +105,7 @@ describe('report workbench pages', () => {
'100 条/页',
]);
await user.click(screen.getByRole('option', { name: '25 条/页' }));
await user.type(screen.getByRole('textbox', { name: '企业/应用/通道/报备对象' }), '测试企业');
await user.type(screen.getByRole('textbox', { name: '企业' }), '测试企业');
await user.click(screen.getByRole('button', { name: '查询' }));
await user.click(screen.getByRole('button', { name: '下一页' }));
await screen.findByText('签名2-1');
@@ -119,7 +119,7 @@ describe('report workbench pages', () => {
expect.objectContaining({
page: 1,
pageSize: 100,
keyword: '测试企业',
enterpriseKeyword: '测试企业',
signatureId: 'signature-filter',
status: 'pending',
}),
@@ -133,7 +133,7 @@ describe('report workbench pages', () => {
await user.click(screen.getByRole('option', { name: `${size} 条/页` }));
await waitFor(() =>
expect(adminApi.listReportDetailsPage).toHaveBeenLastCalledWith(
expect.objectContaining({ page: 1, pageSize: size, keyword: '测试企业' }),
expect.objectContaining({ page: 1, pageSize: size, enterpriseKeyword: '测试企业' }),
),
);
}
@@ -0,0 +1,17 @@
.sms-channel-form .sms-channel-form__carrier-warning {
grid-column: 1 / -1;
padding: var(--space-4);
border: 1px solid var(--color-warning);
border-radius: var(--radius-md);
background: var(--color-warning-soft);
}
.sms-channel-form .sms-channel-form__carrier-warning p {
margin: 0 0 var(--space-3);
}
.sms-channel-form .sms-channel-form__carrier-warning label {
display: flex;
align-items: flex-start;
gap: var(--space-2);
}
+31 -1
View File
@@ -1,4 +1,5 @@
import { useState } from 'react';
import './ChannelFormModal.css';
import { Button, Input, Modal, Select } from '@/components/ui';
import { isValidMoneyInput, moneyUnitsToYuan, yuanToMoneyUnits } from '@/utils/currency';
import { baseCarrierOptions, cmppVersionOptions, regionOptions } from './channelModel';
@@ -17,6 +18,8 @@ export function ChannelFormModal({
const [name, setName] = useState(channel?.name ?? '');
const [carriers, setCarriers] = useState<BaseCarrier[]>(channel?.carriers ?? ['mobile']);
const [carrierError, setCarrierError] = useState('');
const [carrierRemovalConfirmed, setCarrierRemovalConfirmed] = useState(false);
const removedCarriers = (channel?.carriers ?? []).filter((carrier) => !carriers.includes(carrier));
const [unitPrice, setUnitPrice] = useState(channel ? moneyUnitsToYuan(channel.unitPrice).toFixed(4) : '0.0300');
const [unitPriceError, setUnitPriceError] = useState('');
const [region, setRegion] = useState(channel?.sendRegion ?? '全国');
@@ -45,6 +48,10 @@ export function ChannelFormModal({
setCarrierError('至少选择一个运营商');
return;
}
if (removedCarriers.length && !carrierRemovalConfirmed) {
setCarrierError('请先确认移除运营商对通道组的影响');
return;
}
if (!isValidMoneyInput(unitPrice)) {
setUnitPriceError('单价必须是非负金额,且最多保留小数点后 4 位');
return;
@@ -93,7 +100,9 @@ export function ChannelFormModal({
<Button onClick={onClose} variant="ghost">
</Button>
<Button onClick={submit}></Button>
<Button disabled={removedCarriers.length > 0 && !carrierRemovalConfirmed} onClick={submit}>
</Button>
</>
}
onClose={onClose}
@@ -128,6 +137,7 @@ export function ChannelFormModal({
: [...current, item.value],
);
setCarrierError('');
setCarrierRemovalConfirmed(false);
}}
type="checkbox"
/>
@@ -136,6 +146,26 @@ export function ChannelFormModal({
))}
{carrierError ? <small className="form-error">{carrierError}</small> : null}
</div>
{removedCarriers.length > 0 ? (
<div className="sms-channel-form__carrier-warning" role="alert">
<p>
{baseCarrierOptions
.filter((item) => removedCarriers.includes(item.value))
.map((item) => item.label)
.join('、')}
</p>
<label>
<input
type="checkbox"
checked={carrierRemovalConfirmed}
onChange={(event) => setCarrierRemovalConfirmed(event.target.checked)}
/>
</label>
</div>
) : null}
<Input
error={unitPriceError}
label="* 单价(元)"
+37
View File
@@ -0,0 +1,37 @@
import { useMemo } from 'react';
import type { EChartsOption } from 'echarts';
import { Chart } from '@/components/ui/Chart';
import type { HomeSummary } from '@/api/types/home';
export function HourlySendChart({ data, loading }: { data?: HomeSummary['hourlySendTrend']; loading: boolean }) {
const option = useMemo<EChartsOption>(
() => ({
color: ['#2563eb', '#16a34a'],
tooltip: { trigger: 'axis' },
legend: { data: ['提交短信', '发送成功'], bottom: 0 },
grid: { top: 20, left: 12, right: 16, bottom: 52, containLabel: true },
xAxis: { type: 'category', boundaryGap: false, data: data?.map((item) => item.label) ?? [] },
yAxis: { type: 'value', minInterval: 1 },
series: [
{ name: '提交短信', type: 'line', showSymbol: false, data: data?.map((item) => item.submittedCount) ?? [] },
{ name: '发送成功', type: 'line', showSymbol: false, data: data?.map((item) => item.successCount) ?? [] },
],
}),
[data],
);
return (
<section className="surface section-stack" aria-label="小时发送曲线">
<div className="section-heading">
<div>
<h2>线</h2>
<p className="muted"></p>
</div>
</div>
{data ? (
<Chart height={280} option={option} />
) : (
<p role="status">{loading ? '正在加载小时发送数据…' : '小时发送数据暂不可用'}</p>
)}
</section>
);
}
+36 -21
View File
@@ -1,8 +1,4 @@
import type {
SmsMessageRecord,
SmsMessageSegmentAudit,
SmsReceiptRecord,
} from '@/api/adminApi';
import type { SmsMessageRecord, SmsMessageSegmentAudit, SmsReceiptRecord } from '@/api/adminApi';
import { formatCents } from '@/utils/currency';
import type { DateRangeValue } from '@/components/ui';
import type { RouteRow } from './smsRecordTypes';
@@ -10,7 +6,7 @@ import type { RouteRow } from './smsRecordTypes';
export const statusLabelMap: Record<string, string> = {
delivered: '发送成功',
queued: '排队中',
submitted: '提交',
submitted: '提交成功',
submit_failed: '提交失败',
unknown: '未知',
failed: '送达失败',
@@ -91,8 +87,8 @@ export function getRecordStatusLabel(record: SmsMessageRecord) {
}
export function getReceiptNotice(record: SmsMessageRecord) {
const hasPlatformFailureReceipt = (record.receiptRecords ?? []).some((receipt) =>
receipt.gatewayMessageId.startsWith('PLATFORM:') && receipt.rawStatus === 'REJECTD',
const hasPlatformFailureReceipt = (record.receiptRecords ?? []).some(
(receipt) => receipt.gatewayMessageId.startsWith('PLATFORM:') && receipt.rawStatus === 'REJECTD',
);
if (hasPlatformFailureReceipt) {
const deliveries = (record.downstreamDeliveries ?? []).filter((item) => item.deliveryType === 'receipt');
@@ -131,9 +127,10 @@ export function buildRouteRows(record: SmsMessageRecord, segmentAudits: SmsMessa
return {
id: submitId,
attempt: Math.min(...ordered.map((segment) => segment.attempt)),
channel: ordered.find((segment) => segment.channel?.name)?.channel?.name
?? ordered.find((segment) => segment.channelId)?.channelId
?? '-',
channel:
ordered.find((segment) => segment.channel?.name)?.channel?.name ??
ordered.find((segment) => segment.channelId)?.channelId ??
'-',
channelGroup: submitById.get(submitId)?.channelGroupName ?? submitById.get(submitId)?.channelGroup?.name,
sentAt: sentTimes.sort()[0],
receiptAt: receiptTimes.sort()[receiptTimes.length - 1],
@@ -161,14 +158,16 @@ export function buildRouteRows(record: SmsMessageRecord, segmentAudits: SmsMessa
};
});
if (submitRows.length > 0) return submitRows;
return [{
id: record.id,
channel: record.channel?.name ?? record.channelId ?? '-',
sentAt: record.submittedAt ?? record.queuedAt,
receiptAt: record.deliveredAt,
receiptCode: receipts[0]?.rawStatus,
submitStatus: record.submitStatus,
}];
return [
{
id: record.id,
channel: record.channel?.name ?? record.channelId ?? '-',
sentAt: record.submittedAt ?? record.queuedAt,
receiptAt: record.deliveredAt,
receiptCode: receipts[0]?.rawStatus,
submitStatus: record.submitStatus,
},
];
}
function csvCell(value: unknown) {
@@ -178,7 +177,21 @@ function csvCell(value: unknown) {
export function downloadCsv(records: SmsMessageRecord[]) {
const rows = [
['消息编号', '企业', '应用', '提交时间', '手机号', '地区', '运营商', '计费条数', '金额', '通道', '状态', '回执时间', '短信内容'],
[
'消息编号',
'企业',
'应用',
'提交时间',
'手机号',
'地区',
'运营商',
'计费条数',
'金额',
'通道',
'状态',
'回执时间',
'短信内容',
],
...records.map((record) => [
record.messageId,
record.tenant?.name ?? record.tenantId,
@@ -195,7 +208,9 @@ export function downloadCsv(records: SmsMessageRecord[]) {
record.content,
]),
];
const blob = new Blob([`\uFEFF${rows.map((row) => row.map(csvCell).join(',')).join('\n')}`], { type: 'text/csv;charset=utf-8' });
const blob = new Blob([`\uFEFF${rows.map((row) => row.map(csvCell).join(',')).join('\n')}`], {
type: 'text/csv;charset=utf-8',
});
const url = URL.createObjectURL(blob);
const anchor = document.createElement('a');
anchor.href = url;
@@ -25,6 +25,24 @@ vi.mock('@/api/adminApi', () => ({
},
}));
describe('client template form', () => {
it('searches signatures and rejects invalid variable input and pasted placeholders', async () => {
render(<ClientTemplatesPage />);
await screen.findByText('测试模板');
fireEvent.click(screen.getByRole('button', { name: '编辑' }));
fireEvent.click(screen.getByRole('button', { name: /短信签名/ }));
fireEvent.change(screen.getByPlaceholderText('搜索短信签名'), { target: { value: '不存在' } });
expect(screen.getByText('无匹配选项')).toBeInTheDocument();
fireEvent.change(screen.getByPlaceholderText('搜索短信签名'), { target: { value: '签名' } });
fireEvent.click(screen.getByRole('option', { name: '【签名】' }));
fireEvent.click(screen.getByRole('button', { name: '插入变量' }));
const input = screen.getByRole('textbox', { name: '自定义变量名' });
fireEvent.change(input, { target: { value: '姓名' } });
expect(input).toHaveValue('');
fireEvent.change(input, { target: { value: 'code123' } });
expect(input).toHaveValue('code123');
fireEvent.change(screen.getByRole('textbox', { name: /模板内容/ }), { target: { value: '【签名】${中文}' } });
expect(screen.getByRole('button', { name: '提交审核' })).toBeDisabled();
});
it('orders requested fields and keeps standard deletion action', async () => {
render(<ClientTemplatesPage />);
await screen.findByText('测试模板');
+37 -3
View File
@@ -9,6 +9,11 @@ import {
} from '@/api/adminApi';
import { formatDateTime } from '@/utils/dateTime';
import { replaceLeadingSmsSignature } from '@/utils/smsSignature';
import {
templateVariableError,
templateVariableNameHint,
templateVariableNamePattern,
} from '@/utils/templateVariables';
import './ClientTemplatesPage.css';
type TemplateVariable = {
@@ -79,6 +84,7 @@ function TemplateModal({
signatures: ClientSmsSignatureView[];
}) {
const [customVariable, setCustomVariable] = useState('');
const [customVariableError, setCustomVariableError] = useState('');
const [variablesOpen, setVariablesOpen] = useState(false);
const contentRef = useRef<HTMLTextAreaElement>(null);
const initialSignature = signatures.find((signature) => signature.id === item?.signatureId);
@@ -99,6 +105,7 @@ function TemplateModal({
});
const [initialForm] = useState(form);
const dirty = JSON.stringify(form) !== JSON.stringify(initialForm);
const variableError = templateVariableError(form.content);
const application = applications.find((candidate) => candidate.id === form.applicationId);
const availableSignatures = signatures.filter(
(signature) =>
@@ -125,7 +132,11 @@ function TemplateModal({
}
function insertVariable(name: string) {
const normalized = name.trim();
const normalized = name;
if (!templateVariableNamePattern.test(normalized)) {
setCustomVariableError(templateVariableNameHint);
return;
}
if (!normalized) return;
const token = `\${${normalized}}`;
const textarea = contentRef.current;
@@ -154,7 +165,13 @@ function TemplateModal({
</Button>
<Button
disabled={!form.applicationId || !form.signatureId || !form.name || !form.content.trim()}
disabled={
!form.applicationId ||
!form.signatureId ||
!form.name.trim() ||
!form.content.trim() ||
Boolean(variableError)
}
onClick={() => onSubmit({ ...form, variables })}
>
@@ -184,6 +201,8 @@ function TemplateModal({
/>
<Select
label="短信签名"
searchable
searchPlaceholder="搜索短信签名"
onChange={(event) => selectSignature(event.target.value)}
options={[
{ label: '请选择签名', value: '' },
@@ -194,6 +213,7 @@ function TemplateModal({
/>
<Textarea
hint="模板内容必须以所选签名开头;选择或切换签名时系统会自动填入或替换完整签名,例如:【XX公司】验证码为${code}。"
error={variableError}
label="模板内容"
onChange={(event) => setContent(event.target.value)}
placeholder="请选择签名后填写正文,变量格式:${code}"
@@ -223,12 +243,26 @@ function TemplateModal({
<h3></h3>
<div className="template-custom-variable">
<Input
onChange={(event) => setCustomVariable(event.target.value)}
aria-label="自定义变量名"
error={customVariableError}
maxLength={32}
onChange={(event) => {
if (/[^A-Za-z0-9]/.test(event.target.value)) {
setCustomVariableError(templateVariableNameHint);
return;
}
setCustomVariableError('');
setCustomVariable(event.target.value);
}}
placeholder="英文字符或数字"
value={customVariable}
/>
<Button
onClick={() => {
if (!templateVariableNamePattern.test(customVariable)) {
setCustomVariableError(templateVariableNameHint);
return;
}
insertVariable(customVariable);
setCustomVariable('');
}}
+11
View File
@@ -0,0 +1,11 @@
import { describe, expect, it } from 'vitest';
import { templateVariableError } from './templateVariables';
describe('template variable editor validation', () => {
it.each(['${code123}', '${123}', '中文正文${Code}'])('accepts %s', (value) => {
expect(templateVariableError(value)).toBeUndefined();
});
it.each(['${姓名}', '${code_1}', '${a b}', '${}', '${code', '${x}${x}', '${12}'])('rejects %s', (value) => {
expect(templateVariableError(value)).toBeTruthy();
});
});
+18
View File
@@ -0,0 +1,18 @@
export const templateVariableNamePattern = /^[A-Za-z0-9]{1,32}$/;
export const templateVariableNameHint = '变量名仅允许英文字母和数字,长度1至32位';
export function templateVariableError(content: string): string | undefined {
const names = new Set<string>();
let cursor = 0;
while (true) {
const start = content.indexOf('${', cursor);
if (start < 0) return undefined;
const end = content.indexOf('}', start + 2);
if (end < 0) return '模板变量未闭合';
const name = content.slice(start + 2, end);
if (!templateVariableNamePattern.test(name)) return templateVariableNameHint;
if (names.has(name)) return `模板变量 ${name} 重复`;
names.add(name);
cursor = end + 1;
}
}
+6
View File
@@ -429,6 +429,12 @@
"roots": [
"template-optout"
]
},
{
"file": "src/apps/admin/channels/ChannelFormModal.css",
"owners": ["src/apps/admin/channels/ChannelFormModal.tsx"],
"stylelintLegacy": false,
"roots": ["sms-channel-form"]
}
]
}
@@ -0,0 +1,471 @@
import assert from 'node:assert/strict';
import { createRequire } from 'node:module';
import { randomBytes, randomUUID } from 'node:crypto';
import { mkdirSync } from 'node:fs';
import path from 'node:path';
const url = new URL(process.env.OPERATIONS_TEST_DATABASE_URL || '');
assert(['127.0.0.1', 'localhost'].includes(url.hostname) && url.pathname.startsWith('/cmpp_qa_operations_six_'));
const redis = new URL(process.env.OPERATIONS_TEST_REDIS_URL || 'redis://127.0.0.1:16452');
assert(['127.0.0.1', 'localhost'].includes(redis.hostname));
Object.assign(process.env, {
NODE_ENV: 'test',
DATABASE_URL: url.toString(),
REDIS_URL: redis.toString(),
HTTP_API_MASTER_KEY: randomBytes(32).toString('hex'),
MINIO_ENDPOINT: '127.0.0.1:19400',
GATEWAY_CONTROL_URL: 'http://127.0.0.1:19401',
SIGNATURE_ANALYTICS_ENABLED: 'false',
HOME_DASHBOARD_ENABLED: 'false',
GATEWAY_STARTUP_RECONNECT_DELAY_MS: '3600000',
GATEWAY_CONNECTION_RECONCILER_DISABLED: 'true',
GATEWAY_CONNECTING_TIMEOUT_SCANNER_DISABLED: 'true',
SMS_RECEIPT_TIMEOUT_SCAN_ENABLED: 'false',
SMS_SCHEDULED_DISPATCH_SCAN_ENABLED: 'false',
CMPP_INBOUND_LONG_MESSAGE_SCAN_ENABLED: 'false',
UPSTREAM_RECEIPT_INBOX_SCAN_ENABLED: 'false',
CMPP_DOWNSTREAM_REQUEUE_TASK_SCAN_ENABLED: 'false',
CMPP_PROCESS_ROLE: 'api',
API_ENABLE_SEND_WORKER: 'false',
CMPP_INBOUND_WORKFLOW_WORKER_ENABLED: 'false',
});
// Only isolated fixtures are written. No send API, worker, or Gateway is started.
const require = createRequire(new URL('../../api/package.json', import.meta.url));
require('reflect-metadata');
Object.defineProperty(BigInt.prototype, 'toJSON', {
value() {
return Number(this);
},
configurable: true,
});
const { NestFactory } = require('@nestjs/core');
const { AppModule } = require('./dist/app.module');
const { PrismaService } = require('./dist/prisma/prisma.service');
const { UsersService } = require('./dist/users/users.service');
const { SessionService } = require('./dist/auth/session.service');
const { HomeProjection } = require('./dist/home-dashboard/home-projection');
const { todayKey, startOfDay } = require('./dist/signature-analytics/analytics-date');
const { hourlySendTrend } = require('./dist/home-dashboard/home-read');
const app = await NestFactory.create(AppModule, { logger: ['error'] });
app.setGlobalPrefix('api');
let browser;
const pass = (name) => console.log('PASS', name);
try {
await app.listen(Number(process.env.OPERATIONS_TEST_PORT || 17451), '127.0.0.1');
const base = (await app.getUrl()) + '/api';
const db = app.get(PrismaService),
sessions = app.get(SessionService),
users = app.get(UsersService);
const stamp = randomUUID().slice(0, 8),
key = () => randomUUID();
const tenant = await db.tenant.create({ data: { name: '六项验收企业' + stamp, code: key() } });
const makeApp = (name) =>
db.smsApplication.create({
data: {
tenantId: tenant.id,
name,
cmppAccount: key(),
cmppEnterpriseCode: '000001',
secretHash: 'unused',
interfaceEnabled: false,
},
});
const application = await makeApp('通知应用' + stamp),
otherApp = await makeApp('其他应用' + stamp);
const makeSignature = (applicationId, name) =>
db.smsSignature.create({ data: { tenantId: tenant.id, applicationId, name, auditStatus: 'approved' } });
const signature = await makeSignature(application.id, '【验收甲】'),
signature2 = await makeSignature(application.id, '【验收乙】');
const otherSignature = await makeSignature(otherApp.id, '【其他应用】');
const user = await users.create({
username: 'operations' + stamp,
email: stamp + '@example.invalid',
displayName: '隔离验收',
password: randomBytes(24).toString('hex'),
roleCode: 'platform_admin',
});
const session = await sessions.create(user.id, 'admin', 0),
cookie = sessions.cookieName('admin');
const headers = { 'content-type': 'application/json', cookie: cookie + '=' + session.token };
const req = async (route, body, method = body === undefined ? 'GET' : 'PUT', head = headers) => {
const response = await fetch(base + route, {
method,
headers: head,
...(body === undefined ? {} : { body: JSON.stringify(body) }),
});
return { status: response.status, data: await response.json() };
};
const templateBody = (name, sig = signature) => ({
tenantId: tenant.id,
applicationId: sig.applicationId,
signatureId: sig.id,
name,
content: sig.name + '验证码${code123}',
variables: [{ name: 'code123', example: '中文示例', required: true }],
});
const create = (body) => req('/admin/enterprise-templates', body, 'POST');
const customer = await users.create({
username: 'client' + stamp,
email: 'c' + stamp + '@example.invalid',
displayName: '隔离客户',
password: randomBytes(24).toString('hex'),
roleCode: 'enterprise_admin',
tenantId: tenant.id,
});
const clientSession = await sessions.create(customer.id, 'client', 0);
const clientHeaders = {
'content-type': 'application/json',
cookie: sessions.cookieName('client') + '=' + clientSession.token,
};
const first = await create(templateBody('通知模板' + stamp));
assert.equal(first.status, 201, JSON.stringify(first));
const duplicate = await create(templateBody(' 通知模板' + stamp + ' ', signature2));
assert.equal(duplicate.status, 400);
assert.match(duplicate.data.message, /同一.*应用|当前.*应用/);
assert.equal((await create(templateBody('通知模板' + stamp, otherSignature))).status, 201);
assert.equal((await req('/admin/enterprise-templates/' + first.data.id, { name: '通知模板' + stamp })).status, 200);
const concurrent = await Promise.all([
create(templateBody('并发' + stamp)),
create(templateBody('并发' + stamp, signature2)),
]);
assert.deepEqual(concurrent.map((r) => r.status).sort(), [201, 400]);
for (const variable of ['中文', 'a_b', 'a-b', 'a b', '12']) {
const body = templateBody('非法' + key());
body.content = signature.name + '${' + variable + '}';
body.variables[0].name = variable;
assert.equal((await create(body)).status, 400);
}
const numeric = templateBody('数字变量' + stamp);
numeric.content = signature.name + '${123}';
numeric.variables[0].name = '123';
assert.equal((await create(numeric)).status, 201);
const extra = await create(templateBody('修改前' + stamp));
assert.equal((await req('/admin/enterprise-templates/' + extra.data.id, { name: first.data.name })).status, 400);
await db.smsTemplate.update({ where: { id: extra.data.id }, data: { auditStatus: 'deleted' } });
assert.equal((await create(templateBody('修改前' + stamp))).status, 201);
assert.equal(
(await req('/admin/enterprise-templates/' + extra.data.id + '/status', { status: 'approved' }, 'POST')).status,
400,
);
assert.equal((await req('/admin/enterprise-templates', undefined, 'GET', {})).status, 401);
const clientBody = templateBody('客户模板' + stamp);
delete clientBody.tenantId;
assert.equal((await req('/client/templates', clientBody, 'POST', clientHeaders)).status, 201);
assert.equal(
(
await req(
'/client/templates',
{ ...clientBody, signatureId: signature2.id, content: signature2.name + '验证码${code123}' },
'POST',
clientHeaders,
)
).status,
400,
);
assert.equal(
(
await req(
'/client/templates',
{ ...clientBody, name: '非法客户模板', content: signature.name + '${中文}', variables: [{ name: '中文' }] },
'POST',
clientHeaders,
)
).status,
400,
);
assert.equal((await req('/admin/enterprise-templates', undefined, 'GET', clientHeaders)).status, 401);
pass(
'real API application/name uniqueness across signatures, concurrent saves, rename, restore, cross-application and variable validation',
);
const channel = await db.smsChannel.create({
data: {
code: key(),
name: '验收通道' + stamp,
carrier: 'mobile',
carriers: ['mobile', 'unicom', 'telecom'],
status: 'active',
gatewayHost: '127.0.0.1',
gatewayPort: 1,
account: key(),
passwordCipher: 'unused',
srcId: '1069',
},
});
const groups = [];
for (const [carrier, province] of [
['mobile', '全国'],
['unicom', '全国'],
['unicom', '湖北'],
['telecom', '全国'],
]) {
const group = await db.smsChannelGroup.create({
data: {
code: key(),
name: carrier + province + stamp,
carrier,
items: { create: { channelId: channel.id, carrier, province } },
},
});
groups.push(group);
await db.channelRouteRule.create({
data: { tenantId: tenant.id, applicationId: application.id, groupId: group.id, carrier },
});
}
const filters = {
enterpriseKeyword: tenant.name,
applicationKeyword: application.name,
channelKeyword: channel.name,
objectKeyword: signature.name,
pageSize: '1',
};
const details = await req('/admin/report-details?' + new URLSearchParams(filters));
assert.equal(details.status, 200);
assert.equal(details.data.total, 3);
assert.equal(details.data.items.length, 1);
assert.equal(details.data.items[0].virtual, true);
for (const field of ['enterpriseKeyword', 'applicationKeyword', 'channelKeyword', 'objectKeyword']) {
const empty = await req('/admin/report-details?' + new URLSearchParams({ ...filters, [field]: '不匹配' }));
assert.equal(empty.data.total, 0, field);
}
const page2 = await req('/admin/report-details?' + new URLSearchParams({ ...filters, page: '2' }));
assert.notEqual(page2.data.items[0].id, details.data.items[0].id);
pass('four independent AND search fields apply before pagination and retain virtual unreported rows');
const date = todayKey(new Date()),
start = startOfDay(date);
const beforeTrend = await hourlySendTrend(db, date);
const messages = [];
for (const [index, status] of ['submitted', 'unknown', 'delivered', 'failed', 'queued', 'rejected'].entries()) {
messages.push(
await db.smsMessageRecord.create({
data: {
tenantId: tenant.id,
applicationId: application.id,
signatureId: signature.id,
channelId: channel.id,
messageId: key(),
phoneNumber: '13800000000',
content: signature.name + '隔离记录',
status,
queuedAt: new Date(+start + (index ? 3600000 : 0)),
},
}),
);
}
await db.smsMessageRecord.create({
data: {
messageId: key(),
phoneNumber: '13800000000',
content: '前日',
status: 'delivered',
queuedAt: new Date(+start - 1),
},
});
await db.smsSubmitRecord.createMany({
data: [1, 2].map(() => ({
messageRecordId: messages[2].id,
channelId: channel.id,
submitId: key(),
submitStatus: 'submitted',
})),
});
const records = await req(
'/admin/operations/messages?' +
new URLSearchParams({ applicationId: application.id, status: 'unknown', page: '1', pageSize: '20' }),
);
assert.equal(records.status, 200);
assert.deepEqual(records.data.items.map((r) => r.status).sort(), ['submitted', 'unknown']);
const trend = await hourlySendTrend(db, date);
assert.equal(trend.length, 24);
assert.equal(trend[0].submittedCount - beforeTrend[0].submittedCount, 1);
assert.equal(trend[1].submittedCount - beforeTrend[1].submittedCount, 5);
assert.equal(trend[1].successCount - beforeTrend[1].successCount, 1);
assert.equal(trend[2].submittedCount - beforeTrend[2].submittedCount, 0);
await app.get(HomeProjection).tick();
const home = await req('/admin/operations/home/summary');
assert.equal(home.status, 200, JSON.stringify(home));
assert.deepEqual(home.data.hourlySendTrend, trend);
pass(
'unknown maps to submitted and legacy unknown; home uses Beijing hours, zero fill and business rows, not supplier retries',
);
// Force a failure after member deletion: the entire configuration transaction must roll back.
await db.$executeRawUnsafe(
`CREATE FUNCTION qa_reject_channel_audit() RETURNS trigger LANGUAGE plpgsql AS $$ BEGIN IF NEW.action='sms_channel.update' THEN RAISE EXCEPTION 'qa rollback'; END IF; RETURN NEW; END $$`,
);
await db.$executeRawUnsafe(
`CREATE TRIGGER qa_reject_channel_audit BEFORE INSERT ON "OperationLog" FOR EACH ROW EXECUTE FUNCTION qa_reject_channel_audit()`,
);
assert.equal((await req('/admin/channels/' + channel.id, { carriers: ['mobile', 'telecom'] })).status, 500);
assert.equal(await db.smsChannelGroupItem.count({ where: { channelId: channel.id } }), 4);
assert.deepEqual((await db.smsChannel.findUnique({ where: { id: channel.id } })).carriers, [
'mobile',
'unicom',
'telecom',
]);
await db.$executeRawUnsafe('DROP TRIGGER qa_reject_channel_audit ON "OperationLog"');
await db.$executeRawUnsafe('DROP FUNCTION qa_reject_channel_audit()');
assert.equal((await req('/admin/channels/' + channel.id, { carriers: ['mobile', 'telecom'] })).status, 200);
assert.equal(await db.smsChannelGroupItem.count({ where: { channelId: channel.id } }), 2);
assert.equal(await db.channelRouteRule.count({ where: { applicationId: application.id } }), 4);
const audit = await db.operationLog.findFirst({
where: { resourceId: channel.id, action: 'sms_channel.update' },
orderBy: { createdAt: 'desc' },
});
assert.equal(audit.detail.removedGroupItems.length, 2);
await assert.rejects(
db.smsChannelGroupItem.create({ data: { channelId: channel.id, groupId: groups[1].id, carrier: 'unicom' } }),
/not compatible/,
);
pass(
'carrier reduction is atomic, removes national/province memberships, preserves routes and other carriers, records audit and rejects stale membership writes',
);
const { Client } = require('pg');
const writer = new Client({ connectionString: url.toString() });
await writer.connect();
let settled = false,
insertion;
try {
await db.$transaction(async (tx) => {
await tx.smsChannel.update({ where: { id: channel.id }, data: { carriers: ['mobile'] } });
insertion = writer
.query('INSERT INTO "SmsChannelGroupItem" (id,"groupId","channelId",carrier) VALUES ($1,$2,$3,$4)', [
key(),
groups[3].id,
channel.id,
'telecom',
])
.then(
() => ({ success: true }),
(error) => ({ code: error.code }),
)
.finally(() => {
settled = true;
});
await new Promise((resolve) => setTimeout(resolve, 100));
assert.equal(settled, false);
await tx.smsChannelGroupItem.deleteMany({ where: { channelId: channel.id, groupId: groups[3].id } });
});
assert.equal((await insertion).code, '23514');
} finally {
await writer.end();
}
await db.smsChannel.update({ where: { id: channel.id }, data: { carriers: ['mobile', 'telecom'] } });
await db.smsChannelGroupItem.create({ data: { channelId: channel.id, groupId: groups[3].id, carrier: 'telecom' } });
pass('real concurrent member insertion waits for carrier change then rejects stale capability');
if (process.env.OPERATIONS_TEST_BROWSER_URL) {
const uiUrl = new URL(process.env.OPERATIONS_TEST_BROWSER_URL);
assert(['localhost', '127.0.0.1'].includes(uiUrl.hostname));
const { chromium } = await import(process.env.PLAYWRIGHT_MODULE || 'playwright');
const evidence = process.env.OPERATIONS_TEST_EVIDENCE_DIR;
assert(evidence && path.isAbsolute(evidence));
mkdirSync(evidence, { recursive: true });
browser = await chromium.launch({ channel: 'msedge', headless: true });
const context = await browser.newContext();
await context.addCookies([
{ name: cookie, value: session.token, url: uiUrl.origin, httpOnly: true, sameSite: 'Lax' },
]);
const auth = await req('/admin/auth/session');
assert.equal(auth.status, 200);
await context.addInitScript(
(value) => localStorage.setItem('cmpp-auth-session:admin', JSON.stringify(value)),
auth.data,
);
const page = await context.newPage(),
errors = [];
page.on('pageerror', (e) => errors.push(e.message));
for (const [width, height] of [
[1600, 1000],
[1366, 768],
[390, 844],
]) {
await page.setViewportSize({ width, height });
await page.goto(uiUrl.origin + '/admin');
await page.getByRole('heading', { name: '小时发送曲线', exact: true }).waitFor();
await page.locator('[aria-label="小时发送曲线"] canvas').waitFor();
const curve = await page.getByRole('heading', { name: '小时发送曲线', exact: true }).boundingBox();
const rank = await page.getByRole('heading', { name: /企业消费排行/ }).boundingBox();
assert(curve.y < rank.y);
await page.screenshot({ path: path.join(evidence, `home-${width}.png`), fullPage: true });
await page.reload();
await page.locator('[aria-label="小时发送曲线"] canvas').waitFor();
await page.goto(uiUrl.origin + '/admin/report-tasks');
for (const label of ['企业', '应用', '通道', '报备对象'])
await page.getByRole('textbox', { name: label, exact: true }).waitFor();
await page.getByRole('textbox', { name: '企业', exact: true }).fill(tenant.name);
await page.getByRole('button', { name: '查询', exact: true }).click();
await page.screenshot({ path: path.join(evidence, `reports-${width}.png`), fullPage: true });
await page.goto(uiUrl.origin + '/admin/enterprise-templates');
await page.getByRole('textbox', { name: '企业应用', exact: true }).fill(application.name);
await page.getByRole('textbox', { name: '模板名称', exact: true }).fill(first.data.name);
await page.getByRole('button', { name: '查询', exact: true }).click();
await page
.getByRole('article')
.filter({ hasText: first.data.name })
.filter({ hasText: application.name })
.getByRole('button', { name: '编辑', exact: true })
.click();
await page.getByRole('dialog').getByRole('button', { name: /^签名/ }).click();
await page.getByPlaceholder('搜索短信签名').fill('验收甲');
await page.getByRole('option', { name: signature.name, exact: true }).waitFor();
await page.screenshot({ path: path.join(evidence, `template-search-${width}.png`), fullPage: true });
await page.getByRole('option', { name: signature.name, exact: true }).click();
await page.getByRole('textbox', { name: /模板内容/ }).fill(signature.name + '${中文}');
assert(await page.getByRole('button', { name: '保存', exact: true }).isDisabled());
await page.getByRole('button', { name: '取消', exact: true }).click();
await page.getByRole('button', { name: '放弃并关闭', exact: true }).click();
await page.goto(uiUrl.origin + '/admin/channels');
await page.getByRole('textbox', { name: '通道名称', exact: true }).fill(channel.name);
await page.getByRole('button', { name: '查询', exact: true }).click();
await page
.getByRole('article')
.filter({ hasText: channel.name })
.getByRole('button', { name: '编辑', exact: true })
.click();
await page.getByRole('checkbox', { name: '电信', exact: true }).uncheck();
await page
.getByRole('alert')
.filter({ hasText: /通道组/ })
.waitFor();
assert(await page.getByRole('button', { name: '确认', exact: true }).isDisabled());
await page.screenshot({ path: path.join(evidence, `carrier-warning-${width}.png`), fullPage: true });
await page.getByRole('checkbox', { name: /我已确认/ }).check();
assert(await page.getByRole('button', { name: '确认', exact: true }).isEnabled());
await page.getByRole('dialog').getByRole('button', { name: '关闭', exact: true }).last().click();
assert.equal(await db.smsChannelGroupItem.count({ where: { channelId: channel.id } }), 2);
await page.goto(uiUrl.origin + '/admin/analytics');
await page.getByRole('heading', { name: '签名通道发送质量', exact: true }).waitFor();
await page
.getByRole('columnheader', { name: '业务短信', exact: true, includeHidden: true })
.waitFor({ state: 'attached' });
assert.equal(
await page.getByRole('columnheader', { name: '通道提交', exact: true, includeHidden: true }).count(),
0,
);
await page.screenshot({ path: path.join(evidence, `quality-${width}.png`), fullPage: true });
await page.goto(uiUrl.origin + '/admin/sms-records');
await page.getByRole('button', { name: '发送状态', exact: true }).click();
await page.getByRole('option', { name: '未知', exact: true }).click();
const filteredResponse = page.waitForResponse(
(r) =>
r.url().includes('/api/admin/operations/messages?') &&
new URL(r.url()).searchParams.get('status') === 'unknown',
);
await page.getByRole('button', { name: '查询', exact: true }).click();
const filtered = await (await filteredResponse).json();
assert(filtered.items.some((r) => r.status === 'submitted'));
await page.getByText('提交成功', { exact: true }).first().waitFor();
await page.screenshot({ path: path.join(evidence, `unknown-${width}.png`), fullPage: true });
}
assert.deepEqual(errors, []);
pass(
'real authenticated UI: three viewport sizes, refresh, routing, searchable signatures, validation, channel warning and cancel',
);
}
} finally {
await browser?.close();
await app.close();
}
+5 -2
View File
@@ -208,8 +208,9 @@ try {
const reduced = await req('/admin/channels/' + channel.id, { carriers: ['mobile'] });
assert.equal(reduced.status, 200, await reduced.text());
assert.deepEqual((await db.smsChannel.findUnique({ where: { id: channel.id } })).carriers, ['mobile']);
assert.equal(await db.smsChannelGroupItem.count({ where: { groupId: unicom.id, channelId: channel.id } }), 1);
pass('carrier reduction saves with active group references preserved');
assert.equal(await db.smsChannelGroupItem.count({ where: { groupId: unicom.id, channelId: channel.id } }), 0);
assert.equal(await db.smsChannelGroupItem.count({ where: { groupId: group.id, channelId: channel.id } }), 1);
pass('carrier reduction removes incompatible members and preserves supported group members');
const batch = await db.smsBatchTask.create({
data: { tenantId: tenant.id, applicationId: application.id, taskNo: key(), content, phoneTotal: 1 },
});
@@ -373,6 +374,8 @@ try {
.click();
await page.getByRole('heading', { name: '编辑通道', exact: true }).waitFor();
await page.getByRole('checkbox', { name: '联通', exact: true }).uncheck();
assert(await page.getByRole('button', { name: '确认', exact: true }).isDisabled());
await page.getByRole('checkbox', { name: /我已确认移除运营商/ }).check();
await page.screenshot({ path: `.local-data/template-optout-20260920/channel-${width}.png`, fullPage: true });
await page.getByRole('button', { name: '确认', exact: true }).click();
await page.getByRole('heading', { name: '编辑通道', exact: true }).waitFor({ state: 'hidden' });