| 46 | } |
| 47 | |
| 48 | export async function getProjects({ |
| 49 | organizationId, |
| 50 | userId, |
| 51 | }: { |
| 52 | organizationId: string; |
| 53 | userId: string | null; |
| 54 | }) { |
| 55 | if (!userId) { |
| 56 | return []; |
| 57 | } |
| 58 | |
| 59 | const [projects, members, access] = await Promise.all([ |
| 60 | db.project.findMany({ |
| 61 | where: { |
| 62 | organizationId, |
| 63 | }, |
| 64 | orderBy: { |
| 65 | eventsCount: 'desc', |
| 66 | }, |
| 67 | }), |
| 68 | db.member.findMany({ |
| 69 | where: { |
| 70 | userId, |
| 71 | organizationId, |
| 72 | }, |
| 73 | }), |
| 74 | db.projectAccess.findMany({ |
| 75 | where: { |
| 76 | userId, |
| 77 | organizationId, |
| 78 | }, |
| 79 | }), |
| 80 | ]); |
| 81 | |
| 82 | if (members.length === 0) { |
| 83 | return []; |
| 84 | } |
| 85 | |
| 86 | if (access.length > 0) { |
| 87 | return projects.filter((project) => |
| 88 | access.some((a) => a.projectId === project.id) |
| 89 | ); |
| 90 | } |
| 91 | |
| 92 | return projects; |
| 93 | } |
| 94 | |
| 95 | export const getProjectEventsCount = async (projectId: string) => { |
| 96 | const res = await chQuery<{ count: number }>( |