From c4cb92c78dc665cd4733e934a1f019ad54af20ee Mon Sep 17 00:00:00 2001 From: dominicbachmann Date: Wed, 6 Apr 2022 20:15:15 +0200 Subject: [PATCH] Added types for database/gitSource --- src/lib/database/gitSources.ts | 69 +++++++++++++++++++++++++++++----- 1 file changed, 59 insertions(+), 10 deletions(-) diff --git a/src/lib/database/gitSources.ts b/src/lib/database/gitSources.ts index 927907964..2d165b974 100644 --- a/src/lib/database/gitSources.ts +++ b/src/lib/database/gitSources.ts @@ -1,14 +1,31 @@ import { decrypt, encrypt } from '$lib/crypto'; import { prisma } from './common'; +import type { GithubApp, GitlabApp, GitSource, Prisma, Application } from '@prisma/client'; -export async function listSources(teamId) { +export async function listSources( + teamId: string | Prisma.StringFilter +): Promise<(GitSource & { githubApp?: GithubApp; gitlabApp?: GitlabApp })[]> { return await prisma.gitSource.findMany({ where: { teams: { some: { id: teamId } } }, include: { githubApp: true, gitlabApp: true } }); } -export async function newSource({ name, teamId, type, htmlUrl, apiUrl, organization }) { +export async function newSource({ + name, + teamId, + type, + htmlUrl, + apiUrl, + organization +}: { + name: string; + teamId: string; + type: string; + htmlUrl: string; + apiUrl: string; + organization: string; +}): Promise { return await prisma.gitSource.create({ data: { teams: { connect: { id: teamId } }, @@ -20,7 +37,7 @@ export async function newSource({ name, teamId, type, htmlUrl, apiUrl, organizat } }); } -export async function removeSource({ id }) { +export async function removeSource({ id }: { id: string }): Promise { // TODO: Disconnect application with this sourceId! Maybe not needed? const source = await prisma.gitSource.delete({ where: { id }, @@ -30,8 +47,14 @@ export async function removeSource({ id }) { if (source.gitlabAppId) await prisma.gitlabApp.delete({ where: { id: source.gitlabAppId } }); } -export async function getSource({ id, teamId }) { - let body = await prisma.gitSource.findFirst({ +export async function getSource({ + id, + teamId +}: { + id: string; + teamId: string; +}): Promise { + const body = await prisma.gitSource.findFirst({ where: { id, teams: { some: { id: teamId } } }, include: { githubApp: true, gitlabApp: true } }); @@ -43,27 +66,53 @@ export async function getSource({ id, teamId }) { if (body?.gitlabApp?.appSecret) body.gitlabApp.appSecret = decrypt(body.gitlabApp.appSecret); return body; } -export async function addSource({ id, appId, teamId, oauthId, groupName, appSecret }) { - const encrptedAppSecret = encrypt(appSecret); +export async function addSource({ + id, + appId, + teamId, + oauthId, + groupName, + appSecret +}: { + id: string; + appId: string; + teamId: string; + oauthId: number; + groupName: string; + appSecret: string; +}): Promise { + const encryptedAppSecret = encrypt(appSecret); return await prisma.gitlabApp.create({ data: { teams: { connect: { id: teamId } }, appId, oauthId, groupName, - appSecret: encrptedAppSecret, + appSecret: encryptedAppSecret, gitSource: { connect: { id } } } }); } -export async function configureGitsource({ id, gitSourceId }) { +export async function configureGitsource({ + id, + gitSourceId +}: { + id: string; + gitSourceId: string; +}): Promise { return await prisma.application.update({ where: { id }, data: { gitSource: { connect: { id: gitSourceId } } } }); } -export async function updateGitsource({ id, name }) { +export async function updateGitsource({ + id, + name +}: { + id: string; + name: string; +}): Promise { return await prisma.gitSource.update({ where: { id }, data: { name }