@fontoxml/fontoxml-development-tools
Version:
Development tools for Fonto.
85 lines (77 loc) • 2.1 kB
JavaScript
import EARLY_RETURN_SYMBOL from './review-annotations/EarlyReturnSymbol.js';
import httpStatusCodeHelpers from './review-annotations/httpStatusCodeHelpers.js';
import unmergeableRevisionList from './stores/unmergeableRevisionList.js';
export default function configureReviewAnnotationPostRouteHandler(
annotationDatabase,
debugConfiguration
) {
return (req, res) => {
const {
context: { editSessionToken },
annotation,
} = req.body;
if (!annotation) {
res.status(400).send(
'Missing an "annotation" field in the request.'
);
return;
}
debugConfiguration
.getTimeoutConfigForRoute(
req.cms,
editSessionToken,
'/review/annotation',
'POST'
)
.then((timeoutSet) => {
if (timeoutSet) {
return Promise.reject(EARLY_RETURN_SYMBOL);
}
return debugConfiguration.isDebuggingEnabled(
req.cms,
editSessionToken
);
})
.then((isDebuggingEnabled) => {
if (isDebuggingEnabled) {
const stringToVerify = annotation.metadata.comment;
const foundHttpStatusCode =
httpStatusCodeHelpers.getDebugHttpStatusCode(
stringToVerify,
[400, 500]
);
if (foundHttpStatusCode && !foundHttpStatusCode.onDelete) {
httpStatusCodeHelpers.sendDebugHttpResponse(
res,
foundHttpStatusCode
);
return Promise.reject(EARLY_RETURN_SYMBOL);
}
}
const currentSession = req.getFontoSession(editSessionToken);
return annotationDatabase.addAnnotations(
req.cms,
currentSession,
[annotation]
);
})
.then((annotations) => {
const addedAnnotation = annotations[0];
delete addedAnnotation.replies;
// Revisions that are the created context of an annotation cannot be merged.
unmergeableRevisionList.add(
addedAnnotation.createdDocumentRevisionId
);
res.status(201)
.set('content-type', 'application/json; charset=utf-8')
.json({
annotation: addedAnnotation,
});
})
.catch((error) => {
if (error !== EARLY_RETURN_SYMBOL) {
res.status(500).send(error);
}
});
};
}