@fontoxml/fontoxml-development-tools
Version:
Development tools for Fonto.
86 lines (75 loc) • 2.41 kB
JavaScript
import asyncRouteWithLockCleanupHandler from '../asyncRouteWithLockCleanupHandler.js';
import correlationIdRepository from './correlationIdRepository.js';
/**
* This endpoint is a copy of configureReviewAnnotationGetPostRouteHandler.js.
*
* Please keep both endpoints in sync.
*
* But note the differences:
* - For Output there is never a editSessionToken in the request body or query parameters.
* - Output can receive an optional Fonto-Correlation-Id.
*/
/** @typedef {import('../../src/getAppConfig.js').DevCmsConfig} DevCmsConfig */
/**
* @param {DevCmsConfig} config
*/
export default function configureOutputReviewStatePostRouteHandler(config) {
return asyncRouteWithLockCleanupHandler(async (_acquireLock, req, res) => {
const { documentIds, filterFormValueByName, navigatorId } = req.body;
const editSessionToken = correlationIdRepository.getEditSessionTokenForRequest(req);
if (!documentIds) {
res.status(400).send('Missing a "documentIds" in the request.');
return;
}
const timeoutSet = await config.debugConfiguration.getTimeoutConfigForRoute(
req.cms,
editSessionToken,
'/output/review/state',
'POST',
);
if (timeoutSet) {
// Do not handle the request, but let it time out.
return new Promise(() => {});
}
const debugStatusCode =
await config.debugConfiguration.getDebugConfigurationForAnnotationState(
req.cms,
editSessionToken,
);
if (debugStatusCode) {
res.status(debugStatusCode).end();
return;
}
const currentSession = req.getFontoSession(editSessionToken);
const annotationIdentifiers =
await req.repositories.annotation.getAnnotationIdentifiers(
req.cms,
currentSession,
documentIds,
filterFormValueByName,
navigatorId,
);
const results = documentIds.map((documentId) => {
const exist = req.cms.existsSync(documentId, editSessionToken);
const result = {
documentId,
status: exist ? 200 : 404,
annotationIdentifiers: [],
};
if (exist) {
result.annotationIdentifiers = annotationIdentifiers
.filter((candidate) => candidate.documentId === documentId)
.map((annotationIdentifier) => {
return {
id: annotationIdentifier.id,
revisionId: annotationIdentifier.revisionId,
};
});
}
return result;
});
res.set('content-type', 'application/json; charset=utf-8').json({
results,
});
});
}