feat(survey): add api/surveys/create endpoint

This commit is contained in:
Aminejv 2021-07-27 16:17:00 +01:00
parent 9c5e2e87dc
commit 00ee322ee0
2 changed files with 50 additions and 2 deletions

View File

@ -42,7 +42,7 @@ const websocketSend = async (type, data) => {
export const hydratePartial = async (
id,
{ viewer, slates, keys, library, subscriptions, following, followers }
{ viewer, slates, keys, library, subscriptions, following, followers, surveys }
) => {
if (!id) return;
@ -102,6 +102,9 @@ export const hydratePartial = async (
const followers = await Data.getFollowersByUserId({ userId: id });
update.followers = followers;
}
if (surveys) {
update.surveys = { onboarding: true };
}
websocketSend("UPDATE", update);
};
@ -149,13 +152,14 @@ export const getById = async ({ id }) => {
// user.library = await Data.getFilesByUserId({ id });
const [slates, keys, subscriptions, following, followers] = (
const [slates, keys, subscriptions, following, followers, surveyResponse] = (
await Promise.allSettled([
Data.getSlatesByUserId({ ownerId: id, includeFiles: true }),
Data.getAPIKeysByUserId({ userId: id }),
Data.getSubscriptionsByUserId({ ownerId: id }),
Data.getFollowingByUserId({ ownerId: id }),
Data.getFollowersByUserId({ userId: id }),
Data.getSurveyByUserId({ ownerId: id }),
])
).map((item) => item.value);
@ -225,6 +229,9 @@ export const getById = async ({ id }) => {
following,
followers,
libraryCids,
surveys: {
onboarding: onboardingSurvey,
},
};
return viewer;

View File

@ -0,0 +1,41 @@
import * as Data from "~/node_common/data";
import * as Strings from "~/common/strings";
import * as ViewerManager from "~/node_common/managers/viewer";
import * as RequestUtilities from "~/node_common/request-utilities";
import * as Environment from "~/node_common/environment";
export default async (req, res) => {
if (!Strings.isEmpty(Environment.ALLOWED_HOST) && req.headers.host !== Environment.ALLOWED_HOST) {
return res.status(403).send({ decorator: "SERVER_CREATE_SURVEY_NOT_ALLOWED", error: true });
}
const { tools, referrals, useCases } = req?.body?.data;
if (!tools || !referrals || !useCases) {
return res.status(403).send({ decorator: "SERVER_CREATE_SURVEY_INVALID_DATA", error: true });
}
const userInfo = await RequestUtilities.checkAuthorizationInternal(req, res);
if (!userInfo) {
return;
}
const { id } = userInfo;
const survey = Data.createSurvey({
ownerId: id,
prevTools: tools,
usecases: useCases,
referrals,
});
if (!survey) {
return res.status(404).send({ decorator: "SERVER_CREATE_SURVEY_FAILED", error: true });
}
if (survey.error) {
return res.status(500).send({ decorator: "SERVER_CREATE_SURVEY_FAILED", error: true });
}
await ViewerManager.hydratePartial(id, { surveys: true });
return res.status(200).send({ decorator: "SERVER_CREATE_SURVEY_SUCCESS" });
};