From 20ba244ce817e0352eef52752f1b4340c01f61be Mon Sep 17 00:00:00 2001 From: Aminejv Date: Thu, 11 Nov 2021 15:13:35 +0100 Subject: [PATCH] feat(survey): change surveys table name to onboarding --- common/actions.js | 4 +-- common/navigation-data.js | 4 +-- components/core/ApplicationLayout.js | 2 +- node_common/data/index.js | 12 ++++----- ...{create-survey.js => create-onboarding.js} | 6 ++--- node_common/data/methods/delete-user-by-id.js | 2 +- ...ser-id.js => get-onboarding-by-user-id.js} | 7 +++--- node_common/managers/viewer.js | 25 +++++++++++++------ pages/api/{surveys => onboarding}/create.js | 20 ++++++++------- scenes/SceneSurvey.js | 2 +- scripts/seed-database.js | 6 +++-- 11 files changed, 52 insertions(+), 38 deletions(-) rename node_common/data/methods/{create-survey.js => create-onboarding.js} (84%) rename node_common/data/methods/{get-survey-by-user-id.js => get-onboarding-by-user-id.js} (69%) rename pages/api/{surveys => onboarding}/create.js (54%) diff --git a/common/actions.js b/common/actions.js index 93baacf8..24600e33 100644 --- a/common/actions.js +++ b/common/actions.js @@ -521,8 +521,8 @@ export const getUserVersion = async (data) => { }); }; -export const createSurvey = async (data) => { - return await returnJSON(`/api/surveys/create`, { +export const createOnboarding = async (data) => { + return await returnJSON(`/api/onboarding/create`, { ...DEFAULT_OPTIONS, body: JSON.stringify({ data }), }); diff --git a/common/navigation-data.js b/common/navigation-data.js index 1dc75eba..0ba3e56d 100644 --- a/common/navigation-data.js +++ b/common/navigation-data.js @@ -11,7 +11,7 @@ export const getById = (id, viewer) => { return { ...errorPage }; } - if (viewer?.survey.onboarding === false) { + if (viewer?.onboarding.survey === false) { return { ...surveyPage }; } @@ -28,7 +28,7 @@ export const getById = (id, viewer) => { export const getByHref = (href, viewer) => { let pathname; - if (viewer?.surveys?.onboarding === false) { + if (viewer?.onboarding?.survey === false) { return { page: { ...surveyPage } }; } if (href) { diff --git a/components/core/ApplicationLayout.js b/components/core/ApplicationLayout.js index 84136702..a4862336 100644 --- a/components/core/ApplicationLayout.js +++ b/components/core/ApplicationLayout.js @@ -204,7 +204,7 @@ export default class ApplicationLayout extends React.Component { ); } const isHeaderInView = - this.props.page?.id === "NAV_SIGN_IN" || this.props.viewer?.surveys?.onboarding === false; + this.props.page?.id === "NAV_SIGN_IN" || this.props.viewer?.onboarding?.survey === false; return ( diff --git a/node_common/data/index.js b/node_common/data/index.js index 797f8100..bacccc04 100644 --- a/node_common/data/index.js +++ b/node_common/data/index.js @@ -95,9 +95,9 @@ import getVerificationBySid from "~/node_common/data/methods/get-verification-by import pruneVerifications from "~/node_common/data/methods/prune-verifications"; // NOTE(amine): -// Surveys postgres queries -import createSurvey from "~/node_common/data/methods/create-survey"; -import getSurveyByUserId from "~/node_common/data/methods/get-survey-by-user-id"; +// Onboarding postgres queries +import createOnboarding from "~/node_common/data/methods/create-onboarding"; +import getOnboardingByUserId from "~/node_common/data/methods/get-onboarding-by-user-id"; // NOTE(jim): // one-offs @@ -190,7 +190,7 @@ export { updateTwitterToken, // NOTE(martina): Deals createDeal, - // NOTE(amine): Surveys - createSurvey, - getSurveyByUserId, + // NOTE(amine): Onboarding + createOnboarding, + getOnboardingByUserId, }; diff --git a/node_common/data/methods/create-survey.js b/node_common/data/methods/create-onboarding.js similarity index 84% rename from node_common/data/methods/create-survey.js rename to node_common/data/methods/create-onboarding.js index b9579915..0d8743d5 100644 --- a/node_common/data/methods/create-survey.js +++ b/node_common/data/methods/create-onboarding.js @@ -2,7 +2,7 @@ import { runQuery } from "~/node_common/data/utilities"; export default async ({ userId, prevTools, usecases, referrals }) => { return await runQuery({ - label: "CREATE_SURVEY", + label: "CREATE_ONBOARDING", queryFn: async (DB) => { let query = await DB.insert({ userId, @@ -10,7 +10,7 @@ export default async ({ userId, prevTools, usecases, referrals }) => { usecases, referrals, }) - .into("surveys") + .into("onboarding") .returning("*"); if (!query) { @@ -24,7 +24,7 @@ export default async ({ userId, prevTools, usecases, referrals }) => { errorFn: async () => { return { error: true, - decorator: "CREATE_SURVEY", + decorator: "CREATE_ONBOARDING", }; }, }); diff --git a/node_common/data/methods/delete-user-by-id.js b/node_common/data/methods/delete-user-by-id.js index c3fd41eb..5c0980d5 100644 --- a/node_common/data/methods/delete-user-by-id.js +++ b/node_common/data/methods/delete-user-by-id.js @@ -37,7 +37,7 @@ export default async ({ id }) => { const usage = await DB.from("usage").where({ userId: id }).del(); - const surveys = await DB.from("surveys").where({ userId: id }).del(); + const onboarding = await DB.from("onboarding").where({ userId: id }).del(); const data = await DB.from("users").where({ id }).del().returning("*"); diff --git a/node_common/data/methods/get-survey-by-user-id.js b/node_common/data/methods/get-onboarding-by-user-id.js similarity index 69% rename from node_common/data/methods/get-survey-by-user-id.js rename to node_common/data/methods/get-onboarding-by-user-id.js index 09c03bad..2b4122f1 100644 --- a/node_common/data/methods/get-survey-by-user-id.js +++ b/node_common/data/methods/get-onboarding-by-user-id.js @@ -2,9 +2,10 @@ import { runQuery } from "~/node_common/data/utilities"; export default async ({ userId }) => { return await runQuery({ - label: "GET_SURVEY_BY_USER_ID", + label: "GET_ONBOARDING_BY_USER_ID", queryFn: async (DB) => { - let query = await DB.select("*").from("surveys").where({ userId }); + let query = await DB.select("*").from("onboarding").where({ userId }); + console.log(query); if (!query || query.error) { return null; @@ -18,7 +19,7 @@ export default async ({ userId }) => { errorFn: async () => { return { error: true, - decorator: "GET_SURVEY_BY_USER_ID", + decorator: "GET_ONBOARDING_BY_USER_ID", }; }, }); diff --git a/node_common/managers/viewer.js b/node_common/managers/viewer.js index 7e1a3f8e..b732708d 100644 --- a/node_common/managers/viewer.js +++ b/node_common/managers/viewer.js @@ -42,7 +42,7 @@ const websocketSend = async (type, data) => { export const hydratePartial = async ( id, - { viewer, slates, keys, library, subscriptions, following, followers, surveys } + { viewer, slates, keys, library, subscriptions, following, followers, onboarding } ) => { if (!id) return; @@ -102,8 +102,13 @@ export const hydratePartial = async ( const followers = await Data.getFollowersByUserId({ userId: id }); update.followers = followers; } - if (surveys) { - update.surveys = { onboarding: true }; + if (onboarding) { + const onboarding = await Data.getOnboardingByUserId({ userId: id }); + update.onboarding = { + upload: onboarding?.upload, + tags: onboarding?.tags, + survey: !!onboarding?.userId, + }; } websocketSend("UPDATE", update); @@ -152,20 +157,26 @@ export const getById = async ({ id }) => { // user.library = await Data.getFilesByUserId({ id }); - const [slates, keys, subscriptions, following, followers, surveyResponse] = ( + const [slates, keys, subscriptions, following, followers, onboardingResponse] = ( 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({ userId: id }), + Data.getOnboardingByUserId({ userId: id }), ]) ).map((item) => item.value); const libraryCids = user?.library?.reduce((acc, file) => ({ ...acc, [file.cid]: true }), {}) || {}; + const onboarding = { + upload: onboardingResponse?.upload, + tags: onboardingResponse?.tags, + survey: !!onboardingResponse?.userId, + }; + let cids = {}; let bytes = 0; let imageBytes = 0; @@ -229,9 +240,7 @@ export const getById = async ({ id }) => { following, followers, libraryCids, - surveys: { - onboarding: onboardingSurvey, - }, + onboarding, }; return viewer; diff --git a/pages/api/surveys/create.js b/pages/api/onboarding/create.js similarity index 54% rename from pages/api/surveys/create.js rename to pages/api/onboarding/create.js index 771248f0..07e12e3a 100644 --- a/pages/api/surveys/create.js +++ b/pages/api/onboarding/create.js @@ -6,12 +6,14 @@ 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 }); + return res.status(403).send({ decorator: "SERVER_CREATE_ONBOARDING_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 }); + return res + .status(403) + .send({ decorator: "SERVER_CREATE_ONBOARDING_INVALID_DATA", error: true }); } const userInfo = await RequestUtilities.checkAuthorizationInternal(req, res); @@ -20,22 +22,22 @@ export default async (req, res) => { } const { id } = userInfo; - const survey = Data.createSurvey({ + const onboarding = Data.createOnboarding({ userId: id, prevTools: tools, usecases: useCases, referrals, }); - if (!survey) { - return res.status(404).send({ decorator: "SERVER_CREATE_SURVEY_FAILED", error: true }); + if (!onboarding) { + return res.status(404).send({ decorator: "SERVER_CREATE_ONBOARDING_FAILED", error: true }); } - if (survey.error) { - return res.status(500).send({ decorator: "SERVER_CREATE_SURVEY_FAILED", error: true }); + if (onboarding.error) { + return res.status(500).send({ decorator: "SERVER_CREATE_ONBOARDING_FAILED", error: true }); } - await ViewerManager.hydratePartial(id, { surveys: true }); + await ViewerManager.hydratePartial(id, { onboarding: true }); - return res.status(200).send({ decorator: "SERVER_CREATE_SURVEY_SUCCESS" }); + return res.status(200).send({ decorator: "SERVER_CREATE_ONBOARDING_SUCCESS" }); }; diff --git a/scenes/SceneSurvey.js b/scenes/SceneSurvey.js index 478689e2..d7ad41f4 100644 --- a/scenes/SceneSurvey.js +++ b/scenes/SceneSurvey.js @@ -128,7 +128,7 @@ function SceneSurvey() { .map((item) => REFERRAL_OPTIONS[item] || item) .join(","); // call endpoint - const response = await Actions.createSurvey(surveyResults.current); + const response = await Actions.createOnboarding(surveyResults.current); if (Events.hasError(response)) { return; } diff --git a/scripts/seed-database.js b/scripts/seed-database.js index b7cbe7fc..a0a6c81a 100644 --- a/scripts/seed-database.js +++ b/scripts/seed-database.js @@ -181,12 +181,14 @@ const createTwitterTokensTable = db.schema.createTable("twitterTokens", function table.string("verified").nullable(); }); -const createSurveysTable = db.schema.createTable("surveys", function (table) { +const createOnboardingTable = db.schema.createTable("onboarding", function (table) { table.uuid("id").primary().unique().notNullable().defaultTo(db.raw("uuid_generate_v4()")); table.uuid("userId").references("id").inTable("users"); table.string("prevTools").notNullable(); table.string("usecases").notNullable(); table.string("referrals").notNullable(); + table.boolean("upload").defaultTo(false); + table.boolean("tags").defaultTo(false); }); // -------------------------- @@ -208,7 +210,7 @@ Promise.all([ createGlobalTable, createUsageTable, createTwitterTokensTable, - createSurveysTable, + createOnboardingTable, ]); Logging.log(`FINISHED: seed-database.js`);