@fontoxml/fontoxml-development-tools
Version:
Development tools for Fonto.
78 lines (69 loc) • 2.23 kB
JavaScript
import asyncRouteWithLockCleanupHandler from '../asyncRouteWithLockCleanupHandler.js';
/** @typedef {import('../../src/getAppConfig.js').DevCmsConfig} DevCmsConfig */
/**
* @param {DevCmsConfig} _config
*/
export default function configureDocumentRevisionGetPostRouteHandler(_config) {
return asyncRouteWithLockCleanupHandler(async (acquireLock, req, res) => {
const documentsRevisions = req.body.documentsRevisions;
const correlationIdRepository = req.repositories.correlationId;
// Because these requests do not originate from the editor, but from another server, we
// have no edit session token. We do, however, have an correlationId which we may resolve
// to the editSessionToken used by the corresponding call to the proxy.
// For review this endpoint might also be called from the editor, in which case the
// editSessionToken is provided as a body parameter.
const editSessionToken =
req.body.context && req.body.context.editSessionToken
? req.body.context.editSessionToken
: correlationIdRepository.getEditSessionTokenForRequest(req);
const results = await Promise.all(
documentsRevisions.map(async (documentRevisions) => {
const documentId = documentRevisions.documentId;
const documentExists = req.cms.existsSync(documentId);
if (!documentExists) {
return {
documentId,
status: 404,
};
}
const revisions = await Promise.all(
documentRevisions.revisionIds.map(async (revisionId) => {
// Using a file lock per revision, otherwise we might still cause a race condition.
const fileLock = await acquireLock(documentId);
const content = await req.cms.getFileByRevisionId(
documentId,
revisionId,
editSessionToken,
fileLock,
);
fileLock.release();
if (content === null) {
return {
revisionId,
status: 404,
};
}
return {
revisionId,
status: 200,
revision: {
content,
},
};
}),
);
return {
documentId,
status: 200,
revisions,
};
}),
);
res
.status(200)
.set('content-type', 'application/json; charset=utf-8')
.json({
results,
});
});
}