Skip to content

Commit

Permalink
feat(api): returns userData identifier when the insert was successul
Browse files Browse the repository at this point in the history
  • Loading branch information
a-lubert committed Feb 28, 2025
1 parent 5669948 commit 765b473
Show file tree
Hide file tree
Showing 3 changed files with 29 additions and 6 deletions.
24 changes: 22 additions & 2 deletions api/v1/paths/users/data.js
Original file line number Diff line number Diff line change
Expand Up @@ -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 });
Expand Down Expand Up @@ -76,6 +80,10 @@ module.exports = () => {
type: "string",
default: "The resource has been inserted successfully",
},
id: {
type: "number",
default: 1,
},
},
},
},
Expand All @@ -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: {
Expand Down
6 changes: 4 additions & 2 deletions model/userData.js
Original file line number Diff line number Diff line change
Expand Up @@ -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) => {
Expand Down
5 changes: 3 additions & 2 deletions tests/model.userData.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down

0 comments on commit 765b473

Please sign in to comment.