feature: Changes backend creation to use checks, and to automatically enable.
This commit is contained in:
parent
f7f2fc7826
commit
5bfbf5cb05
5 changed files with 105 additions and 45 deletions
|
@ -1,11 +1,15 @@
|
|||
import { hasPermissionByToken } from "../../libs/permissions.js";
|
||||
import type { RouteOptions } from "../../libs/types.js";
|
||||
|
||||
import { backendProviders } from "../../backendimpl/index.js";
|
||||
import { backendInit } from "../../libs/backendInit.js";
|
||||
|
||||
export function route(routeOptions: RouteOptions) {
|
||||
const {
|
||||
fastify,
|
||||
prisma,
|
||||
tokens
|
||||
tokens,
|
||||
backends
|
||||
} = routeOptions;
|
||||
|
||||
function hasPermission(token: string, permissionList: string[]): Promise<boolean> {
|
||||
|
@ -47,6 +51,21 @@ export function route(routeOptions: RouteOptions) {
|
|||
});
|
||||
};
|
||||
|
||||
if (!backendProviders[body.backend]) {
|
||||
return res.status(400).send({
|
||||
error: "Unknown/unsupported/deprecated backend!"
|
||||
});
|
||||
};
|
||||
|
||||
const connectionDetails = JSON.stringify(body.connectionDetails);
|
||||
const connectionDetailsValidityCheck = backendProviders[body.backend].checkParametersBackendInstance(connectionDetails);
|
||||
|
||||
if (!connectionDetailsValidityCheck.success) {
|
||||
return res.status(400).send({
|
||||
error: connectionDetailsValidityCheck.message ?? "Unknown error while attempting to parse connectionDetails (it's on your side)"
|
||||
});
|
||||
};
|
||||
|
||||
const backend = await prisma.desinationProvider.create({
|
||||
data: {
|
||||
name: body.name,
|
||||
|
@ -57,6 +76,16 @@ export function route(routeOptions: RouteOptions) {
|
|||
}
|
||||
});
|
||||
|
||||
const init = await backendInit(backend, backends, prisma);
|
||||
|
||||
if (!init) {
|
||||
// TODO: better error code
|
||||
return res.status(504).send({
|
||||
error: "Backend is created, but failed to initalize correctly",
|
||||
id: backend.id
|
||||
});
|
||||
}
|
||||
|
||||
return {
|
||||
success: true,
|
||||
id: backend.id
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue