fix: correct character:updated broadcast payload and existence check order
- Broadcast enriched DB row (with parsed overrides) instead of raw req.body - Check affectedRows from UPDATE to detect missing characters before SELECT - Import ExecuteValues from mysql2 at the top level instead of inline cast
This commit is contained in:
parent
385d9b6e9e
commit
ec75add5b7
1 changed files with 13 additions and 11 deletions
|
|
@ -1,6 +1,6 @@
|
|||
import { Router } from "express";
|
||||
import type { ParamsDictionary } from "express-serve-static-core";
|
||||
import type { RowDataPacket, ResultSetHeader } from "mysql2";
|
||||
import type { RowDataPacket, ResultSetHeader, ExecuteValues } from "mysql2";
|
||||
import type { Server } from "socket.io";
|
||||
import db from "../db.js";
|
||||
import { broadcastToCampaign } from "../socket.js";
|
||||
|
|
@ -156,23 +156,25 @@ router.patch("/:id", async (req, res) => {
|
|||
}
|
||||
|
||||
values.push(id);
|
||||
await db.execute(`UPDATE characters SET ${updates.join(", ")} WHERE id = ?`, values as import("mysql2").ExecuteValues);
|
||||
const [updateResult] = await db.execute<ResultSetHeader>(`UPDATE characters SET ${updates.join(", ")} WHERE id = ?`, values as ExecuteValues);
|
||||
if (updateResult.affectedRows === 0) {
|
||||
res.status(404).json({ error: "Character not found" });
|
||||
return;
|
||||
}
|
||||
|
||||
const [rows] = await db.execute<RowDataPacket[]>(
|
||||
"SELECT * FROM characters WHERE id = ?",
|
||||
[id]
|
||||
);
|
||||
if (rows.length === 0) {
|
||||
res.status(404).json({ error: "Character not found" });
|
||||
return;
|
||||
}
|
||||
|
||||
const enriched = {
|
||||
...rows[0],
|
||||
overrides: parseJson(rows[0].overrides),
|
||||
};
|
||||
|
||||
const io: Server = req.app.get("io");
|
||||
broadcastToCampaign(io, Number(rows[0].campaign_id), "character:updated", {
|
||||
id: Number(id),
|
||||
...req.body,
|
||||
});
|
||||
res.json(rows[0]);
|
||||
broadcastToCampaign(io, Number(rows[0].campaign_id), "character:updated", enriched);
|
||||
res.json(enriched);
|
||||
} catch (err) {
|
||||
console.error(err);
|
||||
res.status(500).json({ error: "Internal server error" });
|
||||
|
|
|
|||
Loading…
Add table
Reference in a new issue