2021-07-13 22:08:56 +03:00
|
|
|
import * as Utilities from "~/node_common/utilities";
|
|
|
|
import * as Data from "~/node_common/data";
|
|
|
|
import * as Strings from "~/common/strings";
|
|
|
|
import * as ViewerManager from "~/node_common/managers/viewer";
|
|
|
|
import * as Monitor from "~/node_common/monitor";
|
2021-07-14 01:52:21 +03:00
|
|
|
import * as RequestUtilities from "~/node_common/request-utilities";
|
2021-11-12 01:10:53 +03:00
|
|
|
import * as Conversions from "~/common/conversions";
|
|
|
|
|
|
|
|
import SearchManager from "~/node_common/managers/search";
|
2021-07-13 22:08:56 +03:00
|
|
|
|
|
|
|
export default async (req, res) => {
|
2021-07-14 01:52:21 +03:00
|
|
|
const userInfo = await RequestUtilities.checkAuthorizationExternal(req, res);
|
|
|
|
if (!userInfo) return;
|
|
|
|
const { id, key, user } = userInfo;
|
2021-07-13 22:08:56 +03:00
|
|
|
|
|
|
|
if (!req.body?.data?.name) {
|
|
|
|
return res.status(500).send({
|
|
|
|
decorator: "MUST_PROVIDE_DATA",
|
|
|
|
error: true,
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
const slatename = Strings.createSlug(req.body.data.name);
|
|
|
|
|
|
|
|
const existingSlate = await Data.getSlateByName({
|
|
|
|
slatename,
|
|
|
|
ownerId: user.id,
|
|
|
|
});
|
|
|
|
|
|
|
|
if (existingSlate) {
|
|
|
|
return res.status(500).send({ decorator: "EXISTING_SLATE_NAME", error: true });
|
|
|
|
}
|
|
|
|
|
|
|
|
const slate = await Data.createSlate({
|
|
|
|
ownerId: id,
|
2021-11-12 01:10:53 +03:00
|
|
|
slatename: Strings.createSlug(req.body.data.data.name),
|
2021-07-13 22:08:56 +03:00
|
|
|
isPublic: req.body.data.isPublic,
|
2021-11-12 01:42:04 +03:00
|
|
|
name: req.body.data.name,
|
|
|
|
body: req.body.data.body,
|
2021-07-13 22:08:56 +03:00
|
|
|
});
|
|
|
|
|
|
|
|
if (!slate || slate.error) {
|
|
|
|
return res.status(500).send({ decorator: "CREATE_COLLECTION_FAILED", error: true });
|
|
|
|
}
|
|
|
|
|
|
|
|
ViewerManager.hydratePartial(id, { slates: true });
|
|
|
|
|
2021-10-19 00:06:27 +03:00
|
|
|
SearchManager.indexSlate(slate);
|
2021-07-13 22:08:56 +03:00
|
|
|
|
|
|
|
Monitor.createSlate({ user, slate });
|
|
|
|
|
2021-11-12 01:10:53 +03:00
|
|
|
let reformattedSlate = Conversions.convertToV2Slate(slate);
|
|
|
|
|
|
|
|
return res.status(200).send({ decorator: "CREATE_COLLECTION", slate: reformattedSlate });
|
2021-07-13 22:08:56 +03:00
|
|
|
};
|