Files
formbricks/packages/lib/services/integrations.ts
Dhruwang Jariwala 7e626e75a3 feat: Add Management API (#788)
* added some management APIs for response and surveys

* added management API endpoints

* refactored [responseId] route

* added authorisation and made refactors

* removed console logs

* fixed build issues

* remobed session check

* removed getEnvironmentBySession function

* made code refactors

* update variables in route

* add updated routes

* change underscore to hyphen notation

* peoples -> people

* fix build errors

* resolved build errors

* restored types in summary list

* add redirects for old responses and survey endpoints, update docs

* fixed not authenticated response

* update survey create endpoint to only accept necessary fields, clean up

---------

Co-authored-by: Dhruwang Jariwala <dhruwang@Dhruwangs-MacBook-Pro.local>
Co-authored-by: Matthias Nannt <mail@matthiasnannt.com>
2023-09-28 10:57:45 +02:00

71 lines
1.8 KiB
TypeScript

import "server-only";
import { prisma } from "@formbricks/database";
import { Prisma } from "@prisma/client";
import { DatabaseError } from "@formbricks/types/v1/errors";
import { TIntegration } from "@formbricks/types/v1/integrations";
import { cache } from "react";
export async function createOrUpdateIntegration(
environmentId: string,
integrationData: any
): Promise<TIntegration> {
try {
const integration = await prisma.integration.upsert({
where: {
type_environmentId: {
environmentId,
type: integrationData.type,
},
},
update: {
...integrationData,
environment: { connect: { id: environmentId } },
},
create: {
...integrationData,
environment: { connect: { id: environmentId } },
},
});
return integration;
} catch (error) {
if (error instanceof Prisma.PrismaClientKnownRequestError) {
console.error(error);
throw new DatabaseError("Database operation failed");
}
throw error;
}
}
export const getIntegrations = cache(async (environmentId: string): Promise<TIntegration[]> => {
try {
const result = await prisma.integration.findMany({
where: {
environmentId,
},
});
return result;
} catch (error) {
if (error instanceof Prisma.PrismaClientKnownRequestError) {
throw new DatabaseError("Database operation failed");
}
throw error;
}
});
export const deleteIntegration = async (integrationId: string): Promise<void> => {
try {
await prisma.integration.delete({
where: {
id: integrationId,
},
});
} catch (error) {
if (error instanceof Prisma.PrismaClientKnownRequestError) {
throw new DatabaseError("Database operation failed");
}
throw error;
}
};