slate/pages/api/slates/create.js

73 lines
1.6 KiB
JavaScript
Raw Normal View History

2020-07-27 04:51:51 +03:00
import * as MW from "~/node_common/middleware";
import * as Utilities from "~/node_common/utilities";
import * as Data from "~/node_common/data";
import * as Strings from "~/common/strings";
const initCORS = MW.init(MW.CORS);
const initAuth = MW.init(MW.RequireCookieAuthentication);
export default async (req, res) => {
initCORS(req, res);
initAuth(req, res);
const id = Utilities.getIdFromCookie(req);
if (!id) {
2020-08-22 08:19:11 +03:00
return res
.status(500)
.json({ decorator: "SERVER_FIND_USER_CREATE_SLATE", error: true });
2020-07-27 04:51:51 +03:00
}
const user = await Data.getUserById({
id,
});
if (!user) {
2020-07-27 12:10:12 +03:00
return res.status(404).json({
decorator: "SERVER_FIND_USER_CREATE_SLATE_USER_NOT_FOUND",
error: true,
});
2020-07-27 04:51:51 +03:00
}
if (user.error) {
2020-07-27 12:10:12 +03:00
return res.status(500).json({
decorator: "SERVER_FIND_USER_CREATE_SLATE_USER_NOT_FOUND",
error: true,
});
2020-07-27 04:51:51 +03:00
}
const slatename = Strings.createSlug(req.body.data.name);
const found = await Data.getSlateByName({ slatename });
if (found) {
2020-08-22 08:19:11 +03:00
return res
.status(500)
.json({ decorator: "SERVER_EXISTING_SLATE", error: true });
2020-07-27 04:51:51 +03:00
}
const slate = await Data.createSlate({
slatename: Strings.createSlug(req.body.data.name),
2020-07-27 04:51:51 +03:00
data: {
2020-07-27 12:10:12 +03:00
public: true,
2020-07-27 04:51:51 +03:00
ownerId: id,
name: req.body.data.name,
2020-08-22 08:19:11 +03:00
body: "A slate.",
2020-07-27 04:51:51 +03:00
objects: [],
},
});
if (!slate) {
2020-08-22 08:19:11 +03:00
return res
.status(500)
.json({ decorator: "SERVER_CREATE_SLATE", error: true });
2020-07-27 04:51:51 +03:00
}
if (slate.error) {
2020-08-22 08:19:11 +03:00
return res
.status(500)
.json({ decorator: "SERVER_CREATE_SLATE", error: true });
2020-07-27 04:51:51 +03:00
}
return res.status(200).json({ decorator: "SERVER_CREATE_SLATE", slate });
};