Some checks failed
Build and Push Container Image / build-and-push (push) Failing after 3m49s
73 lines
1.9 KiB
TypeScript
73 lines
1.9 KiB
TypeScript
import { redirect } from '@sveltejs/kit';
|
|
import type { PageServerLoad } from './$types';
|
|
import { db } from '$lib/server/db';
|
|
import { project, task, issue } from '$lib/server/db/schema';
|
|
import { desc, eq, sql } from 'drizzle-orm';
|
|
|
|
export const load: PageServerLoad = async (event) => {
|
|
if (!event.locals.user) {
|
|
throw redirect(302, '/auth/login');
|
|
}
|
|
|
|
const projects = await db
|
|
.select({
|
|
id: project.id,
|
|
name: project.name,
|
|
description: project.description,
|
|
status: project.status,
|
|
taskCount: sql<number>`(
|
|
select count(*) from ${task} where ${task.projectId} = ${project.id}
|
|
)`.mapWith(Number),
|
|
doneCount: sql<number>`(
|
|
select count(*) from ${task}
|
|
where ${task.projectId} = ${project.id} and ${task.status} = 'done'
|
|
)`.mapWith(Number),
|
|
issueCount: sql<number>`(
|
|
select count(*) from ${issue} where ${issue.projectId} = ${project.id}
|
|
)`.mapWith(Number)
|
|
})
|
|
.from(project)
|
|
.where(eq(project.ownerId, event.locals.user.id))
|
|
.orderBy(desc(project.updatedAt));
|
|
|
|
const normalizedProjects = projects.map((item) => ({
|
|
...item,
|
|
progress: item.taskCount ? Math.round((item.doneCount / item.taskCount) * 100) : 0
|
|
}));
|
|
|
|
const recentTasks = await db
|
|
.select({
|
|
id: task.id,
|
|
title: task.title,
|
|
status: task.status,
|
|
priority: task.priority,
|
|
projectId: task.projectId
|
|
})
|
|
.from(task)
|
|
.innerJoin(project, eq(task.projectId, project.id))
|
|
.where(eq(project.ownerId, event.locals.user.id))
|
|
.orderBy(desc(task.updatedAt))
|
|
.limit(5);
|
|
|
|
const recentIssues = await db
|
|
.select({
|
|
id: issue.id,
|
|
title: issue.title,
|
|
state: issue.state,
|
|
provider: issue.provider,
|
|
projectId: issue.projectId
|
|
})
|
|
.from(issue)
|
|
.innerJoin(project, eq(issue.projectId, project.id))
|
|
.where(eq(project.ownerId, event.locals.user.id))
|
|
.orderBy(desc(issue.updatedAt))
|
|
.limit(5);
|
|
|
|
return {
|
|
user: event.locals.user,
|
|
projects: normalizedProjects,
|
|
recentTasks,
|
|
recentIssues
|
|
};
|
|
};
|