后端迁移Agiso店铺配置服务
This commit is contained in:
+42
-17
@@ -1,5 +1,3 @@
|
|||||||
// @ts-nocheck
|
|
||||||
|
|
||||||
import fs from 'node:fs'
|
import fs from 'node:fs'
|
||||||
import path from 'node:path'
|
import path from 'node:path'
|
||||||
|
|
||||||
@@ -20,11 +18,37 @@ const AGISO_SHOP_CONFIG_KEYS = [
|
|||||||
'tradeDetailTimeoutMs',
|
'tradeDetailTimeoutMs',
|
||||||
]
|
]
|
||||||
|
|
||||||
export function getAgisoShopsFilePath() {
|
type JsonObject = Record<string, unknown>
|
||||||
|
|
||||||
|
export type AgisoMessagingDefaults = {
|
||||||
|
messageTemplate?: string
|
||||||
|
autoDeliveryMessageTemplate?: string
|
||||||
|
}
|
||||||
|
|
||||||
|
export type AgisoShopConfig = {
|
||||||
|
enabled?: boolean
|
||||||
|
shopName?: string
|
||||||
|
accessToken?: string
|
||||||
|
messageTemplate?: string
|
||||||
|
autoDeliveryMessageTemplate?: string
|
||||||
|
appSecret?: string
|
||||||
|
apiVersion?: string
|
||||||
|
sendMessageEndpoint?: string
|
||||||
|
tradeDetailEndpoint?: string
|
||||||
|
tradeDetailApiVersion?: string
|
||||||
|
tradeDetailTimeoutMs?: string
|
||||||
|
}
|
||||||
|
|
||||||
|
export type AgisoMessagingConfigDocument = {
|
||||||
|
defaults: AgisoMessagingDefaults
|
||||||
|
shops: Record<string, AgisoShopConfig>
|
||||||
|
}
|
||||||
|
|
||||||
|
export function getAgisoShopsFilePath(): string {
|
||||||
return AGISO_SHOPS_FILE_PATH
|
return AGISO_SHOPS_FILE_PATH
|
||||||
}
|
}
|
||||||
|
|
||||||
export function getAgisoShopConfigMap() {
|
export function getAgisoShopConfigMap(): Record<string, AgisoShopConfig> {
|
||||||
const envConfig = normalizeAgisoShopConfigMap(runtimeConfig.platforms?.agiso?.messaging?.shops || {})
|
const envConfig = normalizeAgisoShopConfigMap(runtimeConfig.platforms?.agiso?.messaging?.shops || {})
|
||||||
const fileConfig = loadAgisoMessagingConfigDocumentFromFile()
|
const fileConfig = loadAgisoMessagingConfigDocumentFromFile()
|
||||||
|
|
||||||
@@ -34,7 +58,7 @@ export function getAgisoShopConfigMap() {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
export function getAgisoShopConfig(shopId) {
|
export function getAgisoShopConfig(shopId: unknown): AgisoShopConfig | null {
|
||||||
const normalizedShopId = String(shopId || '').trim()
|
const normalizedShopId = String(shopId || '').trim()
|
||||||
if (!normalizedShopId) {
|
if (!normalizedShopId) {
|
||||||
return null
|
return null
|
||||||
@@ -43,18 +67,18 @@ export function getAgisoShopConfig(shopId) {
|
|||||||
return getAgisoShopConfigMap()[normalizedShopId] || null
|
return getAgisoShopConfigMap()[normalizedShopId] || null
|
||||||
}
|
}
|
||||||
|
|
||||||
export function getAgisoMessagingDefaults() {
|
export function getAgisoMessagingDefaults(): AgisoMessagingDefaults {
|
||||||
return loadAgisoMessagingConfigDocumentFromFile().defaults
|
return loadAgisoMessagingConfigDocumentFromFile().defaults
|
||||||
}
|
}
|
||||||
|
|
||||||
export function saveAgisoMessagingConfig(rawValue) {
|
export function saveAgisoMessagingConfig(rawValue: unknown): AgisoMessagingConfigDocument {
|
||||||
const normalized = normalizeAgisoMessagingConfigDocument(rawValue)
|
const normalized = normalizeAgisoMessagingConfigDocument(rawValue)
|
||||||
fs.mkdirSync(path.dirname(AGISO_SHOPS_FILE_PATH), { recursive: true })
|
fs.mkdirSync(path.dirname(AGISO_SHOPS_FILE_PATH), { recursive: true })
|
||||||
fs.writeFileSync(AGISO_SHOPS_FILE_PATH, `${JSON.stringify(normalized, null, 2)}\n`, 'utf8')
|
fs.writeFileSync(AGISO_SHOPS_FILE_PATH, `${JSON.stringify(normalized, null, 2)}\n`, 'utf8')
|
||||||
return normalized
|
return normalized
|
||||||
}
|
}
|
||||||
|
|
||||||
function loadAgisoMessagingConfigDocumentFromFile() {
|
function loadAgisoMessagingConfigDocumentFromFile(): AgisoMessagingConfigDocument {
|
||||||
if (!fs.existsSync(AGISO_SHOPS_FILE_PATH)) {
|
if (!fs.existsSync(AGISO_SHOPS_FILE_PATH)) {
|
||||||
return {
|
return {
|
||||||
defaults: {},
|
defaults: {},
|
||||||
@@ -74,16 +98,17 @@ function loadAgisoMessagingConfigDocumentFromFile() {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
function normalizeAgisoShopConfigMap(rawValue) {
|
function normalizeAgisoShopConfigMap(rawValue: unknown): Record<string, AgisoShopConfig> {
|
||||||
const output = {}
|
const output: Record<string, AgisoShopConfig> = {}
|
||||||
|
|
||||||
for (const [shopId, config] of Object.entries(rawValue || {})) {
|
const entries = isPlainObject(rawValue) ? Object.entries(rawValue) : []
|
||||||
|
for (const [shopId, config] of entries) {
|
||||||
const normalizedShopId = String(shopId || '').trim()
|
const normalizedShopId = String(shopId || '').trim()
|
||||||
if (!normalizedShopId || !isPlainObject(config)) {
|
if (!normalizedShopId || !isPlainObject(config)) {
|
||||||
continue
|
continue
|
||||||
}
|
}
|
||||||
|
|
||||||
const next = {}
|
const next: AgisoShopConfig = {}
|
||||||
const enabled = normalizeBooleanLike(config.enabled)
|
const enabled = normalizeBooleanLike(config.enabled)
|
||||||
if (enabled !== null) {
|
if (enabled !== null) {
|
||||||
next.enabled = enabled
|
next.enabled = enabled
|
||||||
@@ -102,8 +127,8 @@ function normalizeAgisoShopConfigMap(rawValue) {
|
|||||||
return output
|
return output
|
||||||
}
|
}
|
||||||
|
|
||||||
function normalizeAgisoMessagingDefaults(rawValue) {
|
function normalizeAgisoMessagingDefaults(rawValue: unknown): AgisoMessagingDefaults {
|
||||||
const output = {}
|
const output: AgisoMessagingDefaults = {}
|
||||||
|
|
||||||
if (!isPlainObject(rawValue)) {
|
if (!isPlainObject(rawValue)) {
|
||||||
return output
|
return output
|
||||||
@@ -119,7 +144,7 @@ function normalizeAgisoMessagingDefaults(rawValue) {
|
|||||||
return output
|
return output
|
||||||
}
|
}
|
||||||
|
|
||||||
function normalizeAgisoMessagingConfigDocument(rawValue) {
|
function normalizeAgisoMessagingConfigDocument(rawValue: unknown): AgisoMessagingConfigDocument {
|
||||||
const normalizedValue = isPlainObject(rawValue) ? rawValue : {}
|
const normalizedValue = isPlainObject(rawValue) ? rawValue : {}
|
||||||
const hasStructuredShape = Object.prototype.hasOwnProperty.call(normalizedValue, 'defaults')
|
const hasStructuredShape = Object.prototype.hasOwnProperty.call(normalizedValue, 'defaults')
|
||||||
|| Object.prototype.hasOwnProperty.call(normalizedValue, 'shops')
|
|| Object.prototype.hasOwnProperty.call(normalizedValue, 'shops')
|
||||||
@@ -130,7 +155,7 @@ function normalizeAgisoMessagingConfigDocument(rawValue) {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
function normalizeBooleanLike(value) {
|
function normalizeBooleanLike(value: unknown): boolean | null {
|
||||||
if (typeof value === 'boolean') {
|
if (typeof value === 'boolean') {
|
||||||
return value
|
return value
|
||||||
}
|
}
|
||||||
@@ -151,6 +176,6 @@ function normalizeBooleanLike(value) {
|
|||||||
return null
|
return null
|
||||||
}
|
}
|
||||||
|
|
||||||
function isPlainObject(value) {
|
function isPlainObject(value: unknown): value is JsonObject {
|
||||||
return Object.prototype.toString.call(value) === '[object Object]'
|
return Object.prototype.toString.call(value) === '[object Object]'
|
||||||
}
|
}
|
||||||
@@ -357,6 +357,14 @@
|
|||||||
- `npm run typecheck`
|
- `npm run typecheck`
|
||||||
- `npm run build`
|
- `npm run build`
|
||||||
- `npm test` 共 136 个用例通过
|
- `npm test` 共 136 个用例通过
|
||||||
|
60. Agiso 店铺配置服务迁移到 `.ts`:
|
||||||
|
- `src/services/platforms/agiso/shop-config-service.ts`
|
||||||
|
61. Agiso 店铺配置文档、默认消息模板、店铺 token/API 配置、配置 map 归一化结果已显式类型化
|
||||||
|
62. Docker 内验证通过:
|
||||||
|
- Agiso 配置 / 自动发货 / 消息 / 订单详情相关 5 个测试文件共 26 个用例通过
|
||||||
|
- `npm run typecheck`
|
||||||
|
- `npm run build`
|
||||||
|
- `npm test` 共 136 个用例通过
|
||||||
|
|
||||||
## 下一步建议
|
## 下一步建议
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user