Skip to content

Commit

Permalink
fix: send latest created roles first
Browse files Browse the repository at this point in the history
  • Loading branch information
Chakravarthy7102 committed Nov 14, 2023
1 parent 3cb4c2e commit 362da88
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 26 deletions.
23 changes: 10 additions & 13 deletions lib/build/recipe/dashboard/api/userroles/roles/getAllRoles.js
Original file line number Diff line number Diff line change
Expand Up @@ -43,15 +43,11 @@ const getAllRoles = async (_, __, options, ____) => {
const response = await userroles_1.default.getAllRoles();
const totalPages = Math.ceil(response.roles.length / limit);
const rolesCount = response.roles.length;
if (page > totalPages) {
return {
roles: [],
rolesCount,
totalPages,
status: "OK",
};
if (page > totalPages && page !== 1) {
throw new Error("Please provide a valid page number");
}
const paginatedRoles = response.roles.slice(skip, skip + limit);
//reversing the roles to show latest created roles at first.
const paginatedRoles = response.roles.reverse().slice(skip, skip + limit);
let roles = [];
for (let i = 0; i < paginatedRoles.length; i++) {
const role = paginatedRoles[i];
Expand All @@ -64,10 +60,7 @@ const getAllRoles = async (_, __, options, ____) => {
});
} else {
//this case should never happen.
roles.push({
role,
permissions: [],
});
throw new Error("Should never come here.");
}
} catch (_) {}
}
Expand All @@ -79,7 +72,11 @@ const getAllRoles = async (_, __, options, ____) => {
};
} else {
const response = await userroles_1.default.getAllRoles();
return response;
//reversing the roles to show latest created roles at first.
return {
status: "OK",
roles: response.roles.reverse(),
};
}
};
exports.default = getAllRoles;
24 changes: 11 additions & 13 deletions lib/ts/recipe/dashboard/api/userroles/roles/getAllRoles.ts
Original file line number Diff line number Diff line change
Expand Up @@ -69,15 +69,12 @@ const getAllRoles: APIFunction = async (
const totalPages = Math.ceil(response.roles.length / limit);
const rolesCount = response.roles.length;

if (page > totalPages) {
return {
roles: [],
rolesCount,
totalPages,
status: "OK",
};
if (page > totalPages && page !== 1) {
throw new Error("Please provide a valid page number");
}
const paginatedRoles = response.roles.slice(skip, skip + limit);

//reversing the roles to show latest created roles at first.
const paginatedRoles = response.roles.reverse().slice(skip, skip + limit);

let roles: Roles = [];

Expand All @@ -93,10 +90,7 @@ const getAllRoles: APIFunction = async (
});
} else {
//this case should never happen.
roles.push({
role,
permissions: [],
});
throw new Error("Should never come here.");
}
} catch (_) {}
}
Expand All @@ -109,7 +103,11 @@ const getAllRoles: APIFunction = async (
};
} else {
const response = await UserRoles.getAllRoles();
return response;
//reversing the roles to show latest created roles at first.
return {
status: "OK",
roles: response.roles.reverse(),
};
}
};

Expand Down

0 comments on commit 362da88

Please sign in to comment.