feat: survey add webhook url field which can send webhook when receive any survey
This commit is contained in:
parent
de572426eb
commit
f00163b2f1
@ -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,
|
||||||
|
@ -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();
|
||||||
|
@ -0,0 +1,2 @@
|
|||||||
|
-- AlterTable
|
||||||
|
ALTER TABLE "Survey" ADD COLUMN "webhookUrl" TEXT NOT NULL DEFAULT '';
|
@ -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(),
|
||||||
})
|
})
|
||||||
|
@ -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));
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user