@fontoxml/fontoxml-development-tools
Version:
Development tools for Fonto.
88 lines (82 loc) • 2.14 kB
JavaScript
import EARLY_RETURN_SYMBOL from './review-annotations/EarlyReturnSymbol.js';
import httpStatusCodeHelpers from './review-annotations/httpStatusCodeHelpers.js';
export default function configureReviewAnnotationReplyPostRouteHandler(
annotationDatabase,
debugConfiguration
) {
return (req, res) => {
const {
context: { editSessionToken },
annotationIdentifier,
reply,
} = req.body;
if (!reply) {
res.status(400).send('Missing a "reply" field in the request.');
return;
}
if (!annotationIdentifier) {
res.status(400).send(
'Missing an "annotationIdentifier" field in the request.'
);
return;
}
debugConfiguration
.getTimeoutConfigForRoute(
req.cms,
editSessionToken,
'/review/annotation/reply',
'POST'
)
.then((timeoutSet) => {
if (timeoutSet) {
return Promise.reject(EARLY_RETURN_SYMBOL);
}
return debugConfiguration.isDebuggingEnabled(
req.cms,
editSessionToken
);
})
.then((isDebuggingEnabled) => {
if (isDebuggingEnabled) {
const stringToVerify = reply.metadata.reply;
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.addReply(
req.cms,
currentSession,
reply,
annotationIdentifier
);
})
.then((addReplyResult) => {
const httpStatusCode =
httpStatusCodeHelpers.mapAnnotationResultStatusToHttpStatusCode(
addReplyResult.status,
true
);
res.status(httpStatusCode)
.set('content-type', 'application/json; charset=utf-8')
.json({
revisionId: addReplyResult.revisionId,
reply: addReplyResult.annotation,
});
})
.catch((error) => {
if (error !== EARLY_RETURN_SYMBOL) {
res.status(500).send(error);
}
});
};
}