/* global defineEventHandler, readBody, createError */ import { ResultSetHeader } from "mysql2"; import { checkIsClient } from "../clients.post"; import { client } from "~/utils/types/database"; import { database, data } from "~/server/utils/database"; export default defineEventHandler(async (e) => { const body = await readBody(e); const id = e.context.params?.id as string; if (!checkIsClient(body, false)) return; // checkIsClient already throws an detailed error for (const [k, v] of Object.entries(body)) { const [res] = await database.query( // I believe it is safe to put key in the template // because it is limited to 4 values here `UPDATE \`clients\` SET \`${k}\` = ? WHERE \`id\` = ?`, [v, id], ) as unknown as [ResultSetHeader]; if (res.affectedRows !== 1) { throw createError({ statusCode: 404, }); } } const [data] = await database.query( "SELECT *, CONVERT(`id`, CHAR) AS `id` FROM `clients` WHERE `id` = ?", [id], ) as unknown as data; return data[0]; });