From f848a44f1a3aced1a3c2a6960f3be7d94e501b77 Mon Sep 17 00:00:00 2001 From: Shubham Palriwala Date: Tue, 2 Jan 2024 19:38:11 +0530 Subject: [PATCH] feat: integrate formbricks in help feedback box (#12276) * feat: integrate formbricks in help feedback box * Update yarn.lock * Update yarn.lock * fix: use formbricks/api@v1.1 & set user with userId linked to feedback * fix: use separate env vars as suggested * test: Add more orgs tests (#12241) * feat: integrate formbricks in help feedback box * Update yarn.lock * fix: yarn lockfile * fix: yarn lockfile again * feat: link cal and formbricks user.id and add attributes of email and username to formbricks person object * Update yarn.lock * Update yarn.lock * fix: type safety in enums --------- Co-authored-by: Peer Richelsen Co-authored-by: Hariom Balhara Co-authored-by: Peer Richelsen --- .env.example | 5 +++ packages/lib/formbricks.ts | 42 +++++++++++++++++++ packages/lib/package.json | 1 + .../loggedInViewer/submitFeedback.handler.ts | 3 ++ turbo.json | 3 ++ 5 files changed, 54 insertions(+) create mode 100644 packages/lib/formbricks.ts diff --git a/.env.example b/.env.example index 71357c2d7c..e15bdefcd9 100644 --- a/.env.example +++ b/.env.example @@ -133,6 +133,11 @@ NEXT_PUBLIC_SENDGRID_SENDER_NAME= # Used for capturing exceptions and logging messages NEXT_PUBLIC_SENTRY_DSN= +# Formbricks Experience Management Integration +FORMBRICKS_HOST_URL=https://app.formbricks.com +FORMBRICKS_ENVIRONMENT_ID= +FORMBRICKS_FEEDBACK_SURVEY_ID= + # Twilio # Used to send SMS reminders in workflows TWILIO_SID= diff --git a/packages/lib/formbricks.ts b/packages/lib/formbricks.ts new file mode 100644 index 0000000000..b5ed06a546 --- /dev/null +++ b/packages/lib/formbricks.ts @@ -0,0 +1,42 @@ +import { FormbricksAPI } from "@formbricks/api"; + +import type { Feedback } from "@calcom/emails/templates/feedback-email"; + +enum Rating { + "Extremely unsatisfied" = 1, + "Unsatisfied" = 2, + "Satisfied" = 3, + "Extremely satisfied" = 4, +} + +export const sendFeedbackFormbricks = async (userId: number, feedback: Feedback) => { + if (!process.env.FORMBRICKS_HOST_URL || !process.env.FORMBRICKS_ENVIRONMENT_ID) + throw new Error("Missing FORMBRICKS_HOST_URL or FORMBRICKS_ENVIRONMENT_ID env variable"); + const api = new FormbricksAPI({ + apiHost: process.env.FORMBRICKS_HOST_URL, + environmentId: process.env.FORMBRICKS_ENVIRONMENT_ID, + }); + if (process.env.FORMBRICKS_FEEDBACK_SURVEY_ID) { + const formbricksUserId = userId.toString(); + const ratingValue = Object.keys(Rating).includes(feedback.rating) + ? Rating[feedback.rating as keyof typeof Rating] + : undefined; + if (ratingValue === undefined) throw new Error("Invalid rating value"); + + await api.client.response.create({ + surveyId: process.env.FORMBRICKS_FEEDBACK_SURVEY_ID, + userId: formbricksUserId, + finished: true, + data: { + "formbricks-share-comments-question": feedback.comment, + "formbricks-rating-question": ratingValue, + }, + }); + await api.client.people.update(formbricksUserId, { + attributes: { + email: feedback.email, + username: feedback.username, + }, + }); + } +}; diff --git a/packages/lib/package.json b/packages/lib/package.json index b2ada1df57..fae3a15b8f 100644 --- a/packages/lib/package.json +++ b/packages/lib/package.json @@ -14,6 +14,7 @@ "dependencies": { "@calcom/config": "*", "@calcom/dayjs": "*", + "@formbricks/api": "^1.1.0", "@sendgrid/client": "^7.7.0", "@vercel/og": "^0.5.0", "bcryptjs": "^2.4.3", diff --git a/packages/trpc/server/routers/loggedInViewer/submitFeedback.handler.ts b/packages/trpc/server/routers/loggedInViewer/submitFeedback.handler.ts index 27db135abb..e72932778b 100644 --- a/packages/trpc/server/routers/loggedInViewer/submitFeedback.handler.ts +++ b/packages/trpc/server/routers/loggedInViewer/submitFeedback.handler.ts @@ -1,5 +1,6 @@ import dayjs from "@calcom/dayjs"; import { sendFeedbackEmail } from "@calcom/emails"; +import { sendFeedbackFormbricks } from "@calcom/lib/formbricks"; import { prisma } from "@calcom/prisma"; import type { TrpcSessionUser } from "@calcom/trpc/server/trpc"; @@ -30,6 +31,8 @@ export const submitFeedbackHandler = async ({ ctx, input }: SubmitFeedbackOption comment: comment, }, }); + if (process.env.FORMBRICKS_HOST_URL && process.env.FORMBRICKS_ENVIRONMENT_ID) + sendFeedbackFormbricks(ctx.user.id, feedback); if (process.env.SEND_FEEDBACK_EMAIL && comment) sendFeedbackEmail(feedback); }; diff --git a/turbo.json b/turbo.json index b0ed2de782..e7f0274141 100644 --- a/turbo.json +++ b/turbo.json @@ -256,6 +256,9 @@ "EMAIL_SERVER_USER", "EMAIL_SERVER", "EXCHANGE_DEFAULT_EWS_URL", + "FORMBRICKS_HOST_URL", + "FORMBRICKS_ENVIRONMENT_ID", + "FORMBRICKS_FEEDBACK_SURVEY_ID", "GIPHY_API_KEY", "GITHUB_API_REPO_TOKEN", "GOOGLE_API_CREDENTIALS",