@fontoxml/fontoxml-development-tools
Version:
Development tools for Fonto.
152 lines (134 loc) • 4.45 kB
JavaScript
import asyncRouteWithLockCleanupHandler from '../asyncRouteWithLockCleanupHandler.js';
import httpStatusCodeHelpers from './review-annotations/httpStatusCodeHelpers.js';
import mapAnnotationResult from './review-annotations/mapAnnotationResult.js';
import { STATUS_OK } from './review-annotations/Result.js';
/** @typedef {import('../../src/getAppConfig.js').DevCmsConfig} DevCmsConfig */
/**
* @param {DevCmsConfig} config
*/
export default function configureReviewAnnotationPutRouteHandler(config) {
return asyncRouteWithLockCleanupHandler(async (_acquireLock, req, res) => {
const {
context: { editSessionToken },
annotations,
} = req.body;
// TODO: ensure correct/valid queryOrBody.context? is this done automatically by other middleware?
if (!annotations || annotations.length === 0) {
// TODO: validate every feedback item?
res
.status(400)
.send('Missing at least one item in "annotations" in the request.');
return;
}
const timeoutSet = await config.debugConfiguration.getTimeoutConfigForRoute(
req.cms,
editSessionToken,
'/review/annotation',
'PUT',
);
if (timeoutSet) {
// Do not handle the request, but let it time out.
return new Promise(() => {});
}
const currentSession = req.getFontoSession(editSessionToken);
const isDebuggingEnabled =
await config.debugConfiguration.isDebuggingEnabled(
req.cms,
editSessionToken,
);
if (!isDebuggingEnabled) {
const editAnnotationResult =
await req.repositories.annotation.editAnnotations(
req.cms,
currentSession,
annotations,
);
const annotationEditResults = editAnnotationResult.map((item) => {
// Do not include replies in the responses since those are managed in separate Reply endpoints.
if (item.status === STATUS_OK) {
// Do not include replies in the responses since those are managed in separate Reply endpoints.
delete item.annotation.replies;
}
return mapAnnotationResult(item);
});
res.set('content-type', 'application/json; charset=utf-8').json({
annotations: annotationEditResults,
});
return;
}
for (let i = 0, length = annotations.length; i < length; i++) {
const annotation = annotations[i];
const foundHttpStatusCode = httpStatusCodeHelpers.getDebugHttpStatusCode(
annotation.metadata.comment,
[400, 500],
);
if (foundHttpStatusCode && !foundHttpStatusCode.onDelete) {
httpStatusCodeHelpers.sendDebugHttpResponse(res, foundHttpStatusCode);
return;
}
}
const annotationEditResults = [];
for (const annotation of annotations) {
let foundHttpStatusCode;
if (isDebuggingEnabled) {
foundHttpStatusCode = httpStatusCodeHelpers.getDebugHttpStatusCode(
annotation.metadata.comment,
[403, 404, 412],
);
}
if (
!isDebuggingEnabled ||
!foundHttpStatusCode ||
foundHttpStatusCode.onDelete
) {
const editAnnotationResult =
await req.repositories.annotation.editAnnotations(
req.cms,
currentSession,
[annotation],
);
// Do not include replies in the responses since those are managed in separate Reply endpoints.
if (editAnnotationResult.status === STATUS_OK) {
// Do not include replies in the responses since those are managed in separate Reply endpoints.
delete editAnnotationResult.annotation.replies;
}
annotationEditResults.push(mapAnnotationResult(editAnnotationResult));
continue;
}
if (foundHttpStatusCode.statusCode === 403) {
annotationEditResults.push({
annotation: { id: annotation.id },
status: foundHttpStatusCode.statusCode,
});
continue;
}
if (foundHttpStatusCode.statusCode === 404) {
annotationEditResults.push({
annotation: { id: annotation.id },
status: foundHttpStatusCode.statusCode,
});
await req.repositories.annotation.deleteAnnotations(
req.cms,
currentSession,
[annotation],
);
continue;
}
if (foundHttpStatusCode.statusCode === 412) {
annotation.metadata.comment += '-simulated-change';
annotationEditResults.push({
annotation: { id: annotation.id },
status: foundHttpStatusCode.statusCode,
});
await req.repositories.annotation.editAnnotations(
req.cms,
currentSession,
[annotation],
);
}
}
res.set('content-type', 'application/json; charset=utf-8').json({
annotations: annotationEditResults,
});
});
}