lasthourcloud/src/routes/dashboard.json.ts

67 lines
2.1 KiB
TypeScript
Raw Normal View History

import { getUserDetails } from '$lib/common';
2022-02-10 15:47:44 +01:00
import * as db from '$lib/database';
import { ErrorHandler } from '$lib/database';
2022-02-10 15:47:44 +01:00
import type { RequestHandler } from '@sveltejs/kit';
export const get: RequestHandler = async (event) => {
const { userId, teamId, status, body } = await getUserDetails(event);
if (status === 401) return { status, body };
try {
2022-03-25 10:36:47 +01:00
const applicationsCount = await db.prisma.application.count({
2022-04-07 15:23:32 +02:00
where: { teams: { some: { id: teamId === '0' ? undefined : teamId } } }
2022-03-25 10:36:47 +01:00
});
const sourcesCount = await db.prisma.gitSource.count({
2022-04-07 15:23:32 +02:00
where: { teams: { some: { id: teamId === '0' ? undefined : teamId } } }
2022-03-25 10:36:47 +01:00
});
const destinationsCount = await db.prisma.destinationDocker.count({
2022-04-07 15:23:32 +02:00
where: { teams: { some: { id: teamId === '0' ? undefined : teamId } } }
2022-03-25 10:36:47 +01:00
});
const teamsCount = await db.prisma.permission.count({ where: { userId } });
const databasesCount = await db.prisma.database.count({
2022-04-07 15:23:32 +02:00
where: { teams: { some: { id: teamId === '0' ? undefined : teamId } } }
2022-03-25 10:36:47 +01:00
});
const servicesCount = await db.prisma.service.count({
2022-04-07 15:23:32 +02:00
where: { teams: { some: { id: teamId === '0' ? undefined : teamId } } }
});
const teams = await db.prisma.permission.findMany({
where: { userId },
include: { team: { include: { _count: { select: { users: true } } } } }
2022-03-25 10:36:47 +01:00
});
2022-02-10 15:47:44 +01:00
return {
body: {
2022-04-07 15:23:32 +02:00
teams,
2022-02-10 15:47:44 +01:00
applicationsCount,
sourcesCount,
destinationsCount,
teamsCount,
databasesCount,
servicesCount
}
};
} catch (error) {
return ErrorHandler(error);
2022-02-10 15:47:44 +01:00
}
};
export const post: RequestHandler = async (event) => {
const { status, body } = await getUserDetails(event, false);
if (status === 401) return { status, body };
const { cookie, value } = await event.request.json();
const from = event.url.searchParams.get('from') || '/';
return {
status: 302,
body: {},
headers: {
'set-cookie': [
`${cookie}=${value}; HttpOnly; Path=/; Max-Age=15778800;`,
'gitlabToken=; path=/; expires=Thu, 01 Jan 1970 00:00:00 GMT',
'githubToken=; path=/; expires=Thu, 01 Jan 1970 00:00:00 GMT'
2022-02-10 15:47:44 +01:00
],
Location: from
}
};
};