Files
stats/packages/db/src/services/project.service.ts
Carl-Gerhard Lindesvärd d31d9924a5 feature(auth): replace clerk.com with custom auth (#103)
* feature(auth): replace clerk.com with custom auth

* minor fixes

* remove notification preferences

* decrease live events interval

fix(api): cookies..

# Conflicts:
#	.gitignore
#	apps/api/src/index.ts
#	apps/dashboard/src/app/providers.tsx
#	packages/trpc/src/trpc.ts
2024-12-20 22:23:07 +01:00

102 lines
1.7 KiB
TypeScript

import { cacheable } from '@openpanel/redis';
import type { Prisma, Project } from '../prisma-client';
import { db } from '../prisma-client';
export type IServiceProject = Project;
export type IServiceProjectWithClients = Prisma.ProjectGetPayload<{
include: {
clients: true;
};
}>;
export async function getProjectById(id: string) {
const res = await db.project.findUnique({
where: {
id,
},
});
if (!res) {
return null;
}
return res;
}
export const getProjectByIdCached = cacheable(getProjectById, 60 * 60 * 24);
export async function getProjectWithClients(id: string) {
const res = await db.project.findUnique({
where: {
id,
},
include: {
clients: true,
},
});
if (!res) {
return null;
}
return res;
}
export async function getProjectsByOrganizationId(organizationId: string) {
return db.project.findMany({
where: {
organizationId,
},
orderBy: {
createdAt: 'desc',
},
});
}
export async function getProjects({
organizationId,
userId,
}: {
organizationId: string;
userId: string | null;
}) {
if (!userId) {
return [];
}
const [projects, members, access] = await Promise.all([
db.project.findMany({
where: {
organizationId,
},
orderBy: {
eventsCount: 'desc',
},
}),
db.member.findMany({
where: {
userId,
organizationId,
},
}),
db.projectAccess.findMany({
where: {
userId,
organizationId,
},
}),
]);
if (members.length === 0) {
return [];
}
if (access.length > 0) {
return projects.filter((project) =>
access.some((a) => a.projectId === project.id),
);
}
return projects;
}