feat: add reconciliation and quality reporting
This commit is contained in:
+59
@@ -0,0 +1,59 @@
|
||||
ALTER TABLE "SmsSubmitRecord"
|
||||
ADD COLUMN "costUnitPrice" INTEGER NOT NULL DEFAULT 0,
|
||||
ADD COLUMN "costAmountCents" INTEGER NOT NULL DEFAULT 0;
|
||||
|
||||
UPDATE "SmsSubmitRecord" submit
|
||||
SET
|
||||
"costUnitPrice" = channel."unitPrice",
|
||||
"costAmountCents" = channel."unitPrice" * message."billingUnits"
|
||||
FROM "SmsChannel" channel, "SmsMessageRecord" message
|
||||
WHERE submit."channelId" = channel.id
|
||||
AND submit."messageRecordId" = message.id;
|
||||
|
||||
CREATE TABLE "DailyReconciliationReport" (
|
||||
"id" TEXT NOT NULL,
|
||||
"reportDate" DATE NOT NULL,
|
||||
"tenantId" TEXT NOT NULL,
|
||||
"tenantName" TEXT NOT NULL,
|
||||
"applicationId" TEXT NOT NULL,
|
||||
"applicationName" TEXT NOT NULL,
|
||||
"sentUnits" INTEGER NOT NULL DEFAULT 0,
|
||||
"successUnits" INTEGER NOT NULL DEFAULT 0,
|
||||
"generatedAt" TIMESTAMP(3) NOT NULL DEFAULT CURRENT_TIMESTAMP,
|
||||
"updatedAt" TIMESTAMP(3) NOT NULL,
|
||||
CONSTRAINT "DailyReconciliationReport_pkey" PRIMARY KEY ("id")
|
||||
);
|
||||
|
||||
CREATE TABLE "DailyProfitReport" (
|
||||
"id" TEXT NOT NULL,
|
||||
"reportDate" DATE NOT NULL,
|
||||
"dimensionType" TEXT NOT NULL,
|
||||
"dimensionId" TEXT NOT NULL,
|
||||
"dimensionName" TEXT NOT NULL,
|
||||
"tenantId" TEXT,
|
||||
"tenantName" TEXT,
|
||||
"applicationId" TEXT,
|
||||
"channelId" TEXT,
|
||||
"sentUnits" INTEGER NOT NULL DEFAULT 0,
|
||||
"successUnits" INTEGER NOT NULL DEFAULT 0,
|
||||
"revenueCents" INTEGER NOT NULL DEFAULT 0,
|
||||
"costCents" INTEGER NOT NULL DEFAULT 0,
|
||||
"profitCents" INTEGER NOT NULL DEFAULT 0,
|
||||
"profitRateBps" INTEGER NOT NULL DEFAULT 0,
|
||||
"generatedAt" TIMESTAMP(3) NOT NULL DEFAULT CURRENT_TIMESTAMP,
|
||||
"updatedAt" TIMESTAMP(3) NOT NULL,
|
||||
CONSTRAINT "DailyProfitReport_pkey" PRIMARY KEY ("id")
|
||||
);
|
||||
|
||||
CREATE UNIQUE INDEX "DailyReconciliationReport_reportDate_tenantId_applicationId_key"
|
||||
ON "DailyReconciliationReport"("reportDate", "tenantId", "applicationId");
|
||||
CREATE INDEX "DailyReconciliationReport_reportDate_idx" ON "DailyReconciliationReport"("reportDate");
|
||||
CREATE INDEX "DailyReconciliationReport_tenantId_reportDate_idx" ON "DailyReconciliationReport"("tenantId", "reportDate");
|
||||
CREATE INDEX "DailyReconciliationReport_applicationId_reportDate_idx" ON "DailyReconciliationReport"("applicationId", "reportDate");
|
||||
|
||||
CREATE UNIQUE INDEX "DailyProfitReport_reportDate_dimensionType_dimensionId_key"
|
||||
ON "DailyProfitReport"("reportDate", "dimensionType", "dimensionId");
|
||||
CREATE INDEX "DailyProfitReport_dimensionType_reportDate_idx" ON "DailyProfitReport"("dimensionType", "reportDate");
|
||||
CREATE INDEX "DailyProfitReport_tenantId_reportDate_idx" ON "DailyProfitReport"("tenantId", "reportDate");
|
||||
CREATE INDEX "DailyProfitReport_applicationId_reportDate_idx" ON "DailyProfitReport"("applicationId", "reportDate");
|
||||
CREATE INDEX "DailyProfitReport_channelId_reportDate_idx" ON "DailyProfitReport"("channelId", "reportDate");
|
||||
@@ -0,0 +1,23 @@
|
||||
CREATE TABLE "CommonReportField" (
|
||||
"id" TEXT NOT NULL,
|
||||
"drainageFieldId" TEXT NOT NULL,
|
||||
"reportType" TEXT NOT NULL,
|
||||
"required" BOOLEAN NOT NULL DEFAULT false,
|
||||
"sortOrder" INTEGER NOT NULL DEFAULT 100,
|
||||
"status" TEXT NOT NULL DEFAULT 'active',
|
||||
"createdAt" TIMESTAMP(3) NOT NULL DEFAULT CURRENT_TIMESTAMP,
|
||||
"updatedAt" TIMESTAMP(3) NOT NULL,
|
||||
|
||||
CONSTRAINT "CommonReportField_pkey" PRIMARY KEY ("id")
|
||||
);
|
||||
|
||||
CREATE UNIQUE INDEX "CommonReportField_drainageFieldId_reportType_key"
|
||||
ON "CommonReportField"("drainageFieldId", "reportType");
|
||||
|
||||
CREATE INDEX "CommonReportField_reportType_status_sortOrder_idx"
|
||||
ON "CommonReportField"("reportType", "status", "sortOrder");
|
||||
|
||||
ALTER TABLE "CommonReportField"
|
||||
ADD CONSTRAINT "CommonReportField_drainageFieldId_fkey"
|
||||
FOREIGN KEY ("drainageFieldId") REFERENCES "DrainageField"("id")
|
||||
ON DELETE RESTRICT ON UPDATE CASCADE;
|
||||
@@ -0,0 +1,65 @@
|
||||
ALTER TABLE "SmsMessageRecord" ADD COLUMN "drainageInfoId" TEXT;
|
||||
|
||||
ALTER TABLE "SmsMessageRecord"
|
||||
ADD CONSTRAINT "SmsMessageRecord_drainageInfoId_fkey"
|
||||
FOREIGN KEY ("drainageInfoId") REFERENCES "SmsDrainageInfo"("id") ON DELETE SET NULL ON UPDATE CASCADE;
|
||||
|
||||
CREATE INDEX "SmsMessageRecord_drainageInfoId_queuedAt_idx"
|
||||
ON "SmsMessageRecord"("drainageInfoId", "queuedAt");
|
||||
|
||||
WITH drainage_matches AS (
|
||||
SELECT
|
||||
message.id AS "messageRecordId",
|
||||
drainage.id AS "drainageInfoId",
|
||||
ROW_NUMBER() OVER (
|
||||
PARTITION BY message.id
|
||||
ORDER BY LENGTH(drainage.url) DESC, drainage."updatedAt" DESC, drainage.id
|
||||
) AS match_rank,
|
||||
COUNT(*) OVER (
|
||||
PARTITION BY message.id, LENGTH(drainage.url)
|
||||
) AS same_length_matches
|
||||
FROM "SmsMessageRecord" message
|
||||
JOIN "SmsDrainageInfo" drainage
|
||||
ON drainage."signatureId" = message."signatureId"
|
||||
AND drainage."auditStatus" = 'approved'
|
||||
AND LENGTH(BTRIM(drainage.url)) > 0
|
||||
AND POSITION(BTRIM(drainage.url) IN message.content) > 0
|
||||
WHERE message."drainageInfoId" IS NULL
|
||||
)
|
||||
UPDATE "SmsMessageRecord" message
|
||||
SET "drainageInfoId" = matched."drainageInfoId"
|
||||
FROM drainage_matches matched
|
||||
WHERE message.id = matched."messageRecordId"
|
||||
AND matched.match_rank = 1
|
||||
AND matched.same_length_matches = 1;
|
||||
|
||||
CREATE TABLE "DailyQualityReport" (
|
||||
"id" TEXT NOT NULL,
|
||||
"reportDate" DATE NOT NULL,
|
||||
"dimensionType" TEXT NOT NULL,
|
||||
"dimensionId" TEXT NOT NULL,
|
||||
"dimensionName" TEXT NOT NULL,
|
||||
"tenantId" TEXT,
|
||||
"tenantName" TEXT,
|
||||
"applicationId" TEXT,
|
||||
"channelId" TEXT,
|
||||
"signatureId" TEXT,
|
||||
"drainageInfoId" TEXT,
|
||||
"sentUnits" INTEGER NOT NULL DEFAULT 0,
|
||||
"successUnits" INTEGER NOT NULL DEFAULT 0,
|
||||
"successRateBps" INTEGER NOT NULL DEFAULT 0,
|
||||
"avgArrivalMs" INTEGER,
|
||||
"generatedAt" TIMESTAMP(3) NOT NULL DEFAULT CURRENT_TIMESTAMP,
|
||||
"updatedAt" TIMESTAMP(3) NOT NULL,
|
||||
CONSTRAINT "DailyQualityReport_pkey" PRIMARY KEY ("id")
|
||||
);
|
||||
|
||||
CREATE UNIQUE INDEX "DailyQualityReport_reportDate_dimensionType_dimensionId_key"
|
||||
ON "DailyQualityReport"("reportDate", "dimensionType", "dimensionId");
|
||||
CREATE INDEX "DailyQualityReport_dimensionType_reportDate_sentUnits_idx"
|
||||
ON "DailyQualityReport"("dimensionType", "reportDate", "sentUnits");
|
||||
CREATE INDEX "DailyQualityReport_tenantId_reportDate_idx" ON "DailyQualityReport"("tenantId", "reportDate");
|
||||
CREATE INDEX "DailyQualityReport_applicationId_reportDate_idx" ON "DailyQualityReport"("applicationId", "reportDate");
|
||||
CREATE INDEX "DailyQualityReport_channelId_reportDate_idx" ON "DailyQualityReport"("channelId", "reportDate");
|
||||
CREATE INDEX "DailyQualityReport_signatureId_reportDate_idx" ON "DailyQualityReport"("signatureId", "reportDate");
|
||||
CREATE INDEX "DailyQualityReport_drainageInfoId_reportDate_idx" ON "DailyQualityReport"("drainageInfoId", "reportDate");
|
||||
Reference in New Issue
Block a user