Improved typing and quality of applications.ts

This commit is contained in:
dominicbachmann 2022-04-05 20:48:33 +02:00
parent 8fb5260809
commit 9fdac2741a
2 changed files with 105 additions and 73 deletions

View File

@ -1,14 +1,29 @@
import { decrypt, encrypt } from '$lib/crypto'; import { decrypt, encrypt } from '$lib/crypto';
import { asyncExecShell, getEngine } from '$lib/common'; import { asyncExecShell, getEngine } from '$lib/common';
import { getDomain, removeDestinationDocker } from '$lib/common'; import { removeDestinationDocker } from '$lib/common';
import { prisma } from './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<Application[]> {
return await prisma.application.findMany({ where: { teams: { some: { id: teamId } } } }); 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<Application> {
return await prisma.application.create({ return await prisma.application.create({
data: { data: {
name, name,
@ -18,34 +33,17 @@ export async function newApplication({ name, teamId }) {
}); });
} }
export async function importApplication({ export async function removeApplication({
name, id,
teamId, teamId
fqdn, }: {
port, id: string;
buildCommand, teamId: string;
startCommand, }): Promise<void> {
installCommand const { destinationDockerId, destinationDocker } = await prisma.application.findUnique({
}) {
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({
where: { id }, where: { id },
include: { destinationDocker: true } include: { destinationDocker: true }
}); });
const domain = getDomain(fqdn);
if (destinationDockerId) { if (destinationDockerId) {
const host = getEngine(destinationDocker.engine); const host = getEngine(destinationDocker.engine);
const { stdout: containers } = await asyncExecShell( const { stdout: containers } = await asyncExecShell(
@ -56,7 +54,6 @@ export async function removeApplication({ id, teamId }) {
for (const container of containersArray) { for (const container of containersArray) {
const containerObj = JSON.parse(container); const containerObj = JSON.parse(container);
const id = containerObj.ID; const id = containerObj.ID;
const preview = containerObj.Image.split('-')[1];
await removeDestinationDocker({ id, engine: destinationDocker.engine }); 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 } } } }); 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 { try {
let application = await prisma.application.findFirst({ const application = await prisma.application.findFirst({
where: { projectId, branch, settings: { autodeploy: true } }, where: { projectId, branch, settings: { autodeploy: true } },
include: { include: {
destinationDocker: true, destinationDocker: true,
@ -121,16 +132,23 @@ export async function getApplicationWebhook({ projectId, branch }) {
throw { status: 404, body: { message: e.message } }; 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
}: {
id: string;
teamId: string;
}): Promise<
Application & {
destinationDocker: DestinationDocker;
settings: ApplicationSettings;
gitSource: GitSource;
secrets: Secret[];
persistentStorage: ApplicationPersistentStorage[];
} }
export async function getApplication({ id, teamId }) { > {
let body = await prisma.application.findFirst({ const body = await prisma.application.findFirst({
where: { id, teams: { some: { id: teamId } } }, where: { id, teams: { some: { id: teamId } } },
include: { include: {
destinationDocker: true, destinationDocker: true,
@ -170,7 +188,14 @@ export async function configureGitRepository({
projectId, projectId,
webhookToken, webhookToken,
autodeploy autodeploy
}) { }: {
id: string;
repository: string;
branch: string;
projectId: number;
webhookToken: string;
autodeploy: boolean;
}): Promise<void> {
if (webhookToken) { if (webhookToken) {
const encryptedWebhookToken = encrypt(webhookToken); const encryptedWebhookToken = encrypt(webhookToken);
await prisma.application.update({ 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<Application, 'id' | 'buildPack'>): Promise<Application> {
return await prisma.application.update({ where: { id }, data: { buildPack } }); return await prisma.application.update({ where: { id }, data: { buildPack } });
} }
@ -218,7 +246,21 @@ export async function configureApplication({
pythonWSGI, pythonWSGI,
pythonModule, pythonModule,
pythonVariable 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<Application> {
return await prisma.application.update({ return await prisma.application.update({
where: { id }, where: { id },
data: { data: {
@ -238,11 +280,24 @@ export async function configureApplication({
}); });
} }
export async function checkDoubleBranch(branch, projectId) { export async function checkDoubleBranch(branch: string, projectId: number): Promise<boolean> {
const applications = await prisma.application.findMany({ where: { branch, projectId } }); const applications = await prisma.application.findMany({ where: { branch, projectId } });
return applications.length > 1; 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<Application & { destinationDocker: DestinationDocker }> {
return await prisma.application.update({ return await prisma.application.update({
where: { id }, where: { id },
data: { settings: { update: { debug, previews, dualCerts, autodeploy } } }, data: { settings: { update: { debug, previews, dualCerts, autodeploy } } },
@ -250,29 +305,6 @@ export async function setApplicationSettings({ id, debug, previews, dualCerts, a
}); });
} }
export async function createBuild({ export async function getPersistentStorage(id: string): Promise<ApplicationPersistentStorage[]> {
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) {
return await prisma.applicationPersistentStorage.findMany({ where: { applicationId: id } }); return await prisma.applicationPersistentStorage.findMany({ where: { applicationId: id } });
} }

View File

@ -17,10 +17,10 @@ export type BuilderJob = {
startCommand?: string; startCommand?: string;
baseDirectory: string; baseDirectory: string;
publishDirectory: string; publishDirectory: string;
phpModules: unknown; // probably an array of some type; phpModules: string;
pythonWSGI: unknown; // probably a string?; pythonWSGI: string;
pythonModule: unknown; // probably a string?; pythonModule: string;
pythonVariable: unknown; // probably a string?; pythonVariable: string;
createdAt: string; createdAt: string;
updatedAt: string; updatedAt: string;
destinationDockerId: string; destinationDockerId: string;