feat: migrate app routes and business modules
This commit is contained in:
@@ -0,0 +1,169 @@
|
||||
import { listPreviewPublicGenealogies } from '@/data/preview/genealogies.js'
|
||||
import { hasRemoteConfig } from '@/utils/runtime-config.js'
|
||||
import {
|
||||
normalizeAppGenealogy,
|
||||
normalizeCreatedGenealogy,
|
||||
normalizeGenealogyCreatePayload,
|
||||
normalizeGenealogyOrderIds,
|
||||
normalizeGenealogyPathId,
|
||||
normalizeGenealogyQuota,
|
||||
normalizeGenealogySettings,
|
||||
normalizeGenealogyUpdatePayload,
|
||||
normalizeMyGenealogies,
|
||||
normalizePublicGenealogies,
|
||||
projectPreviewPublicGenealogies
|
||||
} from './genealogy-contract.js'
|
||||
import {
|
||||
createRequestError,
|
||||
requestStrict
|
||||
} from './request-client.js'
|
||||
|
||||
const requireRemoteGenealogy = (label, operation) => {
|
||||
if (hasRemoteConfig()) return
|
||||
throw createRequestError(
|
||||
`${label}${operation}需要真实服务,当前本地预览不会伪造结果`,
|
||||
operation === '读取' ? 'REMOTE_READ_REQUIRED' : 'REMOTE_WRITE_REQUIRED'
|
||||
)
|
||||
}
|
||||
|
||||
const writeGenealogyState = (genealogyId, action, label, requestOptions) => {
|
||||
requireRemoteGenealogy(label, '写入')
|
||||
return requestStrict({
|
||||
url: `/genealogy/app/genealogies/${genealogyId}/${action}`,
|
||||
method: 'PUT'
|
||||
}, {
|
||||
requestController: requestOptions.requestController ?? null
|
||||
})
|
||||
}
|
||||
|
||||
export const genealogyApi = {
|
||||
async getPublicGenealogies(requestOptions = {}) {
|
||||
if (!hasRemoteConfig()) {
|
||||
return projectPreviewPublicGenealogies(listPreviewPublicGenealogies())
|
||||
}
|
||||
const publicGenealogyRows = await requestStrict({
|
||||
url: '/genealogy/app/genealogies/public',
|
||||
method: 'GET'
|
||||
}, {
|
||||
requestController: requestOptions.requestController ?? null
|
||||
})
|
||||
return normalizePublicGenealogies(publicGenealogyRows)
|
||||
},
|
||||
|
||||
async getMyGenealogies(requestOptions = {}) {
|
||||
requireRemoteGenealogy('我的家谱', '读取')
|
||||
const genealogyRows = await requestStrict({
|
||||
url: '/genealogy/app/genealogies/mine',
|
||||
method: 'GET'
|
||||
}, {
|
||||
requestController: requestOptions.requestController ?? null
|
||||
})
|
||||
return normalizeMyGenealogies(genealogyRows)
|
||||
},
|
||||
|
||||
async saveMyGenealogyOrder(genealogyIds, requestOptions = {}) {
|
||||
const orderedIds = normalizeGenealogyOrderIds(genealogyIds)
|
||||
requireRemoteGenealogy('保存家谱排序', '写入')
|
||||
const confirmedGenealogies = normalizeMyGenealogies(await requestStrict({
|
||||
url: '/genealogy/app/genealogies/mine/order',
|
||||
method: 'PUT',
|
||||
data: { genealogyIds: orderedIds }
|
||||
}, {
|
||||
requestController: requestOptions.requestController ?? null
|
||||
}))
|
||||
if (
|
||||
confirmedGenealogies.length !== orderedIds.length ||
|
||||
confirmedGenealogies.some((genealogy, index) => genealogy.id !== orderedIds[index])
|
||||
) {
|
||||
throw createRequestError('家谱排序回读与提交顺序不一致', 'GENEALOGY_ORDER_RESPONSE_INVALID')
|
||||
}
|
||||
return confirmedGenealogies
|
||||
},
|
||||
|
||||
async getGenealogyQuota(requestOptions = {}) {
|
||||
requireRemoteGenealogy('家谱配额', '读取')
|
||||
const quota = await requestStrict({
|
||||
url: '/genealogy/app/genealogies/quota',
|
||||
method: 'GET'
|
||||
}, {
|
||||
requestController: requestOptions.requestController ?? null
|
||||
})
|
||||
return normalizeGenealogyQuota(quota)
|
||||
},
|
||||
|
||||
async createGenealogy(payload, requestOptions = {}) {
|
||||
const genealogyDraft = normalizeGenealogyCreatePayload(payload)
|
||||
requireRemoteGenealogy('创建家谱', '写入')
|
||||
const createdGenealogy = await requestStrict({
|
||||
url: '/genealogy/app/genealogies',
|
||||
method: 'POST',
|
||||
data: genealogyDraft
|
||||
}, {
|
||||
requestController: requestOptions.requestController ?? null
|
||||
})
|
||||
return normalizeCreatedGenealogy(createdGenealogy)
|
||||
},
|
||||
|
||||
async getGenealogySettings(genealogyId, requestOptions = {}) {
|
||||
const normalizedGenealogyId = normalizeGenealogyPathId(genealogyId)
|
||||
requireRemoteGenealogy('家谱设置', '读取')
|
||||
const genealogySettings = await requestStrict({
|
||||
url: `/genealogy/app/genealogies/${normalizedGenealogyId}`,
|
||||
method: 'GET'
|
||||
}, {
|
||||
requestController: requestOptions.requestController ?? null
|
||||
})
|
||||
return normalizeGenealogySettings(genealogySettings, normalizedGenealogyId)
|
||||
},
|
||||
|
||||
async updateGenealogy(genealogyId, payload, requestOptions = {}) {
|
||||
const normalizedGenealogyId = normalizeGenealogyPathId(genealogyId)
|
||||
const genealogyChanges = normalizeGenealogyUpdatePayload(payload)
|
||||
requireRemoteGenealogy('更新家谱', '写入')
|
||||
return requestStrict({
|
||||
url: `/genealogy/app/genealogies/${normalizedGenealogyId}`,
|
||||
method: 'PUT',
|
||||
data: genealogyChanges
|
||||
}, {
|
||||
requireData: false,
|
||||
requestController: requestOptions.requestController ?? null
|
||||
})
|
||||
},
|
||||
|
||||
async archiveGenealogy(genealogyId, requestOptions = {}) {
|
||||
const normalizedGenealogyId = normalizeGenealogyPathId(genealogyId)
|
||||
const genealogy = await writeGenealogyState(
|
||||
normalizedGenealogyId,
|
||||
'archive',
|
||||
'家谱归档',
|
||||
requestOptions
|
||||
)
|
||||
return normalizeAppGenealogy(genealogy)
|
||||
},
|
||||
|
||||
async restoreGenealogy(genealogyId, requestOptions = {}) {
|
||||
const normalizedGenealogyId = normalizeGenealogyPathId(genealogyId)
|
||||
const genealogy = await writeGenealogyState(
|
||||
normalizedGenealogyId,
|
||||
'restore',
|
||||
'家谱恢复',
|
||||
requestOptions
|
||||
)
|
||||
return normalizeAppGenealogy(genealogy)
|
||||
},
|
||||
|
||||
async getOverview(genealogyId, requestOptions = {}) {
|
||||
const normalizedGenealogyId = normalizeGenealogyPathId(genealogyId)
|
||||
requireRemoteGenealogy('家谱概览', '读取')
|
||||
const overview = normalizeAppGenealogy(await requestStrict({
|
||||
url: `/genealogy/app/genealogies/${normalizedGenealogyId}/overview`,
|
||||
method: 'GET'
|
||||
}, {
|
||||
requestController: requestOptions.requestController ?? null
|
||||
}))
|
||||
if (overview.id !== normalizedGenealogyId) {
|
||||
throw createRequestError('家谱概览响应标识不匹配', 'GENEALOGY_RESPONSE_INVALID')
|
||||
}
|
||||
return overview
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user