diff --git a/api/package-lock.json b/api/package-lock.json index c3eb4eb..8365881 100644 --- a/api/package-lock.json +++ b/api/package-lock.json @@ -18,6 +18,7 @@ "bullmq": "^5.79.2", "class-transformer": "^0.5.1", "class-validator": "^0.14.3", + "exceljs": "^4.4.0", "ioredis": "^5.11.1", "minio": "^8.0.7", "pg": "^8.22.0", @@ -648,6 +649,47 @@ "tslib": "^2.4.0" } }, + "node_modules/@fast-csv/format": { + "version": "4.3.5", + "resolved": "https://registry.npmjs.org/@fast-csv/format/-/format-4.3.5.tgz", + "integrity": "sha512-8iRn6QF3I8Ak78lNAa+Gdl5MJJBM5vRHivFtMRUWINdevNo00K7OXxS2PshawLKTejVwieIlPmK5YlLu6w4u8A==", + "license": "MIT", + "dependencies": { + "@types/node": "^14.0.1", + "lodash.escaperegexp": "^4.1.2", + "lodash.isboolean": "^3.0.3", + "lodash.isequal": "^4.5.0", + "lodash.isfunction": "^3.0.9", + "lodash.isnil": "^4.0.0" + } + }, + "node_modules/@fast-csv/format/node_modules/@types/node": { + "version": "14.18.63", + "resolved": "https://registry.npmjs.org/@types/node/-/node-14.18.63.tgz", + "integrity": "sha512-fAtCfv4jJg+ExtXhvCkCqUKZ+4ok/JQk01qDKhL5BDDoS3AxKXhV5/MAVUZyQnSEd2GT92fkgZl0pz0Q0AzcIQ==", + "license": "MIT" + }, + "node_modules/@fast-csv/parse": { + "version": "4.3.6", + "resolved": "https://registry.npmjs.org/@fast-csv/parse/-/parse-4.3.6.tgz", + "integrity": "sha512-uRsLYksqpbDmWaSmzvJcuApSEe38+6NQZBUsuAyMZKqHxH0g1wcJgsKUvN3WC8tewaqFjBMMGrkHmC+T7k8LvA==", + "license": "MIT", + "dependencies": { + "@types/node": "^14.0.1", + "lodash.escaperegexp": "^4.1.2", + "lodash.groupby": "^4.6.0", + "lodash.isfunction": "^3.0.9", + "lodash.isnil": "^4.0.0", + "lodash.isundefined": "^3.0.1", + "lodash.uniq": "^4.5.0" + } + }, + "node_modules/@fast-csv/parse/node_modules/@types/node": { + "version": "14.18.63", + "resolved": "https://registry.npmjs.org/@types/node/-/node-14.18.63.tgz", + "integrity": "sha512-fAtCfv4jJg+ExtXhvCkCqUKZ+4ok/JQk01qDKhL5BDDoS3AxKXhV5/MAVUZyQnSEd2GT92fkgZl0pz0Q0AzcIQ==", + "license": "MIT" + }, "node_modules/@hono/node-server": { "version": "1.19.13", "resolved": "https://registry.npmjs.org/@hono/node-server/-/node-server-1.19.13.tgz", @@ -2596,6 +2638,127 @@ "integrity": "sha512-klpgFSWLW1ZEs8svjfb7g4qWY0YS5imI82dTg+QahUvJ8YqAY0P10Uk8tTyh9ZGuYEZEMaeJYCF5BFuX552hsw==", "license": "MIT" }, + "node_modules/archiver": { + "version": "5.3.2", + "resolved": "https://registry.npmjs.org/archiver/-/archiver-5.3.2.tgz", + "integrity": "sha512-+25nxyyznAXF7Nef3y0EbBeqmGZgeN/BxHX29Rs39djAfaFalmQ89SE6CWyDCHzGL0yt/ycBtNOmGTW0FyGWNw==", + "license": "MIT", + "dependencies": { + "archiver-utils": "^2.1.0", + "async": "^3.2.4", + "buffer-crc32": "^0.2.1", + "readable-stream": "^3.6.0", + "readdir-glob": "^1.1.2", + "tar-stream": "^2.2.0", + "zip-stream": "^4.1.0" + }, + "engines": { + "node": ">= 10" + } + }, + "node_modules/archiver-utils": { + "version": "2.1.0", + "resolved": "https://registry.npmjs.org/archiver-utils/-/archiver-utils-2.1.0.tgz", + "integrity": "sha512-bEL/yUb/fNNiNTuUz979Z0Yg5L+LzLxGJz8x79lYmR54fmTIb6ob/hNQgkQnIUDWIFjZVQwl9Xs356I6BAMHfw==", + "license": "MIT", + "dependencies": { + "glob": "^7.1.4", + "graceful-fs": "^4.2.0", + "lazystream": "^1.0.0", + "lodash.defaults": "^4.2.0", + "lodash.difference": "^4.5.0", + "lodash.flatten": "^4.4.0", + "lodash.isplainobject": "^4.0.6", + "lodash.union": "^4.6.0", + "normalize-path": "^3.0.0", + "readable-stream": "^2.0.0" + }, + "engines": { + "node": ">= 6" + } + }, + "node_modules/archiver-utils/node_modules/brace-expansion": { + "version": "1.1.16", + "resolved": "https://registry.npmjs.org/brace-expansion/-/brace-expansion-1.1.16.tgz", + "integrity": "sha512-IDw48K2/2kRkg9LdJxurvq3lV3aBgq0REY89duEqFRthjlPdXHKMj7EnQOXVckxzgisinf3nHfrcE2FufFLXMw==", + "license": "MIT", + "dependencies": { + "balanced-match": "^1.0.0", + "concat-map": "0.0.1" + } + }, + "node_modules/archiver-utils/node_modules/glob": { + "version": "7.2.3", + "resolved": "https://registry.npmjs.org/glob/-/glob-7.2.3.tgz", + "integrity": "sha512-nFR0zLpU2YCaRxwoCJvL6UvCH2JFyFVIvwTLsIf21AuHlMskA1hhTdk+LlYJtOlYt9v6dvszD2BGRqBL+iQK9Q==", + "deprecated": "Old versions of glob are not supported, and contain widely publicized security vulnerabilities, which have been fixed in the current version. Please update. Support for old versions may be purchased (at exorbitant rates) by contacting i@izs.me", + "license": "ISC", + "dependencies": { + "fs.realpath": "^1.0.0", + "inflight": "^1.0.4", + "inherits": "2", + "minimatch": "^3.1.1", + "once": "^1.3.0", + "path-is-absolute": "^1.0.0" + }, + "engines": { + "node": "*" + }, + "funding": { + "url": "https://github.com/sponsors/isaacs" + } + }, + "node_modules/archiver-utils/node_modules/minimatch": { + "version": "3.1.5", + "resolved": "https://registry.npmjs.org/minimatch/-/minimatch-3.1.5.tgz", + "integrity": "sha512-VgjWUsnnT6n+NUk6eZq77zeFdpW2LWDzP6zFGrCbHXiYNul5Dzqk2HHQ5uFH2DNW5Xbp8+jVzaeNt94ssEEl4w==", + "license": "ISC", + "dependencies": { + "brace-expansion": "^1.1.7" + }, + "engines": { + "node": "*" + } + }, + "node_modules/archiver-utils/node_modules/readable-stream": { + "version": "2.3.8", + "resolved": "https://registry.npmjs.org/readable-stream/-/readable-stream-2.3.8.tgz", + "integrity": "sha512-8p0AUk4XODgIewSi0l8Epjs+EVnWiK7NoDIEGU0HhE7+ZyY8D1IMY7odu5lRrFXGg71L15KG8QrPmum45RTtdA==", + "license": "MIT", + "dependencies": { + "core-util-is": "~1.0.0", + "inherits": "~2.0.3", + "isarray": "~1.0.0", + "process-nextick-args": "~2.0.0", + "safe-buffer": "~5.1.1", + "string_decoder": "~1.1.1", + "util-deprecate": "~1.0.1" + } + }, + "node_modules/archiver-utils/node_modules/safe-buffer": { + "version": "5.1.2", + "resolved": "https://registry.npmjs.org/safe-buffer/-/safe-buffer-5.1.2.tgz", + "integrity": "sha512-Gd2UZBJDkXlY7GbJxfsE8/nvKkUEU1G38c1siN6QP6a9PT9MmHB8GnpscSmMJSoF8LOIrt8ud/wPtojys4G6+g==", + "license": "MIT" + }, + "node_modules/archiver-utils/node_modules/string_decoder": { + "version": "1.1.1", + "resolved": "https://registry.npmjs.org/string_decoder/-/string_decoder-1.1.1.tgz", + "integrity": "sha512-n/ShnvDi6FHbbVfviro+WojiFzv+s8MPMHBczVePfUpDJLwoLT0ht1l4YwBCbi8pJAveEEdnkHyPyTP/mzRfwg==", + "license": "MIT", + "dependencies": { + "safe-buffer": "~5.1.0" + } + }, + "node_modules/archiver/node_modules/buffer-crc32": { + "version": "0.2.13", + "resolved": "https://registry.npmjs.org/buffer-crc32/-/buffer-crc32-0.2.13.tgz", + "integrity": "sha512-VO9Ht/+p3SN7SKWqcrgEzjGbRSJYTx+Q1pTQC0wrWqHx0vpJraQ6GtHx8tvcg1rlK1byhU5gccxgOgj7B0TDkQ==", + "license": "MIT", + "engines": { + "node": "*" + } + }, "node_modules/arg": { "version": "4.1.3", "resolved": "https://registry.npmjs.org/arg/-/arg-4.1.3.tgz", @@ -2728,7 +2891,26 @@ "version": "1.0.2", "resolved": "https://registry.npmjs.org/balanced-match/-/balanced-match-1.0.2.tgz", "integrity": "sha512-3oSeUO0TMV67hN1AmbXsK4yaqU7tjiHlbxRDZOpH0KW9+CeX4bRAaX0Anxt0tx2MrpRpWwQaPwIlISEJhYU5Pw==", - "dev": true, + "license": "MIT" + }, + "node_modules/base64-js": { + "version": "1.5.1", + "resolved": "https://registry.npmjs.org/base64-js/-/base64-js-1.5.1.tgz", + "integrity": "sha512-AKpaYlHn8t4SVbOHCy+b5+KKgvR4vrsD8vbvrbiQJps7fKDTkjkDry6ji0rUJjC0kzbNePLwzxq8iypo41qeWA==", + "funding": [ + { + "type": "github", + "url": "https://github.com/sponsors/feross" + }, + { + "type": "patreon", + "url": "https://www.patreon.com/feross" + }, + { + "type": "consulting", + "url": "https://feross.org/support" + } + ], "license": "MIT" }, "node_modules/baseline-browser-mapping": { @@ -2751,6 +2933,39 @@ "devOptional": true, "license": "MIT" }, + "node_modules/big-integer": { + "version": "1.6.52", + "resolved": "https://registry.npmjs.org/big-integer/-/big-integer-1.6.52.tgz", + "integrity": "sha512-QxD8cf2eVqJOOz63z6JIN9BzvVs/dlySa5HGSBH5xtR8dPteIRQnBxxKqkNTiT6jbDTF6jAfrd4oMcND9RGbQg==", + "license": "Unlicense", + "engines": { + "node": ">=0.6" + } + }, + "node_modules/binary": { + "version": "0.3.0", + "resolved": "https://registry.npmjs.org/binary/-/binary-0.3.0.tgz", + "integrity": "sha512-D4H1y5KYwpJgK8wk1Cue5LLPgmwHKYSChkbspQg5JtVuR5ulGckxfR62H3AE9UDkdMC8yyXlqYihuz3Aqg2XZg==", + "license": "MIT", + "dependencies": { + "buffers": "~0.1.1", + "chainsaw": "~0.1.0" + }, + "engines": { + "node": "*" + } + }, + "node_modules/bl": { + "version": "4.1.0", + "resolved": "https://registry.npmjs.org/bl/-/bl-4.1.0.tgz", + "integrity": "sha512-1W07cM9gS6DcLperZfFSj+bWLtaPGSOHWhPiGzXmvVJbRLdG82sH/Kn8EtW1VqWVA54AKf2h5k5BbnIbwF3h6w==", + "license": "MIT", + "dependencies": { + "buffer": "^5.5.0", + "inherits": "^2.0.4", + "readable-stream": "^3.4.0" + } + }, "node_modules/block-stream2": { "version": "2.1.0", "resolved": "https://registry.npmjs.org/block-stream2/-/block-stream2-2.1.0.tgz", @@ -2760,6 +2975,12 @@ "readable-stream": "^3.4.0" } }, + "node_modules/bluebird": { + "version": "3.4.7", + "resolved": "https://registry.npmjs.org/bluebird/-/bluebird-3.4.7.tgz", + "integrity": "sha512-iD3898SR7sWVRHbiQv+sHUtHnMvC1o3nW5rAcqnq3uOn07DSAppZYUkIGslDz6gXC7HfunPe7YVBgoEJASPcHA==", + "license": "MIT" + }, "node_modules/body-parser": { "version": "2.3.0", "resolved": "https://registry.npmjs.org/body-parser/-/body-parser-2.3.0.tgz", @@ -2801,7 +3022,6 @@ "version": "2.1.1", "resolved": "https://registry.npmjs.org/brace-expansion/-/brace-expansion-2.1.1.tgz", "integrity": "sha512-WR1cURNjuvBLMZBMbqM0UoE+WAfdUcEV1ccD8PVBVOI+Z3ND4+SZbN8RsfT2bMuG1qwz5RFvPukSZm5fF2D5eA==", - "dev": true, "license": "MIT", "dependencies": { "balanced-match": "^1.0.0" @@ -2870,6 +3090,30 @@ "node-int64": "^0.4.0" } }, + "node_modules/buffer": { + "version": "5.7.1", + "resolved": "https://registry.npmjs.org/buffer/-/buffer-5.7.1.tgz", + "integrity": "sha512-EHcyIPBQ4BSGlvjB16k5KgAJ27CIsHY/2JBmCRReo48y9rQ3MaUzWX3KVlBa4U7MyX02HdVj0K7C3WaB3ju7FQ==", + "funding": [ + { + "type": "github", + "url": "https://github.com/sponsors/feross" + }, + { + "type": "patreon", + "url": "https://www.patreon.com/feross" + }, + { + "type": "consulting", + "url": "https://feross.org/support" + } + ], + "license": "MIT", + "dependencies": { + "base64-js": "^1.3.1", + "ieee754": "^1.1.13" + } + }, "node_modules/buffer-crc32": { "version": "1.0.0", "resolved": "https://registry.npmjs.org/buffer-crc32/-/buffer-crc32-1.0.0.tgz", @@ -2885,6 +3129,23 @@ "integrity": "sha512-E+XQCRwSbaaiChtv6k6Dwgc+bx+Bs6vuKJHHl5kox/BaKbhiXzqQOwK4cO22yElGp2OCmjwVhT3HmxgyPGnJfQ==", "license": "MIT" }, + "node_modules/buffer-indexof-polyfill": { + "version": "1.0.2", + "resolved": "https://registry.npmjs.org/buffer-indexof-polyfill/-/buffer-indexof-polyfill-1.0.2.tgz", + "integrity": "sha512-I7wzHwA3t1/lwXQh+A5PbNvJxgfo5r3xulgpYDB5zckTu/Z9oUK9biouBKQUjEqzaz3HnAT6TYoovmE+GqSf7A==", + "license": "MIT", + "engines": { + "node": ">=0.10" + } + }, + "node_modules/buffers": { + "version": "0.1.1", + "resolved": "https://registry.npmjs.org/buffers/-/buffers-0.1.1.tgz", + "integrity": "sha512-9q/rDEGSb/Qsvv2qvzIzdluL5k7AaJOTrw23z9reQthrbF7is4CtlT0DXyO1oei2DCp4uojjzQ7igaSHp1kAEQ==", + "engines": { + "node": ">=0.2.0" + } + }, "node_modules/bullmq": { "version": "5.79.2", "resolved": "https://registry.npmjs.org/bullmq/-/bullmq-5.79.2.tgz", @@ -3059,6 +3320,18 @@ ], "license": "CC-BY-4.0" }, + "node_modules/chainsaw": { + "version": "0.1.0", + "resolved": "https://registry.npmjs.org/chainsaw/-/chainsaw-0.1.0.tgz", + "integrity": "sha512-75kWfWt6MEKNC8xYXIdRpDehRYY/tNSgwKaJq+dbbDcxORuVrrQ+SEHoWsniVn9XPYfP4gmdWIeDk/4YNp1rNQ==", + "license": "MIT/X11", + "dependencies": { + "traverse": ">=0.3.0 <0.4" + }, + "engines": { + "node": "*" + } + }, "node_modules/chalk": { "version": "4.1.2", "resolved": "https://registry.npmjs.org/chalk/-/chalk-4.1.2.tgz", @@ -3280,11 +3553,34 @@ "dev": true, "license": "MIT" }, + "node_modules/compress-commons": { + "version": "4.1.2", + "resolved": "https://registry.npmjs.org/compress-commons/-/compress-commons-4.1.2.tgz", + "integrity": "sha512-D3uMHtGc/fcO1Gt1/L7i1e33VOvD4A9hfQLP+6ewd+BvG/gQ84Yh4oftEhAdjSMgBgwGL+jsppT7JYNpo6MHHg==", + "license": "MIT", + "dependencies": { + "buffer-crc32": "^0.2.13", + "crc32-stream": "^4.0.2", + "normalize-path": "^3.0.0", + "readable-stream": "^3.6.0" + }, + "engines": { + "node": ">= 10" + } + }, + "node_modules/compress-commons/node_modules/buffer-crc32": { + "version": "0.2.13", + "resolved": "https://registry.npmjs.org/buffer-crc32/-/buffer-crc32-0.2.13.tgz", + "integrity": "sha512-VO9Ht/+p3SN7SKWqcrgEzjGbRSJYTx+Q1pTQC0wrWqHx0vpJraQ6GtHx8tvcg1rlK1byhU5gccxgOgj7B0TDkQ==", + "license": "MIT", + "engines": { + "node": "*" + } + }, "node_modules/concat-map": { "version": "0.0.1", "resolved": "https://registry.npmjs.org/concat-map/-/concat-map-0.0.1.tgz", "integrity": "sha512-/Srv4dswyQNBfohGpz9o6Yb3Gz3SrUDqBH5rTuhGR7ahtlbYKnVxw2bCFMRljaA7EXHaXZ8wsHdodFvbkhKmqg==", - "dev": true, "license": "MIT" }, "node_modules/concat-stream": { @@ -3356,6 +3652,12 @@ "node": ">=6.6.0" } }, + "node_modules/core-util-is": { + "version": "1.0.3", + "resolved": "https://registry.npmjs.org/core-util-is/-/core-util-is-1.0.3.tgz", + "integrity": "sha512-ZQBvi1DcpJ4GDqanjucZ2Hj3wEO5pZDS89BWbkcrvdxksJorwUDDZamX9ldFkp9aw2lmBDLgkObEA4DWNJ9FYQ==", + "license": "MIT" + }, "node_modules/cors": { "version": "2.8.6", "resolved": "https://registry.npmjs.org/cors/-/cors-2.8.6.tgz", @@ -3373,6 +3675,31 @@ "url": "https://opencollective.com/express" } }, + "node_modules/crc-32": { + "version": "1.2.2", + "resolved": "https://registry.npmjs.org/crc-32/-/crc-32-1.2.2.tgz", + "integrity": "sha512-ROmzCKrTnOwybPcJApAA6WBWij23HVfGVNKqqrZpuyZOHqK2CwHSvpGuyt/UNNvaIjEd8X5IFGp4Mh+Ie1IHJQ==", + "license": "Apache-2.0", + "bin": { + "crc32": "bin/crc32.njs" + }, + "engines": { + "node": ">=0.8" + } + }, + "node_modules/crc32-stream": { + "version": "4.0.3", + "resolved": "https://registry.npmjs.org/crc32-stream/-/crc32-stream-4.0.3.tgz", + "integrity": "sha512-NT7w2JVU7DFroFdYkeq8cywxrgjPHWkdX1wjpRQXPX5Asews3tA+Ght6lddQO5Mkumffp3X7GEqku3epj2toIw==", + "license": "MIT", + "dependencies": { + "crc-32": "^1.2.0", + "readable-stream": "^3.4.0" + }, + "engines": { + "node": ">= 10" + } + }, "node_modules/create-require": { "version": "1.1.1", "resolved": "https://registry.npmjs.org/create-require/-/create-require-1.1.1.tgz", @@ -3415,6 +3742,12 @@ "license": "MIT", "peer": true }, + "node_modules/dayjs": { + "version": "1.11.21", + "resolved": "https://registry.npmjs.org/dayjs/-/dayjs-1.11.21.tgz", + "integrity": "sha512-98IT+HOahAisibz/yjKbzuOBwYcjJ7BCLPzARyHiyEBmRz4fatF+KPJszEHXsGYjUG234aH/cOjW1wwTbKUZlA==", + "license": "MIT" + }, "node_modules/debug": { "version": "4.4.3", "resolved": "https://registry.npmjs.org/debug/-/debug-4.4.3.tgz", @@ -3591,6 +3924,45 @@ "node": ">= 0.4" } }, + "node_modules/duplexer2": { + "version": "0.1.4", + "resolved": "https://registry.npmjs.org/duplexer2/-/duplexer2-0.1.4.tgz", + "integrity": "sha512-asLFVfWWtJ90ZyOUHMqk7/S2w2guQKxUI2itj3d92ADHhxUSbCMGi1f1cBcJ7xM1To+pE/Khbwo1yuNbMEPKeA==", + "license": "BSD-3-Clause", + "dependencies": { + "readable-stream": "^2.0.2" + } + }, + "node_modules/duplexer2/node_modules/readable-stream": { + "version": "2.3.8", + "resolved": "https://registry.npmjs.org/readable-stream/-/readable-stream-2.3.8.tgz", + "integrity": "sha512-8p0AUk4XODgIewSi0l8Epjs+EVnWiK7NoDIEGU0HhE7+ZyY8D1IMY7odu5lRrFXGg71L15KG8QrPmum45RTtdA==", + "license": "MIT", + "dependencies": { + "core-util-is": "~1.0.0", + "inherits": "~2.0.3", + "isarray": "~1.0.0", + "process-nextick-args": "~2.0.0", + "safe-buffer": "~5.1.1", + "string_decoder": "~1.1.1", + "util-deprecate": "~1.0.1" + } + }, + "node_modules/duplexer2/node_modules/safe-buffer": { + "version": "5.1.2", + "resolved": "https://registry.npmjs.org/safe-buffer/-/safe-buffer-5.1.2.tgz", + "integrity": "sha512-Gd2UZBJDkXlY7GbJxfsE8/nvKkUEU1G38c1siN6QP6a9PT9MmHB8GnpscSmMJSoF8LOIrt8ud/wPtojys4G6+g==", + "license": "MIT" + }, + "node_modules/duplexer2/node_modules/string_decoder": { + "version": "1.1.1", + "resolved": "https://registry.npmjs.org/string_decoder/-/string_decoder-1.1.1.tgz", + "integrity": "sha512-n/ShnvDi6FHbbVfviro+WojiFzv+s8MPMHBczVePfUpDJLwoLT0ht1l4YwBCbi8pJAveEEdnkHyPyTP/mzRfwg==", + "license": "MIT", + "dependencies": { + "safe-buffer": "~5.1.0" + } + }, "node_modules/eastasianwidth": { "version": "0.2.0", "resolved": "https://registry.npmjs.org/eastasianwidth/-/eastasianwidth-0.2.0.tgz", @@ -3661,6 +4033,15 @@ "node": ">= 0.8" } }, + "node_modules/end-of-stream": { + "version": "1.4.5", + "resolved": "https://registry.npmjs.org/end-of-stream/-/end-of-stream-1.4.5.tgz", + "integrity": "sha512-ooEGc6HP26xXq/N+GCGOT0JKCLDGrq2bQUZrQ7gyrJiZANJ/8YDTxTpQBXGMn+WbIQXNVpyWymm7KYVICQnyOg==", + "license": "MIT", + "dependencies": { + "once": "^1.4.0" + } + }, "node_modules/env-paths": { "version": "3.0.0", "resolved": "https://registry.npmjs.org/env-paths/-/env-paths-3.0.0.tgz", @@ -3769,6 +4150,26 @@ "integrity": "sha512-mlsTRyGaPBjPedk6Bvw+aqbsXDtoAyAzm5MO7JgU+yVRyMQ5O8bD4Kcci7BS85f93veegeCPkL8R4GLClnjLFw==", "license": "MIT" }, + "node_modules/exceljs": { + "version": "4.4.0", + "resolved": "https://registry.npmjs.org/exceljs/-/exceljs-4.4.0.tgz", + "integrity": "sha512-XctvKaEMaj1Ii9oDOqbW/6e1gXknSY4g/aLCDicOXqBE4M0nRWkUu0PTp++UPNzoFY12BNHMfs/VadKIS6llvg==", + "license": "MIT", + "dependencies": { + "archiver": "^5.0.0", + "dayjs": "^1.8.34", + "fast-csv": "^4.3.1", + "jszip": "^3.10.1", + "readable-stream": "^3.6.0", + "saxes": "^5.0.1", + "tmp": "^0.2.0", + "unzipper": "^0.10.11", + "uuid": "^8.3.0" + }, + "engines": { + "node": ">=8.3.0" + } + }, "node_modules/execa": { "version": "5.1.1", "resolved": "https://registry.npmjs.org/execa/-/execa-5.1.1.tgz", @@ -3901,6 +4302,19 @@ "node": ">=8.0.0" } }, + "node_modules/fast-csv": { + "version": "4.3.6", + "resolved": "https://registry.npmjs.org/fast-csv/-/fast-csv-4.3.6.tgz", + "integrity": "sha512-2RNSpuwwsJGP0frGsOmTb9oUF+VkFSM4SyLTDgwf2ciHWTarN0lQTC+F2f/t5J9QjW+c65VFIAAu85GsvMIusw==", + "license": "MIT", + "dependencies": { + "@fast-csv/format": "4.3.5", + "@fast-csv/parse": "4.3.6" + }, + "engines": { + "node": ">=10.0.0" + } + }, "node_modules/fast-deep-equal": { "version": "3.1.3", "resolved": "https://registry.npmjs.org/fast-deep-equal/-/fast-deep-equal-3.1.3.tgz", @@ -4084,11 +4498,16 @@ "node": ">= 0.8" } }, + "node_modules/fs-constants": { + "version": "1.0.0", + "resolved": "https://registry.npmjs.org/fs-constants/-/fs-constants-1.0.0.tgz", + "integrity": "sha512-y6OAwoSIf7FyjMIv94u+b5rdheZEjzR63GTyZJm5qh4Bi+2YgwLCcI/fPFZkL5PSixOt6ZNKm+w+Hfp/Bciwow==", + "license": "MIT" + }, "node_modules/fs.realpath": { "version": "1.0.0", "resolved": "https://registry.npmjs.org/fs.realpath/-/fs.realpath-1.0.0.tgz", "integrity": "sha512-OO0pH2lK6a0hZnAdau5ItzHPI6pUlvI7jMVnxUQRtw4owF2wk8lOSabtGDCTP4Ggrg2MbGnWO9X8K1t4+fGMDw==", - "dev": true, "license": "ISC" }, "node_modules/fsevents": { @@ -4106,6 +4525,22 @@ "node": "^8.16.0 || ^10.6.0 || >=11.0.0" } }, + "node_modules/fstream": { + "version": "1.0.12", + "resolved": "https://registry.npmjs.org/fstream/-/fstream-1.0.12.tgz", + "integrity": "sha512-WvJ193OHa0GHPEL+AycEJgxvBEwyfRkN1vhjca23OaPVMCaLCXTd5qAu82AjTcgP1UJmytkOKb63Ypde7raDIg==", + "deprecated": "This package is no longer supported.", + "license": "ISC", + "dependencies": { + "graceful-fs": "^4.1.2", + "inherits": "~2.0.0", + "mkdirp": ">=0.5 0", + "rimraf": "2" + }, + "engines": { + "node": ">=0.6" + } + }, "node_modules/function-bind": { "version": "1.1.2", "resolved": "https://registry.npmjs.org/function-bind/-/function-bind-1.1.2.tgz", @@ -4260,7 +4695,6 @@ "version": "4.2.11", "resolved": "https://registry.npmjs.org/graceful-fs/-/graceful-fs-4.2.11.tgz", "integrity": "sha512-RbJ5/jmFcNNCcDV5o9eTnBLJ/HszWV0P73bc+Ff4nS/rJj+YaS6IGyiOL0VoBYX+l1Wrl3k63h/KrH+nhJ0XvQ==", - "devOptional": true, "license": "ISC" }, "node_modules/grammex": { @@ -4423,6 +4857,12 @@ ], "license": "BSD-3-Clause" }, + "node_modules/immediate": { + "version": "3.0.6", + "resolved": "https://registry.npmjs.org/immediate/-/immediate-3.0.6.tgz", + "integrity": "sha512-XXOFtyqDjNDAQxVfYxuF7g9Il/IbWmmlQg2MYKOH8ExIT1qg6xc4zyS3HaEEATgs1btfzxq15ciUiY7gjSXRGQ==", + "license": "MIT" + }, "node_modules/import-local": { "version": "3.2.0", "resolved": "https://registry.npmjs.org/import-local/-/import-local-3.2.0.tgz", @@ -4458,7 +4898,6 @@ "resolved": "https://registry.npmjs.org/inflight/-/inflight-1.0.6.tgz", "integrity": "sha512-k92I/b08q4wvFscXCLvqfsHCrjrF7yiXsQuIVvVE7N82W3+aqpzuUdBbfhWcy/FZR3/4IgflMgKLOsvPDrGCJA==", "deprecated": "This module is not supported, and leaks memory. Do not use it. Check out lru-cache if you want a good and tested way to coalesce async requests by a key value, which is much more comprehensive and powerful.", - "dev": true, "license": "ISC", "dependencies": { "once": "^1.3.0", @@ -4567,6 +5006,12 @@ ], "license": "MIT" }, + "node_modules/isarray": { + "version": "1.0.0", + "resolved": "https://registry.npmjs.org/isarray/-/isarray-1.0.0.tgz", + "integrity": "sha512-VLghIWNM6ELQzo7zwmcg0NmTVyWKYjvIeM83yjp0wRDTmUnrM678fQbcKBo6n2CJEF0szoG//ytg+TKla89ALQ==", + "license": "MIT" + }, "node_modules/isexe": { "version": "2.0.0", "resolved": "https://registry.npmjs.org/isexe/-/isexe-2.0.0.tgz", @@ -5357,6 +5802,90 @@ "node": ">=6" } }, + "node_modules/jszip": { + "version": "3.10.1", + "resolved": "https://registry.npmjs.org/jszip/-/jszip-3.10.1.tgz", + "integrity": "sha512-xXDvecyTpGLrqFrvkrUSoxxfJI5AH7U8zxxtVclpsUtMCq4JQ290LY8AW5c7Ggnr/Y/oK+bQMbqK2qmtk3pN4g==", + "license": "(MIT OR GPL-3.0-or-later)", + "dependencies": { + "lie": "~3.3.0", + "pako": "~1.0.2", + "readable-stream": "~2.3.6", + "setimmediate": "^1.0.5" + } + }, + "node_modules/jszip/node_modules/readable-stream": { + "version": "2.3.8", + "resolved": "https://registry.npmjs.org/readable-stream/-/readable-stream-2.3.8.tgz", + "integrity": "sha512-8p0AUk4XODgIewSi0l8Epjs+EVnWiK7NoDIEGU0HhE7+ZyY8D1IMY7odu5lRrFXGg71L15KG8QrPmum45RTtdA==", + "license": "MIT", + "dependencies": { + "core-util-is": "~1.0.0", + "inherits": "~2.0.3", + "isarray": "~1.0.0", + "process-nextick-args": "~2.0.0", + "safe-buffer": "~5.1.1", + "string_decoder": "~1.1.1", + "util-deprecate": "~1.0.1" + } + }, + "node_modules/jszip/node_modules/safe-buffer": { + "version": "5.1.2", + "resolved": "https://registry.npmjs.org/safe-buffer/-/safe-buffer-5.1.2.tgz", + "integrity": "sha512-Gd2UZBJDkXlY7GbJxfsE8/nvKkUEU1G38c1siN6QP6a9PT9MmHB8GnpscSmMJSoF8LOIrt8ud/wPtojys4G6+g==", + "license": "MIT" + }, + "node_modules/jszip/node_modules/string_decoder": { + "version": "1.1.1", + "resolved": "https://registry.npmjs.org/string_decoder/-/string_decoder-1.1.1.tgz", + "integrity": "sha512-n/ShnvDi6FHbbVfviro+WojiFzv+s8MPMHBczVePfUpDJLwoLT0ht1l4YwBCbi8pJAveEEdnkHyPyTP/mzRfwg==", + "license": "MIT", + "dependencies": { + "safe-buffer": "~5.1.0" + } + }, + "node_modules/lazystream": { + "version": "1.0.1", + "resolved": "https://registry.npmjs.org/lazystream/-/lazystream-1.0.1.tgz", + "integrity": "sha512-b94GiNHQNy6JNTrt5w6zNyffMrNkXZb3KTkCZJb2V1xaEGCk093vkZ2jk3tpaeP33/OiXC+WvK9AxUebnf5nbw==", + "license": "MIT", + "dependencies": { + "readable-stream": "^2.0.5" + }, + "engines": { + "node": ">= 0.6.3" + } + }, + "node_modules/lazystream/node_modules/readable-stream": { + "version": "2.3.8", + "resolved": "https://registry.npmjs.org/readable-stream/-/readable-stream-2.3.8.tgz", + "integrity": "sha512-8p0AUk4XODgIewSi0l8Epjs+EVnWiK7NoDIEGU0HhE7+ZyY8D1IMY7odu5lRrFXGg71L15KG8QrPmum45RTtdA==", + "license": "MIT", + "dependencies": { + "core-util-is": "~1.0.0", + "inherits": "~2.0.3", + "isarray": "~1.0.0", + "process-nextick-args": "~2.0.0", + "safe-buffer": "~5.1.1", + "string_decoder": "~1.1.1", + "util-deprecate": "~1.0.1" + } + }, + "node_modules/lazystream/node_modules/safe-buffer": { + "version": "5.1.2", + "resolved": "https://registry.npmjs.org/safe-buffer/-/safe-buffer-5.1.2.tgz", + "integrity": "sha512-Gd2UZBJDkXlY7GbJxfsE8/nvKkUEU1G38c1siN6QP6a9PT9MmHB8GnpscSmMJSoF8LOIrt8ud/wPtojys4G6+g==", + "license": "MIT" + }, + "node_modules/lazystream/node_modules/string_decoder": { + "version": "1.1.1", + "resolved": "https://registry.npmjs.org/string_decoder/-/string_decoder-1.1.1.tgz", + "integrity": "sha512-n/ShnvDi6FHbbVfviro+WojiFzv+s8MPMHBczVePfUpDJLwoLT0ht1l4YwBCbi8pJAveEEdnkHyPyTP/mzRfwg==", + "license": "MIT", + "dependencies": { + "safe-buffer": "~5.1.0" + } + }, "node_modules/leven": { "version": "3.1.0", "resolved": "https://registry.npmjs.org/leven/-/leven-3.1.0.tgz", @@ -5373,6 +5902,15 @@ "integrity": "sha512-rvr3HIMdOgzhz1RFGjftji+wjoAFlzhqCNqJOU/MKTZQ8d9NZxAR/tI+0weDicyoucqVR0U1GCniqHJ0f8aM2A==", "license": "MIT" }, + "node_modules/lie": { + "version": "3.3.0", + "resolved": "https://registry.npmjs.org/lie/-/lie-3.3.0.tgz", + "integrity": "sha512-UaiMJzeWRlEujzAuw5LokY1L5ecNQYZKfmyZ9L7wDHb/p5etKaxXhohBcrw0EYby+G/NA52vRSN4N39dxHAIwQ==", + "license": "MIT", + "dependencies": { + "immediate": "~3.0.5" + } + }, "node_modules/lines-and-columns": { "version": "1.2.4", "resolved": "https://registry.npmjs.org/lines-and-columns/-/lines-and-columns-1.2.4.tgz", @@ -5380,6 +5918,12 @@ "dev": true, "license": "MIT" }, + "node_modules/listenercount": { + "version": "1.0.1", + "resolved": "https://registry.npmjs.org/listenercount/-/listenercount-1.0.1.tgz", + "integrity": "sha512-3mk/Zag0+IJxeDrxSgaDPy4zZ3w05PRZeJNnlWhzFz5OkX49J4krc+A8X2d2M69vGMBEX0uyl8M+W+8gH+kBqQ==", + "license": "ISC" + }, "node_modules/load-esm": { "version": "1.0.3", "resolved": "https://registry.npmjs.org/load-esm/-/load-esm-1.0.3.tgz", @@ -5424,12 +5968,73 @@ "integrity": "sha512-qjxPLHd3r5DnsdGacqOMU6pb/avJzdh9tFX2ymgoZE27BmjXrNy/y4LoaiTeAb+O3gL8AfpJGtqfX/ae2leYYQ==", "license": "MIT" }, + "node_modules/lodash.difference": { + "version": "4.5.0", + "resolved": "https://registry.npmjs.org/lodash.difference/-/lodash.difference-4.5.0.tgz", + "integrity": "sha512-dS2j+W26TQ7taQBGN8Lbbq04ssV3emRw4NY58WErlTO29pIqS0HmoT5aJ9+TUQ1N3G+JOZSji4eugsWwGp9yPA==", + "license": "MIT" + }, + "node_modules/lodash.escaperegexp": { + "version": "4.1.2", + "resolved": "https://registry.npmjs.org/lodash.escaperegexp/-/lodash.escaperegexp-4.1.2.tgz", + "integrity": "sha512-TM9YBvyC84ZxE3rgfefxUWiQKLilstD6k7PTGt6wfbtXF8ixIJLOL3VYyV/z+ZiPLsVxAsKAFVwWlWeb2Y8Yyw==", + "license": "MIT" + }, + "node_modules/lodash.flatten": { + "version": "4.4.0", + "resolved": "https://registry.npmjs.org/lodash.flatten/-/lodash.flatten-4.4.0.tgz", + "integrity": "sha512-C5N2Z3DgnnKr0LOpv/hKCgKdb7ZZwafIrsesve6lmzvZIRZRGaZ/l6Q8+2W7NaT+ZwO3fFlSCzCzrDCFdJfZ4g==", + "license": "MIT" + }, + "node_modules/lodash.groupby": { + "version": "4.6.0", + "resolved": "https://registry.npmjs.org/lodash.groupby/-/lodash.groupby-4.6.0.tgz", + "integrity": "sha512-5dcWxm23+VAoz+awKmBaiBvzox8+RqMgFhi7UvX9DHZr2HdxHXM/Wrf8cfKpsW37RNrvtPn6hSwNqurSILbmJw==", + "license": "MIT" + }, "node_modules/lodash.isarguments": { "version": "3.1.0", "resolved": "https://registry.npmjs.org/lodash.isarguments/-/lodash.isarguments-3.1.0.tgz", "integrity": "sha512-chi4NHZlZqZD18a0imDHnZPrDeBbTtVN7GXMwuGdRH9qotxAjYs3aVLKc7zNOG9eddR5Ksd8rvFEBc9SsggPpg==", "license": "MIT" }, + "node_modules/lodash.isboolean": { + "version": "3.0.3", + "resolved": "https://registry.npmjs.org/lodash.isboolean/-/lodash.isboolean-3.0.3.tgz", + "integrity": "sha512-Bz5mupy2SVbPHURB98VAcw+aHh4vRV5IPNhILUCsOzRmsTmSQ17jIuqopAentWoehktxGd9e/hbIXq980/1QJg==", + "license": "MIT" + }, + "node_modules/lodash.isequal": { + "version": "4.5.0", + "resolved": "https://registry.npmjs.org/lodash.isequal/-/lodash.isequal-4.5.0.tgz", + "integrity": "sha512-pDo3lu8Jhfjqls6GkMgpahsF9kCyayhgykjyLMNFTKWrpVdAQtYyB4muAMWozBB4ig/dtWAmsMxLEI8wuz+DYQ==", + "deprecated": "This package is deprecated. Use require('node:util').isDeepStrictEqual instead.", + "license": "MIT" + }, + "node_modules/lodash.isfunction": { + "version": "3.0.9", + "resolved": "https://registry.npmjs.org/lodash.isfunction/-/lodash.isfunction-3.0.9.tgz", + "integrity": "sha512-AirXNj15uRIMMPihnkInB4i3NHeb4iBtNg9WRWuK2o31S+ePwwNmDPaTL3o7dTJ+VXNZim7rFs4rxN4YU1oUJw==", + "license": "MIT" + }, + "node_modules/lodash.isnil": { + "version": "4.0.0", + "resolved": "https://registry.npmjs.org/lodash.isnil/-/lodash.isnil-4.0.0.tgz", + "integrity": "sha512-up2Mzq3545mwVnMhTDMdfoG1OurpA/s5t88JmQX809eH3C8491iu2sfKhTfhQtKY78oPNhiaHJUpT/dUDAAtng==", + "license": "MIT" + }, + "node_modules/lodash.isplainobject": { + "version": "4.0.6", + "resolved": "https://registry.npmjs.org/lodash.isplainobject/-/lodash.isplainobject-4.0.6.tgz", + "integrity": "sha512-oSXzaWypCMHkPC3NvBEaPHf0KsA5mvPrOPgQWDsbg8n7orZ290M0BmC/jgRZ4vcJ6DTAhjrsSYgdsW/F+MFOBA==", + "license": "MIT" + }, + "node_modules/lodash.isundefined": { + "version": "3.0.1", + "resolved": "https://registry.npmjs.org/lodash.isundefined/-/lodash.isundefined-3.0.1.tgz", + "integrity": "sha512-MXB1is3s899/cD8jheYYE2V9qTHwKvt+npCwpD+1Sxm3Q3cECXCiYHjeHWXNwr6Q0SOBPrYUDxendrO6goVTEA==", + "license": "MIT" + }, "node_modules/lodash.memoize": { "version": "4.1.2", "resolved": "https://registry.npmjs.org/lodash.memoize/-/lodash.memoize-4.1.2.tgz", @@ -5437,6 +6042,18 @@ "dev": true, "license": "MIT" }, + "node_modules/lodash.union": { + "version": "4.6.0", + "resolved": "https://registry.npmjs.org/lodash.union/-/lodash.union-4.6.0.tgz", + "integrity": "sha512-c4pB2CdGrGdjMKYLA+XiRDO7Y0PRQbm/Gzg8qMj+QH+pFVAoTp5sBpO0odL3FjoPCGjK96p6qsP+yQoiLoOBcw==", + "license": "MIT" + }, + "node_modules/lodash.uniq": { + "version": "4.5.0", + "resolved": "https://registry.npmjs.org/lodash.uniq/-/lodash.uniq-4.5.0.tgz", + "integrity": "sha512-xfBaXQd9ryd9dlSDvnvI0lvxfLJlYAZzXomUYzLKtUeOQvOP5piqAWuGtrhWeqaXK9hhoM/iyJc5AV+XfsX3HQ==", + "license": "MIT" + }, "node_modules/long": { "version": "5.3.2", "resolved": "https://registry.npmjs.org/long/-/long-5.3.2.tgz", @@ -5604,7 +6221,6 @@ "version": "1.2.8", "resolved": "https://registry.npmjs.org/minimist/-/minimist-1.2.8.tgz", "integrity": "sha512-2yyAR8qBkN3YuheJanUpWC5U3bb5osDywNB8RzDVlDwDHbocAJveqqj1u8+SVD7jkWT4yvsHCpWqqWqAxb0zCA==", - "dev": true, "license": "MIT", "funding": { "url": "https://github.com/sponsors/ljharb" @@ -5674,6 +6290,18 @@ "node": ">=16 || 14 >=14.17" } }, + "node_modules/mkdirp": { + "version": "0.5.6", + "resolved": "https://registry.npmjs.org/mkdirp/-/mkdirp-0.5.6.tgz", + "integrity": "sha512-FP+p8RB8OWpF3YZBCrP5gtADmtXApB5AMLn+vdyA+PyxCjrCs00mjyUozssO33cwDeT3wNGdLxJ5M//YqtHAJw==", + "license": "MIT", + "dependencies": { + "minimist": "^1.2.6" + }, + "bin": { + "mkdirp": "bin/cmd.js" + } + }, "node_modules/ms": { "version": "2.1.3", "resolved": "https://registry.npmjs.org/ms/-/ms-2.1.3.tgz", @@ -5888,7 +6516,6 @@ "version": "3.0.0", "resolved": "https://registry.npmjs.org/normalize-path/-/normalize-path-3.0.0.tgz", "integrity": "sha512-6eZs5Ls3WtCisHWp9S2GUy8dqkpGi4BVSz3GaqiE6ezub0512ESztXUwUB6C6IKbQkY2Pnb/mD4WYojCRwcwLA==", - "dev": true, "license": "MIT", "engines": { "node": ">=0.10.0" @@ -6034,6 +6661,12 @@ "dev": true, "license": "BlueOak-1.0.0" }, + "node_modules/pako": { + "version": "1.0.11", + "resolved": "https://registry.npmjs.org/pako/-/pako-1.0.11.tgz", + "integrity": "sha512-4hLB8Py4zZce5s4yd9XzopqwVv/yGNhV1Bl8NTmCq1763HeK2+EwVTv+leGeL13Dnh2wfbqowVPXCIO0z4taYw==", + "license": "(MIT AND Zlib)" + }, "node_modules/parse-json": { "version": "5.2.0", "resolved": "https://registry.npmjs.org/parse-json/-/parse-json-5.2.0.tgz", @@ -6091,7 +6724,6 @@ "version": "1.0.1", "resolved": "https://registry.npmjs.org/path-is-absolute/-/path-is-absolute-1.0.1.tgz", "integrity": "sha512-AVbw3UJ2e9bq64vSaS9Am0fje1Pa8pbGqTTsmXfaIiMpnr5DlDhfJOuLj9Sf95ZPVDAUerDfEk88MPmPe7UCQg==", - "dev": true, "license": "MIT", "engines": { "node": ">=0.10.0" @@ -6424,6 +7056,12 @@ } } }, + "node_modules/process-nextick-args": { + "version": "2.0.1", + "resolved": "https://registry.npmjs.org/process-nextick-args/-/process-nextick-args-2.0.1.tgz", + "integrity": "sha512-3ouUOpQhtgrbOa17J7+uxOTpITYWaGP7/AhoR3+A+/1e9skrzelGi/dXzEYyvbxubEF6Wn2ypscTKiKJFFn1ag==", + "license": "MIT" + }, "node_modules/proper-lockfile": { "version": "4.1.2", "resolved": "https://registry.npmjs.org/proper-lockfile/-/proper-lockfile-4.1.2.tgz", @@ -6601,6 +7239,27 @@ "node": ">= 6" } }, + "node_modules/readdir-glob": { + "version": "1.1.3", + "resolved": "https://registry.npmjs.org/readdir-glob/-/readdir-glob-1.1.3.tgz", + "integrity": "sha512-v05I2k7xN8zXvPD9N+z/uhXPaj0sUFCe2rcWZIpBsqxfP7xXFQ0tipAd/wjj1YxWyWtUS5IDJpOG82JKt2EAVA==", + "license": "Apache-2.0", + "dependencies": { + "minimatch": "^5.1.0" + } + }, + "node_modules/readdir-glob/node_modules/minimatch": { + "version": "5.1.9", + "resolved": "https://registry.npmjs.org/minimatch/-/minimatch-5.1.9.tgz", + "integrity": "sha512-7o1wEA2RyMP7Iu7GNba9vc0RWWGACJOCZBJX2GJWip0ikV+wcOsgVuY9uE8CPiyQhkGFSlhuSkZPavN7u1c2Fw==", + "license": "ISC", + "dependencies": { + "brace-expansion": "^2.0.1" + }, + "engines": { + "node": ">=10" + } + }, "node_modules/readdirp": { "version": "5.0.0", "resolved": "https://registry.npmjs.org/readdirp/-/readdirp-5.0.0.tgz", @@ -6705,6 +7364,62 @@ "node": ">= 4" } }, + "node_modules/rimraf": { + "version": "2.7.1", + "resolved": "https://registry.npmjs.org/rimraf/-/rimraf-2.7.1.tgz", + "integrity": "sha512-uWjbaKIK3T1OSVptzX7Nl6PvQ3qAGtKEtVRjRuazjfL3Bx5eI409VZSqgND+4UNnmzLVdPj9FqFJNPqBZFve4w==", + "deprecated": "Rimraf versions prior to v4 are no longer supported", + "license": "ISC", + "dependencies": { + "glob": "^7.1.3" + }, + "bin": { + "rimraf": "bin.js" + } + }, + "node_modules/rimraf/node_modules/brace-expansion": { + "version": "1.1.16", + "resolved": "https://registry.npmjs.org/brace-expansion/-/brace-expansion-1.1.16.tgz", + "integrity": "sha512-IDw48K2/2kRkg9LdJxurvq3lV3aBgq0REY89duEqFRthjlPdXHKMj7EnQOXVckxzgisinf3nHfrcE2FufFLXMw==", + "license": "MIT", + "dependencies": { + "balanced-match": "^1.0.0", + "concat-map": "0.0.1" + } + }, + "node_modules/rimraf/node_modules/glob": { + "version": "7.2.3", + "resolved": "https://registry.npmjs.org/glob/-/glob-7.2.3.tgz", + "integrity": "sha512-nFR0zLpU2YCaRxwoCJvL6UvCH2JFyFVIvwTLsIf21AuHlMskA1hhTdk+LlYJtOlYt9v6dvszD2BGRqBL+iQK9Q==", + "deprecated": "Old versions of glob are not supported, and contain widely publicized security vulnerabilities, which have been fixed in the current version. Please update. Support for old versions may be purchased (at exorbitant rates) by contacting i@izs.me", + "license": "ISC", + "dependencies": { + "fs.realpath": "^1.0.0", + "inflight": "^1.0.4", + "inherits": "2", + "minimatch": "^3.1.1", + "once": "^1.3.0", + "path-is-absolute": "^1.0.0" + }, + "engines": { + "node": "*" + }, + "funding": { + "url": "https://github.com/sponsors/isaacs" + } + }, + "node_modules/rimraf/node_modules/minimatch": { + "version": "3.1.5", + "resolved": "https://registry.npmjs.org/minimatch/-/minimatch-3.1.5.tgz", + "integrity": "sha512-VgjWUsnnT6n+NUk6eZq77zeFdpW2LWDzP6zFGrCbHXiYNul5Dzqk2HHQ5uFH2DNW5Xbp8+jVzaeNt94ssEEl4w==", + "license": "ISC", + "dependencies": { + "brace-expansion": "^1.1.7" + }, + "engines": { + "node": "*" + } + }, "node_modules/router": { "version": "2.2.0", "resolved": "https://registry.npmjs.org/router/-/router-2.2.0.tgz", @@ -6765,6 +7480,18 @@ "node": ">=11.0.0" } }, + "node_modules/saxes": { + "version": "5.0.1", + "resolved": "https://registry.npmjs.org/saxes/-/saxes-5.0.1.tgz", + "integrity": "sha512-5LBh1Tls8c9xgGjw3QrMwETmTMVk0oFgvrFSvWx62llR2hcEInrKNZ2GZCCuuy2lvWrdl5jhbpeqc5hRYKFOcw==", + "license": "ISC", + "dependencies": { + "xmlchars": "^2.2.0" + }, + "engines": { + "node": ">=10" + } + }, "node_modules/scheduler": { "version": "0.27.0", "resolved": "https://registry.npmjs.org/scheduler/-/scheduler-0.27.0.tgz", @@ -6836,6 +7563,12 @@ "url": "https://opencollective.com/express" } }, + "node_modules/setimmediate": { + "version": "1.0.5", + "resolved": "https://registry.npmjs.org/setimmediate/-/setimmediate-1.0.5.tgz", + "integrity": "sha512-MATJdZp8sLqDl/68LfQmbP8zKPLQNV6BIZoIgrscFDQ+RsvK/BxeDQOgyxKKoh0y/8h3BqVFnCqQ/gd+reiIXA==", + "license": "MIT" + }, "node_modules/setprototypeof": { "version": "1.2.0", "resolved": "https://registry.npmjs.org/setprototypeof/-/setprototypeof-1.2.0.tgz", @@ -7335,6 +8068,22 @@ "url": "https://opencollective.com/synckit" } }, + "node_modules/tar-stream": { + "version": "2.2.0", + "resolved": "https://registry.npmjs.org/tar-stream/-/tar-stream-2.2.0.tgz", + "integrity": "sha512-ujeqbceABgwMZxEJnk2HDY2DlnUZ+9oEcb1KzTVfYHio0UE6dG71n60d8D2I4qNvleWrrXpmjpt7vZeF1LnMZQ==", + "license": "MIT", + "dependencies": { + "bl": "^4.0.3", + "end-of-stream": "^1.4.1", + "fs-constants": "^1.0.0", + "inherits": "^2.0.3", + "readable-stream": "^3.1.1" + }, + "engines": { + "node": ">=6" + } + }, "node_modules/test-exclude": { "version": "6.0.0", "resolved": "https://registry.npmjs.org/test-exclude/-/test-exclude-6.0.0.tgz", @@ -7405,6 +8154,15 @@ "readable-stream": "3" } }, + "node_modules/tmp": { + "version": "0.2.7", + "resolved": "https://registry.npmjs.org/tmp/-/tmp-0.2.7.tgz", + "integrity": "sha512-e0votIpp4Uo2AJYSzVHV6xCcawuiez3DzqDAbrTc3YxBkplN6e+dM13ZeIcZnDg/QpSuU2zfZ3rzwY8ukEnaXw==", + "license": "MIT", + "engines": { + "node": ">=14.14" + } + }, "node_modules/tmpl": { "version": "1.0.5", "resolved": "https://registry.npmjs.org/tmpl/-/tmpl-1.0.5.tgz", @@ -7439,6 +8197,15 @@ "url": "https://github.com/sponsors/Borewit" } }, + "node_modules/traverse": { + "version": "0.3.9", + "resolved": "https://registry.npmjs.org/traverse/-/traverse-0.3.9.tgz", + "integrity": "sha512-iawgk0hLP3SxGKDfnDJf8wTz4p2qImnyihM5Hh/sGvQ3K37dPi/w8sRhdNIxYA1TwFwc5mDhIJq+O0RsvXBKdQ==", + "license": "MIT/X11", + "engines": { + "node": "*" + } + }, "node_modules/ts-jest": { "version": "29.4.11", "resolved": "https://registry.npmjs.org/ts-jest/-/ts-jest-29.4.11.tgz", @@ -7720,6 +8487,54 @@ "@unrs/resolver-binding-win32-x64-msvc": "1.12.2" } }, + "node_modules/unzipper": { + "version": "0.10.14", + "resolved": "https://registry.npmjs.org/unzipper/-/unzipper-0.10.14.tgz", + "integrity": "sha512-ti4wZj+0bQTiX2KmKWuwj7lhV+2n//uXEotUmGuQqrbVZSEGFMbI68+c6JCQ8aAmUWYvtHEz2A8K6wXvueR/6g==", + "license": "MIT", + "dependencies": { + "big-integer": "^1.6.17", + "binary": "~0.3.0", + "bluebird": "~3.4.1", + "buffer-indexof-polyfill": "~1.0.0", + "duplexer2": "~0.1.4", + "fstream": "^1.0.12", + "graceful-fs": "^4.2.2", + "listenercount": "~1.0.1", + "readable-stream": "~2.3.6", + "setimmediate": "~1.0.4" + } + }, + "node_modules/unzipper/node_modules/readable-stream": { + "version": "2.3.8", + "resolved": "https://registry.npmjs.org/readable-stream/-/readable-stream-2.3.8.tgz", + "integrity": "sha512-8p0AUk4XODgIewSi0l8Epjs+EVnWiK7NoDIEGU0HhE7+ZyY8D1IMY7odu5lRrFXGg71L15KG8QrPmum45RTtdA==", + "license": "MIT", + "dependencies": { + "core-util-is": "~1.0.0", + "inherits": "~2.0.3", + "isarray": "~1.0.0", + "process-nextick-args": "~2.0.0", + "safe-buffer": "~5.1.1", + "string_decoder": "~1.1.1", + "util-deprecate": "~1.0.1" + } + }, + "node_modules/unzipper/node_modules/safe-buffer": { + "version": "5.1.2", + "resolved": "https://registry.npmjs.org/safe-buffer/-/safe-buffer-5.1.2.tgz", + "integrity": "sha512-Gd2UZBJDkXlY7GbJxfsE8/nvKkUEU1G38c1siN6QP6a9PT9MmHB8GnpscSmMJSoF8LOIrt8ud/wPtojys4G6+g==", + "license": "MIT" + }, + "node_modules/unzipper/node_modules/string_decoder": { + "version": "1.1.1", + "resolved": "https://registry.npmjs.org/string_decoder/-/string_decoder-1.1.1.tgz", + "integrity": "sha512-n/ShnvDi6FHbbVfviro+WojiFzv+s8MPMHBczVePfUpDJLwoLT0ht1l4YwBCbi8pJAveEEdnkHyPyTP/mzRfwg==", + "license": "MIT", + "dependencies": { + "safe-buffer": "~5.1.0" + } + }, "node_modules/update-browserslist-db": { "version": "1.2.3", "resolved": "https://registry.npmjs.org/update-browserslist-db/-/update-browserslist-db-1.2.3.tgz", @@ -7757,6 +8572,19 @@ "integrity": "sha512-EPD5q1uXyFxJpCrLnCc1nHnq3gOa6DZBocAIiI2TaSCA7VCJ1UJDMagCzIkXNsUYfD1daK//LTEQ8xiIbrHtcw==", "license": "MIT" }, + "node_modules/uuid": { + "version": "11.1.1", + "resolved": "https://registry.npmjs.org/uuid/-/uuid-11.1.1.tgz", + "integrity": "sha512-vIYxrBCC/N/K+Js3qSN88go7kIfNPssr/hHCesKCQNAjmgvYS2oqr69kIufEG+O4+PfezOH4EbIeHCfFov8ZgQ==", + "funding": [ + "https://github.com/sponsors/broofa", + "https://github.com/sponsors/ctavan" + ], + "license": "MIT", + "bin": { + "uuid": "dist/esm/bin/uuid" + } + }, "node_modules/v8-compile-cache-lib": { "version": "3.0.1", "resolved": "https://registry.npmjs.org/v8-compile-cache-lib/-/v8-compile-cache-lib-3.0.1.tgz", @@ -8008,6 +8836,12 @@ "node": ">=4.0" } }, + "node_modules/xmlchars": { + "version": "2.2.0", + "resolved": "https://registry.npmjs.org/xmlchars/-/xmlchars-2.2.0.tgz", + "integrity": "sha512-JZnDKK8B0RCDw84FNdDAIpZK+JuJw+s7Lz8nksI7SIuU3UXJJslUthsi+uWBUYOwPFwW7W7PRLRfUKpxjtjFCw==", + "license": "MIT" + }, "node_modules/xtend": { "version": "4.0.2", "resolved": "https://registry.npmjs.org/xtend/-/xtend-4.0.2.tgz", @@ -8141,6 +8975,84 @@ "grammex": "^3.1.11", "graphmatch": "^1.1.0" } + }, + "node_modules/zip-stream": { + "version": "4.1.1", + "resolved": "https://registry.npmjs.org/zip-stream/-/zip-stream-4.1.1.tgz", + "integrity": "sha512-9qv4rlDiopXg4E69k+vMHjNN63YFMe9sZMrdlvKnCjlCRWeCBswPPMPUfx+ipsAWq1LXHe70RcbaHdJJpS6hyQ==", + "license": "MIT", + "dependencies": { + "archiver-utils": "^3.0.4", + "compress-commons": "^4.1.2", + "readable-stream": "^3.6.0" + }, + "engines": { + "node": ">= 10" + } + }, + "node_modules/zip-stream/node_modules/archiver-utils": { + "version": "3.0.4", + "resolved": "https://registry.npmjs.org/archiver-utils/-/archiver-utils-3.0.4.tgz", + "integrity": "sha512-KVgf4XQVrTjhyWmx6cte4RxonPLR9onExufI1jhvw/MQ4BB6IsZD5gT8Lq+u/+pRkWna/6JoHpiQioaqFP5Rzw==", + "license": "MIT", + "dependencies": { + "glob": "^7.2.3", + "graceful-fs": "^4.2.0", + "lazystream": "^1.0.0", + "lodash.defaults": "^4.2.0", + "lodash.difference": "^4.5.0", + "lodash.flatten": "^4.4.0", + "lodash.isplainobject": "^4.0.6", + "lodash.union": "^4.6.0", + "normalize-path": "^3.0.0", + "readable-stream": "^3.6.0" + }, + "engines": { + "node": ">= 10" + } + }, + "node_modules/zip-stream/node_modules/brace-expansion": { + "version": "1.1.16", + "resolved": "https://registry.npmjs.org/brace-expansion/-/brace-expansion-1.1.16.tgz", + "integrity": "sha512-IDw48K2/2kRkg9LdJxurvq3lV3aBgq0REY89duEqFRthjlPdXHKMj7EnQOXVckxzgisinf3nHfrcE2FufFLXMw==", + "license": "MIT", + "dependencies": { + "balanced-match": "^1.0.0", + "concat-map": "0.0.1" + } + }, + "node_modules/zip-stream/node_modules/glob": { + "version": "7.2.3", + "resolved": "https://registry.npmjs.org/glob/-/glob-7.2.3.tgz", + "integrity": "sha512-nFR0zLpU2YCaRxwoCJvL6UvCH2JFyFVIvwTLsIf21AuHlMskA1hhTdk+LlYJtOlYt9v6dvszD2BGRqBL+iQK9Q==", + "deprecated": "Old versions of glob are not supported, and contain widely publicized security vulnerabilities, which have been fixed in the current version. Please update. Support for old versions may be purchased (at exorbitant rates) by contacting i@izs.me", + "license": "ISC", + "dependencies": { + "fs.realpath": "^1.0.0", + "inflight": "^1.0.4", + "inherits": "2", + "minimatch": "^3.1.1", + "once": "^1.3.0", + "path-is-absolute": "^1.0.0" + }, + "engines": { + "node": "*" + }, + "funding": { + "url": "https://github.com/sponsors/isaacs" + } + }, + "node_modules/zip-stream/node_modules/minimatch": { + "version": "3.1.5", + "resolved": "https://registry.npmjs.org/minimatch/-/minimatch-3.1.5.tgz", + "integrity": "sha512-VgjWUsnnT6n+NUk6eZq77zeFdpW2LWDzP6zFGrCbHXiYNul5Dzqk2HHQ5uFH2DNW5Xbp8+jVzaeNt94ssEEl4w==", + "license": "ISC", + "dependencies": { + "brace-expansion": "^1.1.7" + }, + "engines": { + "node": "*" + } } } } diff --git a/api/package.json b/api/package.json index 87b5058..df1ba40 100644 --- a/api/package.json +++ b/api/package.json @@ -24,6 +24,7 @@ "bullmq": "^5.79.2", "class-transformer": "^0.5.1", "class-validator": "^0.14.3", + "exceljs": "^4.4.0", "ioredis": "^5.11.1", "minio": "^8.0.7", "pg": "^8.22.0", @@ -41,5 +42,13 @@ }, "overrides": { "@hono/node-server": "1.19.13" + }, + "overrides": { + "exceljs": { + "uuid": "11.1.1" + }, + "@prisma/dev": { + "@hono/node-server": "1.19.13" + } } } diff --git a/api/prisma/migrations/20260715190000_add_report_material_import_export_workflow/migration.sql b/api/prisma/migrations/20260715190000_add_report_material_import_export_workflow/migration.sql new file mode 100644 index 0000000..108ea3d --- /dev/null +++ b/api/prisma/migrations/20260715190000_add_report_material_import_export_workflow/migration.sql @@ -0,0 +1,134 @@ +ALTER TABLE "SmsSignature" + ADD COLUMN "materialVersion" INTEGER NOT NULL DEFAULT 1, + ADD COLUMN "pendingReport" BOOLEAN NOT NULL DEFAULT true, + ADD COLUMN "reportChangedAt" TIMESTAMP(3) NOT NULL DEFAULT CURRENT_TIMESTAMP; + +ALTER TABLE "SmsDrainageInfo" + ADD COLUMN "materialVersion" INTEGER NOT NULL DEFAULT 1, + ADD COLUMN "pendingReport" BOOLEAN NOT NULL DEFAULT true, + ADD COLUMN "reportChangedAt" TIMESTAMP(3) NOT NULL DEFAULT CURRENT_TIMESTAMP; + +ALTER TABLE "ChannelReportField" + ADD COLUMN "exportName" TEXT, + ADD COLUMN "columnWidth" INTEGER NOT NULL DEFAULT 18, + ADD COLUMN "imageWidth" INTEGER NOT NULL DEFAULT 120, + ADD COLUMN "imageHeight" INTEGER NOT NULL DEFAULT 80, + ADD COLUMN "defaultValue" TEXT, + ADD COLUMN "transform" TEXT; + +ALTER TABLE "ReportExportFile" + ALTER COLUMN "taskId" DROP NOT NULL, + ADD COLUMN "batchId" TEXT, + ADD COLUMN "channelId" TEXT; + +CREATE TABLE "ReportMaterialBatch" ( + "id" TEXT NOT NULL, + "batchNo" TEXT NOT NULL, + "status" TEXT NOT NULL DEFAULT 'generating', + "createdById" TEXT, + "selectedCount" INTEGER NOT NULL DEFAULT 0, + "channelCount" INTEGER NOT NULL DEFAULT 0, + "fileCount" INTEGER NOT NULL DEFAULT 0, + "errorMessage" TEXT, + "createdAt" TIMESTAMP(3) NOT NULL DEFAULT CURRENT_TIMESTAMP, + "completedAt" TIMESTAMP(3), + CONSTRAINT "ReportMaterialBatch_pkey" PRIMARY KEY ("id") +); + +CREATE TABLE "ReportMaterialBatchItem" ( + "id" TEXT NOT NULL, + "batchId" TEXT NOT NULL, + "signatureId" TEXT NOT NULL, + "drainageItemId" TEXT, + "reportType" TEXT NOT NULL, + "materialVersion" INTEGER NOT NULL, + "snapshot" JSONB NOT NULL, + "createdAt" TIMESTAMP(3) NOT NULL DEFAULT CURRENT_TIMESTAMP, + CONSTRAINT "ReportMaterialBatchItem_pkey" PRIMARY KEY ("id") +); + +CREATE TABLE "ReportExportFileItem" ( + "id" TEXT NOT NULL, + "exportFileId" TEXT NOT NULL, + "batchItemId" TEXT NOT NULL, + "taskId" TEXT NOT NULL, + "rowNumber" INTEGER NOT NULL, + CONSTRAINT "ReportExportFileItem_pkey" PRIMARY KEY ("id") +); + +CREATE TABLE "ReportMaterialImportProfile" ( + "id" TEXT NOT NULL, + "name" TEXT NOT NULL, + "reportType" TEXT NOT NULL, + "tenantId" TEXT, + "applicationId" TEXT, + "sheetName" TEXT, + "headerRowCount" INTEGER NOT NULL DEFAULT 1, + "dataStartRow" INTEGER NOT NULL DEFAULT 2, + "status" TEXT NOT NULL DEFAULT 'active', + "createdAt" TIMESTAMP(3) NOT NULL DEFAULT CURRENT_TIMESTAMP, + "updatedAt" TIMESTAMP(3) NOT NULL, + CONSTRAINT "ReportMaterialImportProfile_pkey" PRIMARY KEY ("id") +); + +CREATE TABLE "ReportMaterialImportProfileColumn" ( + "id" TEXT NOT NULL, + "profileId" TEXT NOT NULL, + "sourceHeader" TEXT NOT NULL, + "sourceHeaderPath" TEXT, + "sourceColumnIndex" INTEGER NOT NULL, + "targetFieldCode" TEXT NOT NULL, + "targetKind" TEXT NOT NULL DEFAULT 'dynamic', + "fieldType" TEXT NOT NULL DEFAULT 'string', + "required" BOOLEAN NOT NULL DEFAULT false, + "transform" TEXT, + "sortOrder" INTEGER NOT NULL DEFAULT 100, + "createdAt" TIMESTAMP(3) NOT NULL DEFAULT CURRENT_TIMESTAMP, + CONSTRAINT "ReportMaterialImportProfileColumn_pkey" PRIMARY KEY ("id") +); + +CREATE TABLE "ReportMaterialImportBatch" ( + "id" TEXT NOT NULL, + "tenantId" TEXT NOT NULL, + "applicationId" TEXT, + "profileId" TEXT, + "fileObjectId" TEXT NOT NULL, + "fileName" TEXT NOT NULL, + "reportType" TEXT NOT NULL, + "status" TEXT NOT NULL DEFAULT 'analyzed', + "sheetName" TEXT NOT NULL, + "headerRowCount" INTEGER NOT NULL DEFAULT 1, + "dataStartRow" INTEGER NOT NULL DEFAULT 2, + "mapping" JSONB NOT NULL, + "preview" JSONB, + "result" JSONB, + "rowCount" INTEGER NOT NULL DEFAULT 0, + "successCount" INTEGER NOT NULL DEFAULT 0, + "failedCount" INTEGER NOT NULL DEFAULT 0, + "createdAt" TIMESTAMP(3) NOT NULL DEFAULT CURRENT_TIMESTAMP, + "completedAt" TIMESTAMP(3), + CONSTRAINT "ReportMaterialImportBatch_pkey" PRIMARY KEY ("id") +); + +CREATE UNIQUE INDEX "ReportMaterialBatch_batchNo_key" ON "ReportMaterialBatch"("batchNo"); +CREATE INDEX "ReportMaterialBatch_status_createdAt_idx" ON "ReportMaterialBatch"("status", "createdAt"); +CREATE INDEX "ReportMaterialBatchItem_batchId_reportType_idx" ON "ReportMaterialBatchItem"("batchId", "reportType"); +CREATE INDEX "ReportMaterialBatchItem_signatureId_drainageItemId_idx" ON "ReportMaterialBatchItem"("signatureId", "drainageItemId"); +CREATE UNIQUE INDEX "ReportExportFileItem_exportFileId_batchItemId_key" ON "ReportExportFileItem"("exportFileId", "batchItemId"); +CREATE INDEX "ReportExportFileItem_taskId_idx" ON "ReportExportFileItem"("taskId"); +CREATE INDEX "ReportExportFile_batchId_channelId_idx" ON "ReportExportFile"("batchId", "channelId"); +CREATE INDEX "ReportMaterialImportProfile_reportType_status_idx" ON "ReportMaterialImportProfile"("reportType", "status"); +CREATE INDEX "ReportMaterialImportProfile_tenantId_applicationId_idx" ON "ReportMaterialImportProfile"("tenantId", "applicationId"); +CREATE UNIQUE INDEX "ReportMaterialImportProfileColumn_profileId_sourceColumnIndex_key" ON "ReportMaterialImportProfileColumn"("profileId", "sourceColumnIndex"); +CREATE INDEX "ReportMaterialImportProfileColumn_profileId_sortOrder_idx" ON "ReportMaterialImportProfileColumn"("profileId", "sortOrder"); +CREATE INDEX "ReportMaterialImportBatch_tenantId_createdAt_idx" ON "ReportMaterialImportBatch"("tenantId", "createdAt"); +CREATE INDEX "ReportMaterialImportBatch_status_createdAt_idx" ON "ReportMaterialImportBatch"("status", "createdAt"); + +ALTER TABLE "ReportExportFile" ADD CONSTRAINT "ReportExportFile_batchId_fkey" FOREIGN KEY ("batchId") REFERENCES "ReportMaterialBatch"("id") ON DELETE CASCADE ON UPDATE CASCADE; +ALTER TABLE "ReportMaterialBatchItem" ADD CONSTRAINT "ReportMaterialBatchItem_batchId_fkey" FOREIGN KEY ("batchId") REFERENCES "ReportMaterialBatch"("id") ON DELETE CASCADE ON UPDATE CASCADE; +ALTER TABLE "ReportMaterialBatchItem" ADD CONSTRAINT "ReportMaterialBatchItem_signatureId_fkey" FOREIGN KEY ("signatureId") REFERENCES "SmsSignature"("id") ON DELETE RESTRICT ON UPDATE CASCADE; +ALTER TABLE "ReportMaterialBatchItem" ADD CONSTRAINT "ReportMaterialBatchItem_drainageItemId_fkey" FOREIGN KEY ("drainageItemId") REFERENCES "SmsDrainageInfo"("id") ON DELETE SET NULL ON UPDATE CASCADE; +ALTER TABLE "ReportExportFileItem" ADD CONSTRAINT "ReportExportFileItem_exportFileId_fkey" FOREIGN KEY ("exportFileId") REFERENCES "ReportExportFile"("id") ON DELETE CASCADE ON UPDATE CASCADE; +ALTER TABLE "ReportExportFileItem" ADD CONSTRAINT "ReportExportFileItem_batchItemId_fkey" FOREIGN KEY ("batchItemId") REFERENCES "ReportMaterialBatchItem"("id") ON DELETE CASCADE ON UPDATE CASCADE; +ALTER TABLE "ReportExportFileItem" ADD CONSTRAINT "ReportExportFileItem_taskId_fkey" FOREIGN KEY ("taskId") REFERENCES "ChannelSignatureReportTask"("id") ON DELETE CASCADE ON UPDATE CASCADE; +ALTER TABLE "ReportMaterialImportProfileColumn" ADD CONSTRAINT "ReportMaterialImportProfileColumn_profileId_fkey" FOREIGN KEY ("profileId") REFERENCES "ReportMaterialImportProfile"("id") ON DELETE CASCADE ON UPDATE CASCADE; diff --git a/api/prisma/migrations/20260715193000_scope_channel_report_fields_by_type/migration.sql b/api/prisma/migrations/20260715193000_scope_channel_report_fields_by_type/migration.sql new file mode 100644 index 0000000..f2eebb5 --- /dev/null +++ b/api/prisma/migrations/20260715193000_scope_channel_report_fields_by_type/migration.sql @@ -0,0 +1,18 @@ +DROP INDEX IF EXISTS "ChannelReportField_channelId_code_key"; + +INSERT INTO "ChannelReportField" ( + "id", "channelId", "drainageFieldId", "reportType", "code", "name", "exportName", "fieldType", + "required", "description", "sortOrder", "columnWidth", "imageWidth", "imageHeight", "defaultValue", + "transform", "status", "createdAt", "updatedAt" +) +SELECT + "id" || '_drainage', "channelId", "drainageFieldId", 'drainage', "code", "name", "exportName", "fieldType", + "required", "description", "sortOrder", "columnWidth", "imageWidth", "imageHeight", "defaultValue", + "transform", "status", "createdAt", "updatedAt" +FROM "ChannelReportField" +WHERE "reportType" = 'both'; + +UPDATE "ChannelReportField" SET "reportType" = 'signature' WHERE "reportType" = 'both'; + +CREATE UNIQUE INDEX "ChannelReportField_channelId_code_reportType_key" + ON "ChannelReportField"("channelId", "code", "reportType"); diff --git a/api/prisma/migrations/20260715194000_initialize_existing_report_material_pending/migration.sql b/api/prisma/migrations/20260715194000_initialize_existing_report_material_pending/migration.sql new file mode 100644 index 0000000..3700e9a --- /dev/null +++ b/api/prisma/migrations/20260715194000_initialize_existing_report_material_pending/migration.sql @@ -0,0 +1,4 @@ +-- Existing rows predate the pending-material workflow and must not be treated as newly changed. +-- New rows continue to use the schema default (true), while later edits explicitly set true. +UPDATE "SmsSignature" SET "pendingReport" = false; +UPDATE "SmsDrainageInfo" SET "pendingReport" = false; diff --git a/api/prisma/migrations/20260715200000_drop_channel_group_item_rate_limit/migration.sql b/api/prisma/migrations/20260715200000_drop_channel_group_item_rate_limit/migration.sql new file mode 100644 index 0000000..138e837 --- /dev/null +++ b/api/prisma/migrations/20260715200000_drop_channel_group_item_rate_limit/migration.sql @@ -0,0 +1 @@ +ALTER TABLE "SmsChannelGroupItem" DROP COLUMN "rateLimitPerSecond"; diff --git a/api/prisma/schema.prisma b/api/prisma/schema.prisma index 4004538..50d5afd 100644 --- a/api/prisma/schema.prisma +++ b/api/prisma/schema.prisma @@ -417,6 +417,9 @@ model SmsSignature { auditStatus String @default("draft") reportStatus String @default("waiting_material") rejectReason String? + materialVersion Int @default(1) + pendingReport Boolean @default(true) + reportChangedAt DateTime @default(now()) createdAt DateTime @default(now()) updatedAt DateTime @updatedAt @@ -429,6 +432,7 @@ model SmsSignature { drainageItems SmsDrainageInfo[] reportTasks ChannelSignatureReportTask[] messageRecords SmsMessageRecord[] + reportBatchItems ReportMaterialBatchItem[] @@index([tenantId, auditStatus]) @@index([tenantId, reportStatus]) @@ -445,6 +449,9 @@ model SmsDrainageInfo { reportValues Json? auditStatus String @default("pending") rejectReason String? + materialVersion Int @default(1) + pendingReport Boolean @default(true) + reportChangedAt DateTime @default(now()) submittedAt DateTime @default(now()) reviewedAt DateTime? createdAt DateTime @default(now()) @@ -455,6 +462,7 @@ model SmsDrainageInfo { application SmsApplication? @relation(fields: [applicationId], references: [id]) reportTasks ChannelSignatureReportTask[] messageRecords SmsMessageRecord[] + reportBatchItems ReportMaterialBatchItem[] @@index([tenantId, auditStatus, updatedAt]) @@index([signatureId, auditStatus]) @@ -651,7 +659,6 @@ model SmsChannelGroupItem { priority Int @default(100) weight Int @default(1) isBackup Boolean @default(false) - rateLimitPerSecond Int? createdAt DateTime @default(now()) group SmsChannelGroup @relation(fields: [groupId], references: [id], onDelete: Cascade) @@ -707,10 +714,16 @@ model ChannelReportField { reportType String @default("both") code String name String + exportName String? fieldType String required Boolean @default(false) description String? sortOrder Int @default(100) + columnWidth Int @default(18) + imageWidth Int @default(120) + imageHeight Int @default(80) + defaultValue String? + transform String? status String @default("active") createdAt DateTime @default(now()) updatedAt DateTime @updatedAt @@ -718,7 +731,7 @@ model ChannelReportField { channel SmsChannel @relation(fields: [channelId], references: [id], onDelete: Cascade) drainageField DrainageField? @relation(fields: [drainageFieldId], references: [id]) - @@unique([channelId, code]) + @@unique([channelId, code, reportType]) @@index([drainageFieldId, reportType]) } @@ -791,6 +804,7 @@ model ChannelSignatureReportTask { records ChannelSignatureReportRecord[] exportFiles ReportExportFile[] receiptImports ReportReceiptImport[] + exportItems ReportExportFileItem[] @@index([tenantId, status]) @@index([signatureId, channelId]) @@ -819,14 +833,136 @@ model ChannelSignatureReportRecord { model ReportExportFile { id String @id @default(cuid()) - taskId String + taskId String? + batchId String? + channelId String? fileObjectId String? fileName String rowCount Int @default(0) status String @default("generated") createdAt DateTime @default(now()) - task ChannelSignatureReportTask @relation(fields: [taskId], references: [id], onDelete: Cascade) + task ChannelSignatureReportTask? @relation(fields: [taskId], references: [id], onDelete: Cascade) + batch ReportMaterialBatch? @relation(fields: [batchId], references: [id], onDelete: Cascade) + items ReportExportFileItem[] + + @@index([batchId, channelId]) +} + +model ReportMaterialBatch { + id String @id @default(cuid()) + batchNo String @unique + status String @default("generating") + createdById String? + selectedCount Int @default(0) + channelCount Int @default(0) + fileCount Int @default(0) + errorMessage String? + createdAt DateTime @default(now()) + completedAt DateTime? + + items ReportMaterialBatchItem[] + exportFiles ReportExportFile[] + + @@index([status, createdAt]) +} + +model ReportMaterialBatchItem { + id String @id @default(cuid()) + batchId String + signatureId String + drainageItemId String? + reportType String + materialVersion Int + snapshot Json + createdAt DateTime @default(now()) + + batch ReportMaterialBatch @relation(fields: [batchId], references: [id], onDelete: Cascade) + signature SmsSignature @relation(fields: [signatureId], references: [id]) + drainageInfo SmsDrainageInfo? @relation(fields: [drainageItemId], references: [id]) + exportItems ReportExportFileItem[] + + @@index([batchId, reportType]) + @@index([signatureId, drainageItemId]) +} + +model ReportExportFileItem { + id String @id @default(cuid()) + exportFileId String + batchItemId String + taskId String + rowNumber Int + + exportFile ReportExportFile @relation(fields: [exportFileId], references: [id], onDelete: Cascade) + batchItem ReportMaterialBatchItem @relation(fields: [batchItemId], references: [id], onDelete: Cascade) + task ChannelSignatureReportTask @relation(fields: [taskId], references: [id], onDelete: Cascade) + + @@unique([exportFileId, batchItemId]) + @@index([taskId]) +} + +model ReportMaterialImportProfile { + id String @id @default(cuid()) + name String + reportType String + tenantId String? + applicationId String? + sheetName String? + headerRowCount Int @default(1) + dataStartRow Int @default(2) + status String @default("active") + createdAt DateTime @default(now()) + updatedAt DateTime @updatedAt + + columns ReportMaterialImportProfileColumn[] + + @@index([reportType, status]) + @@index([tenantId, applicationId]) +} + +model ReportMaterialImportProfileColumn { + id String @id @default(cuid()) + profileId String + sourceHeader String + sourceHeaderPath String? + sourceColumnIndex Int + targetFieldCode String + targetKind String @default("dynamic") + fieldType String @default("string") + required Boolean @default(false) + transform String? + sortOrder Int @default(100) + createdAt DateTime @default(now()) + + profile ReportMaterialImportProfile @relation(fields: [profileId], references: [id], onDelete: Cascade) + + @@unique([profileId, sourceColumnIndex]) + @@index([profileId, sortOrder]) +} + +model ReportMaterialImportBatch { + id String @id @default(cuid()) + tenantId String + applicationId String? + profileId String? + fileObjectId String + fileName String + reportType String + status String @default("analyzed") + sheetName String + headerRowCount Int @default(1) + dataStartRow Int @default(2) + mapping Json + preview Json? + result Json? + rowCount Int @default(0) + successCount Int @default(0) + failedCount Int @default(0) + createdAt DateTime @default(now()) + completedAt DateTime? + + @@index([tenantId, createdAt]) + @@index([status, createdAt]) } model ReportReceiptImport { diff --git a/api/src/app.module.ts b/api/src/app.module.ts index 584ed10..f9c57ca 100644 --- a/api/src/app.module.ts +++ b/api/src/app.module.ts @@ -14,6 +14,7 @@ import { OperationsModule } from './operations/operations.module'; import { PrismaModule } from './prisma/prisma.module'; import { RiskReviewModule } from './risk-review/risk-review.module'; import { ReportsModule } from './reports/reports.module'; +import { ReportMaterialsModule } from './report-materials/report-materials.module'; import { SendChainModule } from './send-chain/send-chain.module'; import { SmsConfigModule } from './sms-config/sms-config.module'; import { TenantsModule } from './tenants/tenants.module'; @@ -38,6 +39,7 @@ import { UsersModule } from './users/users.module'; ChannelsModule, RiskReviewModule, ReportsModule, + ReportMaterialsModule, SendChainModule, OperationsModule, ], diff --git a/api/src/channels/channels.controller.ts b/api/src/channels/channels.controller.ts index 0b6a932..5909681 100644 --- a/api/src/channels/channels.controller.ts +++ b/api/src/channels/channels.controller.ts @@ -13,6 +13,7 @@ import { CreateReportFieldDto, CreateReportMaterialDto, CreateReportTaskDto, + ReplaceReportFieldsDto, ChangeReportTaskStatusesDto, CreateRouteRuleDto, TestChannelDto, @@ -147,6 +148,12 @@ export class ChannelsController { return this.channels.createReportField(body); } + @Put('channels/:channelId/report-fields/:reportType') + @RequireRecentAuthentication() + replaceReportFields(@Param('channelId') channelId: string, @Param('reportType') reportType: 'signature' | 'drainage', @Body() body: ReplaceReportFieldsDto) { + return this.channels.replaceReportFields(channelId, reportType, body); + } + @Get('signature-report-materials') listReportMaterials(@Query('signatureId') signatureId?: string, @Query('channelId') channelId?: string) { return this.channels.listReportMaterials(signatureId, channelId); diff --git a/api/src/channels/channels.service.spec.ts b/api/src/channels/channels.service.spec.ts index 1d2f76c..722e55b 100644 --- a/api/src/channels/channels.service.spec.ts +++ b/api/src/channels/channels.service.spec.ts @@ -96,6 +96,7 @@ function createPrismaMock() { }, drainageField: { findUnique: jest.fn().mockResolvedValue({ id: 'library-1', code: 'license', name: '营业执照', fieldType: 'file', required: true, status: 'active', description: '执照文件' }), + findMany: jest.fn().mockResolvedValue([{ id: 'library-1', code: 'license', name: '营业执照', fieldType: 'file', required: true, status: 'active', description: '执照文件' }]), }, signatureReportMaterial: { findMany: jest.fn().mockResolvedValue([{ signatureId: 'sig-1', fieldCode: 'license', fieldValue: '营业执照', fileObjectId: 'file-1' }]), @@ -178,6 +179,26 @@ describe('ChannelsService', () => { }); }); + it('replaces one report type while preserving legacy both fields for the opposite type', async () => { + const prisma = createPrismaMock(); + const legacyBoth = { id: 'legacy-1', channelId: 'channel-1', drainageFieldId: 'library-1', reportType: 'both', code: 'license', name: '营业执照', exportName: '旧表头', fieldType: 'file', required: true, description: null, sortOrder: 10, columnWidth: 18, imageWidth: 120, imageHeight: 80, defaultValue: null, transform: null, status: 'active', createdAt: new Date(), updatedAt: new Date() }; + const tx = { + channelReportField: { + findMany: jest.fn().mockResolvedValueOnce([legacyBoth]).mockResolvedValueOnce([]).mockResolvedValueOnce([{ id: 'signature-field' }]), + deleteMany: jest.fn(), + create: jest.fn().mockImplementation(({ data }) => Promise.resolve({ id: `created-${data.reportType}`, ...data })), + }, + }; + prisma.$transaction.mockImplementation((callback) => callback(tx)); + const service = new ChannelsService(prisma as never); + + await service.replaceReportFields('channel-1', 'signature', { fields: [{ drainageFieldId: 'library-1', exportName: '新签名表头', required: true }] }); + + expect(tx.channelReportField.deleteMany).toHaveBeenCalledWith({ where: { channelId: 'channel-1', reportType: { in: ['signature', 'both'] } } }); + expect(tx.channelReportField.create).toHaveBeenCalledWith({ data: expect.objectContaining({ channelId: 'channel-1', code: 'license', reportType: 'drainage', exportName: '旧表头' }) }); + expect(tx.channelReportField.create).toHaveBeenCalledWith({ data: expect.objectContaining({ channelId: 'channel-1', code: 'license', reportType: 'signature', exportName: '新签名表头' }) }); + }); + it('lists report tasks for one real channel', async () => { const prisma = createPrismaMock(); prisma.channelSignatureReportTask.findMany.mockResolvedValue([]); @@ -460,6 +481,7 @@ describe('ChannelsService', () => { expect(prisma.smsChannelGroupItem.create).toHaveBeenCalledWith({ data: expect.objectContaining({ groupId: 'group-1', channelId: 'channel-1', carrier: 'mobile', province: '山东' }), }); + expect(prisma.smsChannelGroupItem.create.mock.calls[0][0].data).not.toHaveProperty('rateLimitPerSecond'); await expect(service.addGroupItem({ groupId: 'group-1', channelId: 'channel-1', carrier: 'unicom' })) .rejects.toThrow('Channel group items must use the same carrier'); @@ -545,6 +567,9 @@ describe('ChannelsService', () => { where: { id: 'group-1' }, data: expect.objectContaining({ retryTimeLimitHours: 13, retryTimeLimitMinutes: 750 }), }); + for (const item of tx.smsChannelGroupItem.createMany.mock.calls[0][0].data) { + expect(item).not.toHaveProperty('rateLimitPerSecond'); + } await expect(service.updateGroup('group-1', { carrier: 'mobile', diff --git a/api/src/channels/channels.service.ts b/api/src/channels/channels.service.ts index ec20186..8ccf244 100644 --- a/api/src/channels/channels.service.ts +++ b/api/src/channels/channels.service.ts @@ -47,7 +47,6 @@ export interface CreateChannelGroupItemDto { priority?: number; weight?: number; isBackup?: boolean; - rateLimitPerSecond?: number; } export interface UpdateChannelGroupDto { @@ -83,9 +82,19 @@ export interface CreateReportFieldDto { required?: boolean; description?: string; sortOrder?: number; + exportName?: string; + columnWidth?: number; + imageWidth?: number; + imageHeight?: number; + defaultValue?: string; + transform?: string; status?: string; } +export interface ReplaceReportFieldsDto { + fields: Array>; +} + export interface CreateReportMaterialDto { signatureId: string; channelId: string; @@ -787,7 +796,6 @@ export class ChannelsService implements OnModuleInit, OnModuleDestroy { priority: data.priority ?? 100, weight: data.weight ?? 1, isBackup: data.isBackup ?? false, - rateLimitPerSecond: data.rateLimitPerSecond, }, }); } @@ -834,7 +842,6 @@ export class ChannelsService implements OnModuleInit, OnModuleDestroy { priority: item.priority ?? 100, weight: item.weight ?? 1, isBackup: item.isBackup ?? false, - rateLimitPerSecond: item.rateLimitPerSecond, })), }); } @@ -928,15 +935,69 @@ export class ChannelsService implements OnModuleInit, OnModuleDestroy { reportType, code: field.code, name: field.name, + exportName: data.exportName?.trim() || field.name, fieldType: field.fieldType, required: data.required ?? field.required, description: data.description ?? field.description, sortOrder: data.sortOrder ?? 100, + columnWidth: normalizeSpreadsheetSize(data.columnWidth, 18, 6, 80), + imageWidth: normalizeSpreadsheetSize(data.imageWidth, 120, 24, 600), + imageHeight: normalizeSpreadsheetSize(data.imageHeight, 80, 24, 600), + defaultValue: data.defaultValue, + transform: data.transform, status: data.status ?? 'active', }, }); } + async replaceReportFields(channelId: string, reportType: 'signature' | 'drainage', data: ReplaceReportFieldsDto) { + const channel = await this.prisma.smsChannel.findUnique({ where: { id: channelId } }); + if (!channel) throw new NotFoundException('Channel not found'); + const ids = data.fields.map((field) => field.drainageFieldId); + if (new Set(ids).size !== ids.length) throw new BadRequestException('同一通道报备类型不能重复配置字段'); + const libraryFields = await this.prisma.drainageField.findMany({ where: { id: { in: ids }, status: 'active' } }); + if (libraryFields.length !== ids.length) throw new BadRequestException('报备字段库字段不存在或已停用'); + const fieldById = new Map(libraryFields.map((field) => [field.id, field])); + return this.prisma.$transaction(async (tx) => { + const oppositeType = reportType === 'signature' ? 'drainage' : 'signature'; + const [legacyBoth, oppositeFields] = await Promise.all([ + tx.channelReportField.findMany({ where: { channelId, reportType: 'both' } }), + tx.channelReportField.findMany({ where: { channelId, reportType: oppositeType } }), + ]); + const oppositeCodes = new Set(oppositeFields.map((field) => field.code)); + await tx.channelReportField.deleteMany({ where: { channelId, reportType: { in: [reportType, 'both'] } } }); + for (const legacy of legacyBoth) { + if (oppositeCodes.has(legacy.code)) continue; + const { id: _id, createdAt: _createdAt, updatedAt: _updatedAt, ...legacyData } = legacy; + await tx.channelReportField.create({ data: { ...legacyData, reportType: oppositeType } }); + } + for (const [index, configured] of data.fields.entries()) { + const field = fieldById.get(configured.drainageFieldId)!; + await tx.channelReportField.create({ + data: { + channelId, + drainageFieldId: field.id, + reportType, + code: field.code, + name: field.name, + exportName: configured.exportName?.trim() || field.name, + fieldType: field.fieldType, + required: configured.required ?? field.required, + description: configured.description ?? field.description, + sortOrder: configured.sortOrder ?? (index + 1) * 10, + columnWidth: normalizeSpreadsheetSize(configured.columnWidth, 18, 6, 80), + imageWidth: normalizeSpreadsheetSize(configured.imageWidth, 120, 24, 600), + imageHeight: normalizeSpreadsheetSize(configured.imageHeight, 80, 24, 600), + defaultValue: configured.defaultValue, + transform: configured.transform, + status: configured.status ?? 'active', + }, + }); + } + return tx.channelReportField.findMany({ where: { channelId, reportType }, include: { drainageField: true }, orderBy: [{ sortOrder: 'asc' }, { createdAt: 'asc' }] }); + }); + } + listReportMaterials(signatureId?: string, channelId?: string) { return this.prisma.signatureReportMaterial.findMany({ where: { @@ -1652,6 +1713,11 @@ function normalizeRetryTimeLimitMinutes(minutes: number | undefined, hours: numb return value; } +function normalizeSpreadsheetSize(value: number | undefined, fallback: number, minimum: number, maximum: number) { + if (value === undefined || !Number.isFinite(value)) return fallback; + return Math.min(maximum, Math.max(minimum, Math.round(value))); +} + function normalizeBusinessCarrier(carrier?: string | null) { const normalized = normalizeChannelCarrier(carrier); if (!['mobile', 'unicom', 'telecom'].includes(normalized)) { diff --git a/api/src/operations/admin-operations.controller.ts b/api/src/operations/admin-operations.controller.ts index d4d8bb7..937d6d2 100644 --- a/api/src/operations/admin-operations.controller.ts +++ b/api/src/operations/admin-operations.controller.ts @@ -1,5 +1,7 @@ import { Body, Controller, Get, Param, Post, Query, Res } from '@nestjs/common'; import { ApiTags } from '@nestjs/swagger'; +import { CurrentSessionUserId } from '../auth/current-session-user.decorator'; +import { RequireRecentAuthentication } from '../auth/require-recent-authentication.decorator'; import { SendChainService } from '../send-chain/send-chain.service'; import { OperationsService } from './operations.service'; @@ -145,8 +147,13 @@ export class AdminOperationsController { } @Post('gateway-submit-dead-letters/:id/requeue') - requeueGatewaySubmitDeadLetter(@Param('id') id: string) { - return this.sendChain.requeueGatewaySubmitDeadLetter(id); + @RequireRecentAuthentication() + requeueGatewaySubmitDeadLetter( + @Param('id') id: string, + @Body() body: { confirmedNotSubmitted?: boolean; reason?: string }, + @CurrentSessionUserId() operatorId?: string, + ) { + return this.sendChain.requeueGatewaySubmitDeadLetter(id, { ...body, operatorId }); } @Get('downstream-deliveries') diff --git a/api/src/operations/operations.service.spec.ts b/api/src/operations/operations.service.spec.ts index 27c3733..ad59ff6 100644 --- a/api/src/operations/operations.service.spec.ts +++ b/api/src/operations/operations.service.spec.ts @@ -77,11 +77,15 @@ function createPrismaMock() { status: 'pending', failureCode: 'SUBMIT_PROCESSING_FAILED', failureMessage: 'network down', + rawPayload: '{"upstream":{"passwordCipher":"secret"}}', + commandPayload: { upstream: { account: 'sp', passwordCipher: 'secret' } }, tenant: { name: '租户A' }, application: { name: '应用A' }, channel: { code: 'CMPP-A' }, }]), count: jest.fn().mockResolvedValue(1), + groupBy: jest.fn().mockResolvedValue([{ status: 'pending', _count: { _all: 1 } }]), + findFirst: jest.fn().mockResolvedValue({ createdAt: new Date('2026-07-08T12:00:00.000Z') }), }, gatewayDownstreamRecoveryStatus: { findMany: jest.fn().mockResolvedValue([{ @@ -408,10 +412,22 @@ describe('OperationsService', () => { page: 1, pageSize: 10, })).resolves.toEqual({ - items: [expect.objectContaining({ id: 'dead-1', status: 'pending' })], + items: [expect.objectContaining({ + id: 'dead-1', + status: 'pending', + rawPayloadAvailable: true, + commandPayload: { upstream: { account: 'sp', passwordCipher: '[REDACTED]' } }, + })], total: 1, page: 1, pageSize: 10, + summary: { + pending: 1, + requeueing: 0, + requeued: 0, + resolved: 0, + oldestPendingAt: new Date('2026-07-08T12:00:00.000Z'), + }, }); expect(prisma.gatewaySubmitDeadLetter.findMany).toHaveBeenCalledWith({ diff --git a/api/src/operations/operations.service.ts b/api/src/operations/operations.service.ts index 1745797..97d4607 100644 --- a/api/src/operations/operations.service.ts +++ b/api/src/operations/operations.service.ts @@ -372,11 +372,10 @@ export class OperationsService { async listGatewaySubmitDeadLetters(query: GatewaySubmitDeadLetterQuery) { const page = Math.max(1, Number(query.page ?? 1)); const pageSize = Math.min(100, Math.max(1, Number(query.pageSize ?? 10))); - const where: Prisma.GatewaySubmitDeadLetterWhereInput = { + const baseWhere: Prisma.GatewaySubmitDeadLetterWhereInput = { tenantId: query.tenantId, applicationId: query.applicationId, channelId: query.channelId, - status: query.status && query.status !== 'all' ? query.status : undefined, OR: query.keyword ? [ { streamMessageId: { contains: query.keyword } }, { traceId: { contains: query.keyword } }, @@ -386,7 +385,11 @@ export class OperationsService { { failureMessage: { contains: query.keyword } }, ] : undefined, }; - const [items, total] = await Promise.all([ + const where: Prisma.GatewaySubmitDeadLetterWhereInput = { + ...baseWhere, + status: query.status && query.status !== 'all' ? query.status : undefined, + }; + const [items, total, statusGroups, oldestPending] = await Promise.all([ this.prisma.gatewaySubmitDeadLetter.findMany({ where, include: { tenant: true, application: true, channel: true }, @@ -395,8 +398,39 @@ export class OperationsService { take: pageSize, }), this.prisma.gatewaySubmitDeadLetter.count({ where }), + this.prisma.gatewaySubmitDeadLetter.groupBy({ + by: ['status'], + where: baseWhere, + _count: { _all: true }, + }), + this.prisma.gatewaySubmitDeadLetter.findFirst({ + where: { ...baseWhere, status: 'pending' }, + orderBy: { createdAt: 'asc' }, + select: { createdAt: true }, + }), ]); - return { items, total, page, pageSize }; + const statusCounts = new Map(statusGroups.map((item) => [item.status, item._count._all])); + const messageIds = items.map((item) => item.messageId).filter((value): value is string => Boolean(value)); + const messageStates = messageIds.length > 0 + ? await this.prisma.smsMessageRecord.findMany({ + where: { messageId: { in: messageIds } }, + select: { messageId: true, status: true, submitStatus: true, receiptStatus: true, phoneNumber: true, content: true }, + }) + : []; + const messageStateById = new Map(messageStates.map((item) => [item.messageId, item])); + return { + items: items.map((item) => sanitizeGatewaySubmitException(item, item.messageId ? messageStateById.get(item.messageId) : undefined)), + total, + page, + pageSize, + summary: { + pending: statusCounts.get('pending') ?? 0, + requeueing: statusCounts.get('requeueing') ?? 0, + requeued: statusCounts.get('requeued') ?? 0, + resolved: statusCounts.get('resolved') ?? 0, + oldestPendingAt: oldestPending?.createdAt ?? null, + }, + }; } async listDownstreamDeliveries(query: DownstreamDeliveryQuery) { @@ -1119,3 +1153,47 @@ function normalizeOperationLog(log: Prisma.OperationLogGetPayload<{ include: { t userAgent: log.userAgent ?? '', }; } + +function sanitizeGatewaySubmitException( + item: Prisma.GatewaySubmitDeadLetterGetPayload<{ include: { tenant: true; application: true; channel: true } }>, + messageState?: { status: string; submitStatus: string | null; receiptStatus: string | null; phoneNumber: string; content: string }, +) { + const { rawPayload, commandPayload, tenant, application, channel, ...record } = item; + return { + ...record, + tenant: tenant ? { id: tenant.id, name: tenant.name, code: tenant.code, status: tenant.status } : null, + application: application ? { id: application.id, tenantId: application.tenantId, name: application.name, status: application.status } : null, + channel: channel ? { + id: channel.id, + code: channel.code, + name: channel.name, + status: channel.status, + carrier: channel.carrier, + sendRegion: channel.sendRegion, + rateLimitPerSecond: channel.rateLimitPerSecond, + } : null, + rawPayloadAvailable: Boolean(rawPayload), + commandPayload: redactGatewayCommandValue(commandPayload), + messageState: messageState ?? null, + }; +} + +function redactGatewayCommandValue(value: Prisma.JsonValue | null): Prisma.JsonValue | null { + if (Array.isArray(value)) { + return value.map((item) => redactGatewayCommandValue(item)); + } + if (value && typeof value === 'object') { + const redacted: Record = {}; + for (const [key, child] of Object.entries(value)) { + const normalizedKey = key.toLowerCase(); + redacted[key] = [ + 'password', 'passwordcipher', 'secret', 'secrethash', 'authsource', + 'token', 'apikey', 'accesskey', 'secretkey', + ].includes(normalizedKey) + ? '[REDACTED]' + : redactGatewayCommandValue(child as Prisma.JsonValue); + } + return redacted; + } + return value; +} diff --git a/api/src/report-materials/report-materials.controller.ts b/api/src/report-materials/report-materials.controller.ts new file mode 100644 index 0000000..f7ad8ac --- /dev/null +++ b/api/src/report-materials/report-materials.controller.ts @@ -0,0 +1,61 @@ +import { BadRequestException, Body, Controller, Get, Param, Post, Put, Query, UploadedFile, UseInterceptors } from '@nestjs/common'; +import { FileInterceptor } from '@nestjs/platform-express'; +import { ApiTags } from '@nestjs/swagger'; +import { RequireRecentAuthentication } from '../auth/require-recent-authentication.decorator'; +import { CreateImportProfileDto, CreateReportBatchDto, ImportCommitDto, ReportMaterialsService } from './report-materials.service'; + +type UploadedWorkbook = { originalname: string; mimetype: string; size: number; buffer: Buffer }; + +@ApiTags('report-materials') +@Controller('admin/report-materials') +export class ReportMaterialsController { + constructor(private readonly service: ReportMaterialsService) {} + + @Get('pending') + listPending(@Query('reportType') reportType?: 'signature' | 'drainage', @Query('tenantId') tenantId?: string, @Query('applicationId') applicationId?: string) { + return this.service.listPending({ reportType, tenantId, applicationId }); + } + + @Get('import-profiles') + listImportProfiles(@Query('reportType') reportType?: 'signature' | 'drainage') { + return this.service.listImportProfiles(reportType); + } + + @Post('import-profiles') + @RequireRecentAuthentication() + saveImportProfile(@Body() body: CreateImportProfileDto) { + return this.service.saveImportProfile(body); + } + + @Post('imports/analyze') + @UseInterceptors(FileInterceptor('file', { limits: { fileSize: 100 * 1024 * 1024, files: 1, fields: 12, parts: 13 } })) + analyzeImport(@UploadedFile() file: UploadedWorkbook, @Body() body: Record) { + if (!file) throw new BadRequestException('请选择 XLSX 文件'); + return this.service.analyzeImport(file, { + tenantId: body.tenantId, + applicationId: body.applicationId || undefined, + reportType: body.reportType as 'signature' | 'drainage', + sheetName: body.sheetName || undefined, + headerRowCount: Number(body.headerRowCount || 1), + dataStartRow: Number(body.dataStartRow || 2), + profileId: body.profileId || undefined, + }); + } + + @Put('imports/:id/commit') + @RequireRecentAuthentication() + commitImport(@Param('id') id: string, @Body() body: ImportCommitDto) { + return this.service.commitImport(id, body); + } + + @Get('batches') + listBatches() { + return this.service.listBatches(); + } + + @Post('batches') + @RequireRecentAuthentication() + createBatch(@Body() body: CreateReportBatchDto) { + return this.service.createBatch(body); + } +} diff --git a/api/src/report-materials/report-materials.module.ts b/api/src/report-materials/report-materials.module.ts new file mode 100644 index 0000000..da1b796 --- /dev/null +++ b/api/src/report-materials/report-materials.module.ts @@ -0,0 +1,13 @@ +import { Module } from '@nestjs/common'; +import { FilesModule } from '../files/files.module'; +import { SmsConfigModule } from '../sms-config/sms-config.module'; +import { ReportMaterialsController } from './report-materials.controller'; +import { ReportMaterialsService } from './report-materials.service'; + +@Module({ + imports: [FilesModule, SmsConfigModule], + controllers: [ReportMaterialsController], + providers: [ReportMaterialsService], + exports: [ReportMaterialsService], +}) +export class ReportMaterialsModule {} diff --git a/api/src/report-materials/report-materials.service.spec.ts b/api/src/report-materials/report-materials.service.spec.ts new file mode 100644 index 0000000..8f7b52e --- /dev/null +++ b/api/src/report-materials/report-materials.service.spec.ts @@ -0,0 +1,103 @@ +import ExcelJS from 'exceljs'; +import { ReportMaterialsService } from './report-materials.service'; + +describe('ReportMaterialsService', () => { + it('detects WPS-compatible embedded images and source columns during XLSX analysis', async () => { + const workbook = new ExcelJS.Workbook(); + const sheet = workbook.addWorksheet('签名资料'); + sheet.addRow(['短信签名', '营业执照']); + sheet.addRow(['测试签名', '']); + const imageId = workbook.addImage({ base64: 'data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAAAEAAAABCAQAAAC1HAwCAAAAC0lEQVR42mP8/x8AAusB9Y9ZsmAAAAAASUVORK5CYII=', extension: 'png' }); + sheet.addImage(imageId, { tl: { col: 1, row: 1 }, ext: { width: 80, height: 60 } }); + const buffer = Buffer.from(await workbook.xlsx.writeBuffer()); + const prisma = { + reportMaterialImportProfile: { findUnique: jest.fn().mockResolvedValue({ sheetName: '签名资料', columns: [{ sourceHeader: '短信签名', sourceHeaderPath: '短信签名', sourceColumnIndex: 9, targetFieldCode: 'signature_name', targetKind: 'signatureName', fieldType: 'string', required: true, sortOrder: 10 }] }) }, + reportMaterialImportBatch: { create: jest.fn().mockImplementation(({ data }: { data: Record }) => Promise.resolve({ id: 'import-1', ...data })) }, + }; + const files = { upload: jest.fn().mockResolvedValue({ id: 'source-1', fileName: '签名资料.xlsx', contentType: 'application/vnd.openxmlformats-officedocument.spreadsheetml.sheet' }) }; + const service = new ReportMaterialsService(prisma as never, files as never, {} as never); + + const result = await service.analyzeImport({ originalname: '签名资料.xlsx', mimetype: 'application/vnd.openxmlformats-officedocument.spreadsheetml.sheet', size: buffer.length, buffer }, { tenantId: 'tenant-1', applicationId: 'app-1', reportType: 'signature', headerRowCount: 1, dataStartRow: 2, profileId: 'profile-1' }); + + expect(result.imageCount).toBe(1); + expect(result.columns).toEqual(expect.arrayContaining([expect.objectContaining({ sourceHeader: '营业执照', imageCount: 1 })])); + expect(result.rows).toEqual([expect.objectContaining({ rowNumber: 2, imageColumns: [2] })]); + expect(result.suggestedMappings).toEqual([expect.objectContaining({ sourceColumnIndex: 1, targetKind: 'signatureName' })]); + }); + + it('expands one selected signature to every routed channel and embeds images in each XLSX', async () => { + const uploadedWorkbooks: Buffer[] = []; + let batchItemSequence = 0; + let exportSequence = 0; + const channels = [{ id: 'channel-a', name: '通道A', status: 'active' }, { id: 'channel-b', name: '通道B', status: 'active' }]; + const prisma = { + reportMaterialBatch: { + create: jest.fn().mockResolvedValue({ id: 'batch-1', batchNo: 'RB001' }), + update: jest.fn().mockImplementation(({ data }: { data: Record }) => Promise.resolve({ id: 'batch-1', batchNo: 'RB001', ...data })), + }, + smsSignature: { + findUnique: jest.fn().mockResolvedValue({ id: 'signature-1', tenantId: 'tenant-1', applicationId: 'app-1', name: '测试签名', purpose: '验证码', auditStatus: 'approved', materialVersion: 3, drainageInfo: { signatureReportValues: { license: { fileObjectId: 'image-1', fileName: 'license.png', contentType: 'image/png' } } }, tenant: { name: '测试企业' }, application: { name: '测试应用' } }), + update: jest.fn().mockResolvedValue({}), + }, + smsDrainageInfo: { findUnique: jest.fn(), update: jest.fn() }, + channelRouteRule: { findMany: jest.fn().mockResolvedValue([{ group: { items: channels.map((channel, index) => ({ priority: index, channel })) } }]) }, + reportMaterialBatchItem: { create: jest.fn().mockImplementation(() => Promise.resolve({ id: `batch-item-${++batchItemSequence}` })) }, + smsChannel: { findUnique: jest.fn().mockImplementation(({ where }: { where: { id: string } }) => Promise.resolve(channels.find((channel) => channel.id === where.id))) }, + channelReportField: { findMany: jest.fn().mockResolvedValue([ + { code: 'sign', name: '短信签名', exportName: '通道签名', required: true, columnWidth: 18, imageWidth: 120, imageHeight: 80, transform: null, defaultValue: null }, + { code: 'license', name: '营业执照', exportName: '营业执照图片', required: true, columnWidth: 24, imageWidth: 120, imageHeight: 80, transform: null, defaultValue: null }, + ]) }, + channelSignatureReportTask: { findFirst: jest.fn().mockResolvedValue(null), create: jest.fn().mockImplementation(({ data }: { data: Record }) => Promise.resolve({ id: `task-${String(data.channelId)}`, ...data })), update: jest.fn() }, + channelSignatureReportRecord: { create: jest.fn().mockResolvedValue({}) }, + reportExportFile: { create: jest.fn().mockImplementation(({ data }: { data: Record }) => Promise.resolve({ id: `export-${++exportSequence}`, ...data })) }, + reportExportFileItem: { createMany: jest.fn().mockResolvedValue({ count: 1 }) }, + }; + const files = { + getDownload: jest.fn().mockResolvedValue({ fileObject: { fileName: 'license.png', contentType: 'image/png' }, content: Buffer.from('iVBORw0KGgoAAAANSUhEUgAAAAEAAAABCAQAAAC1HAwCAAAAC0lEQVR42mP8/x8AAusB9Y9ZsmAAAAAASUVORK5CYII=', 'base64') }), + upload: jest.fn().mockImplementation((_options: unknown, file: { originalname: string; mimetype: string; buffer: Buffer }) => { + uploadedWorkbooks.push(file.buffer); + return Promise.resolve({ id: `file-${uploadedWorkbooks.length}`, fileName: file.originalname, contentType: file.mimetype }); + }), + }; + const service = new ReportMaterialsService(prisma as never, files as never, {} as never); + + const result = await service.createBatch({ items: [{ reportType: 'signature', signatureId: 'signature-1' }] }); + + expect(result).toMatchObject({ status: 'completed', channelCount: 2, fileCount: 2 }); + expect(prisma.channelSignatureReportTask.create).toHaveBeenCalledTimes(2); + expect(prisma.smsSignature.update).toHaveBeenCalledWith({ where: { id: 'signature-1' }, data: { pendingReport: false } }); + expect(uploadedWorkbooks).toHaveLength(2); + for (const buffer of uploadedWorkbooks) { + const workbook = new ExcelJS.Workbook(); + await workbook.xlsx.load(buffer as never); + const sheet = workbook.getWorksheet('签名报备'); + expect(sheet?.getCell('A1').text).toBe('通道签名'); + expect(sheet?.getCell('A2').text).toBe('测试签名'); + expect(sheet?.getImages()).toHaveLength(1); + } + }); + + it('keeps material pending and marks the task waiting when the channel has no field configuration', async () => { + const prisma = { + reportMaterialBatch: { create: jest.fn().mockResolvedValue({ id: 'batch-2' }), update: jest.fn().mockImplementation(({ data }: { data: Record }) => Promise.resolve(data)) }, + smsSignature: { findUnique: jest.fn().mockResolvedValue({ id: 'signature-2', tenantId: 'tenant-1', applicationId: 'app-1', name: '测试签名', auditStatus: 'approved', materialVersion: 1, drainageInfo: {}, tenant: { name: '企业' }, application: { name: '应用' } }), update: jest.fn() }, + smsDrainageInfo: { findUnique: jest.fn(), update: jest.fn() }, + channelRouteRule: { findMany: jest.fn().mockResolvedValue([{ group: { items: [{ channel: { id: 'channel-a', name: '通道A', status: 'active' } }] } }]) }, + reportMaterialBatchItem: { create: jest.fn().mockResolvedValue({ id: 'batch-item-2' }) }, + smsChannel: { findUnique: jest.fn().mockResolvedValue({ id: 'channel-a', name: '通道A' }) }, + channelReportField: { findMany: jest.fn().mockResolvedValue([]) }, + channelSignatureReportTask: { findFirst: jest.fn().mockResolvedValue(null), create: jest.fn().mockImplementation(({ data }: { data: Record }) => Promise.resolve({ id: 'task-2', ...data })) }, + channelSignatureReportRecord: { create: jest.fn().mockResolvedValue({}) }, + reportExportFile: { create: jest.fn().mockResolvedValue({ id: 'export-2' }) }, + reportExportFileItem: { createMany: jest.fn() }, + }; + const files = { upload: jest.fn().mockResolvedValue({ id: 'file-2', fileName: 'empty.xlsx', contentType: 'application/vnd.openxmlformats-officedocument.spreadsheetml.sheet' }) }; + const service = new ReportMaterialsService(prisma as never, files as never, {} as never); + + const result = await service.createBatch({ items: [{ reportType: 'signature', signatureId: 'signature-2' }] }); + + expect(result).toMatchObject({ status: 'partial_failed' }); + expect(prisma.channelSignatureReportTask.create).toHaveBeenCalledWith({ data: expect.objectContaining({ status: 'waiting_material', reason: '通道未配置当前资料类型的报备字段' }) }); + expect(prisma.smsSignature.update).not.toHaveBeenCalled(); + }); +}); diff --git a/api/src/report-materials/report-materials.service.ts b/api/src/report-materials/report-materials.service.ts new file mode 100644 index 0000000..47d57b4 --- /dev/null +++ b/api/src/report-materials/report-materials.service.ts @@ -0,0 +1,529 @@ +import { BadRequestException, Injectable, NotFoundException } from '@nestjs/common'; +import { Prisma } from '@prisma/client'; +import ExcelJS from 'exceljs'; +import { randomUUID } from 'node:crypto'; +import { extname } from 'node:path'; +import { FilesService } from '../files/files.service'; +import { PrismaService } from '../prisma/prisma.service'; +import { SmsConfigService } from '../sms-config/sms-config.service'; + +export type ImportMapping = { + sourceHeader: string; + sourceHeaderPath?: string; + sourceColumnIndex: number; + targetFieldCode: string; + targetKind: 'signatureName' | 'purpose' | 'siteName' | 'url' | 'remark' | 'dynamic'; + fieldType: 'string' | 'image' | 'file'; + required?: boolean; + transform?: string; + sortOrder?: number; +}; + +export interface CreateImportProfileDto { + id?: string; + name: string; + reportType: 'signature' | 'drainage'; + tenantId?: string; + applicationId?: string; + sheetName?: string; + headerRowCount?: number; + dataStartRow?: number; + status?: string; + columns: ImportMapping[]; +} + +export interface ImportCommitDto { + mappings: ImportMapping[]; + profile?: CreateImportProfileDto; +} + +export interface CreateReportBatchDto { + createdById?: string; + items: Array<{ reportType: 'signature' | 'drainage'; signatureId: string; drainageItemId?: string }>; +} + +type AnalyzeImportOptions = { + tenantId: string; + applicationId?: string; + reportType: 'signature' | 'drainage'; + sheetName?: string; + headerRowCount: number; + dataStartRow: number; + profileId?: string; +}; + +type EmbeddedImage = { row: number; column: number; extension: string; buffer: Buffer }; + +@Injectable() +export class ReportMaterialsService { + constructor( + private readonly prisma: PrismaService, + private readonly files: FilesService, + private readonly smsConfig: SmsConfigService, + ) {} + + async listPending(query: { reportType?: 'signature' | 'drainage'; tenantId?: string; applicationId?: string }) { + const [signatures, drainageInfos] = await Promise.all([ + query.reportType === 'drainage' ? Promise.resolve([]) : this.prisma.smsSignature.findMany({ + where: { pendingReport: true, auditStatus: 'approved', tenantId: query.tenantId, applicationId: query.applicationId }, + include: { tenant: true, application: true }, + orderBy: { reportChangedAt: 'desc' }, + }), + query.reportType === 'signature' ? Promise.resolve([]) : this.prisma.smsDrainageInfo.findMany({ + where: { pendingReport: true, auditStatus: 'approved', tenantId: query.tenantId, applicationId: query.applicationId }, + include: { tenant: true, application: true, signature: true }, + orderBy: { reportChangedAt: 'desc' }, + }), + ]); + return [ + ...signatures.map((item) => ({ id: `signature:${item.id}`, reportType: 'signature', signatureId: item.id, drainageItemId: null, materialVersion: item.materialVersion, changedAt: item.reportChangedAt, name: item.name, detail: item.purpose, tenant: item.tenant, application: item.application })), + ...drainageInfos.map((item) => ({ id: `drainage:${item.id}`, reportType: 'drainage', signatureId: item.signatureId, drainageItemId: item.id, materialVersion: item.materialVersion, changedAt: item.reportChangedAt, name: item.siteName, detail: item.url, signatureName: item.signature.name, tenant: item.tenant, application: item.application })), + ].sort((left, right) => new Date(right.changedAt).getTime() - new Date(left.changedAt).getTime()); + } + + listImportProfiles(reportType?: 'signature' | 'drainage') { + return this.prisma.reportMaterialImportProfile.findMany({ + where: { reportType, status: 'active' }, + include: { columns: { orderBy: [{ sortOrder: 'asc' }, { sourceColumnIndex: 'asc' }] } }, + orderBy: { updatedAt: 'desc' }, + }); + } + + async saveImportProfile(data: CreateImportProfileDto) { + validateProfile(data); + return this.prisma.$transaction(async (tx) => { + const profile = data.id + ? await tx.reportMaterialImportProfile.update({ where: { id: data.id }, data: profileData(data) }) + : await tx.reportMaterialImportProfile.create({ data: profileData(data) }); + await tx.reportMaterialImportProfileColumn.deleteMany({ where: { profileId: profile.id } }); + await tx.reportMaterialImportProfileColumn.createMany({ + data: data.columns.map((column, index) => ({ + profileId: profile.id, + sourceHeader: column.sourceHeader, + sourceHeaderPath: column.sourceHeaderPath, + sourceColumnIndex: column.sourceColumnIndex, + targetFieldCode: column.targetFieldCode, + targetKind: column.targetKind, + fieldType: column.fieldType, + required: column.required ?? false, + transform: column.transform, + sortOrder: column.sortOrder ?? (index + 1) * 10, + })), + }); + return tx.reportMaterialImportProfile.findUnique({ where: { id: profile.id }, include: { columns: { orderBy: { sortOrder: 'asc' } } } }); + }); + } + + async analyzeImport(file: { originalname: string; mimetype: string; size: number; buffer: Buffer }, options: AnalyzeImportOptions) { + if (!options.tenantId) throw new BadRequestException('tenantId is required'); + if (!['signature', 'drainage'].includes(options.reportType)) throw new BadRequestException('reportType must be signature or drainage'); + if (extname(file.originalname).toLowerCase() !== '.xlsx' || file.buffer[0] !== 0x50 || file.buffer[1] !== 0x4b) throw new BadRequestException('仅支持有效的 XLSX 文件'); + const workbook = await loadWorkbook(file.buffer); + const profile = options.profileId ? await this.prisma.reportMaterialImportProfile.findUnique({ where: { id: options.profileId }, include: { columns: { orderBy: { sortOrder: 'asc' } } } }) : null; + const selectedSheetName = options.sheetName || profile?.sheetName || undefined; + const worksheet = selectedSheetName ? workbook.getWorksheet(selectedSheetName) : workbook.worksheets[0]; + if (!worksheet) throw new BadRequestException('工作簿没有可读取的工作表'); + const headerRowCount = clamp(options.headerRowCount, 1, 5); + const dataStartRow = Math.max(options.dataStartRow, headerRowCount + 1); + const images = readEmbeddedImages(workbook, worksheet); + const columnCount = Math.min(worksheet.columnCount, 200); + const columns = Array.from({ length: columnCount }, (_, offset) => { + const sourceColumnIndex = offset + 1; + const parts = Array.from({ length: headerRowCount }, (_, headerOffset) => cellText(worksheet.getCell(headerOffset + 1, sourceColumnIndex))).filter(Boolean); + const sourceHeaderPath = [...new Set(parts)].join('/'); + return { + sourceColumnIndex, + columnLetter: worksheet.getColumn(sourceColumnIndex).letter, + sourceHeader: parts.at(-1) || `第${sourceColumnIndex}列`, + sourceHeaderPath, + imageCount: images.filter((image) => image.column === sourceColumnIndex).length, + }; + }).filter((column) => column.sourceHeaderPath || column.imageCount > 0); + const previewRows = []; + for (let rowNumber = dataStartRow; rowNumber <= Math.min(worksheet.rowCount, dataStartRow + 9); rowNumber += 1) { + const values = Object.fromEntries(columns.map((column) => [String(column.sourceColumnIndex), cellText(worksheet.getCell(rowNumber, column.sourceColumnIndex))])); + const imageColumns = images.filter((image) => image.row === rowNumber).map((image) => image.column); + if (Object.values(values).some(Boolean) || imageColumns.length) previewRows.push({ rowNumber, values, imageColumns }); + } + const sourceFile = await this.files.upload({ tenantId: options.tenantId, purpose: 'report_material_import', prefix: 'report-material-imports' }, file); + const profileMappings = profile?.columns.map((column) => ({ + sourceHeader: column.sourceHeader, + sourceHeaderPath: column.sourceHeaderPath ?? undefined, + sourceColumnIndex: column.sourceColumnIndex, + targetFieldCode: column.targetFieldCode, + targetKind: column.targetKind as ImportMapping['targetKind'], + fieldType: column.fieldType as ImportMapping['fieldType'], + required: column.required, + transform: column.transform ?? undefined, + sortOrder: column.sortOrder, + })); + const suggestedMappings = profileMappings?.length ? remapProfileColumns(profileMappings, columns) : suggestMappings(columns, options.reportType); + const batch = await this.prisma.reportMaterialImportBatch.create({ + data: { + tenantId: options.tenantId, + applicationId: options.applicationId, + profileId: options.profileId, + fileObjectId: sourceFile.id, + fileName: sourceFile.fileName, + reportType: options.reportType, + sheetName: worksheet.name, + headerRowCount, + dataStartRow, + mapping: suggestedMappings as Prisma.InputJsonValue, + preview: { sheets: workbook.worksheets.map((sheet) => sheet.name), columns, rows: previewRows, imageCount: images.length } as Prisma.InputJsonValue, + rowCount: Math.max(0, worksheet.rowCount - dataStartRow + 1), + }, + }); + return { ...batch, sourceFile, sheets: workbook.worksheets.map((sheet) => sheet.name), columns, rows: previewRows, imageCount: images.length, suggestedMappings }; + } + + async commitImport(batchId: string, data: ImportCommitDto) { + const batch = await this.prisma.reportMaterialImportBatch.findUnique({ where: { id: batchId } }); + if (!batch) throw new NotFoundException('导入批次不存在'); + if (!data.mappings?.length) throw new BadRequestException('请至少配置一个导入字段映射'); + if (data.profile) await this.saveImportProfile({ ...data.profile, reportType: batch.reportType as 'signature' | 'drainage', columns: data.mappings }); + const { content } = await this.files.getDownload(batch.fileObjectId); + const workbook = await loadWorkbook(content); + const worksheet = workbook.getWorksheet(batch.sheetName); + if (!worksheet) throw new BadRequestException('导入工作表不存在'); + const images = readEmbeddedImages(workbook, worksheet); + const imageByCell = new Map(images.map((image) => [`${image.row}:${image.column}`, image])); + let successCount = 0; + const failures: Array<{ rowNumber: number; reason: string }> = []; + for (let rowNumber = batch.dataStartRow; rowNumber <= worksheet.rowCount; rowNumber += 1) { + try { + const values: Record = {}; + for (const mapping of data.mappings) { + const image = imageByCell.get(`${rowNumber}:${mapping.sourceColumnIndex}`); + if (image && mapping.fieldType !== 'string') { + const uploaded = await this.files.upload({ tenantId: batch.tenantId, purpose: 'report_material', prefix: `report-materials/import-${batch.id}` }, { + originalname: `${mapping.targetFieldCode}-row-${rowNumber}.${normalizeImageExtension(image.extension)}`, + mimetype: imageContentType(image.extension), + size: image.buffer.length, + buffer: image.buffer, + }); + values[mapping.targetFieldCode] = { fileObjectId: uploaded.id, fileName: uploaded.fileName, contentType: uploaded.contentType }; + } else { + values[mapping.targetFieldCode] = transformValue(cellText(worksheet.getCell(rowNumber, mapping.sourceColumnIndex)), mapping.transform); + } + } + if (!Object.values(values).some(hasValue)) continue; + for (const mapping of data.mappings.filter((item) => item.required)) { + if (!hasValue(values[mapping.targetFieldCode])) throw new Error(`缺少必填字段:${mapping.sourceHeader}`); + } + if (batch.reportType === 'signature') await this.importSignatureRow(batch.tenantId, batch.applicationId ?? undefined, data.mappings, values); + else await this.importDrainageRow(batch.tenantId, batch.applicationId ?? undefined, data.mappings, values); + successCount += 1; + } catch (error) { + failures.push({ rowNumber, reason: error instanceof Error ? error.message : '导入失败' }); + } + } + return this.prisma.reportMaterialImportBatch.update({ + where: { id: batchId }, + data: { + status: failures.length ? (successCount ? 'partial_failed' : 'failed') : 'completed', + mapping: data.mappings as Prisma.InputJsonValue, + result: { failures } as Prisma.InputJsonValue, + successCount, + failedCount: failures.length, + completedAt: new Date(), + }, + }); + } + + listBatches() { + return this.prisma.reportMaterialBatch.findMany({ + include: { exportFiles: true, items: true }, + orderBy: { createdAt: 'desc' }, + take: 100, + }); + } + + async createBatch(data: CreateReportBatchDto) { + if (!data.items?.length) throw new BadRequestException('请选择需要报备的签名或引流信息'); + const uniqueItems = [...new Map(data.items.map((item) => [`${item.reportType}:${item.drainageItemId ?? item.signatureId}`, item])).values()]; + const batch = await this.prisma.reportMaterialBatch.create({ + data: { batchNo: `RB${new Date().toISOString().replace(/\D/g, '').slice(0, 14)}${randomUUID().slice(0, 4).toUpperCase()}`, createdById: data.createdById, selectedCount: uniqueItems.length }, + }); + try { + const prepared = []; + for (const selected of uniqueItems) prepared.push(await this.prepareBatchItem(batch.id, selected)); + const channelMap = new Map>(); + for (const item of prepared) { + for (const channel of item.channels) { + const current = channelMap.get(channel.id) ?? []; + current.push({ ...item, channels: [channel] }); + channelMap.set(channel.id, current); + } + } + const exportedFiles = []; + const incomplete = new Set(prepared.filter((item) => item.channels.length === 0).map((item) => item.batchItem.id)); + for (const [channelId, items] of channelMap) { + const result = await this.exportChannelBatch(batch.id, channelId, items); + exportedFiles.push(result.file); + for (const itemId of result.incompleteBatchItemIds) incomplete.add(itemId); + } + for (const item of prepared) { + if (incomplete.has(item.batchItem.id) || item.channels.length === 0) continue; + if (item.reportType === 'signature') await this.prisma.smsSignature.update({ where: { id: item.signature.id }, data: { pendingReport: false } }); + else await this.prisma.smsDrainageInfo.update({ where: { id: item.drainageInfo!.id }, data: { pendingReport: false } }); + } + return this.prisma.reportMaterialBatch.update({ where: { id: batch.id }, data: { status: incomplete.size ? 'partial_failed' : 'completed', channelCount: channelMap.size, fileCount: exportedFiles.length, completedAt: new Date() }, include: { exportFiles: true, items: true } }); + } catch (error) { + await this.prisma.reportMaterialBatch.update({ where: { id: batch.id }, data: { status: 'failed', errorMessage: error instanceof Error ? error.message : '生成报备批次失败', completedAt: new Date() } }); + throw error; + } + } + + private async importSignatureRow(tenantId: string, applicationId: string | undefined, mappings: ImportMapping[], values: Record) { + const name = mappedCoreValue(mappings, values, 'signatureName'); + if (!name) throw new Error('缺少短信签名'); + const purpose = mappedCoreValue(mappings, values, 'purpose'); + const signatureReportValues = dynamicValues(mappings, values); + const existing = await this.prisma.smsSignature.findFirst({ where: { tenantId, applicationId: applicationId ?? null, name, auditStatus: { not: 'deleted' } } }); + if (existing) return this.smsConfig.updateSignature(existing.id, { applicationId, name, purpose, drainageInfo: { ...jsonRecord(existing.drainageInfo), signatureReportValues } }); + return this.smsConfig.createSignature({ tenantId, applicationId, name, purpose, drainageInfo: { signatureReportValues } }, { initialAuditStatus: 'approved' }); + } + + private async importDrainageRow(tenantId: string, applicationId: string | undefined, mappings: ImportMapping[], values: Record) { + const signatureName = mappedCoreValue(mappings, values, 'signatureName'); + const siteName = mappedCoreValue(mappings, values, 'siteName'); + const url = mappedCoreValue(mappings, values, 'url'); + if (!signatureName || !siteName || !url) throw new Error('引流信息必须包含短信签名、站点名称和URL'); + const signature = await this.prisma.smsSignature.findFirst({ where: { tenantId, applicationId: applicationId ?? null, name: signatureName, auditStatus: 'approved' } }); + if (!signature) throw new Error(`未找到已审核签名:${signatureName}`); + const remark = mappedCoreValue(mappings, values, 'remark'); + const reportValues = dynamicValues(mappings, values); + const existing = await this.prisma.smsDrainageInfo.findFirst({ where: { signatureId: signature.id, url, auditStatus: { not: 'deleted' } } }); + if (existing) return this.smsConfig.updateDrainageInfo(existing.id, { siteName, url, remark, reportValues }, { initialAuditStatus: 'approved' }); + return this.smsConfig.createDrainageInfo(signature.id, { siteName, url, remark, reportValues }, { initialAuditStatus: 'approved' }); + } + + private async prepareBatchItem(batchId: string, selected: CreateReportBatchDto['items'][number]) { + const signature = await this.prisma.smsSignature.findUnique({ where: { id: selected.signatureId }, include: { tenant: true, application: true } }); + if (!signature || signature.auditStatus !== 'approved') throw new BadRequestException('签名不存在或未审核通过'); + const drainageInfo = selected.reportType === 'drainage' && selected.drainageItemId + ? await this.prisma.smsDrainageInfo.findUnique({ where: { id: selected.drainageItemId } }) : null; + if (selected.reportType === 'drainage' && (!drainageInfo || drainageInfo.signatureId !== signature.id || drainageInfo.auditStatus !== 'approved')) throw new BadRequestException('引流信息不存在或未审核通过'); + const routes = signature.applicationId ? await this.prisma.channelRouteRule.findMany({ + where: { applicationId: signature.applicationId, status: 'active' }, + include: { group: { include: { items: { include: { channel: true }, orderBy: { priority: 'asc' } } } } }, + orderBy: { priority: 'asc' }, + }) : []; + const channels = [...new Map(routes.flatMap((route) => route.group.items.map((entry) => entry.channel)).filter((channel) => channel.status === 'active').map((channel) => [channel.id, channel])).values()]; + const snapshot = selected.reportType === 'signature' + ? { reportType: 'signature', signature: { id: signature.id, name: signature.name, purpose: signature.purpose, tenantName: signature.tenant.name, applicationName: signature.application?.name }, values: jsonRecord(jsonRecord(signature.drainageInfo).signatureReportValues) } + : { reportType: 'drainage', signature: { id: signature.id, name: signature.name, tenantName: signature.tenant.name, applicationName: signature.application?.name }, drainage: { id: drainageInfo!.id, siteName: drainageInfo!.siteName, url: drainageInfo!.url, remark: drainageInfo!.remark }, values: jsonRecord(drainageInfo!.reportValues) }; + const materialVersion = selected.reportType === 'signature' ? signature.materialVersion : drainageInfo!.materialVersion; + const batchItem = await this.prisma.reportMaterialBatchItem.create({ data: { batchId, signatureId: signature.id, drainageItemId: drainageInfo?.id, reportType: selected.reportType, materialVersion, snapshot: snapshot as Prisma.InputJsonValue } }); + return { batchItem, signature, drainageInfo, reportType: selected.reportType, snapshot, channels }; + } + + private async exportChannelBatch(batchId: string, channelId: string, items: Array>>) { + const channel = await this.prisma.smsChannel.findUnique({ where: { id: channelId } }); + if (!channel) throw new NotFoundException('通道不存在'); + const reportTypes = [...new Set(items.map((item) => item.reportType))]; + const workbook = new ExcelJS.Workbook(); + const fileRows: Array<{ item: (typeof items)[number]; taskId: string; rowNumber: number }> = []; + const incompleteBatchItemIds: string[] = []; + let totalRows = 0; + for (const reportType of reportTypes) { + const fields = await this.prisma.channelReportField.findMany({ where: { channelId, status: 'active', reportType: { in: [reportType, 'both'] } }, orderBy: [{ sortOrder: 'asc' }, { createdAt: 'asc' }] }); + const sheet = workbook.addWorksheet(reportType === 'signature' ? '签名报备' : '引流信息报备', { views: [{ state: 'frozen', ySplit: 1 }] }); + sheet.properties.defaultRowHeight = 22; + sheet.columns = fields.map((field) => ({ header: field.exportName || field.name, key: field.code, width: field.columnWidth })); + styleHeader(sheet.getRow(1)); + for (const item of items.filter((current) => current.reportType === reportType)) { + const values = fields.map((field) => resolveExportValue(item.snapshot, field.code, field.name) ?? field.defaultValue ?? ''); + const missing = fields.filter((field, index) => field.required && !hasValue(values[index])); + const missingReason = fields.length === 0 ? '通道未配置当前资料类型的报备字段' : missing.length ? `缺少字段:${missing.map((field) => field.exportName || field.name).join('、')}` : null; + const existingTask = await this.prisma.channelSignatureReportTask.findFirst({ where: { signatureId: item.signature.id, channelId, reportType, drainageItemId: reportType === 'drainage' ? item.drainageInfo!.id : null } }); + const task = existingTask + ? await this.prisma.channelSignatureReportTask.update({ where: { id: existingTask.id }, data: { status: missingReason ? 'waiting_material' : 'exporting', reason: missingReason } }) + : await this.prisma.channelSignatureReportTask.create({ data: { tenantId: item.signature.tenantId, signatureId: item.signature.id, channelId, reportType, drainageItemId: item.drainageInfo?.id, status: missingReason ? 'waiting_material' : 'exporting', reason: missingReason } }); + if (missingReason) { + incompleteBatchItemIds.push(item.batchItem.id); + await this.recordTask(task.id, channelId, existingTask?.status, 'waiting_material', task.reason ?? undefined); + continue; + } + const row = sheet.addRow(values.map((value, index) => isFileRef(value) ? value.fileName : applyExportTransform(value, fields[index]?.transform))); + totalRows += 1; + let targetHeight = 22; + for (const [index, value] of values.entries()) { + if (!isFileRef(value)) continue; + const downloaded = await this.files.getDownload(value.fileObjectId); + if (!downloaded.fileObject.contentType.startsWith('image/')) continue; + const extension = normalizeImageExtension(extname(downloaded.fileObject.fileName).slice(1) || downloaded.fileObject.contentType.split('/')[1]); + if (!['png', 'jpeg', 'gif'].includes(extension)) continue; + const imageId = workbook.addImage({ base64: `data:${downloaded.fileObject.contentType};base64,${downloaded.content.toString('base64')}`, extension: extension as 'png' | 'jpeg' | 'gif' }); + const widthCells = Math.max(0.8, fields[index].imageWidth / Math.max(60, fields[index].columnWidth * 7)); + const heightRows = Math.max(0.8, fields[index].imageHeight / 20); + sheet.addImage(imageId, { tl: { col: index + 0.08, row: row.number - 1 + 0.08 }, br: { col: index + Math.min(0.95, widthCells), row: row.number - 1 + Math.min(0.95, heightRows) }, editAs: 'oneCell' } as never); + targetHeight = Math.max(targetHeight, fields[index].imageHeight * 0.75 + 8); + } + row.height = targetHeight; + fileRows.push({ item, taskId: task.id, rowNumber: row.number }); + await this.recordTask(task.id, channelId, existingTask?.status, 'exporting'); + } + } + if (workbook.worksheets.every((sheet) => sheet.rowCount <= 1)) { + for (const sheet of [...workbook.worksheets]) workbook.removeWorksheet(sheet.id); + const empty = workbook.addWorksheet('无可导出数据'); + empty.getCell('A1').value = '所选资料缺少当前通道必填字段,请补充后重新生成。'; + empty.getColumn(1).width = 64; + } + const buffer = Buffer.from(await workbook.xlsx.writeBuffer()); + const fileName = `${safeFileName(channel.name)}-${batchId.slice(-8)}.xlsx`; + const uploaded = await this.files.upload({ purpose: 'report_export', prefix: `report-exports/${batchId}` }, { originalname: fileName, mimetype: 'application/vnd.openxmlformats-officedocument.spreadsheetml.sheet', size: buffer.length, buffer }); + const file = await this.prisma.reportExportFile.create({ data: { batchId, channelId, fileObjectId: uploaded.id, fileName, rowCount: totalRows } }); + if (fileRows.length) await this.prisma.reportExportFileItem.createMany({ data: fileRows.map((entry) => ({ exportFileId: file.id, batchItemId: entry.item.batchItem.id, taskId: entry.taskId, rowNumber: entry.rowNumber })) }); + return { file: { ...file, fileObject: uploaded }, incompleteBatchItemIds }; + } + + private recordTask(taskId: string, channelId: string, statusBefore: string | undefined, statusAfter: string, reason?: string) { + return this.prisma.channelSignatureReportRecord.create({ data: { taskId, channelId, action: 'batch_export', statusBefore, statusAfter, reason, sourceEntry: 'report_task' } }); + } +} + +function profileData(data: CreateImportProfileDto) { + return { name: data.name.trim(), reportType: data.reportType, tenantId: data.tenantId, applicationId: data.applicationId, sheetName: data.sheetName, headerRowCount: clamp(data.headerRowCount ?? 1, 1, 5), dataStartRow: Math.max(data.dataStartRow ?? 2, 2), status: data.status ?? 'active' }; +} + +function validateProfile(data: CreateImportProfileDto) { + if (!data.name?.trim()) throw new BadRequestException('映射模板名称不能为空'); + if (!data.columns?.length) throw new BadRequestException('映射模板至少包含一个字段'); + const indexes = data.columns.map((column) => column.sourceColumnIndex); + if (new Set(indexes).size !== indexes.length) throw new BadRequestException('同一源列不能重复映射'); +} + +async function loadWorkbook(buffer: Buffer) { + const workbook = new ExcelJS.Workbook(); + await workbook.xlsx.load(buffer as never); + return workbook; +} + +function readEmbeddedImages(workbook: ExcelJS.Workbook, worksheet: ExcelJS.Worksheet): EmbeddedImage[] { + const getImages = (worksheet as unknown as { getImages?: () => Array<{ imageId: number; range: { tl: { nativeRow?: number; nativeCol?: number; row?: number; col?: number } } }> }).getImages; + if (!getImages) return []; + return getImages.call(worksheet).flatMap((drawing) => { + const image = (workbook as unknown as { getImage?: (id: number) => { buffer?: Buffer; base64?: string; extension?: string } }).getImage?.(drawing.imageId); + if (!image) return []; + const row = (drawing.range.tl.nativeRow ?? drawing.range.tl.row ?? 0) + 1; + const column = (drawing.range.tl.nativeCol ?? drawing.range.tl.col ?? 0) + 1; + const buffer = image.buffer ?? (image.base64 ? Buffer.from(image.base64.replace(/^data:[^;]+;base64,/, ''), 'base64') : undefined); + return buffer ? [{ row, column, extension: image.extension ?? 'png', buffer }] : []; + }); +} + +function suggestMappings(columns: Array<{ sourceColumnIndex: number; sourceHeader: string; sourceHeaderPath: string; imageCount: number }>, reportType: 'signature' | 'drainage'): ImportMapping[] { + return columns.flatMap((column, index) => { + const normalized = normalizeHeader(`${column.sourceHeaderPath}/${column.sourceHeader}`); + const core = reportType === 'signature' ? signatureCoreMapping(normalized) : drainageCoreMapping(normalized); + if (!core && !column.imageCount) return []; + return [{ sourceHeader: column.sourceHeader, sourceHeaderPath: column.sourceHeaderPath, sourceColumnIndex: column.sourceColumnIndex, targetFieldCode: core?.code ?? normalizeFieldCode(column.sourceHeader), targetKind: core?.kind ?? 'dynamic', fieldType: column.imageCount ? 'image' : 'string', required: Boolean(core?.required), sortOrder: (index + 1) * 10 }]; + }); +} + +function remapProfileColumns(profileColumns: ImportMapping[], sourceColumns: Array<{ sourceColumnIndex: number; sourceHeader: string; sourceHeaderPath: string; imageCount: number }>): ImportMapping[] { + const used = new Set(); + return profileColumns.flatMap((profileColumn) => { + const headerPath = normalizeHeader(profileColumn.sourceHeaderPath || profileColumn.sourceHeader); + const header = normalizeHeader(profileColumn.sourceHeader); + const source = sourceColumns.find((column) => !used.has(column.sourceColumnIndex) && normalizeHeader(column.sourceHeaderPath) === headerPath) + ?? sourceColumns.find((column) => !used.has(column.sourceColumnIndex) && normalizeHeader(column.sourceHeader) === header); + if (!source) return []; + used.add(source.sourceColumnIndex); + return [{ ...profileColumn, sourceColumnIndex: source.sourceColumnIndex, sourceHeader: source.sourceHeader, sourceHeaderPath: source.sourceHeaderPath, fieldType: source.imageCount > 0 && profileColumn.fieldType === 'string' ? 'image' : profileColumn.fieldType }]; + }); +} + +function signatureCoreMapping(header: string): { code: string; kind: ImportMapping['targetKind']; required?: boolean } | undefined { + if (/短信签名|签名名称|签名/.test(header)) return { code: 'signature_name', kind: 'signatureName', required: true }; + if (/用途|签名依据/.test(header)) return { code: 'purpose', kind: 'purpose' }; + return undefined; +} + +function drainageCoreMapping(header: string): { code: string; kind: ImportMapping['targetKind']; required?: boolean } | undefined { + if (/短信签名|签名名称/.test(header)) return { code: 'signature_name', kind: 'signatureName', required: true }; + if (/站点|网站名称/.test(header)) return { code: 'site_name', kind: 'siteName', required: true }; + if (/引流地址|网址|url|链接/.test(header)) return { code: 'url', kind: 'url', required: true }; + if (/备注|说明/.test(header)) return { code: 'remark', kind: 'remark' }; + return undefined; +} + +function normalizeHeader(value: string) { return value.toLowerCase().replace(/[\s**::()()_-]/g, ''); } +function normalizeFieldCode(value: string) { return `import_${value.trim().toLowerCase().replace(/[^a-z0-9\u4e00-\u9fa5]+/g, '_').slice(0, 40) || randomUUID().slice(0, 8)}`; } +function clamp(value: number, minimum: number, maximum: number) { return Math.min(maximum, Math.max(minimum, Number.isFinite(value) ? Math.round(value) : minimum)); } + +function cellText(cell: ExcelJS.Cell) { + const value = cell.value; + if (value === null || value === undefined) return ''; + if (typeof value === 'number') return Number.isInteger(value) ? String(value) : String(value); + if (typeof value === 'string' || typeof value === 'boolean') return String(value).trim(); + if ('result' in value && value.result !== undefined) return String(value.result ?? '').trim(); + if ('richText' in value) return value.richText.map((item) => item.text).join('').trim(); + if ('text' in value) return String(value.text).trim(); + return cell.text.trim(); +} + +function transformValue(value: string, transform?: string) { + if (!transform || transform === 'trim') return value.trim(); + if (transform === 'digits') return value.replace(/\D/g, ''); + if (transform === 'uppercase') return value.trim().toUpperCase(); + if (transform === 'lowercase') return value.trim().toLowerCase(); + return value.trim(); +} + +function mappedCoreValue(mappings: ImportMapping[], values: Record, kind: ImportMapping['targetKind']) { + const mapping = mappings.find((item) => item.targetKind === kind); + return mapping ? String(values[mapping.targetFieldCode] ?? '').trim() : ''; +} + +function dynamicValues(mappings: ImportMapping[], values: Record) { + return Object.fromEntries(mappings.filter((item) => item.targetKind === 'dynamic').map((item) => [item.targetFieldCode, values[item.targetFieldCode]])); +} + +function jsonRecord(value: unknown): Record { return value && typeof value === 'object' && !Array.isArray(value) ? value as Record : {}; } +function hasValue(value: unknown) { return isFileRef(value) ? Boolean(value.fileObjectId) : value !== null && value !== undefined && String(value).trim().length > 0; } +function isFileRef(value: unknown): value is { fileObjectId: string; fileName: string; contentType?: string } { return Boolean(value) && typeof value === 'object' && !Array.isArray(value) && typeof (value as Record).fileObjectId === 'string'; } + +function resolveExportValue(snapshot: Record, code: string, name?: string) { + const values = jsonRecord(snapshot.values); + if (hasValue(values[code])) return values[code]; + const signature = jsonRecord(snapshot.signature); + const drainage = jsonRecord(snapshot.drainage); + const aliases: Record = { + signature_name: signature.name, sign_name: signature.name, signatureName: signature.name, + purpose: signature.purpose, enterprise_name: signature.tenantName, company_name: signature.tenantName, + application_name: signature.applicationName, site_name: drainage.siteName, url: drainage.url, remark: drainage.remark, + }; + if (hasValue(aliases[code])) return aliases[code]; + const semantic = normalizeHeader(`${code}/${name ?? ''}`); + if (/短信签名|签名名称|signaturename|sms(?:signature|sign)|^sign$/.test(semantic)) return signature.name; + if (/签名用途|签名依据|purpose/.test(semantic)) return signature.purpose; + if (/企业名称|公司名称|enterprisename|companyname/.test(semantic)) return signature.tenantName; + if (/应用名称|applicationname|appname/.test(semantic)) return signature.applicationName; + if (/站点名称|网站名称|sitename/.test(semantic)) return drainage.siteName; + if (/引流地址|网址|链接|url/.test(semantic)) return drainage.url; + if (/备注|说明|remark/.test(semantic)) return drainage.remark; + return undefined; +} + +function applyExportTransform(value: unknown, transform?: string | null) { + const text = value === null || value === undefined ? '' : String(value); + return transformValue(text, transform ?? undefined); +} + +function styleHeader(row: ExcelJS.Row) { + row.height = 28; + row.eachCell((cell) => { + cell.font = { bold: true, color: { argb: 'FFFFFFFF' } }; + cell.fill = { type: 'pattern', pattern: 'solid', fgColor: { argb: 'FF2563EB' } }; + cell.alignment = { vertical: 'middle', horizontal: 'center', wrapText: true }; + cell.border = { bottom: { style: 'thin', color: { argb: 'FFD1D5DB' } } }; + }); +} + +function normalizeImageExtension(value: string) { const normalized = value.toLowerCase().replace(/^\./, ''); return normalized === 'jpg' ? 'jpeg' : normalized; } +function imageContentType(extension: string) { const normalized = normalizeImageExtension(extension); return normalized === 'jpeg' ? 'image/jpeg' : normalized === 'gif' ? 'image/gif' : 'image/png'; } +function safeFileName(value: string) { return value.replace(/[\\/:*?"<>|]/g, '_').slice(0, 80) || '通道报备'; } diff --git a/api/src/send-chain/send-chain.service.spec.ts b/api/src/send-chain/send-chain.service.spec.ts index 994aca1..4863329 100644 --- a/api/src/send-chain/send-chain.service.spec.ts +++ b/api/src/send-chain/send-chain.service.spec.ts @@ -224,9 +224,12 @@ function createPrismaMock() { findUnique: jest.fn().mockResolvedValue({ id: 'dead-1', tenantId: 'tenant-1', + channelId: 'channel-1', streamMessageId: '1710000000000-0', submitId: 'SUB-1', messageId: 'MSG-1', + status: 'pending', + manualRetryCount: 0, commandPayload: { schemaVersion: 'v1', messageType: 'SubmitCommand', @@ -1517,7 +1520,7 @@ describe('SendChainService', () => { }); }); - it('records gateway submit dead letters and allows manual requeue', async () => { + it('records gateway submit exceptions and safely allows manual requeue', async () => { const { service, prisma } = createService(); service['publishGatewaySubmitCommand'] = jest.fn().mockResolvedValue('1710000001000-0'); @@ -1551,9 +1554,17 @@ describe('SendChainService', () => { }), }); - await service.requeueGatewaySubmitDeadLetter('dead-1'); + await service.requeueGatewaySubmitDeadLetter('dead-1', { + confirmedNotSubmitted: true, + reason: '确认通道连接失败且运营商未收到该短信', + operatorId: 'user-1', + }); expect(service['publishGatewaySubmitCommand']).toHaveBeenCalledWith(expect.objectContaining({ submitId: 'SUB-1' })); + expect(prisma.gatewaySubmitDeadLetter.updateMany).toHaveBeenCalledWith({ + where: { id: 'dead-1', status: 'pending' }, + data: { status: 'requeueing' }, + }); expect(prisma.gatewaySubmitDeadLetter.update).toHaveBeenCalledWith({ where: { id: 'dead-1' }, data: expect.objectContaining({ @@ -1567,10 +1578,32 @@ describe('SendChainService', () => { action: 'gateway.submit_dead_letter_requeue', resource: 'gateway_submit_dead_letter', resourceId: 'dead-1', + userId: 'user-1', + detail: expect.objectContaining({ + reason: '确认通道连接失败且运营商未收到该短信', + confirmedNotSubmitted: true, + }), }), }); }); + it('blocks submit exception requeue when the upstream result may already be accepted', async () => { + const { service, prisma } = createService(); + prisma.smsMessageRecord.findUnique.mockResolvedValueOnce({ + id: 'record-1', + messageId: 'MSG-1', + status: 'submitted', + submitStatus: 'accepted', + receiptStatus: null, + }); + + await expect(service.requeueGatewaySubmitDeadLetter('dead-1', { + confirmedNotSubmitted: true, + reason: '尝试重新发送这条短信', + })).rejects.toThrow('为避免重复发送,禁止重新入队'); + expect(service['publishGatewaySubmitCommand']).not.toHaveBeenCalled(); + }); + it('records gateway downstream recovery statuses', async () => { const { service, prisma } = createService(); diff --git a/api/src/send-chain/send-chain.service.ts b/api/src/send-chain/send-chain.service.ts index 2ac7486..2b86f89 100644 --- a/api/src/send-chain/send-chain.service.ts +++ b/api/src/send-chain/send-chain.service.ts @@ -158,6 +158,12 @@ export interface GatewaySubmitDeadLetterDto { deadLetteredAt?: string; } +export interface RequeueGatewaySubmitExceptionDto { + confirmedNotSubmitted?: boolean; + reason?: string; + operatorId?: string; +} + export interface GatewayDownstreamRecoveryStatusDto { account: string; gatewayInstanceId?: string; @@ -1244,15 +1250,69 @@ export class SendChainService implements OnModuleInit, OnModuleDestroy { return updated; } - async requeueGatewaySubmitDeadLetter(id: string) { + async requeueGatewaySubmitDeadLetter(id: string, data: RequeueGatewaySubmitExceptionDto = {}) { const deadLetter = await this.prisma.gatewaySubmitDeadLetter.findUnique({ where: { id } }); if (!deadLetter) { - throw new NotFoundException('Gateway submit dead letter not found'); + throw new NotFoundException('Gateway提交异常记录不存在'); } - if (!deadLetter.commandPayload || typeof deadLetter.commandPayload !== 'object') { - throw new BadRequestException('该死信缺少可重放的 SubmitCommand'); + if (deadLetter.status !== 'pending') { + throw new BadRequestException('该提交异常当前状态不允许重新入队'); + } + if (!data.confirmedNotSubmitted) { + throw new BadRequestException('请确认运营商未接收该短信后再重新入队'); + } + const reason = String(data.reason ?? '').trim(); + if (reason.length < 5 || reason.length > 500) { + throw new BadRequestException('请填写5至500字的重新入队原因'); + } + if (!deadLetter.commandPayload || !isObjectRecord(deadLetter.commandPayload)) { + throw new BadRequestException('该提交异常缺少可重新入队的SubmitCommand'); + } + if (deadLetter.manualRetryCount >= 3) { + throw new BadRequestException('该提交异常已达到人工重新入队次数上限'); + } + const message = deadLetter.messageId + ? await this.prisma.smsMessageRecord.findUnique({ where: { messageId: deadLetter.messageId } }) + : null; + if (message && ( + message.submitStatus === 'accepted' + || ['submitted', 'delivered', 'unknown'].includes(message.status) + || ['delivered', 'unknown'].includes(message.receiptStatus ?? '') + )) { + throw new BadRequestException('该短信已有成功或不确定的上游结果,为避免重复发送,禁止重新入队'); + } + const commandChannelId = String(deadLetter.commandPayload.channelId ?? deadLetter.channelId ?? '').trim(); + if (!commandChannelId) { + throw new BadRequestException('该提交异常缺少通道信息'); + } + const channel = await this.prisma.smsChannel.findUnique({ + where: { id: commandChannelId }, + include: { connectionStates: true }, + }); + if (!channel || channel.status !== 'active') { + throw new BadRequestException('原通道不存在或已停用,不能重新入队'); + } + if (!channel.connectionStates.some((state) => state.status === 'connected' && state.currentConnections > 0)) { + throw new BadRequestException('原通道当前没有可用CMPP连接,请先恢复通道'); + } + const claimed = await this.prisma.gatewaySubmitDeadLetter.updateMany({ + where: { id, status: 'pending' }, + data: { status: 'requeueing' }, + }); + if (claimed.count !== 1) { + throw new BadRequestException('该提交异常已被其他操作处理,请刷新后重试'); + } + let retryStreamMessageId: string; + try { + const publishedStreamMessageId = await this.publishGatewaySubmitCommand(deadLetter.commandPayload); + if (!publishedStreamMessageId) { + throw new Error('Gateway提交异常重新入队未返回Stream消息编号'); + } + retryStreamMessageId = publishedStreamMessageId; + } catch (error) { + await this.prisma.gatewaySubmitDeadLetter.update({ where: { id }, data: { status: 'pending' } }); + throw error; } - const retryStreamMessageId = await this.publishGatewaySubmitCommand(deadLetter.commandPayload); const updated = await this.prisma.gatewaySubmitDeadLetter.update({ where: { id }, data: { @@ -1265,6 +1325,7 @@ export class SendChainService implements OnModuleInit, OnModuleDestroy { await this.prisma.operationLog.create({ data: { tenantId: updated.tenantId ?? undefined, + userId: data.operatorId, action: 'gateway.submit_dead_letter_requeue', resource: 'gateway_submit_dead_letter', resourceId: updated.id, @@ -1273,6 +1334,8 @@ export class SendChainService implements OnModuleInit, OnModuleDestroy { retryStreamMessageId, submitId: updated.submitId, messageId: updated.messageId, + reason, + confirmedNotSubmitted: true, }, }, }); diff --git a/api/src/sms-config/sms-config.service.ts b/api/src/sms-config/sms-config.service.ts index 9ef5b85..14b0be6 100644 --- a/api/src/sms-config/sms-config.service.ts +++ b/api/src/sms-config/sms-config.service.ts @@ -853,6 +853,9 @@ export class SmsConfigService { purpose: data.purpose, auditStatus: data.auditStatus, drainageInfo: drainageInfo as Prisma.InputJsonValue | undefined, + materialVersion: { increment: 1 }, + pendingReport: true, + reportChangedAt: new Date(), }, include: { materials: true, tenant: true, application: true }, }); @@ -935,6 +938,9 @@ export class SmsConfigService { rejectReason: null, submittedAt: new Date(), reviewedAt: auditStatus === 'approved' ? new Date() : null, + materialVersion: { increment: 1 }, + pendingReport: true, + reportChangedAt: new Date(), }, include: { tenant: true, signature: true, application: true }, }); diff --git a/docs/first-version-development-requirements.md b/docs/first-version-development-requirements.md index bd324df..e0b19b7 100644 --- a/docs/first-version-development-requirements.md +++ b/docs/first-version-development-requirements.md @@ -234,7 +234,8 @@ 7. Gateway 必须实现真实 CMPP Submit,包括短信内容编码、长短信拆分、RegisteredDelivery、serviceId、srcId、destTerminalId、msgFmt、feeType/feeCode 等字段映射。 8. Gateway 必须消费 NestJS 投递的 `SubmitCommand` 队列或等价内部接口;提交成功、提交失败、超时均必须回传 `SubmitResult`,不得只停留在 API 侧入队。 9. Gateway 必须按通道连接和窗口容量控制并发,处理窗口满、SMSC 慢响应、sequence 回绕、连接断开时的在途消息状态。 -10. Gateway 不承担业务审核、计费、签名报备、通道组路由、黑名单或敏感词判断;这些由 NestJS 完成,Gateway 只执行已授权通道提交与协议事件回传。 +10. Gateway 必须对每个物理通道执行 Redis 分布式 TPS 限速。`SmsChannel.rateLimitPerSecond` 是单通道上限,不是平台总上限;同一通道被多个通道组或多个 Gateway 实例使用时共享同一额度,不同通道独立计数。通道连接命令下发的配置值是最终上限,提交命令携带的值只能进一步降低、不能放大该上限。超流速消息必须继续保留在 Redis Stream pending 中等待可用时隙,不能因等待直接标记发送失败;Gateway 重启后仍可由 consumer group 恢复。worker 必须并发处理一个读取批次,让不同通道独立等待,不能因低 TPS 通道造成其他通道队头阻塞;单通道实际并发仍由 Redis 限速和 CMPP 窗口共同约束。 +11. Gateway 不承担业务审核、计费、签名报备、通道组路由、黑名单或敏感词判断;这些由 NestJS 完成,Gateway 只执行已授权通道提交与协议事件回传。 #### 4.8.2 下游客户 CMPP 接入能力 @@ -278,7 +279,8 @@ - 已实现 SubmitCommand 在途恢复第一版:Go Gateway submit worker 在消费新消息前会对 Redis Stream consumer group 中空闲超过阈值的 pending 命令执行 `XAUTOCLAIM`,重新提交并按正常成功路径 ack,避免 Gateway 重启后命令永久滞留在 PEL。 - 已实现上游连接断开时的 pending submit 状态补偿第一版:如果某条上游 CMPP 连接在收到 submit resp 前断开,Gateway 会立即唤醒该连接上等待中的 pending submit,请求返回 `timeout/CONNECTION_LOST`,由 NestJS 进入既有补发或释放冻结逻辑,不再只依赖固定超时。 - 已实现“上游可能已受理但 submit resp 丢失”场景的保守补偿第一版:Gateway 在 receipt 事件中补充手机号;NestJS 对无法按 `messageId/gatewayMessageId` 精确命中的回执,只在“同通道、同手机号、72 小时窗口内、且仅存在 1 条 `timeout + gatewayMessageId=null` 的 submit 记录”时才回填并接收该回执,避免误绑到其他短信。 -- 已实现 SubmitCommand 死信治理第一版:Go Gateway 对多次处理仍失败的 `SubmitCommand` 不再无限滞留在 PEL,而是按阈值写入 NestJS 真实 `GatewaySubmitDeadLetter` 表;运营端后端接口可分页查询死信,并支持人工将原始 `SubmitCommand` 重新写回 Redis Stream。 +- 已实现 Gateway 提交异常治理:Go Gateway 对多次处理仍失败的 `SubmitCommand` 不再无限滞留在 PEL,而是按阈值写入 NestJS 真实 `GatewaySubmitDeadLetter` 表(数据库表名和内部接口保留技术兼容名,页面统一称“Gateway提交异常”)。运营端 `/admin/gateway-submit-exceptions` 提供真实分页、筛选、汇总、脱敏详情和单条重新入队;原始 payload、密码、密钥不得返回浏览器。重新入队必须要求近期认证、填写原因、勾选“已确认上游未受理”,并校验短信尚未 accepted/submitted/delivered/unknown、通道 active 且 connected、人工次数小于 3;服务端以 pending 到 requeueing 的原子状态抢占防止重复点击,成功写回 Redis Stream 后记录操作人、原因、Stream ID 和时间。收到后续 SubmitResult 时必须将对应异常记录闭环为 resolved。 +- 已实现 Gateway 通道级 Redis 限速:NestJS 入队前保留业务层通道限速,Go Gateway 在真正调用上游 Submit 前再次按通道 ID 预约发送时隙;连接命令把权威 TPS 写入 Redis,提交按权威值与消息值的较小者执行。普通 Stream 消息在等待期间不 ACK、不转失败,多实例共同使用同一限速状态;worker 对同批消息并发调度,低 TPS 通道等待不阻塞其他通道。 - 已实现客户侧下游投递重试第二版:客户系统负责断线后重连;平台在客户离线或投递失败时把 Deliver Receipt/上行 Deliver 保留在 `CmppDownstreamDelivery`,客户 bind 成功后立即拉取 pending,且 Gateway 会对当前在线账号周期补投;超过重试上限后转 `failed` 并写失败审计。 - 已实现下游投递失败审计与人工重投第一版:运营端后端与页面可分页查看 `CmppDownstreamDelivery` 的 pending/awaiting_ack/failed/unconfirmed/rejected/delivered 记录,支持按状态、类型、应用和关键字筛选,并可对非 `awaiting_ack` 记录执行人工重投,真实调用 Gateway `/downstream/receipt` 或 `/downstream/uplink`。主记录必须分开保存自动重试次数 `retryCount`、人工重投次数 `manualRetryCount` 和最近人工重投时间 `lastRetriedAt`,操作日志保留重投前状态与自动重试次数。 - 已实现下游投递批量重投第一版:运营端可在当前页勾选多条 `pending/failed` 下游投递记录,调用真实批量接口逐条重投并返回成功/失败汇总,不允许用前端循环假装成功。 @@ -315,7 +317,7 @@ - 客户侧 Deliver 重投当前已经具备账号级周期恢复、退避、Redis token 租约锁和控制面状态观测;恢复状态已同步到 NestJS 持久化审计表并进入运营端独立页面,且具备第一版失败分类分析和多 Gateway 抢占协调。 - 普通上行匹配已覆盖 messageId、接入号、手机号时间窗口和共享接入号多候选人工认领;后续仍需补更复杂的批量认领、认领规则推荐和认领准确率指标。 - 长短信分片当前已具备真实分片提交/回执/补偿审计,运营端短信详情可查看 `SmsMessageSegmentAudit`;后续仍需补“按单个分片自动重投”和分片级人工补偿操作。 -- 多连接窗口当前覆盖单进程内连接池和窗口满等待;在途恢复当前覆盖 Redis Stream pending claim、连接断开时的 pending submit 唤醒、receipt 驱动的保守唯一候选补偿、SubmitCommand 死信入库/人工重入队第一版,以及下游客户在线时的周期补投、Gateway 重启后的恢复候选扫描、账号级 Redis token 租约锁/退避/状态观测、恢复状态入库/运营端可视化;尚未实现连接级状态持久化、窗口指标回写、死信后台自动重试策略、长恢复任务锁续租,以及“上游已受理但 submit resp 丢失”场景的强确认或完整幂等补偿。 +- 多连接窗口当前覆盖单进程内连接池和窗口满等待;在途恢复当前覆盖 Redis Stream pending claim、连接断开时的 pending submit 唤醒、receipt 驱动的保守唯一候选补偿、Gateway 提交异常转存/安全人工重新入队,以及下游客户在线时的周期补投、Gateway 重启后的恢复候选扫描、账号级 Redis token 租约锁/退避/状态观测、恢复状态入库/运营端可视化;尚未实现连接级状态持久化、窗口指标回写、提交异常后台自动重试策略、长恢复任务锁续租,以及“上游已受理但 submit resp 丢失”场景的强确认或完整幂等补偿。 基于当前真实代码,CMPP 端到端链路剩余缺口可以明确收敛为以下几类: @@ -698,6 +700,8 @@ 9. 按企业应用绑定的对应运营商通道组执行路由:通道组只能是移动、联通、电信之一,发送时必须同时满足路由规则运营商、通道组运营商、通道组明细 carrier 与号码识别运营商一致;再校验通道本体 carrier 为对应运营商或三网;最后先匹配省网通道,再匹配全国通道,不得直接绑定或 fallback 到非授权单通道。 10. 过滤业务 disabled、连接离线、认证失败、心跳超时或无可用连接数的通道。 11. 通过通道限速器控制 TPS;优先队列不得突破通道配置的供应商 TPS 和连接窗口上限。 + - TPS 按物理通道独立计数,不是整个平台共享一个总额度。例如通道 A、B 均配置 100 TPS 时,各自最多 100 TPS,平台理论合计为 200 TPS。 + - 同一物理通道被多个通道组使用时,共享 `SmsChannel.rateLimitPerSecond` 的同一额度;通道组成员不提供单独流速配置,避免多个组并发使用同一供应商账号时重复计算额度。 12. 调用 Gateway Adapter 提交短信。 13. 写入 submit 状态。 14. Submit rejected、submit timeout、Gateway 连接断开或未提交成功、receipt failed 等场景按通道组策略补发到下一可用全国通道;unknown、超过 72 小时、超过通道组补发时间上限或关闭补发时不再补发。 @@ -1473,3 +1477,12 @@ 然后按计划逐步实现。每完成一步都要运行构建或测试,并更新文档。 ``` + +### 2026-07-15 签名与引流资料批量导入、通道映射及统一报备 + +1. 运营端在“报备任务”下提供“待报备资料”工作台。WPS 在线表格须先由用户另存为 `.xlsx`,系统读取真实工作簿、工作表、表头、单元格和内嵌图片;原始文件及拆出的图片写入 MinIO,导入批次、映射和业务资料写入 PostgreSQL,不支持用 CSV、前端静态数组或浏览器本地存储冒充图片导入。 +2. 导入分为“解析预览”和“确认入库”两步。用户可指定企业、企业应用、资料类型、表头行数、数据起始行并复用映射方案;每个源列可映射到签名名称、用途、所属签名、站点名称、URL、备注或报备字段库中的动态字段,同时配置文本/图片/文件、必填和转换规则。源文件字段名称和顺序不固定,映射方案必须可持久化复用。 +3. 导入和业务页面的新建/修改只将已审核签名或引流信息标记为待报备,并递增材料版本;不得在每次导入后自动创建通道报备任务。运营人员可跨签名、跨引流信息勾选资料,一次创建统一报备批次。 +4. 创建批次时按每条资料所属企业应用的当前生效路由规则展开所有通道;一个签名走多个通道时,必须为每个通道创建或重置独立报备任务并生成一份该通道的 `.xlsx`。无生效路由、通道未配置字段或缺少通道必填资料时,该资料继续保留在待报备池,任务进入“资料待补充”,不得伪装为已完成。 +5. 通道“配置签名报备字段”和“配置引流信息字段”弹窗使用字段池,按资料类型分别配置。每列包含标准字段、通道导出表头、列顺序、必填、说明、列宽、文本转换、缺省值以及图片宽高;导出表头和列顺序必须严格使用通道配置,不受导入表格原始名称和顺序影响。 +6. 通道导出文件必须为 WPS/Excel 可打开的 `.xlsx`,图片直接内嵌到对应单元格区域,而不是仅写 MinIO URL 或本地路径。批次保留所选材料版本快照、通道文件、行号和通道任务关联,可从最近批次直接下载每个通道文件。 diff --git a/docs/production-deployment.md b/docs/production-deployment.md index 016cf2d..f07c165 100644 --- a/docs/production-deployment.md +++ b/docs/production-deployment.md @@ -64,6 +64,8 @@ PROD_ADMIN_PASSWORD='change-me' `API_ENABLE_SEND_WORKER=true` 是生产发送链路必填项。后续发布脚本会在构建和迁移前校验该开关以及正整数 `API_SEND_WORKER_CONCURRENCY`;缺失时直接终止发布,防止 API/Gateway 健康但 BullMQ 短信队列无人消费。 +Gateway 的最终 TPS 防线依赖与 API 相同的 Redis。通道连接时会写入 `rate:gateway:channel:config:` 权威上限,实际预约使用 `rate:gateway:channel:`;这些 key 不应在正常发布时清理。多 Gateway 实例必须指向同一 Redis,才能共享单通道额度。超速的 `gateway.submit.commands` 消息会保持在 consumer group pending 中等待,不应通过手工 `XACK` 或删除 Stream 处理积压;先检查通道配置、Redis key、consumer group 和 Gateway 日志。 + 日报任务默认启用,并由 `REPORT_REFRESH_INTERVAL_MS` 每小时检查一次北京时间业务日是否变化;每个业务日只执行一次 T-4 至 T-1 重算。服务重启后也会自动补跑最近四个完整自然日,确保 72 小时回执更新反映到对账和利润报表。 如生产验证服务器临时无法稳定下载 MinIO,可显式传入 `OBJECT_STORAGE_DRIVER=local`,文件会通过真实 API 保存到服务器本地目录 `OBJECT_STORAGE_LOCAL_ROOT`,`cmpp-minio` 服务会跳过安装和启动。该模式只建议用于验证环境;正式生产建议恢复 `OBJECT_STORAGE_DRIVER=minio`。 @@ -103,6 +105,8 @@ curl http://127.0.0.1:12026/ redis-cli -h 127.0.0.1 -p 6379 ping pg_isready -d "$(grep '^DATABASE_URL=' /etc/cmpp-platform/cmpp-platform.env | cut -d= -f2-)" grep -E '^(API_ENABLE_SEND_WORKER|API_SEND_WORKER_CONCURRENCY)=' /etc/cmpp-platform/cmpp-platform.env +redis-cli --scan --pattern 'rate:gateway:channel:*' +redis-cli XINFO GROUPS gateway.submit.commands ``` ## 回滚 diff --git a/docs/system-functional-test-cases.md b/docs/system-functional-test-cases.md index e7e6fd3..7eb066f 100644 --- a/docs/system-functional-test-cases.md +++ b/docs/system-functional-test-cases.md @@ -1306,21 +1306,21 @@ - 如果存在多条候选或无候选,则拒绝归因,不得误绑到其他短信。 - 已经由新通道成功送达的短信,旧尝试迟到回执仍只记历史,不覆盖最终送达状态。 -### TC-GW-012 SubmitCommand 死信入库与人工重入队 +### TC-GW-012 Gateway 提交异常入库与人工重新入队 - 优先级:P0 -- 前置条件:Gateway submit worker 已连接 Redis Stream `gateway.submit.commands`;准备一条会持续触发处理错误的 `SubmitCommand`;NestJS `/gateway/events/dead-letter` 和运营端 `/api/admin/operations/gateway-submit-dead-letters` 真实可用。 +- 前置条件:Gateway submit worker 已连接 Redis Stream `gateway.submit.commands`;准备一条会持续触发处理错误的 `SubmitCommand`;NestJS 内部异常上报接口和运营端 Gateway 提交异常 API 真实可用。 - 步骤: - 1. 让同一条 `SubmitCommand` 连续处理失败,达到 Gateway 配置的死信阈值。 + 1. 让同一条 `SubmitCommand` 连续处理失败,达到 Gateway 配置的异常转存阈值。 2. 检查 Redis PEL 中该消息是否被 ack,不再无限 pending。 3. 检查 NestJS 是否在真实数据库写入一条 `GatewaySubmitDeadLetter`,保存失败原因、尝试次数和原始命令载荷。 - 4. 调用运营端真实接口查询死信列表。 - 5. 调用人工重入队接口,将该死信重新写回 `gateway.submit.commands`。 + 4. 从运营端“Gateway提交异常”页面查询异常列表并打开脱敏详情。 + 5. 完成风险确认、原因和状态校验后,将该异常命令重新写回 `gateway.submit.commands`。 - 预期结果: - - 达到阈值后,Gateway 会把该消息转为死信,而不是永久卡在 PEL。 - - 死信记录来自真实数据库,包含 `streamMessageId`、`messageId/submitId`、失败原因、尝试次数和原始 `SubmitCommand`。 - - 人工重入队成功后,死信状态更新为 `requeued`,记录新的 Redis Stream 消息 ID,并写系统日志。 - - 重入队后如后续收到真实 `SubmitResult`,对应死信记录应自动转为 `resolved`。 + - 达到阈值后,Gateway 会把该消息转存为提交异常,而不是永久卡在 PEL。 + - 异常记录来自真实数据库,包含 `streamMessageId`、`messageId/submitId`、失败原因和尝试次数;浏览器端只收到脱敏命令,不收到原始 payload 或密码密钥。 + - 人工重新入队成功后,异常状态更新为 `requeued`,记录新的 Redis Stream 消息 ID、操作人和原因,并写系统日志。 + - 重新入队后如后续收到真实 `SubmitResult`,对应异常记录应自动转为 `resolved`。 ### TC-GW-013 下游客户在线时周期补投与失败封顶 @@ -3213,6 +3213,15 @@ npm run test:gateway npm run verify:phase8 ``` +## 2026-07-15 通道 TPS 归属与通道组成员字段清理 + +| 用例编号 | 场景 | 操作 | 预期结果 | +| --- | --- | --- | --- | +| TC-CHANNEL-TPS-001 | 单通道限速 | 通道 A 配置 100 TPS,持续提交超过 100 条/秒 | PostgreSQL 保存 `SmsChannel.rateLimitPerSecond=100`;NestJS 以通道 A 的 ID 建立 Redis 限速桶;单个自然秒放行不超过 100 条。 | +| TC-CHANNEL-TPS-002 | 多通道独立限速 | 通道 A、B 均配置 100 TPS,并发向两个通道提交 | A、B 使用不同通道 ID 的 Redis 限速桶,各自最多 100 TPS;平台不存在共享的 100 TPS 总额度。 | +| TC-CHANNEL-TPS-003 | 多通道组共享同一物理通道 | 两个通道组同时选中通道 A 并产生发送 | 两组发送共享通道 A 的同一个 100 TPS 限速桶,合计不超过通道 A 配置;通道组成员接口和页面均无单独流速字段。 | +| TC-CHANNEL-TPS-004 | 数据库结构清理 | 执行 Prisma migration 并读取 `SmsChannelGroupItem` 结构 | `SmsChannelGroupItem.rateLimitPerSecond` 已删除,通道自身的 `SmsChannel.rateLimitPerSecond` 保留。 | + 测试环境必须具备 PostgreSQL、Redis、MinIO 或等价本地服务后,才能将 E2E smoke 和真实 API HTTP 测试记为系统功能通过;缺失时对应用例标记为阻塞或未执行。 ## 17. 新增和更新用例细化执行清单 @@ -3419,3 +3428,27 @@ npm run verify:phase8 | TC-GW-ACK-005 | 客户 Submit 后由业务校验立即生成失败回执,并覆盖在线即时投递、Gateway 重启后恢复投递;另模拟客户端对 `Msg_Id=0` 返回 Result=0。 | 客户收到的第一个响应包必须是对应 `CMPP_SUBMIT_RESP`,之后 Deliver 的 `Msg_Id` 非 0 且与 SubmitResp 完全一致;重启后根据持久化 Submit Sequence_Id 重建同一 Msg_Id;Result=0/Msg_Id=0 不得写为 delivered。 | | TC-GW-ACK-006 | 对一条 Gateway 已丢失原消息映射且 payload 缺少 `submitSequenceId` 的历史状态回执执行人工重投;另对字段完整但客户离线的记录重投。 | 缺少序列号的记录由 Gateway 返回 `retryable=false/MISSING_SUBMIT_SEQUENCE_ID`,NestJS 立即终结为 `failed` 并保存明确原因;客户暂时离线的记录返回 `retryable=true/CLIENT_DISCONNECTED`,按次数上限和指数退避继续处理,不得无限保持 `retryCount=0`。 | | TC-GW-ACK-007 | 构造一条从未重投且 pending 超过 72 小时的记录,以及一条人工重投后尚未满 72 小时但原 `createdAt` 很早的记录,执行自动扫描并查看告警。 | 第一条自动转为 `failed/queue_timeout`;第二条仍保持 pending,终结时间和 10 分钟积压告警均从 `lastRetriedAt` 重新计算,刚重投后不立即告警。 | + +### 17.12 签名与引流资料导入及统一通道报备 + +| 用例编号 | 操作 | 预期结果 | +| --- | --- | --- | +| TC-REPORT-MATERIAL-IMPORT-001 | 将含两行表头、文本列和营业执照/身份证等内嵌图片的 WPS 在线表格另存为 `.xlsx`,选择企业、应用和签名资料后解析。 | NestJS 读取真实工作表及图片锚点,返回列、组合表头、前十行和图片数预览;原文件写 MinIO,导入批次写 PostgreSQL;未确认前不改签名、不建通道任务。 | +| TC-REPORT-MATERIAL-IMPORT-002 | 将源列分别映射到短信签名、签名用途和动态报备字段,调整数据类型/必填/转换规则,保存映射方案后确认导入;再用列顺序不同但表头相同的文件复用方案。 | 新签名或已存在签名的资料真实入库,内嵌图片拆出并写 MinIO 引用,材料版本递增且进入待报备池;映射方案持久化并可再次选择,源列顺序不影响目标字段。 | +| TC-REPORT-MATERIAL-IMPORT-003 | 导入引流资料,将所属签名、站点、URL、备注和动态图片映射后确认;其中一行引用不存在或未审核签名。 | 合法行创建/更新真实 `SmsDrainageInfo` 并进入待报备池;非法行记录行号和原因,批次为部分失败,不因单行错误回滚其他合法行,也不自动创建报备任务。 | +| TC-REPORT-CHANNEL-FIELD-001 | 在同一通道分别打开签名和引流字段配置,添加字段、修改通道表头、上下排序、设置必填/列宽/图片宽高后保存并刷新。 | 两类配置相互独立且完整持久化;刷新后字段池、映射表头和顺序一致;重复字段、停用字段和非法尺寸由 API 拒绝或归一化。 | +| TC-REPORT-BATCH-001 | 一个应用配置两个生效通道,选择一个待报备签名创建统一批次。 | 系统从真实应用路由展开两个通道,生成两个独立通道任务和两个 `.xlsx`;每个文件表头名称、列顺序和列宽均来自对应通道配置,批次可下载两份文件。 | +| TC-REPORT-BATCH-002 | 两个通道对同一标准字段配置不同表头和顺序,并包含图片列,生成批次后分别用 WPS 打开。 | 两份工作簿各自使用对应通道映射,图片直接显示在数据行内且尺寸按通道配置;文件不是 URL 清单,文本与图片属于同一材料快照。 | +| TC-REPORT-BATCH-003 | 分别制造无生效路由、通道未配置字段、缺少通道必填图片,再创建批次。 | 对应资料不会清除待报备标记;有通道但资料不全时任务为 `waiting_material` 并记录原因;批次为部分失败,无任何假成功任务。 | +| TC-REPORT-BATCH-004 | 同一签名修改资料后再次选择生成批次。 | 材料版本递增;复用同一签名/通道任务并重置到新一轮状态,批次项目保留当次版本和快照,历史导出文件仍可追溯。 | + +### 17.13 Gateway 提交异常与通道级 TPS 限速 + +| 用例编号 | 操作 | 预期结果 | +| --- | --- | --- | +| TC-GW-SUBMIT-EXCEPTION-001 | 制造一条超过 Gateway 最大处理次数的真实 `SubmitCommand`,打开运营端“Gateway提交异常”,按状态、应用、通道和关键字筛选并查看详情。 | 记录写入 PostgreSQL,页面汇总、分页和详情来自 NestJS API;手机号脱敏,命令中的密码、密钥和原始 payload 不返回浏览器,页面不使用“死信”作为业务名称。 | +| TC-GW-SUBMIT-EXCEPTION-002 | 对短信仍处于 pending/failed、通道 active 且 connected 的异常记录,输入 5~500 字原因,勾选“已确认上游未受理”并重新入队。 | 近期认证通过后服务端原子抢占记录、真实写入 Redis Stream;记录变为 requeued,人工次数、操作人、原因、Stream ID 和时间完整留痕,收到 SubmitResult 后变为 resolved。 | +| TC-GW-SUBMIT-EXCEPTION-003 | 不勾选确认、原因过短、重复点击同一记录,或分别把短信置为 accepted/submitted/delivered/unknown、把通道置为停用/断开、人工重试达到 3 次后尝试重新入队。 | API 拒绝危险或重复操作,不产生额外 Stream 命令;页面显示可读原因,操作日志不伪造成功。 | +| TC-GW-RATE-001 | 给通道 A 配置 10 TPS,连续投递 20 条;通道 B 同时配置 20 TPS 并投递,另让提交命令携带高于通道配置的数值。 | Gateway A 实际提交节奏不超过 10 TPS,B 独立按自身额度执行;消息值不能放大 A 的权威上限,同一通道跨通道组共享额度。 | +| TC-GW-RATE-002 | 超过通道 TPS 后观察 Redis Stream consumer group,并在存在等待消息时重启 Gateway。 | 超流速消息保留在 Stream pending,不直接失败;重启后通过 PEL/XAUTOCLAIM 恢复并继续按通道 TPS 排队提交,不丢失、不重复 ACK。 | +| TC-GW-RATE-003 | 启动两个共享同一 Redis 的 Gateway 消费实例,同时向同一通道发送,再向两个不同通道发送。 | 同一通道的两个实例共享 Redis 限速额度,总 TPS 不叠加;不同通道使用独立 key,不被合并成平台总 TPS。 | diff --git a/docs/testing-progress.md b/docs/testing-progress.md index 8db395e..1bb4943 100644 --- a/docs/testing-progress.md +++ b/docs/testing-progress.md @@ -1877,6 +1877,7 @@ git diff --check ## 2026-07-14 服务端安全会话与自动锁定 - 将可预测的 `dev-token:userId:sessionVersion` 和 localStorage 访问令牌替换为 256 位随机会话标识;浏览器只通过 HttpOnly、SameSite Cookie 携带,Redis 使用会话标识 SHA-256 键保存真实状态。生产模式 Cookie 默认 `Secure`;本次按用户要求部署到现有 HTTP 生产验证环境时显式配置 `SESSION_COOKIE_SECURE=false`,正式生产切换 HTTPS 后必须恢复为 `true`。 + - 运营端/客户端无操作阈值分别为 60/120 分钟,提前 5 分钟提醒;超时进入密码锁屏,4 小时内可用当前密码解锁并轮换会话标识,超过后完整登录。绝对会话时长 12 小时不可滑动续期;敏感操作最近密码认证窗口为 30 分钟。 - NestJS 中间件对运营端和客户端受保护 API 强制要求 Redis 会话,逐次校验用户状态和 `sessionVersion`;Gateway 回调和 health 保持原内部链路,不被浏览器会话门禁拦截。自动轮询只有检测到近期真实浏览器操作时才携带活动标识,不能长期保活无人值守会话。 - 用户/权限、企业状态、应用密钥、通道和路由、报备状态、手工充值/退款/调整已接后端最近认证 Guard。前端收到 `RECENT_AUTHENTICATION_REQUIRED` 后要求当前密码,成功后自动重试;普通 JSON、Blob 和文件上传统一处理会话 401。会话创建、锁定、解锁、再认证和退出写 `OperationLog`,多标签页同步状态。 @@ -1914,3 +1915,25 @@ git diff --check - 工作区完整改动已提交并 push:`a7a4e8d9f6aba00b8137e5b70c59bdef67ed3b57`(`feat: polish reporting templates and shared controls`)。部署前确认本地 `main` 与 `origin/main` 一致,并备份生产 PostgreSQL、运行源码和环境配置至 `/opt/cmpp-platform/backups/releases/20260715-163418`;三份备份均通过 SHA-256 复核和压缩包完整性检查,其中数据库备份 SHA-256 为 `a59d3b7f4538f99cdc73a1092ac31628efa45bc38d4adf20a05d6a1f075ec5da`,运行源码备份为 `305c529fb94c71fd6e49f6228717b2ce93f14fff84a84e477d1f202f3192ef58`。 - 发布快照本地与服务器 SHA-256 均为 `489d6c969252403689dfdcca15b87e4f5a93b65187551fa6650451527366942e`。生产 `.deployed-commit=a7a4e8d9f6aba00b8137e5b70c59bdef67ed3b57`,47 条 migration 全部齐全;`cmpp-api`、`cmpp-gateway`、MinIO、Nginx、PostgreSQL、Redis 均为 active,`12026/17890/8090/3000/9000` 正常监听,API/Gateway health、Redis PONG、PostgreSQL readiness 和外部首页/运营端/API HTTP 均通过,部署后 API/Gateway 无 error 级日志。 - 生产浏览器确认登录页加载成功且标题为“聆界短信管理平台”。服务器凭据文件对存量管理员仅记录 `password=unchanged`,不是可用明文密码,因此未擅自重置生产密码;通用 Select 的企业搜索、企业与应用联动、弹窗越界和普通筛选区 Portal 交互已在部署前通过本地真实 NestJS API、PostgreSQL 数据和生产同构建验证,未使用 mock、localStorage 或静态数组。 + +## 2026-07-15 签名与引流资料批量导入及统一通道报备(未提交) + +- 新增真实待报备资料工作台:WPS 表格另存 `.xlsx` 后由 NestJS + ExcelJS 解析多行表头、文本和内嵌图片,原文件及图片走 MinIO,导入批次、可复用映射方案、材料版本和待报备状态走 Prisma/PostgreSQL;导入只更新资料池,不自动生成通道任务。 +- 新增统一报备批次:运营勾选新建/修改的签名与引流信息后,按企业应用当前生效路由展开全部通道,每通道生成一份内嵌图片的 `.xlsx`,并关联批次材料版本快照、文件行号和真实通道报备任务。无路由、未配置通道字段或缺必填资料不会清除待报备标记。 +- 通道签名/引流字段配置按设计基线恢复为字段池和已选字段双栏,支持通道导出表头、顺序、必填、列宽、图片尺寸、缺省值和文本转换;导出严格使用各通道自己的映射名称与顺序。 +- Prisma migrations `20260715190000_add_report_material_import_export_workflow`、`20260715193000_scope_channel_report_fields_by_type`、`20260715194000_initialize_existing_report_material_pending` 已在本地真实 PostgreSQL 成功应用,50 条 migration status 齐全;字段范围迁移将旧 `both` 配置拆成签名/引流两份,并允许同一标准字段在两类中使用不同表头和顺序;初始化迁移不把上线前所有历史签名误认成本次新建/修改资料。Prisma validate/generate、API 全量 19 suites/198 项、API build、前端 build、Gateway 全量 Go 测试、根目录与 API 生产依赖 audit、`git diff --check` 均通过;audit 为 0 漏洞,前端仅有既有 Vite chunk size warning,Jest 仍需 `--forceExit` 退出既有异步句柄。 +- 应用内浏览器使用本地真实 NestJS API/PostgreSQL 会话验证 `/admin/report-materials`:真实待报备签名加载成功,勾选后“统一生成通道报备”由禁用变为可用;导入弹窗展示企业/应用、映射方案、表头行和 XLSX 文件控件。进入真实通道报备详情后,签名字段配置弹窗按字段池/导出字段双栏渲染,页面和两次交互均无 console error/warn、无框架错误覆盖。未点击统一生成、未上传客户文件、未写入烟测业务数据。 + +## 2026-07-15 通道 TPS 配置口径清理(未提交) + +- 明确 `SmsChannel.rateLimitPerSecond` 是单个物理通道的 TPS 上限,不是平台总流速;Redis 限速键包含通道 ID,不同通道独立计数。 +- 同一物理通道即使被多个通道组引用,也共享通道自身的同一限速桶,不按通道组重复获得额度。 +- 删除未参与发送链路的 `SmsChannelGroupItem.rateLimitPerSecond`:Prisma 模型、通道组成员 DTO、真实 API 持久化和前端类型均已清理,并新增数据库迁移删除对应列;通道管理中的 `SmsChannel.rateLimitPerSecond` 和现有 NestJS Redis 限速链路保留。 +- 本地真实 PostgreSQL 已应用 `20260715200000_drop_channel_group_item_rate_limit`,Prisma validate/generate/migrate status 通过;通道服务定向测试 29 项、API 全量 19 suites/198 项、API build、前端 build、Gateway `go test ./...` 均通过。Jest 仍存在测试完成后异步句柄不自动退出的既有提示,前端仍只有既有 chunk size warning。 + +## 2026-07-15 Gateway 提交异常处理与最终流速限制(待部署) + +- 运营端新增“Gateway提交异常”页面和真实 NestJS API:支持状态/应用/通道/关键字筛选、服务端汇总、脱敏详情及单条重新入队;浏览器响应不包含原始 payload、通道密码、密钥或鉴权字段,数据库和内部兼容接口暂保留 `GatewaySubmitDeadLetter` 技术命名。 +- 重新入队增加近期认证、人工原因、明确确认上游未受理、短信状态、通道状态/真实连接状态、最多 3 次以及 pending→requeueing 原子抢占校验;写入 Redis Stream 失败会恢复 pending,成功记录操作人、原因、Stream ID 和时间,后续 SubmitResult 自动闭环 resolved。 +- Go Gateway 新增 Redis 分布式单通道限速。连接命令保存权威通道 TPS,提交取权威值与消息值的较小者;同一通道在多实例和多个通道组间共享额度,不同通道独立。Stream 消息超速时保持未 ACK 并等待,不作为发送失败,重启后继续使用既有 pending 恢复机制;worker 对同批消息并发处理,避免低 TPS 通道等待阻塞其他通道。 +- 定向验证已通过:Gateway `internal/ratelimit`、`internal/control`、`internal/submitworker`;API `operations.service.spec.ts`、`send-chain.service.spec.ts` 共 73 项。最终本地门禁通过:Prisma validate/generate、51 条 migration status、API 19 suites/199 项、API build、前端 build、Gateway `go test ./...` 和 `git diff --check`;前端仅有既有 chunk size warning,Jest 仍需 `--forceExit` 退出既有异步句柄。应用内浏览器确认受保护新路由真实跳转运营端登录、标题和 DOM 正常、console 无 error/warn;因真实图形验证码未获授权代解,未绕过认证或注入会话。生产备份、提交、push 与部署结果在发布后补录。 diff --git a/gateway/cmd/gateway/main.go b/gateway/cmd/gateway/main.go index 319d887..03eca4a 100644 --- a/gateway/cmd/gateway/main.go +++ b/gateway/cmd/gateway/main.go @@ -9,6 +9,7 @@ import ( "cmpp-platform/gateway/internal/control" "cmpp-platform/gateway/internal/health" "cmpp-platform/gateway/internal/inbound" + "cmpp-platform/gateway/internal/ratelimit" "cmpp-platform/gateway/internal/submitworker" "cmpp-platform/gateway/internal/upstream" ) @@ -24,6 +25,10 @@ func main() { } apiBaseURL := os.Getenv("API_BASE_URL") upstreamManager := &upstream.Manager{APIBaseURL: apiBaseURL} + channelLimiter, err := ratelimit.New(os.Getenv("REDIS_URL")) + if err != nil { + log.Fatalf("gateway channel rate limiter init failed: %v", err) + } presenceStore, err := inbound.NewRedisPresenceStore(os.Getenv("REDIS_URL")) if err != nil { log.Printf("gateway downstream presence store init failed: %v", err) @@ -69,6 +74,7 @@ func main() { control.Register(mux, control.Server{ APIBaseURL: apiBaseURL, Upstream: upstreamManager, + Limiter: channelLimiter, RecoveryCandidates: func(ctx context.Context) ([]inbound.DownstreamPresence, error) { return inbound.ListRecoveryCandidates(ctx, presenceStore) }, diff --git a/gateway/internal/control/server.go b/gateway/internal/control/server.go index 7c8d44a..5705331 100644 --- a/gateway/internal/control/server.go +++ b/gateway/internal/control/server.go @@ -9,10 +9,12 @@ import ( "cmpp-platform/gateway/internal/inbound" "cmpp-platform/gateway/internal/queue" + "cmpp-platform/gateway/internal/ratelimit" "cmpp-platform/gateway/internal/upstream" ) type ConnectFunc func(context.Context, ConnectChannelCommand) (ConnectionStateCallback, error) +type SubmitFunc func(context.Context, queue.SubmitCommand) (queue.SubmitResult, error) type ConnectChannelCommand struct { SchemaVersion string `json:"schemaVersion"` @@ -54,7 +56,9 @@ type Server struct { APIBaseURL string HTTPClient *http.Client Connect ConnectFunc + Submit SubmitFunc Upstream *upstream.Manager + Limiter ratelimit.Limiter RecoveryCandidates func(context.Context) ([]inbound.DownstreamPresence, error) RecoveryStatuses func(context.Context) ([]inbound.DownstreamRecoveryStatus, error) } @@ -74,6 +78,9 @@ func Register(mux *http.ServeMux, server Server) { if server.Connect == nil { server.Connect = server.connectChannel } + if server.Submit == nil { + server.Submit = server.Upstream.Submit + } mux.HandleFunc("/connections/connect", server.handleConnectChannel) mux.HandleFunc("/upstream/submit", server.handleUpstreamSubmit) mux.HandleFunc("/downstream/receipt", server.handleDownstreamReceipt) @@ -98,6 +105,12 @@ func (s Server) handleConnectChannel(w http.ResponseWriter, r *http.Request) { http.Error(w, err.Error(), http.StatusBadRequest) return } + if configurer, ok := s.Limiter.(ratelimit.Configurer); ok { + if err := configurer.Configure(r.Context(), command.ChannelID, command.Channel.RateLimitPerSecond); err != nil { + http.Error(w, fmt.Sprintf("failed to configure gateway channel rate limit: %v", err), http.StatusServiceUnavailable) + return + } + } status, err := s.Connect(r.Context(), command) if err != nil { @@ -119,7 +132,13 @@ func (s Server) handleUpstreamSubmit(w http.ResponseWriter, r *http.Request) { http.Error(w, fmt.Sprintf("invalid submit command: %v", err), http.StatusBadRequest) return } - result, err := s.Upstream.Submit(r.Context(), command) + if s.Limiter != nil { + if _, err := s.Limiter.Wait(r.Context(), command.ChannelID, command.Route.RateLimitPerSecond); err != nil { + http.Error(w, fmt.Sprintf("gateway channel rate limit unavailable: %v", err), http.StatusServiceUnavailable) + return + } + } + result, err := s.Submit(r.Context(), command) if err != nil { w.WriteHeader(http.StatusBadGateway) _ = json.NewEncoder(w).Encode(result) diff --git a/gateway/internal/control/server_test.go b/gateway/internal/control/server_test.go index f12b441..2cac159 100644 --- a/gateway/internal/control/server_test.go +++ b/gateway/internal/control/server_test.go @@ -10,10 +10,57 @@ import ( "time" "cmpp-platform/gateway/internal/inbound" + "cmpp-platform/gateway/internal/queue" ) +type controlRecordingLimiter struct { + channelID string + rate int + configuredChannelID string + configuredRate int +} + +func (l *controlRecordingLimiter) Wait(_ context.Context, channelID string, rate int) (time.Duration, error) { + l.channelID = channelID + l.rate = rate + return 0, nil +} + +func (l *controlRecordingLimiter) Configure(_ context.Context, channelID string, rate int) error { + l.configuredChannelID = channelID + l.configuredRate = rate + return nil +} + +func TestUpstreamSubmitUsesGatewayChannelLimiter(t *testing.T) { + limiter := &controlRecordingLimiter{} + handler := handlerWithServer(Server{ + Limiter: limiter, + Submit: func(_ context.Context, command queue.SubmitCommand) (queue.SubmitResult, error) { + return queue.SubmitResult{Envelope: command.Envelope, SubmitStatus: "accepted"}, nil + }, + }) + resp := httptest.NewRecorder() + req := httptest.NewRequest(http.MethodPost, "/upstream/submit", strings.NewReader(`{ + "schemaVersion":"v1","messageType":"SubmitCommand","messageId":"msg-1","channelId":"channel-1", + "submitId":"submit-1","tenantId":"tenant-1","applicationId":"app-1","phoneNumber":"13800138000","content":"hello", + "route":{"channelCode":"CMPP-A","cmppAccountCode":"sp","rateLimitPerSecond":100}, + "cmpp":{"serviceId":"SMS","srcId":"10690000","registeredDelivery":1,"msgFmt":8}, + "upstream":{"gatewayHost":"127.0.0.1","gatewayPort":17890,"account":"sp","passwordCipher":"secret","cmppVersion":"3.0"}, + "retry":{"attempt":0,"maxAttempts":1} + }`)) + handler.ServeHTTP(resp, req) + if resp.Code != http.StatusOK { + t.Fatalf("unexpected response status: %d body=%s", resp.Code, resp.Body.String()) + } + if limiter.channelID != "channel-1" || limiter.rate != 100 { + t.Fatalf("unexpected limiter call: %+v", limiter) + } +} + func TestConnectChannelCallbacksConnectedState(t *testing.T) { - handler := handlerWithConnect(func(context.Context, ConnectChannelCommand) (ConnectionStateCallback, error) { + limiter := &controlRecordingLimiter{} + handler := handlerWithServer(Server{Limiter: limiter, Connect: func(context.Context, ConnectChannelCommand) (ConnectionStateCallback, error) { return ConnectionStateCallback{ ChannelID: "channel-1", ConnectionID: "channel-1:primary", @@ -23,7 +70,7 @@ func TestConnectChannelCallbacksConnectedState(t *testing.T) { LastConnectedAt: time.Now().UTC().Format(time.RFC3339Nano), LastHeartbeatAt: time.Now().UTC().Format(time.RFC3339Nano), }, nil - }) + }}) resp := httptest.NewRecorder() req := httptest.NewRequest(http.MethodPost, "/connections/connect", strings.NewReader(validConnectCommand())) @@ -45,6 +92,9 @@ func TestConnectChannelCallbacksConnectedState(t *testing.T) { if callback.LastConnectedAt == "" || callback.LastHeartbeatAt == "" { t.Fatalf("expected connection timestamps: %+v", callback) } + if limiter.configuredChannelID != "channel-1" || limiter.configuredRate != 100 { + t.Fatalf("unexpected configured limiter: %+v", limiter) + } } func TestConnectChannelCallbacksFailedState(t *testing.T) { diff --git a/gateway/internal/ratelimit/limiter.go b/gateway/internal/ratelimit/limiter.go new file mode 100644 index 0000000..20cb62a --- /dev/null +++ b/gateway/internal/ratelimit/limiter.go @@ -0,0 +1,133 @@ +package ratelimit + +import ( + "context" + "fmt" + "strings" + "time" + + "github.com/redis/go-redis/v9" +) + +const ( + defaultRatePerSecond = 100 + defaultKeyPrefix = "rate:gateway:channel:" +) + +// Limiter reserves one distributed, per-channel submit slot and waits until it +// becomes available. The reservation is stored in Redis so multiple Gateway +// instances share one supplier TPS budget. +type Limiter interface { + Wait(context.Context, string, int) (time.Duration, error) +} + +// Configurer stores the authoritative channel limit received with the channel +// connection command. Wait always applies the lower of this value and the +// message value, so a producer cannot raise the supplier TPS ceiling. +type Configurer interface { + Configure(context.Context, string, int) error +} + +type RedisLimiter struct { + Redis *redis.Client + KeyPrefix string +} + +var reserveScript = redis.NewScript(` +local now = redis.call('TIME') +local now_us = (tonumber(now[1]) * 1000000) + tonumber(now[2]) +local requested_rate = tonumber(ARGV[1]) +local configured_rate = tonumber(redis.call('GET', KEYS[2])) +local effective_rate = requested_rate +if configured_rate and configured_rate > 0 and configured_rate < effective_rate then + effective_rate = configured_rate +end +local interval_us = math.ceil(1000000 / effective_rate) +local next_us = tonumber(redis.call('GET', KEYS[1])) or now_us +if next_us < now_us then + next_us = now_us +end +local delay_us = next_us - now_us +local reserved_until_us = next_us + interval_us +local ttl_ms = math.ceil((reserved_until_us - now_us) / 1000) + 1000 +redis.call('PSETEX', KEYS[1], ttl_ms, reserved_until_us) +return delay_us +`) + +func New(redisURL string) (*RedisLimiter, error) { + if strings.TrimSpace(redisURL) == "" { + redisURL = "redis://127.0.0.1:6379" + } + options, err := redis.ParseURL(redisURL) + if err != nil { + return nil, err + } + return NewWithClient(redis.NewClient(options)), nil +} + +func NewWithClient(client *redis.Client) *RedisLimiter { + return &RedisLimiter{Redis: client, KeyPrefix: defaultKeyPrefix} +} + +func (l *RedisLimiter) Configure(ctx context.Context, channelID string, ratePerSecond int) error { + if l == nil || l.Redis == nil { + return fmt.Errorf("gateway rate limiter Redis client is required") + } + channelID = strings.TrimSpace(channelID) + if channelID == "" { + return fmt.Errorf("gateway rate limiter channelId is required") + } + if ratePerSecond <= 0 { + ratePerSecond = defaultRatePerSecond + } + if err := l.Redis.Set(ctx, l.configKey(channelID), ratePerSecond, 0).Err(); err != nil { + return fmt.Errorf("configure gateway channel rate: %w", err) + } + return nil +} + +func (l *RedisLimiter) Wait(ctx context.Context, channelID string, ratePerSecond int) (time.Duration, error) { + if l == nil || l.Redis == nil { + return 0, fmt.Errorf("gateway rate limiter Redis client is required") + } + channelID = strings.TrimSpace(channelID) + if channelID == "" { + return 0, fmt.Errorf("gateway rate limiter channelId is required") + } + if ratePerSecond <= 0 { + ratePerSecond = defaultRatePerSecond + } + delayMicros, err := reserveScript.Run( + ctx, + l.Redis, + []string{l.key(channelID), l.configKey(channelID)}, + ratePerSecond, + ).Int64() + if err != nil { + return 0, fmt.Errorf("reserve gateway channel rate slot: %w", err) + } + delay := time.Duration(delayMicros) * time.Microsecond + if delay <= 0 { + return 0, nil + } + timer := time.NewTimer(delay) + defer timer.Stop() + select { + case <-ctx.Done(): + return delay, ctx.Err() + case <-timer.C: + return delay, nil + } +} + +func (l *RedisLimiter) configKey(channelID string) string { + return l.key("config:" + channelID) +} + +func (l *RedisLimiter) key(channelID string) string { + prefix := l.KeyPrefix + if prefix == "" { + prefix = defaultKeyPrefix + } + return prefix + channelID +} diff --git a/gateway/internal/ratelimit/limiter_test.go b/gateway/internal/ratelimit/limiter_test.go new file mode 100644 index 0000000..5b96d30 --- /dev/null +++ b/gateway/internal/ratelimit/limiter_test.go @@ -0,0 +1,68 @@ +package ratelimit + +import ( + "context" + "testing" + "time" + + "github.com/alicebob/miniredis/v2" + "github.com/redis/go-redis/v9" +) + +func TestRedisLimiterSharesOneChannelBudget(t *testing.T) { + mr := miniredis.RunT(t) + client := redis.NewClient(&redis.Options{Addr: mr.Addr()}) + limiter := NewWithClient(client) + + first, err := limiter.Wait(context.Background(), "channel-a", 20) + if err != nil { + t.Fatalf("first wait: %v", err) + } + second, err := limiter.Wait(context.Background(), "channel-a", 20) + if err != nil { + t.Fatalf("second wait: %v", err) + } + if first != 0 { + t.Fatalf("first delay = %v, want 0", first) + } + if second < 40*time.Millisecond { + t.Fatalf("second delay = %v, want a shared channel delay", second) + } +} + +func TestRedisLimiterUsesIndependentChannelBudgets(t *testing.T) { + mr := miniredis.RunT(t) + client := redis.NewClient(&redis.Options{Addr: mr.Addr()}) + limiter := NewWithClient(client) + + if _, err := limiter.Wait(context.Background(), "channel-a", 1); err != nil { + t.Fatalf("channel a wait: %v", err) + } + delay, err := limiter.Wait(context.Background(), "channel-b", 1) + if err != nil { + t.Fatalf("channel b wait: %v", err) + } + if delay != 0 { + t.Fatalf("channel b delay = %v, want 0", delay) + } +} + +func TestRedisLimiterConfiguredRateIsAuthoritativeUpperBound(t *testing.T) { + mr := miniredis.RunT(t) + client := redis.NewClient(&redis.Options{Addr: mr.Addr()}) + limiter := NewWithClient(client) + + if err := limiter.Configure(context.Background(), "channel-a", 10); err != nil { + t.Fatalf("configure: %v", err) + } + if _, err := limiter.Wait(context.Background(), "channel-a", 1000); err != nil { + t.Fatalf("first wait: %v", err) + } + delay, err := limiter.Wait(context.Background(), "channel-a", 1000) + if err != nil { + t.Fatalf("second wait: %v", err) + } + if delay < 90*time.Millisecond { + t.Fatalf("delay = %v, want configured 10 TPS ceiling", delay) + } +} diff --git a/gateway/internal/submitworker/worker.go b/gateway/internal/submitworker/worker.go index a6f5264..d82973b 100644 --- a/gateway/internal/submitworker/worker.go +++ b/gateway/internal/submitworker/worker.go @@ -9,9 +9,11 @@ import ( "log" "net/http" "strings" + "sync" "time" "cmpp-platform/gateway/internal/queue" + "cmpp-platform/gateway/internal/ratelimit" "cmpp-platform/gateway/internal/upstream" "github.com/redis/go-redis/v9" @@ -28,6 +30,7 @@ const ( type Worker struct { Redis *redis.Client Upstream *upstream.Manager + Limiter ratelimit.Limiter Submit func(context.Context, queue.SubmitCommand) (queue.SubmitResult, error) ReportDeadLetter func(context.Context, DeadLetterEvent) error Stream string @@ -64,7 +67,7 @@ func New(redisURL string, manager *upstream.Manager) (*Worker, error) { if err != nil { return nil, err } - return &Worker{Redis: client, Upstream: manager}, nil + return &Worker{Redis: client, Upstream: manager, Limiter: ratelimit.NewWithClient(client)}, nil } func (w *Worker) Run(ctx context.Context) error { @@ -163,12 +166,18 @@ func (w *Worker) recoverPending(ctx context.Context) error { } func (w *Worker) processMessages(ctx context.Context, messages []redis.XMessage) error { + var group sync.WaitGroup for _, message := range messages { - if err := w.processMessage(ctx, message); err != nil { - w.logf("gateway submit worker message %s failed: %v", message.ID, err) - continue - } + message := message + group.Add(1) + go func() { + defer group.Done() + if err := w.processMessage(ctx, message); err != nil { + w.logf("gateway submit worker message %s failed: %v", message.ID, err) + } + }() } + group.Wait() return nil } @@ -178,6 +187,9 @@ func (w *Worker) processMessage(ctx context.Context, message redis.XMessage) err return w.deadLetterMalformedMessage(ctx, message, err) } if err := w.handleCommand(ctx, command); err != nil { + if ctx.Err() != nil { + return ctx.Err() + } attempts, attemptsErr := w.incrementFailureAttempt(ctx, message.ID) if attemptsErr != nil { w.logf("gateway submit worker increment failure %s failed: %v", message.ID, attemptsErr) @@ -194,6 +206,11 @@ func (w *Worker) processMessage(ctx context.Context, message redis.XMessage) err } func (w *Worker) handleCommand(ctx context.Context, command queue.SubmitCommand) error { + if w.Limiter != nil { + if _, err := w.Limiter.Wait(ctx, command.ChannelID, command.Route.RateLimitPerSecond); err != nil { + return err + } + } submit := w.Submit if submit == nil { if w.Upstream == nil { diff --git a/gateway/internal/submitworker/worker_test.go b/gateway/internal/submitworker/worker_test.go index c9f3f28..f240be3 100644 --- a/gateway/internal/submitworker/worker_test.go +++ b/gateway/internal/submitworker/worker_test.go @@ -3,12 +3,26 @@ package submitworker import ( "context" "testing" + "time" "cmpp-platform/gateway/internal/queue" "github.com/alicebob/miniredis/v2" "github.com/redis/go-redis/v9" ) +type recordingLimiter struct { + channelID string + rate int + called bool +} + +func (l *recordingLimiter) Wait(_ context.Context, channelID string, rate int) (time.Duration, error) { + l.called = true + l.channelID = channelID + l.rate = rate + return 0, nil +} + func TestCommandFromStreamValuesParsesSubmitCommand(t *testing.T) { command, err := CommandFromStreamValues(map[string]interface{}{ "messageType": "SubmitCommand", @@ -70,20 +84,22 @@ func TestCommandFromStreamValuesRejectsMissingData(t *testing.T) { func TestHandleMessageUsesInjectedSubmit(t *testing.T) { var got queue.SubmitCommand + limiter := &recordingLimiter{} worker := &Worker{ + Limiter: limiter, Submit: func(_ context.Context, command queue.SubmitCommand) (queue.SubmitResult, error) { got = command return queue.SubmitResult{SubmitStatus: "accepted"}, nil }, } if err := worker.handleCommand(context.Background(), queue.SubmitCommand{ - Envelope: queue.Envelope{MessageID: "msg-worker-0002"}, + Envelope: queue.Envelope{MessageID: "msg-worker-0002", ChannelID: "channel-1"}, SubmitID: "submit-2", PhoneNumber: "13800138000", Content: "hello", Upstream: queue.UpstreamConfig{GatewayHost: "127.0.0.1", GatewayPort: 17890, Account: "account-a", PasswordCipher: "secret", CMPPVersion: "3.0"}, CMPP: queue.CMPP{ServiceID: "SMS", SrcID: "10690000", RegisteredDelivery: 1, MsgFmt: 8}, - Route: queue.Route{ChannelCode: "CMPP-A"}, + Route: queue.Route{ChannelCode: "CMPP-A", RateLimitPerSecond: 320}, Retry: queue.Retry{Attempt: 0, MaxAttempts: 1}, ApplicationID: "app-1", TenantID: "tenant-1", @@ -93,6 +109,9 @@ func TestHandleMessageUsesInjectedSubmit(t *testing.T) { if got.MessageID != "msg-worker-0002" || got.SubmitID != "submit-2" { t.Fatalf("unexpected command: %+v", got) } + if !limiter.called || limiter.channelID != "channel-1" || limiter.rate != 320 { + t.Fatalf("unexpected limiter call: %+v", limiter) + } } func TestMinIdleDefaultsToThirtySeconds(t *testing.T) { @@ -102,6 +121,52 @@ func TestMinIdleDefaultsToThirtySeconds(t *testing.T) { } } +func TestProcessMessagesDoesNotLetOneChannelBlockAnother(t *testing.T) { + mr := miniredis.RunT(t) + client := redis.NewClient(&redis.Options{Addr: mr.Addr()}) + startedA := make(chan struct{}) + startedB := make(chan struct{}) + releaseA := make(chan struct{}) + worker := &Worker{ + Redis: client, + Submit: func(_ context.Context, command queue.SubmitCommand) (queue.SubmitResult, error) { + switch command.ChannelID { + case "channel-a": + close(startedA) + <-releaseA + case "channel-b": + close(startedB) + } + return queue.SubmitResult{SubmitStatus: "accepted"}, nil + }, + } + messages := []redis.XMessage{ + {ID: "1-0", Values: submitCommandValues("message-a", "channel-a")}, + {ID: "2-0", Values: submitCommandValues("message-b", "channel-b")}, + } + done := make(chan struct{}) + go func() { + _ = worker.processMessages(context.Background(), messages) + close(done) + }() + select { + case <-startedA: + case <-time.After(time.Second): + t.Fatal("channel-a did not start") + } + select { + case <-startedB: + case <-time.After(200 * time.Millisecond): + t.Fatal("channel-b was blocked by channel-a") + } + close(releaseA) + select { + case <-done: + case <-time.After(time.Second): + t.Fatal("message batch did not complete") + } +} + func TestProcessMessageDeadLettersAfterMaxFailures(t *testing.T) { mr := miniredis.RunT(t) client := redis.NewClient(&redis.Options{Addr: mr.Addr()}) @@ -178,3 +243,17 @@ func TestProcessMessageDeadLettersAfterMaxFailures(t *testing.T) { t.Fatalf("failure attempt key was not cleared") } } + +func submitCommandValues(messageID string, channelID string) map[string]interface{} { + return map[string]interface{}{ + "data": `{ + "schemaVersion":"v1","messageType":"SubmitCommand","traceId":"trace-1", + "messageId":"` + messageID + `","channelId":"` + channelID + `","submitId":"submit-1", + "tenantId":"tenant-1","applicationId":"app-1","phoneNumber":"13800138000","content":"hello", + "route":{"channelCode":"CMPP-A","rateLimitPerSecond":100}, + "cmpp":{"serviceId":"SMS","srcId":"10690000","registeredDelivery":1,"msgFmt":8}, + "upstream":{"gatewayHost":"127.0.0.1","gatewayPort":17890,"account":"sp","passwordCipher":"secret","cmppVersion":"3.0"}, + "retry":{"attempt":0,"maxAttempts":1} + }`, + } +} diff --git a/src/api/adminApi.ts b/src/api/adminApi.ts index ca6dda3..0dc6f1a 100644 --- a/src/api/adminApi.ts +++ b/src/api/adminApi.ts @@ -103,6 +103,33 @@ async function requestBlob(path: string, options: RequestOptions = {}): Promise< return response.blob(); } +async function requestForm(path: string, form: FormData, reauthenticationAttempted = false): Promise { + const headers = new Headers(); + const session = readSession(); + if (session && hasRecentUserActivity()) headers.set('x-session-activity', 'user'); + const response = await fetch(`/api${path}`, { method: 'POST', headers, body: form, credentials: 'same-origin' }); + if (response.status === 401 && session) { + const body = await readErrorBody(response.clone()); + if (body.code === 'SESSION_LOCKED') { + dispatchSessionEvent('locked', { message: body.message }); + throw new Error(typeof body.message === 'string' ? body.message : '由于长时间未操作,会话已安全锁定'); + } + clearSession(); + dispatchSessionEvent('logout', { code: body.code, message: body.message }); + window.location.assign(session.portal === 'admin' ? '/admin/login' : '/client/login'); + throw new Error('登录会话已失效,请重新登录'); + } + if (response.status === 403 && session && !reauthenticationAttempted) { + const body = await readErrorBody(response.clone()); + if (body.code === 'RECENT_AUTHENTICATION_REQUIRED') { + await requestReauthentication(); + return requestForm(path, form, true); + } + } + if (!response.ok) throw new Error(await readErrorMessage(response)); + return response.json() as Promise; +} + export type AdminChannel = { id: string; code: string; @@ -556,7 +583,6 @@ export type ChannelGroupItem = DictionaryItem & { priority: number; weight?: number; isBackup?: boolean; - rateLimitPerSecond?: number | null; channel?: AdminChannel; }; @@ -570,9 +596,53 @@ export type ChannelReportField = DictionaryItem & { required: boolean; description?: string | null; sortOrder?: number; + exportName?: string | null; + columnWidth?: number; + imageWidth?: number; + imageHeight?: number; + defaultValue?: string | null; + transform?: string | null; drainageField?: DictionaryItem | null; }; +export type ReportMaterialPendingItem = { + id: string; + reportType: 'signature' | 'drainage'; + signatureId: string; + drainageItemId?: string | null; + materialVersion: number; + changedAt: string; + name: string; + detail?: string | null; + signatureName?: string; + tenant?: TenantOption; + application?: ClientSmsApplication | null; +}; + +export type ReportImportMapping = { + sourceHeader: string; + sourceHeaderPath?: string; + sourceColumnIndex: number; + targetFieldCode: string; + targetKind: 'signatureName' | 'purpose' | 'siteName' | 'url' | 'remark' | 'dynamic'; + fieldType: 'string' | 'image' | 'file'; + required?: boolean; + transform?: string; + sortOrder?: number; +}; + +export type ReportImportProfile = { + id: string; + name: string; + reportType: 'signature' | 'drainage'; + tenantId?: string | null; + applicationId?: string | null; + sheetName?: string | null; + headerRowCount: number; + dataStartRow: number; + columns: ReportImportMapping[]; +}; + export type ApplicationReportField = { id: string; code: string; @@ -962,6 +1032,51 @@ export type GatewayDownstreamRecoveryStatus = { application?: EnterpriseApplication | null; }; +export type GatewaySubmitException = { + id: string; + streamMessageId: string; + tenantId?: string | null; + applicationId?: string | null; + channelId?: string | null; + traceId?: string | null; + messageId?: string | null; + submitId?: string | null; + status: 'pending' | 'requeueing' | 'requeued' | 'resolved' | string; + failureCode: string; + failureMessage: string; + attempts: number; + maxAttempts: number; + commandPayload?: Record | null; + rawPayloadAvailable?: boolean; + messageState?: { + status: string; + submitStatus?: string | null; + receiptStatus?: string | null; + phoneNumber: string; + content: string; + } | null; + manualRetryCount: number; + lastRetryStreamId?: string | null; + lastRetriedAt?: string | null; + resolvedAt?: string | null; + resolvedStatus?: string | null; + createdAt: string; + updatedAt: string; + tenant?: Pick | null; + application?: Pick | null; + channel?: Pick | null; +}; + +export type GatewaySubmitExceptionResponse = PagedResponse & { + summary: { + pending: number; + requeueing: number; + requeued: number; + resolved: number; + oldestPendingAt?: string | null; + }; +}; + export type DownstreamRecoveryStatusResponse = PagedResponse & { summary: { total: number; @@ -1163,6 +1278,25 @@ export const adminApi = { listChannelReportFields: (channelId?: string) => request(withQuery('/admin/channel-report-fields', { channelId })), createChannelReportField: (body: Record) => request('/admin/channel-report-fields', { method: 'POST', body: JSON.stringify(body) }), + replaceChannelReportFields: (channelId: string, reportType: 'signature' | 'drainage', fields: Array>) => + request(`/admin/channels/${channelId}/report-fields/${reportType}`, { method: 'PUT', body: JSON.stringify({ fields }) }), + listPendingReportMaterials: (query: { reportType?: 'signature' | 'drainage'; tenantId?: string; applicationId?: string } = {}) => + request(withQuery('/admin/report-materials/pending', query)), + listReportImportProfiles: (reportType?: 'signature' | 'drainage') => + request(withQuery('/admin/report-materials/import-profiles', { reportType })), + saveReportImportProfile: (body: Omit & { id?: string }) => + request('/admin/report-materials/import-profiles', { method: 'POST', body: JSON.stringify(body) }), + analyzeReportMaterialImport: (file: File, body: { tenantId: string; applicationId?: string; reportType: 'signature' | 'drainage'; sheetName?: string; headerRowCount?: number; dataStartRow?: number; profileId?: string }) => { + const form = new FormData(); + form.set('file', file); + Object.entries(body).forEach(([key, value]) => { if (value !== undefined) form.set(key, String(value)); }); + return requestForm & { id: string; columns: Array<{ sourceColumnIndex: number; columnLetter: string; sourceHeader: string; sourceHeaderPath: string; imageCount: number }>; rows: Array>; suggestedMappings: ReportImportMapping[] }>('/admin/report-materials/imports/analyze', form); + }, + commitReportMaterialImport: (id: string, body: { mappings: ReportImportMapping[]; profile?: Omit & { id?: string } }) => + request>(`/admin/report-materials/imports/${id}/commit`, { method: 'PUT', body: JSON.stringify(body) }), + listReportMaterialBatches: () => request>>('/admin/report-materials/batches'), + createReportMaterialBatch: (body: { createdById?: string; items: Array<{ reportType: 'signature' | 'drainage'; signatureId: string; drainageItemId?: string }> }) => + request>('/admin/report-materials/batches', { method: 'POST', body: JSON.stringify(body) }), listReportTasks: (query: { tenantId?: string; status?: string; channelId?: string; reportType?: 'signature' | 'drainage' } = {}) => request(withQuery('/admin/report-tasks', query)), createReportTask: (body: { tenantId: string; signatureId: string; channelId: string; reportType?: 'signature' | 'drainage'; drainageItemId?: string; createdById?: string }) => request('/admin/report-tasks/generate', { method: 'POST', body: JSON.stringify(body) }), @@ -1188,6 +1322,10 @@ export const adminApi = { claimUplinkMatchCandidate: (uplinkMessageId: string, body: { candidateId: string; operatorId?: string }) => request(`/admin/operations/uplink-messages/${uplinkMessageId}/claim`, { method: 'POST', body: JSON.stringify(body) }), listMonitor: (query: { tenantId?: string; channelId?: string } = {}) => request>(withQuery('/admin/operations/monitor', query)), + listGatewaySubmitExceptions: (query: { tenantId?: string; applicationId?: string; channelId?: string; status?: string; keyword?: string; page?: number; pageSize?: number } = {}) => + request(withQuery('/admin/operations/gateway-submit-dead-letters', query)), + requeueGatewaySubmitException: (id: string, body: { confirmedNotSubmitted: boolean; reason: string }) => + request(`/admin/operations/gateway-submit-dead-letters/${id}/requeue`, { method: 'POST', body: JSON.stringify(body) }), listStatistics: (query: { tenantId?: string; groupBy?: string } = {}) => request>>(withQuery('/admin/operations/statistics', query)), getDownstreamDeliveryDashboard: (query: { tenantId?: string; applicationId?: string; deliveryType?: string; createdAtFrom?: string; createdAtTo?: string } = {}) => request(withQuery('/admin/operations/downstream-deliveries/dashboard', query)), diff --git a/src/apps/admin/AdminChannelReportPage.tsx b/src/apps/admin/AdminChannelReportPage.tsx index bfb00d3..3b05787 100644 --- a/src/apps/admin/AdminChannelReportPage.tsx +++ b/src/apps/admin/AdminChannelReportPage.tsx @@ -4,8 +4,9 @@ import { useNavigate, useParams } from 'react-router-dom'; import { adminApi, type AdminChannel, type ChannelReportField, type ClientSmsSignature, type DictionaryItem, type ReportRecord, type ReportTask } from '@/api/adminApi'; import { Breadcrumb, Button, Input, Modal, Select, Tag, Textarea } from '@/components/ui'; import { formatDateTime } from '@/utils/dateTime'; +import { ReportFieldMappingModal } from './ReportFieldMappingModal'; -type ReportType = 'signature' | 'drainage' | 'both'; +type ReportType = 'signature' | 'drainage'; type DrainageItem = Record & { id?: string; url?: string; siteName?: string; submittedAt?: string; remark?: string }; const statusMeta: Record = { @@ -22,8 +23,6 @@ const statusMeta: Record = { string: '字符串', image: '图片', file: '文件' }; - function asRecord(value: unknown): Record { return value && typeof value === 'object' && !Array.isArray(value) ? value as Record : {}; } @@ -81,9 +80,6 @@ export function AdminChannelReportPage() { const [nextStatus, setNextStatus] = useState('approved'); const [statusReason, setStatusReason] = useState(''); const [configType, setConfigType] = useState(); - const [drainageFieldId, setDrainageFieldId] = useState(''); - const [required, setRequired] = useState(false); - const [description, setDescription] = useState(''); const [error, setError] = useState(''); function loadData() { @@ -119,11 +115,10 @@ export function AdminChannelReportPage() { return records.find((record) => record.taskId === taskId && record.action === action); } - function createField() { - if (!configType || !drainageFieldId) return; - adminApi.createChannelReportField({ channelId, drainageFieldId, reportType: configType, required, description, status: 'active' }) - .then(() => { setConfigType(undefined); setDrainageFieldId(''); setRequired(false); setDescription(''); loadData(); }) - .catch((failure: Error) => setError(failure.message || '报备字段保存失败')); + async function saveFieldMapping(nextFields: Parameters[2]) { + if (!configType) return; + await adminApi.replaceChannelReportFields(channelId, configType, nextFields); + loadData(); } function saveTaskStatus() { @@ -180,13 +175,7 @@ export function AdminChannelReportPage() { {detail ? setDetail(undefined)} /> : null} } onClose={() => setStatusTask(undefined)} open={Boolean(statusTask)} title="修改当前通道报备状态">