186 lines
6.5 KiB
JavaScript
186 lines
6.5 KiB
JavaScript
import {
|
|
normalizeAppGenealogy,
|
|
normalizeCreatedGenealogy,
|
|
normalizeGenealogyCreatePayload,
|
|
normalizeGenealogyOrderIds,
|
|
normalizeGenealogyPathId,
|
|
normalizeGenealogyDeletionTask,
|
|
normalizeGenealogyPermanentDeletionCapability,
|
|
normalizeGenealogyPermanentDeletionPayload,
|
|
normalizeGenealogyQuota,
|
|
normalizeGenealogySettings,
|
|
normalizeGenealogyUpdatePayload,
|
|
normalizeMyGenealogies,
|
|
normalizePublicGenealogyQuery,
|
|
normalizePublicGenealogies
|
|
} from './genealogy-contract.js'
|
|
import {
|
|
createRequestError,
|
|
requestStrict
|
|
} from './request-client.js'
|
|
|
|
const writeGenealogyState = (genealogyId, action, requestOptions) => {
|
|
return requestStrict({
|
|
url: `/genealogy/app/genealogies/${genealogyId}/${action}`,
|
|
method: 'PUT'
|
|
}, {
|
|
requestController: requestOptions.requestController ?? null
|
|
})
|
|
}
|
|
|
|
export const genealogyApi = {
|
|
async getPublicGenealogies(query = {}, requestOptions = {}) {
|
|
const publicGenealogyRows = await requestStrict({
|
|
url: '/genealogy/app/genealogies/public',
|
|
method: 'GET',
|
|
data: normalizePublicGenealogyQuery(query)
|
|
}, {
|
|
requestController: requestOptions.requestController ?? null
|
|
})
|
|
return normalizePublicGenealogies(publicGenealogyRows)
|
|
},
|
|
|
|
async getMyGenealogies(requestOptions = {}) {
|
|
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)
|
|
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 = {}) {
|
|
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)
|
|
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)
|
|
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)
|
|
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 sendPermanentDeletionCode(genealogyId, requestOptions = {}) {
|
|
const normalizedGenealogyId = normalizeGenealogyPathId(genealogyId)
|
|
await requestStrict({
|
|
url: `/genealogy/app/genealogies/${normalizedGenealogyId}/permanent-deletion/code`,
|
|
method: 'POST'
|
|
}, {
|
|
requireData: false,
|
|
requestController: requestOptions.requestController ?? null
|
|
})
|
|
return null
|
|
},
|
|
|
|
async getPermanentDeletionCapability(genealogyId, requestOptions = {}) {
|
|
const normalizedGenealogyId = normalizeGenealogyPathId(genealogyId)
|
|
const capability = await requestStrict({
|
|
url: `/genealogy/app/genealogies/${normalizedGenealogyId}/permanent-deletion/capability`,
|
|
method: 'GET'
|
|
}, { requestController: requestOptions.requestController ?? null })
|
|
return normalizeGenealogyPermanentDeletionCapability(capability)
|
|
},
|
|
|
|
async deleteGenealogyPermanently(genealogyId, payload, requestOptions = {}) {
|
|
const normalizedGenealogyId = normalizeGenealogyPathId(genealogyId)
|
|
const deletionTask = await requestStrict({
|
|
url: `/genealogy/app/genealogies/${normalizedGenealogyId}/permanent-deletion`,
|
|
method: 'POST',
|
|
data: normalizeGenealogyPermanentDeletionPayload(payload)
|
|
}, { requestController: requestOptions.requestController ?? null })
|
|
return normalizeGenealogyDeletionTask(deletionTask, normalizedGenealogyId)
|
|
},
|
|
|
|
async getOverview(genealogyId, requestOptions = {}) {
|
|
const normalizedGenealogyId = normalizeGenealogyPathId(genealogyId)
|
|
const genealogyRows = await requestStrict({
|
|
url: '/genealogy/app/genealogies/mine',
|
|
method: 'GET'
|
|
}, {
|
|
requestController: requestOptions.requestController ?? null
|
|
})
|
|
if (!Array.isArray(genealogyRows)) {
|
|
throw createRequestError('家谱概览响应不是列表', 'GENEALOGY_OVERVIEW_RESPONSE_INVALID')
|
|
}
|
|
const overview = genealogyRows
|
|
.map((item) => normalizeAppGenealogy(item))
|
|
.find((item) => item.id === normalizedGenealogyId)
|
|
if (!overview) throw createRequestError('当前家谱不在我的家谱中', 'GENEALOGY_OVERVIEW_RESPONSE_INVALID')
|
|
return overview
|
|
}
|
|
}
|