feat: add door_key field to items and update related logic in forms and database
This commit is contained in:
@@ -36,7 +36,8 @@ export const editItemById = async (
|
||||
itemId,
|
||||
item_name,
|
||||
can_borrow_role,
|
||||
safe_nr
|
||||
safe_nr,
|
||||
door_key
|
||||
) => {
|
||||
let newSafeNr;
|
||||
if (safe_nr === null || safe_nr === "") {
|
||||
@@ -45,8 +46,8 @@ export const editItemById = async (
|
||||
newSafeNr = safe_nr;
|
||||
}
|
||||
const [result] = await pool.query(
|
||||
"UPDATE items SET item_name = ?, can_borrow_role = ?, safe_nr = ?, entry_updated_at = NOW() WHERE id = ?",
|
||||
[item_name, can_borrow_role, newSafeNr, itemId]
|
||||
"UPDATE items SET item_name = ?, can_borrow_role = ?, safe_nr = ?, door_key = ?, entry_updated_at = NOW() WHERE id = ?",
|
||||
[item_name, can_borrow_role, newSafeNr, door_key, itemId]
|
||||
);
|
||||
if (result.affectedRows > 0) return { success: true };
|
||||
return { success: false };
|
||||
|
||||
@@ -41,13 +41,14 @@ router.post("/create-item", authenticateAdmin, async (req, res) => {
|
||||
|
||||
router.post("/edit-item/:id", authenticateAdmin, async (req, res) => {
|
||||
const itemId = req.params.id;
|
||||
const { item_name, can_borrow_role, safe_nr } = req.body;
|
||||
const { item_name, can_borrow_role, safe_nr, door_key } = req.body;
|
||||
|
||||
const result = await editItemById(
|
||||
itemId,
|
||||
item_name,
|
||||
can_borrow_role,
|
||||
safe_nr
|
||||
safe_nr,
|
||||
door_key
|
||||
);
|
||||
if (result.success) {
|
||||
return res.status(200).json({ message: "Item edited successfully" });
|
||||
|
||||
@@ -117,11 +117,19 @@ export const getAllLoansV2 = async () => {
|
||||
|
||||
export const openDoor = async (doorKey) => {
|
||||
const [result] = await pool.query(
|
||||
"SELECT lockers FROM doorKeys WHERE door_key = ?;",
|
||||
"SELECT safe_nr, id FROM items WHERE door_key = ?;",
|
||||
[doorKey]
|
||||
);
|
||||
if (result.length > 0) {
|
||||
return { success: true, data: result[0] };
|
||||
const [changeItemSate] = await pool.query(
|
||||
"UPDATE items SET in_safe = NOT in_safe WHERE id = ?",
|
||||
[result[0].id]
|
||||
);
|
||||
if (changeItemSate.affectedRows > 0) {
|
||||
return { success: true, data: result[0] };
|
||||
} else {
|
||||
return { success: false };
|
||||
}
|
||||
}
|
||||
return { success: false };
|
||||
};
|
||||
|
||||
@@ -80,6 +80,7 @@ router.post(
|
||||
}
|
||||
);
|
||||
|
||||
// Route for API to open a door
|
||||
router.get("/open-door/:key/:doorKey", authenticate, async (req, res) => {
|
||||
const doorKey = req.params.doorKey;
|
||||
|
||||
|
||||
Reference in New Issue
Block a user