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 de572426eb
commit f00163b2f1
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 { 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,

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 () {
const int = Number.parseInt(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,
feedChannelIds: z.string().array(),
feedTemplate: z.string(),
webhookUrl: z.string(),
createdAt: z.date(),
updatedAt: z.date(),
})

View File

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