From bb7815464c88d5f5487a1843cc8d531682c9ddf3 Mon Sep 17 00:00:00 2001 From: Hariom Balhara Date: Fri, 11 Nov 2022 15:27:44 +0530 Subject: [PATCH] Feature/Routing-Forms/Reporting (#5375) * Add very basic reporting logic * Support more logical operators * Add tests * Fix types * Add playwright test for reporting * Fix table UI * Remove only * Better name variable * Increase max_old_space to allow tests to run without reaciing limit * Update test name * Reuse getServerSideProps * Variable renaming * PR Suggestions Co-authored-by: kodiakhq[bot] <49736102+kodiakhq[bot]@users.noreply.github.com> --- .github/workflows/test.yml | 5 +- jest.config.ts | 10 + .../components/RoutingNavBar.tsx | 5 + .../routing-forms/components/SingleForm.tsx | 69 +++++++ .../react-awesome-query-builder/widgets.tsx | 18 +- .../ee/routing-forms/jsonLogicToPrisma.ts | 168 +++++++++++++++ .../pages/app-routing.config.tsx | 2 + .../pages/form-edit/[...appPages].tsx | 70 +------ .../pages/reporting/[...appPages].tsx | 195 ++++++++++++++++++ .../pages/route-builder/[...appPages].tsx | 82 +------- .../playwright/tests/basic.e2e.ts | 90 +++++--- .../test/lib/jsonLogicToPrisma.test.ts | 167 +++++++++++++++ .../app-store/ee/routing-forms/trpc-router.ts | 75 +++++++ 13 files changed, 781 insertions(+), 175 deletions(-) create mode 100644 packages/app-store/ee/routing-forms/jsonLogicToPrisma.ts create mode 100644 packages/app-store/ee/routing-forms/pages/reporting/[...appPages].tsx create mode 100644 packages/app-store/ee/routing-forms/test/lib/jsonLogicToPrisma.test.ts diff --git a/.github/workflows/test.yml b/.github/workflows/test.yml index f8e2f57d13..20222979d5 100644 --- a/.github/workflows/test.yml +++ b/.github/workflows/test.yml @@ -1,7 +1,7 @@ name: Unit tests on: push: - branches: [fixes/e2e-consolidation] # TODO: Remove this after merged in main + branches: [ feat/routing-forms/reporting ] # TODO: Remove this after merged in main pull_request_target: # So we can test on forks branches: - main @@ -21,7 +21,8 @@ jobs: with: ref: ${{ github.event.pull_request.head.sha }} # So we can test on forks fetch-depth: 2 - - run: echo 'NODE_OPTIONS="--max_old_space_size=4096"' >> $GITHUB_ENV + # Should be an 8GB machine as per https://docs.github.com/en/actions/using-github-hosted-runners/about-github-hosted-runners + - run: echo 'NODE_OPTIONS="--max_old_space_size=6144"' >> $GITHUB_ENV - name: Use Node 16.x uses: actions/setup-node@v3 with: diff --git a/jest.config.ts b/jest.config.ts index 9123946007..5145bfd33d 100644 --- a/jest.config.ts +++ b/jest.config.ts @@ -50,6 +50,16 @@ const config: Config = { testEnvironment: "jsdom", setupFiles: ["/packages/app-store/closecomothercalendar/test/globals.ts"], }, + { + displayName: "@calcom/routing-forms", + roots: ["/packages/app-store/ee/routing-forms"], + testMatch: ["**/test/lib/**/*.(spec|test).(ts|tsx|js)"], + transform: { + "^.+\\.ts?$": "ts-jest", + }, + transformIgnorePatterns: ["/node_modules/", "^.+\\.module\\.(css|sass|scss)$"], + testEnvironment: "jsdom", + }, { displayName: "@calcom/api", roots: ["/apps/api"], diff --git a/packages/app-store/ee/routing-forms/components/RoutingNavBar.tsx b/packages/app-store/ee/routing-forms/components/RoutingNavBar.tsx index 945c535d7f..b9a83dffa2 100644 --- a/packages/app-store/ee/routing-forms/components/RoutingNavBar.tsx +++ b/packages/app-store/ee/routing-forms/components/RoutingNavBar.tsx @@ -19,6 +19,11 @@ export default function RoutingNavBar({ href: `${appUrl}/route-builder/${form?.id}`, className: "pointer-events-none opacity-30 lg:pointer-events-auto lg:opacity-100", }, + { + name: "Reporting", + href: `${appUrl}/reporting/${form?.id}`, + className: "pointer-events-none opacity-30 lg:pointer-events-auto lg:opacity-100", + }, ]; return (
diff --git a/packages/app-store/ee/routing-forms/components/SingleForm.tsx b/packages/app-store/ee/routing-forms/components/SingleForm.tsx index 83471e3a41..f2184a1af3 100644 --- a/packages/app-store/ee/routing-forms/components/SingleForm.tsx +++ b/packages/app-store/ee/routing-forms/components/SingleForm.tsx @@ -5,6 +5,12 @@ import { useForm, UseFormReturn, Controller } from "react-hook-form"; import useApp from "@calcom/lib/hooks/useApp"; import { useLocale } from "@calcom/lib/hooks/useLocale"; import { trpc } from "@calcom/trpc/react"; +import { + AppGetServerSidePropsContext, + AppPrisma, + AppUser, + AppSsrInit, +} from "@calcom/types/AppGetServerSideProps"; import { Icon } from "@calcom/ui"; import { Dialog, DialogContent, DialogClose, DialogFooter, DialogHeader } from "@calcom/ui/Dialog"; import { Button, ButtonGroup } from "@calcom/ui/components"; @@ -15,6 +21,7 @@ import SettingsToggle from "@calcom/ui/v2/core/SettingsToggle"; import { ShellMain } from "@calcom/ui/v2/core/Shell"; import Banner from "@calcom/ui/v2/core/banner"; +import { getSerializableForm } from "../lib/getSerializableForm"; import { processRoute } from "../lib/processRoute"; import { RoutingPages } from "../pages/route-builder/[...appPages]"; import { SerializableForm } from "../types/types"; @@ -392,3 +399,65 @@ export default function SingleFormWrapper({ form: _form, ...props }: SingleFormC } return ; } + +export const getServerSidePropsForSingleFormView = async function getServerSidePropsForSingleFormView( + context: AppGetServerSidePropsContext, + prisma: AppPrisma, + user: AppUser, + ssrInit: AppSsrInit +) { + const ssr = await ssrInit(context); + + if (!user) { + return { + redirect: { + permanent: false, + destination: "/auth/login", + }, + }; + } + const { params } = context; + if (!params) { + return { + notFound: true, + }; + } + const formId = params.appPages[0]; + if (!formId || params.appPages.length > 1) { + return { + notFound: true, + }; + } + + const isAllowed = (await import("../lib/isAllowed")).isAllowed; + if (!(await isAllowed({ userId: user.id, formId }))) { + return { + notFound: true, + }; + } + + const form = await prisma.app_RoutingForms_Form.findUnique({ + where: { + id: formId, + }, + include: { + _count: { + select: { + responses: true, + }, + }, + }, + }); + if (!form) { + return { + notFound: true, + }; + } + + return { + props: { + trpcState: ssr.dehydrate(), + form: getSerializableForm(form), + }, + }; +}; diff --git a/packages/app-store/ee/routing-forms/components/react-awesome-query-builder/widgets.tsx b/packages/app-store/ee/routing-forms/components/react-awesome-query-builder/widgets.tsx index 24bbfaf23d..1812d05425 100644 --- a/packages/app-store/ee/routing-forms/components/react-awesome-query-builder/widgets.tsx +++ b/packages/app-store/ee/routing-forms/components/react-awesome-query-builder/widgets.tsx @@ -150,7 +150,7 @@ function SelectWidget({ ); } -function Button({ type, label, onClick, readonly }: ButtonProps) { +function Button({ config, type, label, onClick, readonly }: ButtonProps) { if (type === "delRule" || type == "delGroup") { return (