cal/packages/lib/formbricks.ts
Shubham Palriwala f848a44f1a
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 <peer@cal.com>
Co-authored-by: Hariom Balhara <hariombalhara@gmail.com>
Co-authored-by: Peer Richelsen <peeroke@gmail.com>
2024-01-02 14:08:11 +00:00

43 lines
1.4 KiB
TypeScript

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,
},
});
}
};