import { normalizeBusinessFileAccess } from './business-file-contract.js' import { normalizeGenealogyPathId } from './genealogy-contract.js' import { normalizePersonDocument, normalizePersonDocumentPayload, normalizePersonDocumentResource, normalizePersonDocumentResourcePayload, normalizePersonDocuments } from './person-document-contract.js' import { assertPlainPayload, normalizeResourcePathId } from './request-normalizers.js' import { contentAccessHeader, normalizeContentAccessGrant, normalizeContentPasswordPayload } from './protected-content-contract.js' import { requestStrict } from './request-client.js' const writePersonDocument = (url, method, data, requestOptions) => { return requestStrict({ url, method, data }, { requestController: requestOptions.requestController ?? null }) } const deletePersonDocumentEndpoint = async (url, requestOptions) => { await requestStrict({ url, method: 'DELETE' }, { requireData: false, requestController: requestOptions.requestController ?? null }) return null } const normalizeDocumentIds = (genealogyId, documentId) => ({ genealogyId: normalizeGenealogyPathId(genealogyId), documentId: normalizeResourcePathId(documentId, '重要证件标识') }) export const personDocumentApi = { async getPersonDocuments(genealogyId, query = {}, requestOptions = {}) { const normalizedGenealogyId = normalizeGenealogyPathId(genealogyId) assertPlainPayload(query, new Set(['lineagePersonId']), '重要证件查询') const queryParams = {} if (query.lineagePersonId !== undefined && query.lineagePersonId !== null && query.lineagePersonId !== '') { queryParams.lineagePersonId = normalizeResourcePathId(query.lineagePersonId, '世系人物标识') } const personDocuments = await requestStrict({ url: `/genealogy/app/genealogies/${normalizedGenealogyId}/person-documents`, method: 'GET', data: queryParams }, { requestController: requestOptions.requestController ?? null }) return normalizePersonDocuments(personDocuments, normalizedGenealogyId) }, async getPersonDocument(genealogyId, documentId, accessToken = '', requestOptions = {}) { const normalizedIds = normalizeDocumentIds(genealogyId, documentId) const personDocument = await requestStrict({ url: `/genealogy/app/genealogies/${normalizedIds.genealogyId}/person-documents/${normalizedIds.documentId}`, method: 'GET', header: contentAccessHeader(accessToken) }, { requestController: requestOptions.requestController ?? null }) return normalizePersonDocument(personDocument, normalizedIds.genealogyId) }, async createPersonDocument(genealogyId, payload, requestOptions = {}) { const normalizedGenealogyId = normalizeGenealogyPathId(genealogyId) const personDocument = await writePersonDocument( `/genealogy/app/genealogies/${normalizedGenealogyId}/person-documents`, 'POST', normalizePersonDocumentPayload(payload), requestOptions ) return normalizePersonDocument(personDocument, normalizedGenealogyId) }, async updatePersonDocument(genealogyId, documentId, payload, requestOptions = {}) { const normalizedIds = normalizeDocumentIds(genealogyId, documentId) const personDocument = await writePersonDocument( `/genealogy/app/genealogies/${normalizedIds.genealogyId}/person-documents/${normalizedIds.documentId}`, 'PUT', normalizePersonDocumentPayload(payload), requestOptions ) return normalizePersonDocument(personDocument, normalizedIds.genealogyId) }, async deletePersonDocument(genealogyId, documentId, requestOptions = {}) { const normalizedIds = normalizeDocumentIds(genealogyId, documentId) return deletePersonDocumentEndpoint( `/genealogy/app/genealogies/${normalizedIds.genealogyId}/person-documents/${normalizedIds.documentId}`, requestOptions ) }, async addPersonDocumentResource(genealogyId, documentId, payload, requestOptions = {}) { const normalizedIds = normalizeDocumentIds(genealogyId, documentId) const documentResource = await writePersonDocument( `/genealogy/app/genealogies/${normalizedIds.genealogyId}/person-documents/${normalizedIds.documentId}/resources`, 'POST', normalizePersonDocumentResourcePayload(payload), requestOptions ) return normalizePersonDocumentResource(documentResource) }, async updatePersonDocumentResource(genealogyId, documentId, resourceId, payload, requestOptions = {}) { const normalizedIds = normalizeDocumentIds(genealogyId, documentId) const normalizedResourceId = normalizeResourcePathId(resourceId, '证件文件标识') const documentResource = await writePersonDocument( `/genealogy/app/genealogies/${normalizedIds.genealogyId}/person-documents/${normalizedIds.documentId}/resources/${normalizedResourceId}`, 'PUT', normalizePersonDocumentResourcePayload(payload), requestOptions ) return normalizePersonDocumentResource(documentResource) }, async deletePersonDocumentResource(genealogyId, documentId, resourceId, requestOptions = {}) { const normalizedIds = normalizeDocumentIds(genealogyId, documentId) const normalizedResourceId = normalizeResourcePathId(resourceId, '证件文件标识') return deletePersonDocumentEndpoint( `/genealogy/app/genealogies/${normalizedIds.genealogyId}/person-documents/${normalizedIds.documentId}/resources/${normalizedResourceId}`, requestOptions ) }, async getPersonDocumentResourceAccess(genealogyId, documentId, resourceId, accessToken = '', requestOptions = {}) { const normalizedIds = normalizeDocumentIds(genealogyId, documentId) const normalizedResourceId = normalizeResourcePathId(resourceId, '证件文件标识') const fileAccess = await requestStrict({ url: `/genealogy/app/genealogies/${normalizedIds.genealogyId}/person-documents/${normalizedIds.documentId}/resources/${normalizedResourceId}/access`, method: 'GET', header: contentAccessHeader(accessToken) }, { requestController: requestOptions.requestController ?? null }) return normalizeBusinessFileAccess( fileAccess, '证件文件访问对象', 'PERSON_DOCUMENT_FILE_ACCESS_INVALID', { required: true } ) }, async setPersonDocumentPassword(genealogyId, documentId, password, requestOptions = {}) { const normalizedIds = normalizeDocumentIds(genealogyId, documentId) await requestStrict({ url: `/genealogy/app/genealogies/${normalizedIds.genealogyId}/person-documents/${normalizedIds.documentId}/content-protection`, method: 'PUT', data: normalizeContentPasswordPayload(password) }, { requireData: false, requestController: requestOptions.requestController ?? null }) return null }, async unlockPersonDocument(genealogyId, documentId, password, requestOptions = {}) { const normalizedIds = normalizeDocumentIds(genealogyId, documentId) const accessGrant = await writePersonDocument( `/genealogy/app/genealogies/${normalizedIds.genealogyId}/person-documents/${normalizedIds.documentId}/content-unlock`, 'POST', normalizeContentPasswordPayload(password), requestOptions ) return normalizeContentAccessGrant(accessGrant) }, async disablePersonDocumentPassword(genealogyId, documentId, requestOptions = {}) { const normalizedIds = normalizeDocumentIds(genealogyId, documentId) return deletePersonDocumentEndpoint( `/genealogy/app/genealogies/${normalizedIds.genealogyId}/person-documents/${normalizedIds.documentId}/content-protection`, requestOptions ) } }