224 lines
8.8 KiB
JavaScript
224 lines
8.8 KiB
JavaScript
// @ts-check
|
|
|
|
import { Router } from 'express'
|
|
|
|
import {
|
|
getAdminAgisoShopConfigs,
|
|
getAdminFulfillmentBindingConfigs,
|
|
getAdminKhhaoSourceConfig,
|
|
lookupAdminFulfillmentBindingOrder,
|
|
queryAdminKhhaoOrders,
|
|
syncAdminKhhaoOrders,
|
|
testAdminKhhaoLogin,
|
|
updateAdminAgisoShopConfigs,
|
|
updateAdminKhhaoSourceConfig,
|
|
updateAdminFulfillmentBindingConfigs,
|
|
} from '../../services/admin/admin-platform-config-service.js'
|
|
import { createJsonHandler, requireAdminRoles } from './shared.js'
|
|
|
|
/** @typedef {import('../../types/admin-route-inputs.js').AdminAgisoShopConfigRouteBody} AdminAgisoShopConfigRouteBody */
|
|
/** @typedef {import('../../types/admin-route-inputs.js').AdminFulfillmentBindingConfigRouteBody} AdminFulfillmentBindingConfigRouteBody */
|
|
/** @typedef {import('../../types/admin-route-inputs.js').AdminFulfillmentBindingLookupRouteBody} AdminFulfillmentBindingLookupRouteBody */
|
|
/** @typedef {import('../../types/admin-route-inputs.js').AdminKhhaoOrderQueryRouteBody} AdminKhhaoOrderQueryRouteBody */
|
|
/** @typedef {import('../../types/admin-route-inputs.js').AdminKhhaoSourceConfigRouteBody} AdminKhhaoSourceConfigRouteBody */
|
|
/** @typedef {import('../../types/admin-route-inputs.js').AdminKhhaoTestLoginRouteBody} AdminKhhaoTestLoginRouteBody */
|
|
/** @typedef {import('../../types/admin-write-models.js').AdminAgisoShopConfigSaveResponse} AdminAgisoShopConfigSaveResponse */
|
|
|
|
const router = Router()
|
|
|
|
router.use('/platform-config', requireAdminRoles(['admin']))
|
|
|
|
router.get('/platform-config/agiso-shops', createJsonHandler(
|
|
() => getAdminAgisoShopConfigs(),
|
|
{
|
|
successMessage: 'ok',
|
|
errorMessage: '读取 Agiso 店铺配置失败',
|
|
scope: '[admin/platform-config/agiso-shops]',
|
|
},
|
|
))
|
|
|
|
router.post('/platform-config/agiso-shops', createJsonHandler(
|
|
(req) => updateAdminAgisoShopConfigs(/** @type {AdminAgisoShopConfigRouteBody} */ (req.body)),
|
|
{
|
|
successMessage: 'Agiso 店铺配置已保存',
|
|
errorMessage: '保存 Agiso 店铺配置失败',
|
|
scope: '[admin/platform-config/agiso-shops]',
|
|
audit: (_req, data) => {
|
|
const result = /** @type {AdminAgisoShopConfigSaveResponse} */ (data)
|
|
return {
|
|
action: 'platform_shop_config_updated',
|
|
targetType: 'platform_config',
|
|
targetId: 'agiso_shops',
|
|
data: {
|
|
shopCount: result.shops.length,
|
|
filePath: result.filePath,
|
|
},
|
|
}
|
|
},
|
|
},
|
|
))
|
|
|
|
router.get('/platform-config/khhao-source', createJsonHandler(
|
|
() => getAdminKhhaoSourceConfig(),
|
|
{
|
|
successMessage: 'ok',
|
|
errorMessage: '读取 khhao 来源配置失败',
|
|
scope: '[admin/platform-config/khhao-source]',
|
|
},
|
|
))
|
|
|
|
router.post('/platform-config/khhao-source', createJsonHandler(
|
|
(req) => updateAdminKhhaoSourceConfig(/** @type {AdminKhhaoSourceConfigRouteBody} */ (req.body)),
|
|
{
|
|
successMessage: 'khhao 来源配置已保存',
|
|
errorMessage: '保存 khhao 来源配置失败',
|
|
scope: '[admin/platform-config/khhao-source]',
|
|
audit: (_req, data) => {
|
|
const result = /** @type {{ filePath?: string, source?: { username?: string, enabled?: boolean } }} */ (data)
|
|
return {
|
|
action: 'platform_khhao_source_updated',
|
|
targetType: 'platform_config',
|
|
targetId: 'khhao_source',
|
|
data: {
|
|
filePath: String(result.filePath || '').trim(),
|
|
username: String(result.source?.username || '').trim(),
|
|
enabled: Boolean(result.source?.enabled),
|
|
},
|
|
}
|
|
},
|
|
},
|
|
))
|
|
|
|
router.post('/platform-config/khhao/test-login', createJsonHandler(
|
|
(req) => testAdminKhhaoLogin(/** @type {AdminKhhaoTestLoginRouteBody} */ (req.body)),
|
|
{
|
|
successMessage: 'khhao 登录测试成功',
|
|
errorMessage: 'khhao 登录测试失败',
|
|
scope: '[admin/platform-config/khhao/test-login]',
|
|
audit: (req, data) => {
|
|
const body = /** @type {AdminKhhaoTestLoginRouteBody} */ (req.body)
|
|
const result = /** @type {{ baseUrl?: string, username?: string, attempt?: number, session?: { cookieCount?: number } }} */ (data)
|
|
return {
|
|
action: 'platform_khhao_test_login',
|
|
targetType: 'platform_config',
|
|
targetId: String(result.username || body.username || '').trim() || 'khhao',
|
|
data: {
|
|
baseUrl: result.baseUrl || String(body.baseUrl || '').trim(),
|
|
attempt: Number(result.attempt || 0),
|
|
cookieCount: Number(result.session?.cookieCount || 0),
|
|
},
|
|
}
|
|
},
|
|
},
|
|
))
|
|
|
|
router.post('/platform-config/khhao/query-orders', createJsonHandler(
|
|
(req) => queryAdminKhhaoOrders(/** @type {AdminKhhaoOrderQueryRouteBody} */ (req.body)),
|
|
{
|
|
successMessage: 'khhao 订单查询成功',
|
|
errorMessage: 'khhao 订单查询失败',
|
|
scope: '[admin/platform-config/khhao/query-orders]',
|
|
audit: (req, data) => {
|
|
const body = /** @type {AdminKhhaoOrderQueryRouteBody} */ (req.body)
|
|
const result = /** @type {{ page?: number, limit?: number, itemCount?: number, total?: number, baseUrl?: string }} */ (data)
|
|
return {
|
|
action: 'platform_khhao_query_orders',
|
|
targetType: 'platform_config',
|
|
targetId: String(body.username || '').trim() || 'khhao',
|
|
data: {
|
|
baseUrl: result.baseUrl || String(body.baseUrl || '').trim(),
|
|
page: Number(result.page || 1),
|
|
limit: Number(result.limit || 50),
|
|
itemCount: Number(result.itemCount || 0),
|
|
total: Number(result.total || 0),
|
|
},
|
|
}
|
|
},
|
|
},
|
|
))
|
|
|
|
router.post('/platform-config/khhao/sync-orders', createJsonHandler(
|
|
(req) => syncAdminKhhaoOrders(/** @type {AdminKhhaoOrderQueryRouteBody} */ (req.body)),
|
|
{
|
|
successMessage: 'khhao 订单同步成功',
|
|
errorMessage: 'khhao 订单同步失败',
|
|
scope: '[admin/platform-config/khhao/sync-orders]',
|
|
audit: (req, data) => {
|
|
const body = /** @type {AdminKhhaoOrderQueryRouteBody} */ (req.body)
|
|
const result = /** @type {{ page?: number, limit?: number, fetchedCount?: number, syncedCount?: number, ignoredCount?: number, baseUrl?: string }} */ (data)
|
|
return {
|
|
action: 'platform_khhao_sync_orders',
|
|
targetType: 'platform_config',
|
|
targetId: String(body.username || '').trim() || 'khhao',
|
|
data: {
|
|
baseUrl: result.baseUrl || String(body.baseUrl || '').trim(),
|
|
page: Number(result.page || 1),
|
|
limit: Number(result.limit || 50),
|
|
fetchedCount: Number(result.fetchedCount || 0),
|
|
syncedCount: Number(result.syncedCount || 0),
|
|
ignoredCount: Number(result.ignoredCount || 0),
|
|
},
|
|
}
|
|
},
|
|
},
|
|
))
|
|
|
|
router.get('/platform-config/fulfillment-bindings', createJsonHandler(
|
|
() => getAdminFulfillmentBindingConfigs(),
|
|
{
|
|
successMessage: 'ok',
|
|
errorMessage: '读取履约配置失败',
|
|
scope: '[admin/platform-config/fulfillment-bindings]',
|
|
},
|
|
))
|
|
|
|
router.post('/platform-config/fulfillment-bindings/lookup-order', createJsonHandler(
|
|
(req) => lookupAdminFulfillmentBindingOrder(/** @type {AdminFulfillmentBindingLookupRouteBody} */ (req.body)),
|
|
{
|
|
successMessage: '订单商品查询成功',
|
|
errorMessage: '手动查询订单商品失败',
|
|
scope: '[admin/platform-config/fulfillment-bindings/lookup-order]',
|
|
audit: (req, data) => {
|
|
const result = /** @type {{ order?: { provider?: string, platform?: string, shopId?: string, platformOrderId?: string }, items?: unknown[] }} */ (data)
|
|
return {
|
|
action: 'platform_fulfillment_order_lookup',
|
|
targetType: 'platform_config',
|
|
targetId: [
|
|
result.order?.provider || 'unknown',
|
|
result.order?.platform || 'unknown',
|
|
result.order?.shopId || 'unknown',
|
|
result.order?.platformOrderId || 'unknown',
|
|
].join(':'),
|
|
data: {
|
|
itemCount: Array.isArray(result.items) ? result.items.length : 0,
|
|
shopId: result.order?.shopId || String((/** @type {AdminFulfillmentBindingLookupRouteBody} */ (req.body))?.shopId || '').trim(),
|
|
platformOrderId: result.order?.platformOrderId || String((/** @type {AdminFulfillmentBindingLookupRouteBody} */ (req.body))?.platformOrderId || '').trim(),
|
|
},
|
|
}
|
|
},
|
|
},
|
|
))
|
|
|
|
router.post('/platform-config/fulfillment-bindings', createJsonHandler(
|
|
(req) => updateAdminFulfillmentBindingConfigs(/** @type {AdminFulfillmentBindingConfigRouteBody} */ (req.body)),
|
|
{
|
|
successMessage: '履约配置已保存',
|
|
errorMessage: '保存履约配置失败',
|
|
scope: '[admin/platform-config/fulfillment-bindings]',
|
|
audit: (_req, data) => {
|
|
const result = /** @type {{ bindings?: unknown[], filePath?: string }} */ (data)
|
|
return {
|
|
action: 'platform_fulfillment_config_updated',
|
|
targetType: 'platform_config',
|
|
targetId: 'fulfillment_bindings',
|
|
data: {
|
|
bindingCount: Array.isArray(result.bindings) ? result.bindings.length : 0,
|
|
filePath: result.filePath || '',
|
|
},
|
|
}
|
|
},
|
|
},
|
|
))
|
|
|
|
export default router
|