From 7d34fcd48f2eeda86ca84f5a19cc42a158bd7c4e Mon Sep 17 00:00:00 2001 From: m5r Date: Sun, 1 Aug 2021 22:01:51 +0800 Subject: [PATCH] session thingy for onboarding checks --- app/api/ddd.ts | 17 +++++-- app/core/hooks/use-current-customer.ts | 6 ++- app/core/hooks/use-customer-phone-number.ts | 9 ++-- app/core/hooks/use-require-onboarding.ts | 9 ++-- app/keypad/pages/keypad.tsx | 10 ++--- app/messages/api/queue/fetch-messages.ts | 12 +++-- app/messages/pages/messages.tsx | 2 +- .../components/onboarding-layout.tsx | 11 +---- app/onboarding/mutations/set-phone-number.ts | 44 ++++++++----------- app/onboarding/pages/welcome/step-one.tsx | 22 +++++----- app/onboarding/pages/welcome/step-three.tsx | 6 +-- app/onboarding/pages/welcome/step-two.tsx | 33 +++++--------- app/pages/index.test.tsx | 3 +- package.json | 2 +- types.ts | 1 + 15 files changed, 85 insertions(+), 102 deletions(-) diff --git a/app/api/ddd.ts b/app/api/ddd.ts index 5fe8608..68a5912 100644 --- a/app/api/ddd.ts +++ b/app/api/ddd.ts @@ -4,14 +4,14 @@ import db from "db"; import twilio from "twilio"; export default async function ddd(req: BlitzApiRequest, res: BlitzApiResponse) { - await Promise.all([ + /*await Promise.all([ db.message.deleteMany(), db.phoneCall.deleteMany(), db.phoneNumber.deleteMany(), - db.customer.deleteMany(), ]); + await db.customer.deleteMany(); + await db.user.deleteMany();*/ - await db.user.deleteMany(); const accountSid = "ACa886d066be0832990d1cf43fb1d53362"; const authToken = "8696a59a64b94bb4eba3548ed815953b"; /*const ddd = await twilio(accountSid, authToken) @@ -37,6 +37,17 @@ export default async function ddd(req: BlitzApiRequest, res: BlitzApiResponse) { to: "+33757592025", from: "+33757592722", });*/ + /*const [messagesSent, messagesReceived] = await Promise.all([ + twilio(accountSid, authToken).messages.list({ + from: "+33757592025", + }), + twilio(accountSid, authToken).messages.list({ + to: "+33757592025", + }), + ]); + + console.log("messagesReceived", messagesReceived.sort((a, b) => a.dateCreated.getTime() - b.dateCreated.getTime())); + // console.log("messagesReceived", messagesReceived);*/ res.status(200).end(); } diff --git a/app/core/hooks/use-current-customer.ts b/app/core/hooks/use-current-customer.ts index e9afb6c..e4d9f6a 100644 --- a/app/core/hooks/use-current-customer.ts +++ b/app/core/hooks/use-current-customer.ts @@ -1,11 +1,13 @@ -import { useQuery } from "blitz"; +import { useAuthenticatedSession, useQuery } from "blitz"; import getCurrentCustomer from "../../customers/queries/get-current-customer"; export default function useCurrentCustomer() { + const session = useAuthenticatedSession(); const [customer] = useQuery(getCurrentCustomer, null); return { customer, - hasCompletedOnboarding: Boolean(!!customer && customer.accountSid && customer.authToken), + hasFilledTwilioCredentials: Boolean(customer && customer.accountSid && customer.authToken), + hasCompletedOnboarding: session.hasCompletedOnboarding, }; } diff --git a/app/core/hooks/use-customer-phone-number.ts b/app/core/hooks/use-customer-phone-number.ts index e1d1544..6ded656 100644 --- a/app/core/hooks/use-customer-phone-number.ts +++ b/app/core/hooks/use-customer-phone-number.ts @@ -4,12 +4,9 @@ import getCurrentCustomerPhoneNumber from "../../phone-numbers/queries/get-curre import useCurrentCustomer from "./use-current-customer"; export default function useCustomerPhoneNumber() { - const { hasCompletedOnboarding } = useCurrentCustomer(); - const [customerPhoneNumber] = useQuery( - getCurrentCustomerPhoneNumber, - {}, - { enabled: hasCompletedOnboarding }, - ); + const { customer } = useCurrentCustomer(); + const hasFilledTwilioCredentials = Boolean(customer && customer.accountSid && customer.authToken); + const [customerPhoneNumber] = useQuery(getCurrentCustomerPhoneNumber, {}, { enabled: hasFilledTwilioCredentials }); return customerPhoneNumber; } diff --git a/app/core/hooks/use-require-onboarding.ts b/app/core/hooks/use-require-onboarding.ts index a75330b..dcd6792 100644 --- a/app/core/hooks/use-require-onboarding.ts +++ b/app/core/hooks/use-require-onboarding.ts @@ -5,10 +5,14 @@ import useCustomerPhoneNumber from "./use-customer-phone-number"; export default function useRequireOnboarding() { const router = useRouter(); - const { customer, hasCompletedOnboarding } = useCurrentCustomer(); + const { hasFilledTwilioCredentials, hasCompletedOnboarding } = useCurrentCustomer(); const customerPhoneNumber = useCustomerPhoneNumber(); - if (!hasCompletedOnboarding) { + if (hasCompletedOnboarding) { + return; + } + + if (!hasFilledTwilioCredentials) { throw router.push(Routes.StepTwo()); } @@ -17,7 +21,6 @@ export default function useRequireOnboarding() { return; }*/ - console.log("customerPhoneNumber", customerPhoneNumber); if (!customerPhoneNumber) { throw router.push(Routes.StepThree()); } diff --git a/app/keypad/pages/keypad.tsx b/app/keypad/pages/keypad.tsx index 0174224..4ac72c0 100644 --- a/app/keypad/pages/keypad.tsx +++ b/app/keypad/pages/keypad.tsx @@ -60,10 +60,10 @@ const Keypad: BlitzPage = () => {
- +
-
- +
+
@@ -112,9 +112,7 @@ const Digit: FunctionComponent<{ digit: string }> = ({ children, digit }) => { ); }; -const DigitLetters: FunctionComponent = ({ children }) => ( -
{children}
-); +const DigitLetters: FunctionComponent = ({ children }) =>
{children}
; const phoneNumberAtom = atom(""); const pressDigitAtom = atom(null, (get, set, digit: string) => { diff --git a/app/messages/api/queue/fetch-messages.ts b/app/messages/api/queue/fetch-messages.ts index e05a415..5c94074 100644 --- a/app/messages/api/queue/fetch-messages.ts +++ b/app/messages/api/queue/fetch-messages.ts @@ -12,14 +12,12 @@ const fetchMessagesQueue = Queue("api/queue/fetch-messages", async ({ c const customer = await db.customer.findFirst({ where: { id: customerId } }); const phoneNumber = await db.phoneNumber.findFirst({ where: { customerId } }); - const [messagesSent, messagesReceived] = await Promise.all([ - twilio(customer!.accountSid!, customer!.authToken!).messages.list({ - from: phoneNumber!.phoneNumber, - }), - twilio(customer!.accountSid!, customer!.authToken!).messages.list({ - to: phoneNumber!.phoneNumber, - }), + const [sent, received] = await Promise.all([ + twilio(customer!.accountSid!, customer!.authToken!).messages.list({ from: phoneNumber!.phoneNumber }), + twilio(customer!.accountSid!, customer!.authToken!).messages.list({ to: phoneNumber!.phoneNumber }), ]); + const messagesSent = sent.filter((message) => message.direction.startsWith("outbound")); + const messagesReceived = received.filter((message) => message.direction === "inbound"); const messages = [...messagesSent, ...messagesReceived].sort( (a, b) => a.dateCreated.getTime() - b.dateCreated.getTime(), ); diff --git a/app/messages/pages/messages.tsx b/app/messages/pages/messages.tsx index 56e2bc3..e5219f1 100644 --- a/app/messages/pages/messages.tsx +++ b/app/messages/pages/messages.tsx @@ -1,4 +1,4 @@ -import { Suspense, useState } from "react"; +import { Suspense } from "react"; import type { BlitzPage } from "blitz"; import { Routes } from "blitz"; import { useAtom } from "jotai"; diff --git a/app/onboarding/components/onboarding-layout.tsx b/app/onboarding/components/onboarding-layout.tsx index 06944a2..c34e5c0 100644 --- a/app/onboarding/components/onboarding-layout.tsx +++ b/app/onboarding/components/onboarding-layout.tsx @@ -1,9 +1,7 @@ import type { FunctionComponent } from "react"; +import { Link } from "blitz"; import { CheckIcon } from "@heroicons/react/solid"; import clsx from "clsx"; -import { Link, Routes, useRouter } from "blitz"; - -import useCustomerPhoneNumber from "../../core/hooks/use-customer-phone-number"; type StepLink = { href: string; @@ -19,13 +17,6 @@ type Props = { const steps = ["Welcome", "Twilio Credentials", "Pick a plan"] as const; const OnboardingLayout: FunctionComponent = ({ children, currentStep, previous, next }) => { - const router = useRouter(); - const customerPhoneNumber = useCustomerPhoneNumber(); - - if (customerPhoneNumber) { - throw router.push(Routes.Messages()); - } - return (
diff --git a/app/onboarding/mutations/set-phone-number.ts b/app/onboarding/mutations/set-phone-number.ts index a750928..1a51d4f 100644 --- a/app/onboarding/mutations/set-phone-number.ts +++ b/app/onboarding/mutations/set-phone-number.ts @@ -12,29 +12,23 @@ const Body = z.object({ phoneNumberSid: z.string(), }); -export default resolver.pipe( - resolver.zod(Body), - resolver.authorize(), - async ({ phoneNumberSid }, context) => { - const customer = await getCurrentCustomer(null, context); - const customerId = customer!.id; - const phoneNumbers = await twilio( - customer!.accountSid!, - customer!.authToken!, - ).incomingPhoneNumbers.list(); - const phoneNumber = phoneNumbers.find((phoneNumber) => phoneNumber.sid === phoneNumberSid)!; - await db.phoneNumber.create({ - data: { - customerId, - phoneNumberSid, - phoneNumber: phoneNumber.phoneNumber, - }, - }); +export default resolver.pipe(resolver.zod(Body), resolver.authorize(), async ({ phoneNumberSid }, context) => { + const customer = await getCurrentCustomer(null, context); + const customerId = customer!.id; + const phoneNumbers = await twilio(customer!.accountSid!, customer!.authToken!).incomingPhoneNumbers.list(); + const phoneNumber = phoneNumbers.find((phoneNumber) => phoneNumber.sid === phoneNumberSid)!; + await db.phoneNumber.create({ + data: { + customerId, + phoneNumberSid, + phoneNumber: phoneNumber.phoneNumber, + }, + }); + context.session.$setPrivateData({ hasCompletedOnboarding: true }); - await Promise.all([ - fetchMessagesQueue.enqueue({ customerId }, { id: `fetch-messages-${customerId}` }), - fetchCallsQueue.enqueue({ customerId }, { id: `fetch-messages-${customerId}` }), - setTwilioWebhooks.enqueue({ customerId }, { id: `set-twilio-webhooks-${customerId}` }), - ]); - }, -); + await Promise.all([ + fetchMessagesQueue.enqueue({ customerId }, { id: `fetch-messages-${customerId}` }), + fetchCallsQueue.enqueue({ customerId }, { id: `fetch-messages-${customerId}` }), + setTwilioWebhooks.enqueue({ customerId }, { id: `set-twilio-webhooks-${customerId}` }), + ]); +}); diff --git a/app/onboarding/pages/welcome/step-one.tsx b/app/onboarding/pages/welcome/step-one.tsx index 73f696e..579a4f3 100644 --- a/app/onboarding/pages/welcome/step-one.tsx +++ b/app/onboarding/pages/welcome/step-one.tsx @@ -16,10 +16,7 @@ const StepOne: BlitzPage = () => { }; StepOne.getLayout = (page) => ( - + {page} ); @@ -39,16 +36,17 @@ export const getServerSideProps: GetServerSideProps = async ({ req, res }) => { } const phoneNumber = await db.phoneNumber.findFirst({ where: { customerId: session.userId } }); - if (!phoneNumber) { - return { props: {} }; + if (phoneNumber) { + await session.$setPublicData({ hasCompletedOnboarding: true }); + return { + redirect: { + destination: Routes.Messages().pathname, + permanent: false, + }, + }; } - return { - redirect: { - destination: Routes.Messages().pathname, - permanent: false, - }, - }; + return { props: {} }; }; export default StepOne; diff --git a/app/onboarding/pages/welcome/step-three.tsx b/app/onboarding/pages/welcome/step-three.tsx index 0b7fb67..47718fd 100644 --- a/app/onboarding/pages/welcome/step-three.tsx +++ b/app/onboarding/pages/welcome/step-three.tsx @@ -102,6 +102,7 @@ export const getServerSideProps: GetServerSideProps = async ({ req, res } const phoneNumber = await db.phoneNumber.findFirst({ where: { customerId: session.userId } }); if (phoneNumber) { + await session.$setPublicData({ hasCompletedOnboarding: true }); return { redirect: { destination: Routes.Messages().pathname, @@ -129,10 +130,7 @@ export const getServerSideProps: GetServerSideProps = async ({ req, res } }; } - const incomingPhoneNumbers = await twilio( - customer.accountSid, - customer.authToken, - ).incomingPhoneNumbers.list(); + const incomingPhoneNumbers = await twilio(customer.accountSid, customer.authToken).incomingPhoneNumbers.list(); const phoneNumbers = incomingPhoneNumbers.map(({ phoneNumber, sid }) => ({ phoneNumber, sid })); return { diff --git a/app/onboarding/pages/welcome/step-two.tsx b/app/onboarding/pages/welcome/step-two.tsx index 5a7c350..ad0680a 100644 --- a/app/onboarding/pages/welcome/step-two.tsx +++ b/app/onboarding/pages/welcome/step-two.tsx @@ -48,10 +48,7 @@ const StepTwo: BlitzPage = () => {
-
-