slate/pages/api/slates/delete.js

62 lines
1.7 KiB
JavaScript
Raw Normal View History

2020-08-07 05:17:49 +03:00
import * as Utilities from "~/node_common/utilities";
import * as Data from "~/node_common/data";
import * as Strings from "~/common/strings";
2020-10-31 02:12:20 +03:00
import * as ViewerManager from "~/node_common/managers/viewer";
2020-11-10 00:20:38 +03:00
import * as SearchManager from "~/node_common/managers/search";
2020-08-07 05:17:49 +03:00
export default async (req, res) => {
const id = Utilities.getIdFromCookie(req);
if (!id) {
2020-10-29 21:39:40 +03:00
return res.status(500).send({ decorator: "SERVER_DELETE_SLATE", error: true });
2020-08-07 05:17:49 +03:00
}
const user = await Data.getUserById({
id,
});
if (!user) {
return res.status(404).send({
2020-08-07 05:17:49 +03:00
decorator: "SERVER_DELETE_SLATE_USER_NOT_FOUND",
error: true,
});
}
if (user.error) {
return res.status(500).send({
2020-08-07 05:17:49 +03:00
decorator: "SERVER_DELETE_SLATE_USER_NOT_FOUND",
error: true,
});
}
const slate = await Data.getSlateById({ id: req.body.data.id });
if (!slate) {
2020-10-29 21:39:40 +03:00
return res.status(404).send({ decorator: "SERVER_DELETE_SLATE_SLATE_NOT_FOUND", error: true });
2020-08-07 05:17:49 +03:00
}
if (slate.error) {
2020-10-29 21:39:40 +03:00
return res.status(500).send({ decorator: "SERVER_DELETE_SLATE_SLATE_NOT_FOUND", error: true });
2020-08-07 05:17:49 +03:00
}
const deleteResponse = await Data.deleteSlateById({ id: slate.id });
if (!deleteResponse) {
2020-10-29 21:39:40 +03:00
return res.status(404).send({ decorator: "SERVER_DELETE_SLATE", error: true });
2020-08-07 05:17:49 +03:00
}
if (deleteResponse.error) {
2020-10-29 21:39:40 +03:00
return res.status(500).send({ decorator: "SERVER_DELETE_SLATE", error: true });
2020-08-07 05:17:49 +03:00
}
2020-10-31 02:12:20 +03:00
let slates = await Data.getSlatesByUserId({ userId: id });
if (slates) {
ViewerManager.hydratePartialSlates(slates, id);
}
2020-11-10 00:20:38 +03:00
if (slate.data.public) {
SearchManager.updateSlate(slate, "REMOVE");
}
2020-10-29 21:39:40 +03:00
return res.status(200).send({ decorator: "SERVER_DELETE_SLATE", error: false });
2020-08-07 05:17:49 +03:00
};