mirror of
https://github.com/formbricks/formbricks.git
synced 2026-03-18 09:41:32 -05:00
* 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
80 lines
1.7 KiB
TypeScript
80 lines
1.7 KiB
TypeScript
import { prisma } from "@formbricks/database";
|
|
import { DatabaseError } from "@formbricks/errors";
|
|
import { TSession } from "@formbricks/types/v1/sessions";
|
|
import { Prisma } from "@prisma/client";
|
|
|
|
const select = {
|
|
id: true,
|
|
createdAt: true,
|
|
updatedAt: true,
|
|
expiresAt: true,
|
|
personId: true,
|
|
};
|
|
|
|
const oneHour = 1000 * 60 * 60;
|
|
|
|
export const getSession = async (sessionId: string): Promise<TSession | null> => {
|
|
try {
|
|
const session = await prisma.session.findUnique({
|
|
where: {
|
|
id: sessionId,
|
|
},
|
|
select,
|
|
});
|
|
|
|
return session;
|
|
} catch (error) {
|
|
if (error instanceof Prisma.PrismaClientKnownRequestError) {
|
|
throw new DatabaseError("Database operation failed");
|
|
}
|
|
|
|
throw error;
|
|
}
|
|
};
|
|
|
|
export const createSession = async (personId: string): Promise<TSession> => {
|
|
try {
|
|
const session = await prisma.session.create({
|
|
data: {
|
|
person: {
|
|
connect: {
|
|
id: personId,
|
|
},
|
|
},
|
|
expiresAt: new Date(Date.now() + oneHour),
|
|
},
|
|
select,
|
|
});
|
|
|
|
return session;
|
|
} catch (error) {
|
|
if (error instanceof Prisma.PrismaClientKnownRequestError) {
|
|
throw new DatabaseError("Database operation failed");
|
|
}
|
|
|
|
throw error;
|
|
}
|
|
};
|
|
|
|
export const extendSession = async (sessionId: string): Promise<TSession> => {
|
|
try {
|
|
const session = await prisma.session.update({
|
|
where: {
|
|
id: sessionId,
|
|
},
|
|
data: {
|
|
expiresAt: new Date(Date.now() + oneHour),
|
|
},
|
|
select,
|
|
});
|
|
|
|
return session;
|
|
} catch (error) {
|
|
if (error instanceof Prisma.PrismaClientKnownRequestError) {
|
|
throw new DatabaseError("Database operation failed");
|
|
}
|
|
|
|
throw error;
|
|
}
|
|
};
|