From 9fdac2741ad990cad3de88db9993cba2916a9b86 Mon Sep 17 00:00:00 2001 From: dominicbachmann Date: Tue, 5 Apr 2022 20:48:33 +0200 Subject: [PATCH] Improved typing and quality of applications.ts --- src/lib/database/applications.ts | 170 ++++++++++++++++++------------- src/lib/types/builderJob.ts | 8 +- 2 files changed, 105 insertions(+), 73 deletions(-) diff --git a/src/lib/database/applications.ts b/src/lib/database/applications.ts index 1d8140144..42887905e 100644 --- a/src/lib/database/applications.ts +++ b/src/lib/database/applications.ts @@ -1,14 +1,29 @@ import { decrypt, encrypt } from '$lib/crypto'; import { asyncExecShell, getEngine } from '$lib/common'; -import { getDomain, removeDestinationDocker } from '$lib/common'; +import { removeDestinationDocker } from '$lib/common'; import { prisma } from './common'; -export async function listApplications(teamId) { +import type { + DestinationDocker, + GitSource, + Secret, + ApplicationSettings, + Application, + ApplicationPersistentStorage +} from '@prisma/client'; + +export async function listApplications(teamId: string): Promise { return await prisma.application.findMany({ where: { teams: { some: { id: teamId } } } }); } -export async function newApplication({ name, teamId }) { +export async function newApplication({ + name, + teamId +}: { + name: string; + teamId: string; +}): Promise { return await prisma.application.create({ data: { name, @@ -18,34 +33,17 @@ export async function newApplication({ name, teamId }) { }); } -export async function importApplication({ - name, - teamId, - fqdn, - port, - buildCommand, - startCommand, - installCommand -}) { - return await prisma.application.create({ - data: { - name, - fqdn, - port, - buildCommand, - startCommand, - installCommand, - teams: { connect: { id: teamId } } - } - }); -} - -export async function removeApplication({ id, teamId }) { - const { fqdn, destinationDockerId, destinationDocker } = await prisma.application.findUnique({ +export async function removeApplication({ + id, + teamId +}: { + id: string; + teamId: string; +}): Promise { + const { destinationDockerId, destinationDocker } = await prisma.application.findUnique({ where: { id }, include: { destinationDocker: true } }); - const domain = getDomain(fqdn); if (destinationDockerId) { const host = getEngine(destinationDocker.engine); const { stdout: containers } = await asyncExecShell( @@ -56,7 +54,6 @@ export async function removeApplication({ id, teamId }) { for (const container of containersArray) { const containerObj = JSON.parse(container); const id = containerObj.ID; - const preview = containerObj.Image.split('-')[1]; await removeDestinationDocker({ id, engine: destinationDocker.engine }); } } @@ -70,9 +67,23 @@ export async function removeApplication({ id, teamId }) { await prisma.application.deleteMany({ where: { id, teams: { some: { id: teamId } } } }); } -export async function getApplicationWebhook({ projectId, branch }) { +export async function getApplicationWebhook({ + projectId, + branch +}: { + projectId: number; + branch: string; +}): Promise< + Application & { + destinationDocker: DestinationDocker; + settings: ApplicationSettings; + gitSource: GitSource; + secrets: Secret[]; + persistentStorage: ApplicationPersistentStorage[]; + } +> { try { - let application = await prisma.application.findFirst({ + const application = await prisma.application.findFirst({ where: { projectId, branch, settings: { autodeploy: true } }, include: { destinationDocker: true, @@ -121,16 +132,23 @@ export async function getApplicationWebhook({ projectId, branch }) { throw { status: 404, body: { message: e.message } }; } } -export async function getApplicationById({ id }) { - const body = await prisma.application.findFirst({ - where: { id }, - include: { destinationDocker: true } - }); - return { ...body }; -} -export async function getApplication({ id, teamId }) { - let body = await prisma.application.findFirst({ +export async function getApplication({ + id, + teamId +}: { + id: string; + teamId: string; +}): Promise< + Application & { + destinationDocker: DestinationDocker; + settings: ApplicationSettings; + gitSource: GitSource; + secrets: Secret[]; + persistentStorage: ApplicationPersistentStorage[]; + } +> { + const body = await prisma.application.findFirst({ where: { id, teams: { some: { id: teamId } } }, include: { destinationDocker: true, @@ -170,7 +188,14 @@ export async function configureGitRepository({ projectId, webhookToken, autodeploy -}) { +}: { + id: string; + repository: string; + branch: string; + projectId: number; + webhookToken: string; + autodeploy: boolean; +}): Promise { if (webhookToken) { const encryptedWebhookToken = encrypt(webhookToken); await prisma.application.update({ @@ -200,7 +225,10 @@ export async function configureGitRepository({ } } -export async function configureBuildPack({ id, buildPack }) { +export async function configureBuildPack({ + id, + buildPack +}: Pick): Promise { return await prisma.application.update({ where: { id }, data: { buildPack } }); } @@ -218,7 +246,21 @@ export async function configureApplication({ pythonWSGI, pythonModule, pythonVariable -}) { +}: { + id: string; + buildPack: string; + name: string; + fqdn: string; + port: number; + installCommand: string; + buildCommand: string; + startCommand: string; + baseDirectory: string; + publishDirectory: string; + pythonWSGI: string; + pythonModule: string; + pythonVariable: string; +}): Promise { return await prisma.application.update({ where: { id }, data: { @@ -238,11 +280,24 @@ export async function configureApplication({ }); } -export async function checkDoubleBranch(branch, projectId) { +export async function checkDoubleBranch(branch: string, projectId: number): Promise { const applications = await prisma.application.findMany({ where: { branch, projectId } }); return applications.length > 1; } -export async function setApplicationSettings({ id, debug, previews, dualCerts, autodeploy }) { + +export async function setApplicationSettings({ + id, + debug, + previews, + dualCerts, + autodeploy +}: { + id: string; + debug: boolean; + previews: boolean; + dualCerts: boolean; + autodeploy: boolean; +}): Promise { return await prisma.application.update({ where: { id }, data: { settings: { update: { debug, previews, dualCerts, autodeploy } } }, @@ -250,29 +305,6 @@ export async function setApplicationSettings({ id, debug, previews, dualCerts, a }); } -export async function createBuild({ - id, - applicationId, - destinationDockerId, - gitSourceId, - githubAppId, - gitlabAppId, - type -}) { - return await prisma.build.create({ - data: { - id, - applicationId, - destinationDockerId, - gitSourceId, - githubAppId, - gitlabAppId, - status: 'running', - type - } - }); -} - -export async function getPersistentStorage(id) { +export async function getPersistentStorage(id: string): Promise { return await prisma.applicationPersistentStorage.findMany({ where: { applicationId: id } }); } diff --git a/src/lib/types/builderJob.ts b/src/lib/types/builderJob.ts index 0f96520e5..1a1652f8a 100644 --- a/src/lib/types/builderJob.ts +++ b/src/lib/types/builderJob.ts @@ -17,10 +17,10 @@ export type BuilderJob = { startCommand?: string; baseDirectory: string; publishDirectory: string; - phpModules: unknown; // probably an array of some type; - pythonWSGI: unknown; // probably a string?; - pythonModule: unknown; // probably a string?; - pythonVariable: unknown; // probably a string?; + phpModules: string; + pythonWSGI: string; + pythonModule: string; + pythonVariable: string; createdAt: string; updatedAt: string; destinationDockerId: string;