feat: add HTTP signature tools and fix independent HTTP send validation
CSS quality / css-quality (push) Has been cancelled

This commit is contained in:
hectorzhao
2026-09-15 15:58:29 +08:00
parent 18ecf8045f
commit c781313de5
23 changed files with 986 additions and 255 deletions
+175 -71
View File
@@ -5,17 +5,32 @@ import { PrismaService } from '../prisma/prisma.service';
import { RiskReviewService } from '../risk-review/risk-review.service';
import { PhoneFrequencyService } from '../risk-review/phone-frequency.service';
import { MetricsService } from '../metrics/metrics.service';
import type { CreateBatchTaskDto, CreateHttpBatchTaskDto, GatewayInboundAuthDto, GatewayInboundSubmitDto, ImportPreviewDto, ConfirmImportDto, SendJob, QueuePriority, RoutedChannel } from './send-chain.contracts';
import type {
CreateBatchTaskDto,
CreateHttpBatchTaskDto,
GatewayInboundAuthDto,
GatewayInboundSubmitDto,
ImportPreviewDto,
ConfirmImportDto,
SendJob,
QueuePriority,
RoutedChannel,
} from './send-chain.contracts';
import { SendBatchEntryService } from './send-batch-entry.service';
import { SendGatewaySubmitService } from './send-gateway-submit.service';
import { SendInboundEntryService } from './send-inbound-entry.service';
import { SendReviewContinuationService } from './send-review-continuation.service';
import { SendScheduledDispatchService } from './send-scheduled-dispatch.service';
export type SendSubmissionCallbacks = {
releaseMessageReservation: (
message: { tenantId: string; batchTaskId: string; messageId: string; amountCents: number | bigint; billingUnits: number },
message: {
tenantId: string;
batchTaskId: string;
messageId: string;
amountCents: number | bigint;
billingUnits: number;
},
remark: string,
) => Promise<void>;
recordCmppFailureReceipt: (
@@ -37,6 +52,7 @@ export type SendSubmissionCallbacks = {
export type SendResourceValidationOptions = {
usePersistedTemplateSnapshot?: boolean;
httpRequest?: boolean;
};
/**
@@ -59,53 +75,92 @@ export class SendSubmissionService {
callbacks: SendSubmissionCallbacks,
metrics?: MetricsService,
) {
this.batchEntry = new SendBatchEntryService(prisma, billing, riskReview, phoneFrequency, phoneRouting, facade, callbacks);
this.inboundEntry = new SendInboundEntryService(prisma, billing, riskReview, phoneFrequency, phoneRouting, facade, callbacks, metrics);
this.reviewContinuation = new SendReviewContinuationService(prisma, billing, riskReview, phoneFrequency, phoneRouting, facade, callbacks);
this.scheduledDispatch = new SendScheduledDispatchService(prisma, billing, riskReview, phoneFrequency, phoneRouting, facade, callbacks);
this.gatewaySubmit = new SendGatewaySubmitService(prisma, billing, riskReview, phoneFrequency, phoneRouting, facade, callbacks, metrics);
this.batchEntry = new SendBatchEntryService(
prisma,
billing,
riskReview,
phoneFrequency,
phoneRouting,
facade,
callbacks,
);
this.inboundEntry = new SendInboundEntryService(
prisma,
billing,
riskReview,
phoneFrequency,
phoneRouting,
facade,
callbacks,
metrics,
);
this.reviewContinuation = new SendReviewContinuationService(
prisma,
billing,
riskReview,
phoneFrequency,
phoneRouting,
facade,
callbacks,
);
this.scheduledDispatch = new SendScheduledDispatchService(
prisma,
billing,
riskReview,
phoneFrequency,
phoneRouting,
facade,
callbacks,
);
this.gatewaySubmit = new SendGatewaySubmitService(
prisma,
billing,
riskReview,
phoneFrequency,
phoneRouting,
facade,
callbacks,
metrics,
);
}
onModuleDestroy() {
return Promise.all([
this.gatewaySubmit.onModuleDestroy(),
this.inboundEntry.stopInboundWorkflowWorker(),
]);
return Promise.all([this.gatewaySubmit.onModuleDestroy(), this.inboundEntry.stopInboundWorkflowWorker()]);
}
async createBatchTask(data: CreateBatchTaskDto) {
async createBatchTask(data: CreateBatchTaskDto) {
return this.batchEntry.createBatchTask(data);
}
async createHttpBatchTask(data: CreateHttpBatchTaskDto) {
async createHttpBatchTask(data: CreateHttpBatchTaskDto) {
return this.batchEntry.createHttpBatchTask(data);
}
async getBatchTask(taskId: string, tenantId?: string, sourceType = 'client') {
async getBatchTask(taskId: string, tenantId?: string, sourceType = 'client') {
return this.batchEntry.getBatchTask(taskId, tenantId, sourceType);
}
async previewImport(data: ImportPreviewDto) {
async previewImport(data: ImportPreviewDto) {
return this.batchEntry.previewImport(data);
}
async confirmImport(data: ConfirmImportDto) {
async confirmImport(data: ConfirmImportDto) {
return this.batchEntry.confirmImport(data);
}
async resolveUnitPrice(tenantId: string, applicationId?: string) {
async resolveUnitPrice(tenantId: string, applicationId?: string) {
return this.batchEntry.resolveUnitPrice(tenantId, applicationId);
}
async resolveQueuePriority(tenantId: string, applicationId?: string): Promise<QueuePriority> {
async resolveQueuePriority(tenantId: string, applicationId?: string): Promise<QueuePriority> {
return this.batchEntry.resolveQueuePriority(tenantId, applicationId);
}
async resolveApplicationAccessNumber(tenantId: string, applicationId?: string) {
async resolveApplicationAccessNumber(tenantId: string, applicationId?: string) {
return this.batchEntry.resolveApplicationAccessNumber(tenantId, applicationId);
}
async resolveTemplateMessageClassification(
async resolveTemplateMessageClassification(
tenantId: string,
applicationId: string | undefined,
templateId: string | undefined,
@@ -114,35 +169,40 @@ async resolveTemplateMessageClassification(
return this.batchEntry.resolveTemplateMessageClassification(tenantId, applicationId, templateId, content);
}
async classifyRejectedPhones(tenantId: string, applicationId: string | undefined, phones: string[]) {
async classifyRejectedPhones(tenantId: string, applicationId: string | undefined, phones: string[]) {
return this.batchEntry.classifyRejectedPhones(tenantId, applicationId, phones);
}
async validateSendResources(tenantId: string, applicationId?: string, templateId?: string, options?: SendResourceValidationOptions) {
async validateSendResources(
tenantId: string,
applicationId?: string,
templateId?: string,
options?: SendResourceValidationOptions,
) {
return this.batchEntry.validateSendResources(tenantId, applicationId, templateId, options);
}
async reserveDailySendQuota(applicationId: string, requestedCount: number) {
async reserveDailySendQuota(applicationId: string, requestedCount: number) {
return this.batchEntry.reserveDailySendQuota(applicationId, requestedCount);
}
async tryReserveDailySendQuota(applicationId: string, requestedCount: number, reservationKey?: string) {
async tryReserveDailySendQuota(applicationId: string, requestedCount: number, reservationKey?: string) {
return this.batchEntry.tryReserveDailySendQuota(applicationId, requestedCount, reservationKey);
}
async authenticateInboundApplication(data: GatewayInboundAuthDto) {
async authenticateInboundApplication(data: GatewayInboundAuthDto) {
return this.inboundEntry.authenticateInboundApplication(data);
}
async submitInboundMessage(data: GatewayInboundSubmitDto) {
async submitInboundMessage(data: GatewayInboundSubmitDto) {
return this.inboundEntry.submitInboundMessage(data);
}
async recoverCompletedInboundLongMessageResponse(messageId: string, phoneNumbers: string[]) {
async recoverCompletedInboundLongMessageResponse(messageId: string, phoneNumbers: string[]) {
return this.inboundEntry.recoverCompletedInboundLongMessageResponse(messageId, phoneNumbers);
}
async submitCompleteInboundMessage(
async submitCompleteInboundMessage(
data: GatewayInboundSubmitDto,
phoneNumbers: string[],
application: Awaited<ReturnType<SendSubmissionService['findInboundApplication']>>,
@@ -150,10 +210,17 @@ async submitCompleteInboundMessage(
requestedMessageIds?: string[],
workflowKey?: string,
) {
return this.inboundEntry.submitCompleteInboundMessage(data, phoneNumbers, application, requestedGroupMessageId, requestedMessageIds, workflowKey);
return this.inboundEntry.submitCompleteInboundMessage(
data,
phoneNumbers,
application,
requestedGroupMessageId,
requestedMessageIds,
workflowKey,
);
}
async collectInboundLongMessageFragment(
async collectInboundLongMessageFragment(
data: GatewayInboundSubmitDto,
application: NonNullable<Awaited<ReturnType<SendSubmissionService['findInboundApplication']>>>,
phoneNumbers: string[],
@@ -161,11 +228,11 @@ async collectInboundLongMessageFragment(
return this.inboundEntry.collectInboundLongMessageFragment(data, application, phoneNumbers);
}
async expireInboundLongMessages(now = new Date()) {
async expireInboundLongMessages(now = new Date()) {
return this.inboundEntry.expireInboundLongMessages(now);
}
async submitInboundSingleMessage(
async submitInboundSingleMessage(
data: GatewayInboundSubmitDto & { phoneNumber: string },
messageId: string,
submitGroupMessageId: string,
@@ -174,78 +241,94 @@ async submitInboundSingleMessage(
receiptRejection?: { code: string; reason: string },
workflowItemKey?: string,
) {
return this.inboundEntry.submitInboundSingleMessage(data, messageId, submitGroupMessageId, application, synchronousRejection, receiptRejection, workflowItemKey);
return this.inboundEntry.submitInboundSingleMessage(
data,
messageId,
submitGroupMessageId,
application,
synchronousRejection,
receiptRejection,
workflowItemKey,
);
}
async evaluateRiskWithPhoneFrequency(input: {
tenantId: string;
applicationId: string;
templateId?: string;
content: string;
variables?: Record<string, unknown>;
phoneNumber: string;
sourceType: 'cmpp';
}, reservationKey?: string) {
async evaluateRiskWithPhoneFrequency(
input: {
tenantId: string;
applicationId: string;
templateId?: string;
content: string;
variables?: Record<string, unknown>;
phoneNumber: string;
sourceType: 'cmpp';
},
reservationKey?: string,
) {
return this.inboundEntry.evaluateRiskWithPhoneFrequency(input, reservationKey);
}
findInboundApplication(account: string) {
findInboundApplication(account: string) {
return this.inboundEntry.findInboundApplication(account);
}
async resolveInboundTemplateCandidate(applicationId: string, content: string) {
async resolveInboundTemplateCandidate(applicationId: string, content: string) {
return this.inboundEntry.resolveInboundTemplateCandidate(applicationId, content);
}
resolveInboundSignatureCandidate(applicationId: string, content: string) {
resolveInboundSignatureCandidate(applicationId: string, content: string) {
return this.inboundEntry.resolveInboundSignatureCandidate(applicationId, content);
}
async resolveDrainageInfoMatch(signatureId: string | null | undefined, content: string) {
async resolveDrainageInfoMatch(signatureId: string | null | undefined, content: string) {
return this.inboundEntry.resolveDrainageInfoMatch(signatureId, content);
}
async attachMessageToReviewTask(reviewTaskId: string, messageRecordId: string, signatureId: string, drainageInfoId?: string) {
async attachMessageToReviewTask(
reviewTaskId: string,
messageRecordId: string,
signatureId: string,
drainageInfoId?: string,
) {
return this.inboundEntry.attachMessageToReviewTask(reviewTaskId, messageRecordId, signatureId, drainageInfoId);
}
async handleReviewDecision(reviewTaskId: string, decision: 'approved' | 'rejected', reason: string) {
async handleReviewDecision(reviewTaskId: string, decision: 'approved' | 'rejected', reason: string) {
return this.reviewContinuation.handleReviewDecision(reviewTaskId, decision, reason);
}
async dispatchDueScheduledTasks(now = new Date()) {
async dispatchDueScheduledTasks(now = new Date()) {
return this.scheduledDispatch.dispatchDueScheduledTasks(now);
}
async runScheduledDispatchScan() {
async runScheduledDispatchScan() {
return this.scheduledDispatch.runScheduledDispatchScan();
}
async enqueueBatchTask(taskId: string, preparedMessage?: { messageRecordId: string; queuePriority?: string | null }) {
async enqueueBatchTask(taskId: string, preparedMessage?: { messageRecordId: string; queuePriority?: string | null }) {
return this.gatewaySubmit.enqueueBatchTask(taskId, preparedMessage);
}
startWorker() {
startWorker() {
return this.gatewaySubmit.startWorker();
}
startSubmitOutboxPublisher() {
startSubmitOutboxPublisher() {
return this.gatewaySubmit.startSubmitOutboxPublisher();
}
startInboundWorkflowWorker() {
startInboundWorkflowWorker() {
return this.inboundEntry.startInboundWorkflowWorker();
}
stopInboundWorkflowWorker() {
stopInboundWorkflowWorker() {
return this.inboundEntry.stopInboundWorkflowWorker();
}
async processSendJob(job: SendJob) {
async processSendJob(job: SendJob) {
return this.gatewaySubmit.processSendJob(job);
}
async submitMessageToGateway(
async submitMessageToGateway(
message: {
id: string;
tenantId: string;
@@ -272,26 +355,42 @@ async submitMessageToGateway(
return this.gatewaySubmit.submitMessageToGateway(message, routed, attempt, retryOfSubmitRecordId);
}
async selectChannelForMessage(
message: { id: string; tenantId: string; applicationId?: string | null; templateId?: string | null; signatureId?: string | null; phoneNumber: string; carrier?: string | null; province?: string | null; template?: { signature?: { id?: string | null } | null } | null; signature?: { id?: string | null } | null },
async selectChannelForMessage(
message: {
id: string;
tenantId: string;
applicationId?: string | null;
templateId?: string | null;
signatureId?: string | null;
phoneNumber: string;
carrier?: string | null;
province?: string | null;
template?: { signature?: { id?: string | null } | null } | null;
signature?: { id?: string | null } | null;
},
options: { forceNational?: boolean; excludeChannelIds?: string[] } = {},
): Promise<RoutedChannel> {
return this.gatewaySubmit.selectChannelForMessage(message, options);
}
async findApplicationRoute(tenantId: string, applicationId: string | undefined, carrier: string, signatureId?: string) {
async findApplicationRoute(
tenantId: string,
applicationId: string | undefined,
carrier: string,
signatureId?: string,
) {
return this.gatewaySubmit.findApplicationRoute(tenantId, applicationId, carrier, signatureId);
}
async identifyCarrier(phoneNumber: string) {
async identifyCarrier(phoneNumber: string) {
return this.gatewaySubmit.identifyCarrier(phoneNumber);
}
async identifyProvince(phoneNumber: string) {
async identifyProvince(phoneNumber: string) {
return this.gatewaySubmit.identifyProvince(phoneNumber);
}
async ensureSignatureReportedForChannel(
async ensureSignatureReportedForChannel(
message: {
id: string;
templateId?: string | null;
@@ -304,31 +403,36 @@ async ensureSignatureReportedForChannel(
return this.gatewaySubmit.ensureSignatureReportedForChannel(message, channelId, carrier);
}
async resolveMessageSignatureId(message: { templateId?: string | null; signatureId?: string | null; template?: { signature?: { id?: string | null } | null } | null; signature?: { id?: string | null } | null }) {
async resolveMessageSignatureId(message: {
templateId?: string | null;
signatureId?: string | null;
template?: { signature?: { id?: string | null } | null } | null;
signature?: { id?: string | null } | null;
}) {
return this.gatewaySubmit.resolveMessageSignatureId(message);
}
async waitForChannelRateLimit(channelId: string, tps: number) {
async waitForChannelRateLimit(channelId: string, tps: number) {
return this.gatewaySubmit.waitForChannelRateLimit(channelId, tps);
}
async refreshTaskProgress(batchTaskId: string, knownSingleMessageStatus?: string) {
async refreshTaskProgress(batchTaskId: string, knownSingleMessageStatus?: string) {
return this.gatewaySubmit.refreshTaskProgress(batchTaskId, knownSingleMessageStatus);
}
getSendQueue(): Queue<SendJob, unknown, 'send-message'> {
getSendQueue(): Queue<SendJob, unknown, 'send-message'> {
return this.gatewaySubmit.getSendQueue();
}
getGatewayQueue(): Queue {
getGatewayQueue(): Queue {
return this.gatewaySubmit.getGatewayQueue();
}
getRedis() {
getRedis() {
return this.gatewaySubmit.getRedis();
}
async publishGatewaySubmitCommand(command: unknown, idempotencyKey?: string) {
async publishGatewaySubmitCommand(command: unknown, idempotencyKey?: string) {
return this.gatewaySubmit.publishGatewaySubmitCommand(command, idempotencyKey);
}
}