Skip to content

Commit

Permalink
Fix returing types and merge create update roles to one single api
Browse files Browse the repository at this point in the history
  • Loading branch information
Chakravarthy7102 committed Nov 8, 2023
1 parent 5811643 commit dd9ee0c
Show file tree
Hide file tree
Showing 16 changed files with 158 additions and 149 deletions.
12 changes: 9 additions & 3 deletions lib/build/recipe/dashboard/api/userroles/addRoleToUser.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,13 @@ declare const addRoleToUser: (
tenantId: string,
options: APIOptions,
__: any
) => Promise<{
status: "OK" | "UNKNOWN_ROLE_ERROR" | "FEATURE_NOT_ENABLED_ERROR";
}>;
) => Promise<
| {
status: "OK";
didUserAlreadyHaveRole: boolean;
}
| {
status: "UNKNOWN_ROLE_ERROR" | "FEATURE_NOT_ENABLED_ERROR";
}
>;
export default addRoleToUser;
12 changes: 9 additions & 3 deletions lib/build/recipe/dashboard/api/userroles/removeUserRole.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,13 @@ declare const removeUserRole: (
tenantId: string,
options: APIOptions,
__: any
) => Promise<{
status: "OK" | "UNKNOWN_ROLE_ERROR" | "FEATURE_NOT_ENABLED_ERROR";
}>;
) => Promise<
| {
status: "OK";
didUserHaveRole: boolean;
}
| {
status: "UNKNOWN_ROLE_ERROR" | "FEATURE_NOT_ENABLED_ERROR";
}
>;
export default removeUserRole;
11 changes: 0 additions & 11 deletions lib/build/recipe/dashboard/api/userroles/roles/createRole.d.ts

This file was deleted.

37 changes: 0 additions & 37 deletions lib/build/recipe/dashboard/api/userroles/roles/createRole.js

