From f00163b2f107bcf08d4ff398fa5dbd92ac36fda8 Mon Sep 17 00:00:00 2001 From: moonrailgun Date: Fri, 11 Oct 2024 00:26:34 +0800 Subject: [PATCH] feat: survey add webhook url field which can send webhook when receive any survey --- src/client/routes/survey/$surveyId/edit.tsx | 6 ++++-- src/server/init.ts | 5 +++++ .../20241010154732_add_survey_webhook/migration.sql | 2 ++ src/server/prisma/zod/survey.ts | 1 + src/server/trpc/routers/survey.ts | 12 +++++++----- 5 files changed, 19 insertions(+), 7 deletions(-) create mode 100644 src/server/prisma/migrations/20241010154732_add_survey_webhook/migration.sql diff --git a/src/client/routes/survey/$surveyId/edit.tsx b/src/client/routes/survey/$surveyId/edit.tsx index a2b7f69..73aca91 100644 --- a/src/client/routes/survey/$surveyId/edit.tsx +++ b/src/client/routes/survey/$surveyId/edit.tsx @@ -2,7 +2,7 @@ import { createFileRoute, useNavigate } from '@tanstack/react-router'; import { useTranslation } from '@i18next-toolkit/react'; import { useEvent } from '@/hooks/useEvent'; import { useCurrentWorkspaceId } from '@/store/user'; -import { trpc } from '@/api/trpc'; +import { defaultErrorHandler, trpc } from '@/api/trpc'; import { Card, CardContent } from '@/components/ui/card'; import { CommonWrapper } from '@/components/CommonWrapper'; import { routeAuthBeforeLoad } from '@/utils/route'; @@ -25,7 +25,9 @@ function PageComponent() { const { surveyId } = Route.useParams<{ surveyId: string }>(); const workspaceId = useCurrentWorkspaceId(); const navigate = useNavigate(); - const mutation = trpc.survey.update.useMutation(); + const mutation = trpc.survey.update.useMutation({ + onError: defaultErrorHandler, + }); const { data: survey, isLoading } = trpc.survey.get.useQuery({ workspaceId, surveyId, diff --git a/src/server/init.ts b/src/server/init.ts index 8a3283d..baaa8a9 100644 --- a/src/server/init.ts +++ b/src/server/init.ts @@ -1,3 +1,8 @@ +import { version } from '@tianji/shared'; +import axios from 'axios'; + +axios.defaults.headers.common['User-Agent'] = `tianji/${version}`; + (BigInt.prototype as any).toJSON = function () { const int = Number.parseInt(this.toString()); return int ?? this.toString(); diff --git a/src/server/prisma/migrations/20241010154732_add_survey_webhook/migration.sql b/src/server/prisma/migrations/20241010154732_add_survey_webhook/migration.sql new file mode 100644 index 0000000..79de8b5 --- /dev/null +++ b/src/server/prisma/migrations/20241010154732_add_survey_webhook/migration.sql @@ -0,0 +1,2 @@ +-- AlterTable +ALTER TABLE "Survey" ADD COLUMN "webhookUrl" TEXT NOT NULL DEFAULT ''; diff --git a/src/server/prisma/zod/survey.ts b/src/server/prisma/zod/survey.ts index c97e49c..202adf1 100644 --- a/src/server/prisma/zod/survey.ts +++ b/src/server/prisma/zod/survey.ts @@ -18,6 +18,7 @@ export const SurveyModelSchema = z.object({ payload: imports.SurveyPayloadSchema, feedChannelIds: z.string().array(), feedTemplate: z.string(), + webhookUrl: z.string(), createdAt: z.date(), updatedAt: z.date(), }) diff --git a/src/server/trpc/routers/survey.ts b/src/server/trpc/routers/survey.ts index 392abaf..185a535 100644 --- a/src/server/trpc/routers/survey.ts +++ b/src/server/trpc/routers/survey.ts @@ -192,7 +192,7 @@ export const surveyRouter = router({ survey.feedTemplate || 'survey {{_surveyName}} receive a new record.'; - survey.feedChannelIds.forEach((channelId) => { + survey.feedChannelIds.forEach(async (channelId) => { try { const surveyPayload = SurveyPayloadSchema.parse(survey.payload); @@ -220,11 +220,13 @@ export const surveyRouter = router({ if (survey && survey.webhookUrl) { axios - .post(survey.webhookUrl) + .post(survey.webhookUrl, { + ...result, + }) .then(() => { - return axios.post(survey.webhookUrl, { - ...result, - }); + logger.info( + `[surveySubmitWebhook] send webhooks to ${survey.webhookUrl} success!` + ); }) .catch((err) => logger.error('[surveySubmitWebhook]', err)); }