From 765b47391dea02d08854c522117a62edac463d16 Mon Sep 17 00:00:00 2001 From: alubert Date: Mon, 17 Feb 2025 14:11:21 +0100 Subject: [PATCH] feat(api): returns userData identifier when the insert was successul --- api/v1/paths/users/data.js | 24 ++++++++++++++++++++++-- model/userData.js | 6 ++++-- tests/model.userData.test.js | 5 +++-- 3 files changed, 29 insertions(+), 6 deletions(-) diff --git a/api/v1/paths/users/data.js b/api/v1/paths/users/data.js index d0d3471f4..a2554a9f9 100644 --- a/api/v1/paths/users/data.js +++ b/api/v1/paths/users/data.js @@ -44,8 +44,12 @@ module.exports = () => { try { const userData = await cleanUserData.clean(req.body); const userInfo = await userManager.getUser(req.user); - await userDataModel.insert({ ...userData, owned_by: userInfo.user.login }); - res.status(200).json({ message: "The resource has been inserted successfully" }); + const results = await userDataModel.insert({ ...userData, owned_by: userInfo.user.login }); + if (results.length === 1) { + res.status(200).json({ message: "The resource has been inserted successfully", id: results[0].id }); + } else { + res.status(422).json({ message: "The resource cannot be insert in the database" }); + } } catch (error) { if (error.cause !== undefined) { res.status(error.cause).json({ message: error.message }); @@ -76,6 +80,10 @@ module.exports = () => { type: "string", default: "The resource has been inserted successfully", }, + id: { + type: "number", + default: 1, + }, }, }, }, @@ -91,6 +99,18 @@ module.exports = () => { }, }, }, + 422: { + description: "The resource cannot be insert in the database", + schema: { + type: "object", + properties: { + message: { + type: "string", + default: "The resource cannot be insert in the database", + }, + }, + }, + }, 500: { description: "An error occurs on the server", schema: { diff --git a/model/userData.js b/model/userData.js index cebcbe421..cf3be25bc 100644 --- a/model/userData.js +++ b/model/userData.js @@ -80,14 +80,16 @@ class UserDataModel { const data = this._check(userData); const connection = getKnexConnection(this._mainConfig.database); - const results = await connection.select("id").from("users").where("login", data.owned_by).first(); + let results = await connection.select("id").from("users").where("login", data.owned_by).first(); if (results === undefined) { cleanupConnection(connection); throw Error("The specified owned_by username do not exists", { cause: 404 }); } data.owned_by = results.id; - await connection.insert(data).into("user_data"); + results = await connection.insert(data, ["id"]).into("user_data"); cleanupConnection(connection); + + return results; }; remove = async (identifier) => { diff --git a/tests/model.userData.test.js b/tests/model.userData.test.js index 108a9ce15..73ccd5f00 100644 --- a/tests/model.userData.test.js +++ b/tests/model.userData.test.js @@ -147,14 +147,15 @@ describe("UserDataModel", () => { tracker.on.select("user_data_list").response(dbUserDataList); tracker.on.select("users").response(dbUsers); tracker.on.select("profiles").response(dbProfiles); - tracker.on.insert("user_data").response([]); + tracker.on.insert("user_data").response([{id: 6}]); const addUserData = { data_path: "data_path", data_type: "data_type", owned_by: "test", } - await userDataModel.insert(addUserData); + const results = await userDataModel.insert(addUserData); + expect(results[0].id).toStrictEqual(6); const updatedUserData = Array.from(dbUserDataList); updatedUserData.push(addUserData);