mirror of
https://github.com/formbricks/formbricks.git
synced 2026-04-25 20:01:53 -05:00
Fix Email Notifications (#583)
* Fix email notifications not working properly * Fix response notification not working * fix response meta schema * fix typo in docs * improve error message in webhooks
This commit is contained in:
@@ -1,56 +1,43 @@
|
||||
import { INTERNAL_SECRET } from "@formbricks/lib/constants";
|
||||
import { prisma } from "@formbricks/database";
|
||||
import { NextResponse } from "next/server";
|
||||
import { AttributeClass } from "@prisma/client";
|
||||
import { responses } from "@/lib/api/response";
|
||||
import { transformErrorToDetails } from "@/lib/api/validator";
|
||||
import { sendResponseFinishedEmail } from "@/lib/email";
|
||||
import { prisma } from "@formbricks/database";
|
||||
import { INTERNAL_SECRET } from "@formbricks/lib/constants";
|
||||
import { convertDatesInObject } from "@formbricks/lib/time";
|
||||
import { Question } from "@formbricks/types/questions";
|
||||
import { NotificationSettings } from "@formbricks/types/users";
|
||||
import { ZPipelineInput } from "@formbricks/types/v1/pipelines";
|
||||
import { headers } from "next/headers";
|
||||
import { NextResponse } from "next/server";
|
||||
|
||||
export async function POST(request: Request) {
|
||||
const { internalSecret, environmentId, surveyId, event, data } = await request.json();
|
||||
if (!internalSecret) {
|
||||
console.error("Pipeline: Missing internalSecret");
|
||||
return new Response("Missing internalSecret", {
|
||||
status: 400,
|
||||
});
|
||||
// check authentication with x-api-key header and CRON_SECRET env variable
|
||||
if (headers().get("x-api-key") !== INTERNAL_SECRET) {
|
||||
return responses.notAuthenticatedResponse();
|
||||
}
|
||||
if (!environmentId) {
|
||||
console.error("Pipeline: Missing environmentId");
|
||||
return new Response("Missing environmentId", {
|
||||
status: 400,
|
||||
});
|
||||
}
|
||||
if (!surveyId) {
|
||||
console.error("Pipeline: Missing surveyId");
|
||||
return new Response("Missing surveyId", {
|
||||
status: 400,
|
||||
});
|
||||
}
|
||||
if (!event) {
|
||||
console.error("Pipeline: Missing event");
|
||||
return new Response("Missing event", {
|
||||
status: 400,
|
||||
});
|
||||
}
|
||||
if (!data) {
|
||||
console.error("Pipeline: Missing data");
|
||||
return new Response("Missing data", {
|
||||
status: 400,
|
||||
});
|
||||
}
|
||||
if (internalSecret !== INTERNAL_SECRET) {
|
||||
console.error("Pipeline: internalSecret doesn't match");
|
||||
return new Response("Invalid internalSecret", {
|
||||
status: 401,
|
||||
});
|
||||
const jsonInput = await request.json();
|
||||
|
||||
convertDatesInObject(jsonInput);
|
||||
|
||||
const inputValidation = ZPipelineInput.safeParse(jsonInput);
|
||||
|
||||
if (!inputValidation.success) {
|
||||
console.error(inputValidation.error);
|
||||
return responses.badRequestResponse(
|
||||
"Fields are missing or incorrectly formatted",
|
||||
transformErrorToDetails(inputValidation.error),
|
||||
true
|
||||
);
|
||||
}
|
||||
|
||||
const { environmentId, surveyId, event, response } = inputValidation.data;
|
||||
|
||||
// get all webhooks of this environment where event in triggers
|
||||
const webhooks = await prisma.webhook.findMany({
|
||||
where: {
|
||||
environmentId,
|
||||
triggers: {
|
||||
hasSome: event,
|
||||
has: event,
|
||||
},
|
||||
OR: [
|
||||
{
|
||||
@@ -75,7 +62,7 @@ export async function POST(request: Request) {
|
||||
body: JSON.stringify({
|
||||
webhookId: webhook.id,
|
||||
event,
|
||||
data,
|
||||
data: response,
|
||||
}),
|
||||
});
|
||||
})
|
||||
@@ -136,32 +123,10 @@ export async function POST(request: Request) {
|
||||
name: surveyData.name,
|
||||
questions: JSON.parse(JSON.stringify(surveyData.questions)) as Question[],
|
||||
};
|
||||
// get person for response
|
||||
let person: {
|
||||
id: string;
|
||||
attributes: { id: string; value: string; attributeClass: AttributeClass }[];
|
||||
} | null;
|
||||
if (data.personId) {
|
||||
person = await prisma.person.findUnique({
|
||||
where: {
|
||||
id: data.personId,
|
||||
},
|
||||
select: {
|
||||
id: true,
|
||||
attributes: {
|
||||
select: {
|
||||
id: true,
|
||||
value: true,
|
||||
attributeClass: true,
|
||||
},
|
||||
},
|
||||
},
|
||||
});
|
||||
}
|
||||
// send email to all users
|
||||
await Promise.all(
|
||||
usersWithNotifications.map(async (user) => {
|
||||
await sendResponseFinishedEmail(user.email, environmentId, survey, data, person);
|
||||
await sendResponseFinishedEmail(user.email, environmentId, survey, response);
|
||||
})
|
||||
);
|
||||
}
|
||||
|
||||
@@ -68,11 +68,7 @@ export async function PUT(
|
||||
event: "responseUpdated",
|
||||
environmentId: survey.environmentId,
|
||||
surveyId: survey.id,
|
||||
// only send the updated fields
|
||||
data: {
|
||||
...response,
|
||||
data: inputValidation.data.data,
|
||||
},
|
||||
response,
|
||||
});
|
||||
|
||||
if (response.finished) {
|
||||
@@ -82,7 +78,7 @@ export async function PUT(
|
||||
event: "responseFinished",
|
||||
environmentId: survey.environmentId,
|
||||
surveyId: survey.id,
|
||||
data: response,
|
||||
response: response,
|
||||
});
|
||||
}
|
||||
|
||||
|
||||
@@ -68,7 +68,7 @@ export async function POST(request: Request): Promise<NextResponse> {
|
||||
event: "responseCreated",
|
||||
environmentId: survey.environmentId,
|
||||
surveyId: response.surveyId,
|
||||
data: response,
|
||||
response: response,
|
||||
});
|
||||
|
||||
if (responseInput.finished) {
|
||||
@@ -76,7 +76,7 @@ export async function POST(request: Request): Promise<NextResponse> {
|
||||
event: "responseFinished",
|
||||
environmentId: survey.environmentId,
|
||||
surveyId: response.surveyId,
|
||||
data: response,
|
||||
response: response,
|
||||
});
|
||||
}
|
||||
|
||||
|
||||
@@ -25,7 +25,7 @@ export async function GET() {
|
||||
if (error instanceof DatabaseError) {
|
||||
return responses.badRequestResponse(error.message);
|
||||
}
|
||||
throw error;
|
||||
return responses.internalServerErrorResponse(error.message);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user