Skip to content

Commit

Permalink
feat(api): filter the /users/data route with the columns query parame…
Browse files Browse the repository at this point in the history
…ters

It can be used like this: `/users/data?columns=id,data_label`
  • Loading branch information
a-lubert committed Feb 28, 2025
1 parent 09408d9 commit e0637fd
Showing 1 changed file with 25 additions and 2 deletions.
27 changes: 25 additions & 2 deletions api/v1/paths/users/data.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,15 +5,38 @@ const userManager = require("../../../../bin/user.");
module.exports = () => {
GET = async (req, res, _next) => {
try {
const data = await userDataModel.all(req.user);
let data = await userDataModel.all(req.user);

if (req.query.columns) {
const columns = req.query.columns;

data = data.map((element) =>
columns.reduce((result, key) => {
result[key.trim()] = element[key.trim()];
return result;
}, {}),
);
}

res.status(200).json(data);
} catch (error) {
console.error(error);
res.status(500).json({ message: "An error occurs on the server" });
}
};
GET.apiDoc = {
parameters: [],
parameters: [
{
in: "query",
name: "columns",
description: "filter the columns of the response",
required: false,
type: "array",
items: {
type: "string",
},
},
],
responses: {
200: {
description: "Retrieve the entire list of User Data",
Expand Down

0 comments on commit e0637fd

Please sign in to comment.