diff --git a/api/src/put/blacklist.ts b/api/src/put/blacklist.ts index 6e545c8..2b08d9e 100644 --- a/api/src/put/blacklist.ts +++ b/api/src/put/blacklist.ts @@ -6,71 +6,69 @@ type BlacklistUpdateBody = { oldVersion: string newVersion: string ecosystem: string + comment: string } -export default async function blacklistPutHandler( - req: FastifyRequest<{ Params: BlacklistUpdateBody }>, - res: FastifyReply -) { - const { ecosystem, name, oldVersion, newVersion } = req.params +export default async function blacklistPutHandler(req: FastifyRequest, res: FastifyReply) { + const { ecosystem, name, oldVersion, newVersion, comment } = req.body as BlacklistUpdateBody - if (!ecosystem || !name || !oldVersion || !newVersion) { - return res - .status(400) - .send({ error: "Missing name, oldVersion, newVersion, or ecosystem." }) - } + if (!ecosystem || !name || !oldVersion || !newVersion || !comment) { + return res + .status(400) + .send({ error: "Missing name, oldVersion, newVersion, ecosystem, comment." }) + } - try { - console.log( - `Replacing blacklist version: name=${name}, oldVersion=${oldVersion}, newVersion=${newVersion}, ecosystem=${ecosystem}` - ) + try { + console.log( + `Replacing blacklist version: name=${name}, oldVersion=${oldVersion}, newVersion=${newVersion}, ecosystem=${ecosystem}, comment=${comment}` + ) - await runInTransaction(async (client) => { - const checkExists = await client.query( - "SELECT name FROM blacklist WHERE name = $1;", - [name] - ) - if (checkExists.rowCount === 0) { - throw new Error("Blacklist entry not found.") - } + await runInTransaction(async (client) => { + const checkExists = await client.query( + "SELECT name FROM blacklist WHERE name = $1;", + [name] + ) + if (checkExists.rowCount === 0) { + throw new Error("Blacklist entry not found.") + } - const updateResult = await client.query( - ` - UPDATE blacklist_versions - SET version = $3 - WHERE name = $1 - AND version = $2; - `, - [name, oldVersion, newVersion] - ) + const updateResult = await client.query( + ` + UPDATE blacklist_versions + SET version = $3 + WHERE name = $1 + AND version = $2; + `, + [name, oldVersion, newVersion] + ) - if (updateResult.rowCount === 0) { - await client.query( - ` - INSERT INTO blacklist_versions (name, version) - VALUES ($1, $2); - `, - [name, newVersion] - ) - } + if (updateResult.rowCount === 0) { + await client.query( + ` + INSERT INTO blacklist_versions (name, version) + VALUES ($1, $2); + `, + [name, newVersion] + ) + } - await client.query( - ` - INSERT INTO blacklist_ecosystems (name, ecosystem) - VALUES ($1, $2) - ON CONFLICT (name, ecosystem) - DO UPDATE SET ecosystem = EXCLUDED.ecosystem; - `, - [name, ecosystem] - ) - }) + await client.query( + ` + INSERT INTO blacklist_ecosystems (name, ecosystem) + VALUES ($1, $2) + ON CONFLICT (name, ecosystem) + DO UPDATE SET ecosystem = EXCLUDED.ecosystem; + `, + [name, ecosystem] + ) + }) - return res.send({ message: "Blacklist entry updated successfully." }) - } catch (error: any) { - console.error("Database error:", error) - if (error.message.includes("not found")) { - return res.status(404).send({ error: error.message }) + return res.send({ message: "Blacklist entry updated successfully." }) + } catch (error: any) { + console.error("Database error:", error) + if (error.message.includes("not found")) { + return res.status(404).send({ error: error.message }) + } + return res.status(500).send({ error: "Internal Server Error" }) } - return res.status(500).send({ error: "Internal Server Error" }) - } } diff --git a/api/src/put/whitelist.ts b/api/src/put/whitelist.ts index aa382e4..8201197 100644 --- a/api/src/put/whitelist.ts +++ b/api/src/put/whitelist.ts @@ -2,75 +2,73 @@ import { FastifyReply, FastifyRequest } from "fastify" import { runInTransaction } from "../db.js" type WhitelistUpdateBody = { - name: string - oldVersion: string - newVersion: string - ecosystem: string + name: string + oldVersion: string + newVersion: string + ecosystem: string + comment: string } -export default async function whitelistPutHandler( - req: FastifyRequest<{ Params: WhitelistUpdateBody }>, - res: FastifyReply -) { - const { ecosystem, name, oldVersion, newVersion } = req.params +export default async function whitelistPutHandler(req: FastifyRequest, res: FastifyReply) { + const { ecosystem, name, oldVersion, newVersion, comment } = req.body as WhitelistUpdateBody - if (!ecosystem || !name || !oldVersion || !newVersion) { - return res - .status(400) - .send({ error: "Missing name, oldVersion, newVersion, or ecosystem." }) - } + if (!ecosystem || !name || !oldVersion || !newVersion || !comment) { + return res + .status(400) + .send({ error: "Missing name, oldVersion, newVersion, ecosystem, comment." }) + } - try { - console.log( - `Replacing whitelist version: name=${name}, oldVersion=${oldVersion}, newVersion=${newVersion}, ecosystem=${ecosystem}` - ) + try { + console.log( + `Replacing whitelist version: name=${name}, oldVersion=${oldVersion}, newVersion=${newVersion}, ecosystem=${ecosystem}, comment=${comment}` + ) - await runInTransaction(async (client) => { - const checkExists = await client.query( - "SELECT name FROM white WHERE name = $1;", - [name] - ) - if (checkExists.rowCount === 0) { - throw new Error("White entry not found.") - } + await runInTransaction(async (client) => { + const checkExists = await client.query( + "SELECT name FROM whitelist WHERE name = $1;", + [name] + ) + if (checkExists.rowCount === 0) { + throw new Error("whitelist entry not found.") + } - const updateResult = await client.query( - ` - UPDATE whitelist_versions - SET version = $3 - WHERE name = $1 - AND version = $2; - `, - [name, oldVersion, newVersion] - ) + const updateResult = await client.query( + ` + UPDATE whitelist_versions + SET version = $3 + WHERE name = $1 + AND version = $2; + `, + [name, oldVersion, newVersion] + ) - if (updateResult.rowCount === 0) { - await client.query( - ` - INSERT INTO whitelist_versions (name, version) - VALUES ($1, $2); - `, - [name, newVersion] - ) - } + if (updateResult.rowCount === 0) { + await client.query( + ` + INSERT INTO whitelist_versions (name, version) + VALUES ($1, $2); + `, + [name, newVersion] + ) + } - await client.query( - ` - INSERT INTO whitelist_ecosystems (name, ecosystem) - VALUES ($1, $2) - ON CONFLICT (name, ecosystem) - DO UPDATE SET ecosystem = EXCLUDED.ecosystem; - `, - [name, ecosystem] - ) - }) + await client.query( + ` + INSERT INTO whitelist_ecosystems (name, ecosystem) + VALUES ($1, $2) + ON CONFLICT (name, ecosystem) + DO UPDATE SET ecosystem = EXCLUDED.ecosystem; + `, + [name, ecosystem] + ) + }) - return res.send({ message: "Whitelist entry updated successfully." }) - } catch (error: any) { - console.error("Database error:", error) - if (error.message.includes("not found")) { - return res.status(404).send({ error: error.message }) + return res.send({ message: "whitelist entry updated successfully." }) + } catch (error: any) { + console.error("Database error:", error) + if (error.message.includes("not found")) { + return res.status(404).send({ error: error.message }) + } + return res.status(500).send({ error: "Internal Server Error" }) } - return res.status(500).send({ error: "Internal Server Error" }) - } }