feature: Adds removing API.
This commit is contained in:
parent
68b5a9ff77
commit
2ae917acd9
13 changed files with 165 additions and 8 deletions
52
src/routes/backends/remove.ts
Normal file
52
src/routes/backends/remove.ts
Normal 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
|
||||
}
|
||||
});
|
||||
}
|
Loading…
Add table
Add a link
Reference in a new issue