feat: survey add webhook url field which can send webhook when receive any survey

This commit is contained in:
moonrailgun 2024-10-11 00:26:34 +08:00
parent fd63f2a22e
commit 8a6a75f3f5
5 changed files with 19 additions and 7 deletions

View File

@ -2,7 +2,7 @@ import { createFileRoute, useNavigate } from '@tanstack/react-router';
import { useTranslation } from '@i18next-toolkit/react'; import { useTranslation } from '@i18next-toolkit/react';
import { useEvent } from '@/hooks/useEvent'; import { useEvent } from '@/hooks/useEvent';
import { useCurrentWorkspaceId } from '@/store/user'; import { useCurrentWorkspaceId } from '@/store/user';
import { trpc } from '@/api/trpc'; import { defaultErrorHandler, trpc } from '@/api/trpc';
import { Card, CardContent } from '@/components/ui/card'; import { Card, CardContent } from '@/components/ui/card';
import { CommonWrapper } from '@/components/CommonWrapper'; import { CommonWrapper } from '@/components/CommonWrapper';
import { routeAuthBeforeLoad } from '@/utils/route'; import { routeAuthBeforeLoad } from '@/utils/route';
@ -25,7 +25,9 @@ function PageComponent() {
const { surveyId } = Route.useParams<{ surveyId: string }>(); const { surveyId } = Route.useParams<{ surveyId: string }>();
const workspaceId = useCurrentWorkspaceId(); const workspaceId = useCurrentWorkspaceId();
const navigate = useNavigate(); 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({ const { data: survey, isLoading } = trpc.survey.get.useQuery({
workspaceId, workspaceId,
surveyId, surveyId,

View File

@ -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 () { (BigInt.prototype as any).toJSON = function () {
const int = Number.parseInt(this.toString()); const int = Number.parseInt(this.toString());
return int ?? this.toString(); return int ?? this.toString();

View File

@ -0,0 +1,2 @@
-- AlterTable
ALTER TABLE "Survey" ADD COLUMN "webhookUrl" TEXT NOT NULL DEFAULT '';

View File

@ -18,6 +18,7 @@ export const SurveyModelSchema = z.object({
payload: imports.SurveyPayloadSchema, payload: imports.SurveyPayloadSchema,
feedChannelIds: z.string().array(), feedChannelIds: z.string().array(),
feedTemplate: z.string(), feedTemplate: z.string(),
webhookUrl: z.string(),
createdAt: z.date(), createdAt: z.date(),
updatedAt: z.date(), updatedAt: z.date(),
}) })

View File

@ -192,7 +192,7 @@ export const surveyRouter = router({
survey.feedTemplate || survey.feedTemplate ||
'survey {{_surveyName}} receive a new record.'; 'survey {{_surveyName}} receive a new record.';
survey.feedChannelIds.forEach((channelId) => { survey.feedChannelIds.forEach(async (channelId) => {
try { try {
const surveyPayload = SurveyPayloadSchema.parse(survey.payload); const surveyPayload = SurveyPayloadSchema.parse(survey.payload);
@ -220,11 +220,13 @@ export const surveyRouter = router({
if (survey && survey.webhookUrl) { if (survey && survey.webhookUrl) {
axios axios
.post(survey.webhookUrl) .post(survey.webhookUrl, {
...result,
})
.then(() => { .then(() => {
return axios.post(survey.webhookUrl, { logger.info(
...result, `[surveySubmitWebhook] send webhooks to ${survey.webhookUrl} success!`
}); );
}) })
.catch((err) => logger.error('[surveySubmitWebhook]', err)); .catch((err) => logger.error('[surveySubmitWebhook]', err));
} }