feature: Adds removing API.

This commit is contained in:
greysoh 2024-04-22 11:03:51 -04:00
parent 68b5a9ff77
commit 2ae917acd9
Signed by: imterah
GPG key ID: 8FA7DD57BA6CEA37
13 changed files with 165 additions and 8 deletions

View file

@ -0,0 +1,52 @@
import type { PrismaClient } from "@prisma/client";
import type { FastifyInstance } from "fastify";
import { ServerOptions, SessionToken } from "../../libs/types.js";
import { hasPermissionByToken } from "../../libs/permissions.js";
export function route(fastify: FastifyInstance, prisma: PrismaClient, tokens: Record<number, SessionToken[]>, options: ServerOptions) {
function hasPermission(token: string, permissionList: string[]): Promise<boolean> {
return hasPermissionByToken(permissionList, token, tokens, prisma);
};
/**
* Creates a new route to use
*/
fastify.post("/api/v1/backends/remove", {
schema: {
body: {
type: "object",
required: ["token", "id"],
properties: {
token: { type: "string" },
id: { type: "number" }
}
}
}
}, async(req, res) => {
// @ts-ignore
const body: {
token: string,
id: number
} = req.body;
if (!await hasPermission(body.token, [
"backends.remove"
])) {
return res.status(403).send({
error: "Unauthorized"
});
};
await prisma.desinationProvider.delete({
where: {
id: body.id
}
});
return {
success: true
}
});
}