Files
formbricks/packages/lib/services/displays.ts
Matti Nannt 8ea6016cf5 Sync surveys and events with formbricks-js automatically (#537)
* add first part of sync service

* add actionClasses to js sync

* fix errors, add product to sync

* rewrite formbricks-js for new states and types

* fix tests

* fix build errors

* add cors

* fix cors errors and other bugs

* comment test in checks until working again
2023-07-13 00:49:58 +02:00

101 lines
2.3 KiB
TypeScript

import { prisma } from "@formbricks/database";
import { TDisplay, TDisplayInput } from "@formbricks/types/v1/displays";
import { Prisma } from "@prisma/client";
import { DatabaseError, ResourceNotFoundError } from "@formbricks/errors";
import { transformPrismaPerson } from "./person";
const selectDisplay = {
id: true,
createdAt: true,
updatedAt: true,
surveyId: true,
person: {
select: {
id: true,
createdAt: true,
updatedAt: true,
attributes: {
select: {
value: true,
attributeClass: {
select: {
name: true,
},
},
},
},
},
},
status: true,
};
export const createDisplay = async (displayInput: TDisplayInput): Promise<TDisplay> => {
try {
const displayPrisma = await prisma.display.create({
data: {
survey: {
connect: {
id: displayInput.surveyId,
},
},
status: "seen",
...(displayInput.personId && {
person: {
connect: {
id: displayInput.personId,
},
},
}),
},
select: selectDisplay,
});
const display: TDisplay = {
...displayPrisma,
person: displayPrisma.person ? transformPrismaPerson(displayPrisma.person) : null,
};
return display;
} catch (error) {
if (error instanceof Prisma.PrismaClientKnownRequestError) {
throw new DatabaseError("Database operation failed");
}
throw error;
}
};
export const markDisplayResponded = async (displayId: string): Promise<TDisplay> => {
try {
if (!displayId) throw new Error("Display ID is required");
const displayPrisma = await prisma.display.update({
where: {
id: displayId,
},
data: {
status: "responded",
},
select: selectDisplay,
});
if (!displayPrisma) {
throw new ResourceNotFoundError("Display", displayId);
}
const display: TDisplay = {
...displayPrisma,
person: displayPrisma.person ? transformPrismaPerson(displayPrisma.person) : null,
};
return display;
} catch (error) {
if (error instanceof Prisma.PrismaClientKnownRequestError) {
throw new DatabaseError("Database operation failed");
}
throw error;
}
};