This file was deleted.

Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
// @ts-nocheck
import { APIInterface, APIOptions } from "../../../types";
declare const addPermissions: (
declare const createRoleOrAddPermissions: (
_: APIInterface,
___: string,
__: string,
options: APIOptions,
__: any
___: any
) => Promise<
| {
status: "OK";
Expand All @@ -14,4 +14,4 @@ declare const addPermissions: (
status: "FEATURE_NOT_ENABLED_ERROR";
}
>;
export default addPermissions;
export default createRoleOrAddPermissions;
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ Object.defineProperty(exports, "__esModule", { value: true });
const recipe_1 = __importDefault(require("../../../../userroles/recipe"));
const userroles_1 = __importDefault(require("../../../../userroles"));
const error_1 = __importDefault(require("../../../../../error"));
const addPermissions = async (_, ___, options, __) => {
const createRoleOrAddPermissions = async (_, __, options, ___) => {
try {
recipe_1.default.getInstanceOrThrowError();
} catch (_) {
Expand All @@ -17,8 +17,8 @@ const addPermissions = async (_, ___, options, __) => {
};
}
const requestBody = await options.req.getJSONBody();
const role = requestBody.role;
const permissions = requestBody.permissions;
const role = requestBody.role;
if (role === undefined || typeof role !== "string") {
throw new error_1.default({
message: "Required parameter 'role' is missing or has an invalid type",
Expand All @@ -34,4 +34,4 @@ const addPermissions = async (_, ___, options, __) => {
const response = await userroles_1.default.createNewRoleOrAddPermissions(role, permissions);
return response;
};
exports.default = addPermissions;
exports.default = createRoleOrAddPermissions;
12 changes: 9 additions & 3 deletions lib/build/recipe/dashboard/api/userroles/roles/deleteRole.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,13 @@ declare const deleteRole: (
___: string,
options: APIOptions,
__: any
) => Promise<{
status: "OK" | "ROLE_DO_NOT_EXISTS" | "FEATURE_NOT_ENABLED_ERROR";
}>;
) => Promise<
| {
status: "OK";
didRoleExist: boolean;
}
| {
status: "FEATURE_NOT_ENABLED_ERROR";
}
>;
export default deleteRole;
41 changes: 38 additions & 3 deletions lib/build/recipe/dashboard/api/userroles/roles/getAllRoles.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,31 +6,66 @@ var __importDefault =
};
Object.defineProperty(exports, "__esModule", { value: true });
const userroles_1 = __importDefault(require("../../../../userroles"));
const error_1 = __importDefault(require("../../../../../error"));
const recipe_1 = __importDefault(require("../../../../userroles/recipe"));
const getAllRoles = async (_, __, ___, ____) => {
const getAllRoles = async (_, __, options, ____) => {
try {
recipe_1.default.getInstanceOrThrowError();
} catch (_) {
return {
status: "FEATURE_NOT_ENABLED_ERROR",
};
}
const limit = Number(options.req.getKeyValueFromQuery("limit"));
let page = Number(options.req.getKeyValueFromQuery("page"));
if (limit === undefined || isNaN(limit) === false) {
throw new error_1.default({
message: "Missing required parameter 'limit'",
type: error_1.default.BAD_INPUT_ERROR,
});
}
if (page === undefined || isNaN(page) === false) {
throw new error_1.default({
message: "Missing required parameter 'page'",
type: error_1.default.BAD_INPUT_ERROR,
});
}
//set default page number to 1 if the page number is negitive or zero
if (page <= 0) {
page = 1;
}
const skip = limit * (page - 1);
const response = await userroles_1.default.getAllRoles();
const totalPages = Math.ceil(response.roles.length / limit);
if (page > totalPages) {
return {
roles: [],
totalPages,
status: "OK",
};
}
const paginatedRoles = response.roles.slice(skip, skip + limit);
let roles = [];
for (let i = 0; i < response.roles.length; i++) {
const role = response.roles[i];
for (let i = 0; i < paginatedRoles.length; i++) {
const role = paginatedRoles[i];
try {
const res = await userroles_1.default.getPermissionsForRole(role);
if (res.status === "OK") {
roles.push({
role,
permissions: res.permissions,
});
} else {
roles.push({
role,
permissions: [],
});
}
} catch (_) {}
}
return {
roles,
totalPages,
status: "OK",
};
};
Expand Down
10 changes: 3 additions & 7 deletions lib/build/recipe/dashboard/recipe.js
Original file line number Diff line number Diff line change
Expand Up @@ -50,14 +50,13 @@ const analytics_1 = __importDefault(require("./api/analytics"));
const listTenants_1 = __importDefault(require("./api/listTenants"));
const userUnlinkGet_1 = require("./api/userdetails/userUnlinkGet");
const getAllRoles_1 = __importDefault(require("./api/userroles/roles/getAllRoles"));
const createRole_1 = __importDefault(require("./api/userroles/roles/createRole"));
const deleteRole_1 = __importDefault(require("./api/userroles/roles/deleteRole"));
const addPermissions_1 = __importDefault(require("./api/userroles/permissions/addPermissions"));
const removePermissions_1 = __importDefault(require("./api/userroles/permissions/removePermissions"));
const getPermissionsForRole_1 = __importDefault(require("./api/userroles/permissions/getPermissionsForRole"));
const addRoleToUser_1 = __importDefault(require("./api/userroles/addRoleToUser"));
const getRolesForUser_1 = __importDefault(require("./api/userroles/getRolesForUser"));
const removeUserRole_1 = __importDefault(require("./api/userroles/removeUserRole"));
const createRoleOrAddPermissions_1 = __importDefault(require("./api/userroles/roles/createRoleOrAddPermissions"));
class Recipe extends recipeModule_1.default {
constructor(recipeId, appInfo, isInServerlessEnv, config) {
super(recipeId, appInfo);
Expand Down Expand Up @@ -394,8 +393,8 @@ class Recipe extends recipeModule_1.default {
} else if (id === constants_1.USERROLES_LIST_API) {
apiFunction = getAllRoles_1.default;
} else if (id === constants_1.USERROLES_ROLE_API) {
if (req.getMethod() === "post") {
apiFunction = createRole_1.default;
if (req.getMethod() === "put") {
apiFunction = createRoleOrAddPermissions_1.default;
}
if (req.getMethod() === "delete") {
apiFunction = deleteRole_1.default;
Expand All @@ -404,9 +403,6 @@ class Recipe extends recipeModule_1.default {
if (req.getMethod() === "get") {
apiFunction = getPermissionsForRole_1.default;
}
if (req.getMethod() === "put") {
apiFunction = addPermissions_1.default;
}
} else if (id === constants_1.USERROLES_REMOVE_PERMISSIONS_API) {
apiFunction = removePermissions_1.default;
} else if (id === constants_1.USERROLES_USER_API) {
Expand Down
12 changes: 9 additions & 3 deletions lib/ts/recipe/dashboard/api/userroles/addRoleToUser.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,15 @@ const addRoleToUser = async (
tenantId: string,
options: APIOptions,
__: any
): Promise<{
status: "OK" | "UNKNOWN_ROLE_ERROR" | "FEATURE_NOT_ENABLED_ERROR";
}> => {
): Promise<
| {
status: "OK";
didUserAlreadyHaveRole: boolean;
}
| {
status: "UNKNOWN_ROLE_ERROR" | "FEATURE_NOT_ENABLED_ERROR";
}
> => {
try {
UserRolesRecipe.getInstanceOrThrowError();
} catch (_) {
Expand Down

This file was deleted.

12 changes: 9 additions & 3 deletions lib/ts/recipe/dashboard/api/userroles/removeUserRole.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,15 @@ const removeUserRole = async (
tenantId: string,
options: APIOptions,
__: any
): Promise<{
status: "OK" | "UNKNOWN_ROLE_ERROR" | "FEATURE_NOT_ENABLED_ERROR";
}> => {
): Promise<
| {
status: "OK";
didUserHaveRole: boolean;
}
| {
status: "UNKNOWN_ROLE_ERROR" | "FEATURE_NOT_ENABLED_ERROR";
}
> => {
try {
UserRolesRecipe.getInstanceOrThrowError();
} catch (_) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,14 +4,12 @@ import UserRoles from "../../../../userroles";

import STError from "../../../../../error";

const createRole = async (
const createRoleOrAddPermissions = async (
_: APIInterface,
__: string,
options: APIOptions,
___: any
): Promise<{
status: "OK" | "ROLE_ALREADY_EXITS" | "FEATURE_NOT_ENABLED_ERROR";
}> => {
): Promise<{ status: "OK"; createdNewRole: boolean } | { status: "FEATURE_NOT_ENABLED_ERROR" }> => {
try {
UserRolesRecipe.getInstanceOrThrowError();
} catch (_) {
Expand Down Expand Up @@ -43,4 +41,4 @@ const createRole = async (
return response;
};

export default createRole;
export default createRoleOrAddPermissions;
12 changes: 9 additions & 3 deletions lib/ts/recipe/dashboard/api/userroles/roles/deleteRole.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,15 @@ const deleteRole = async (
___: string,
options: APIOptions,
__: any
): Promise<{
status: "OK" | "ROLE_DO_NOT_EXISTS" | "FEATURE_NOT_ENABLED_ERROR";
}> => {
): Promise<
| {
status: "OK";
didRoleExist: boolean;
}
| {
status: "FEATURE_NOT_ENABLED_ERROR";
}
> => {
try {
UserRolesRecipe.getInstanceOrThrowError();
} catch (_) {
Expand Down
Loading

0 comments on commit dd9ee0c

Please sign in to comment.