Skip to content

Commit

Permalink
Add feature not enabled check to all the API's
Browse files Browse the repository at this point in the history
  • Loading branch information
Chakravarthy7102 committed Nov 7, 2023
1 parent f11cfcc commit 5811643
Show file tree
Hide file tree
Showing 22 changed files with 172 additions and 118 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,6 @@ declare const addRoleToUser: (
options: APIOptions,
__: any
) => Promise<{
status: "OK" | "UNKNOWN_ROLE_ERROR" | "ROLE_ALREADY_ASSIGNED";
status: "OK" | "UNKNOWN_ROLE_ERROR" | "FEATURE_NOT_ENABLED_ERROR";
}>;
export default addRoleToUser;
22 changes: 9 additions & 13 deletions lib/build/recipe/dashboard/api/userroles/addRoleToUser.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,17 @@ var __importDefault =
return mod && mod.__esModule ? mod : { default: mod };
};
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 addRoleToUser = async (_, tenantId, options, __) => {
try {
recipe_1.default.getInstanceOrThrowError();
} catch (_) {
return {
status: "FEATURE_NOT_ENABLED_ERROR",
};
}
const requestBody = await options.req.getJSONBody();
const userId = requestBody.userId;
const role = requestBody.role;
Expand All @@ -24,18 +32,6 @@ const addRoleToUser = async (_, tenantId, options, __) => {
});
}
const response = await userroles_1.default.addRoleToUser(tenantId, userId, role);
if (response.status === "OK" && response.didUserAlreadyHaveRole === true) {
return {
status: "ROLE_ALREADY_ASSIGNED",
};
}
if (response.status === "UNKNOWN_ROLE_ERROR") {
return {
status: "UNKNOWN_ROLE_ERROR",
};
}
return {
status: "OK",
};
return response;
};
exports.default = addRoleToUser;
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,13 @@ declare const addPermissions: (
___: string,
options: APIOptions,
__: any
) => Promise<{
status: "OK";
createdNewRole: boolean;
}>;
) => Promise<
| {
status: "OK";
createdNewRole: boolean;
}
| {
status: "FEATURE_NOT_ENABLED_ERROR";
}
>;
export default addPermissions;
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,17 @@ var __importDefault =
return mod && mod.__esModule ? mod : { default: mod };
};
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, __) => {
try {
recipe_1.default.getInstanceOrThrowError();
} catch (_) {
return {
status: "FEATURE_NOT_ENABLED_ERROR",
};
}
const requestBody = await options.req.getJSONBody();
const role = requestBody.role;
const permissions = requestBody.permissions;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,13 @@ declare const getPermissionsForRole: (
___: string,
options: APIOptions,
__: any
) => Promise<{
status: "OK";
permissions: string[];
}>;
) => Promise<
| {
status: "OK";
permissions: string[];
}
| {
status: "FEATURE_NOT_ENABLED_ERROR" | "UNKNOWN_ROLE_ERROR";
}
>;
export default getPermissionsForRole;
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,17 @@ var __importDefault =
return mod && mod.__esModule ? mod : { default: mod };
};
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 getPermissionsForRole = async (_, ___, options, __) => {
try {
recipe_1.default.getInstanceOrThrowError();
} catch (_) {
return {
status: "FEATURE_NOT_ENABLED_ERROR",
};
}
const role = options.req.getKeyValueFromQuery("role");
if (role === undefined || typeof role !== "string") {
throw new error_1.default({
Expand All @@ -16,12 +24,6 @@ const getPermissionsForRole = async (_, ___, options, __) => {
});
}
const response = await userroles_1.default.getPermissionsForRole(role);
if (response.status === "UNKNOWN_ROLE_ERROR") {
return {
status: "OK",
permissions: [],
};
}
return response;
};
exports.default = getPermissionsForRole;
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,6 @@ declare const removePermissionsFromRole: (
options: APIOptions,
__: any
) => Promise<{
status: "OK" | "UNKNOWN_ROLE_ERROR";
status: "OK" | "UNKNOWN_ROLE_ERROR" | "FEATURE_NOT_ENABLED_ERROR";
}>;
export default removePermissionsFromRole;
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,17 @@ var __importDefault =
return mod && mod.__esModule ? mod : { default: mod };
};
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 removePermissionsFromRole = async (_, ___, options, __) => {
try {
recipe_1.default.getInstanceOrThrowError();
} catch (_) {
return {
status: "FEATURE_NOT_ENABLED_ERROR",
};
}
const requestBody = await options.req.getJSONBody();
const role = requestBody.role;
const permissions = requestBody.permissions;
Expand Down
11 changes: 3 additions & 8 deletions lib/build/recipe/dashboard/api/userroles/removeUserRole.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,12 +5,7 @@ declare const removeUserRole: (
tenantId: string,
options: APIOptions,
__: any
) => Promise<
| {
status: "OK";
}
| {
status: "UNKNOWN_ROLE_ERROR";
}
>;
) => Promise<{
status: "OK" | "UNKNOWN_ROLE_ERROR" | "FEATURE_NOT_ENABLED_ERROR";
}>;
export default removeUserRole;
8 changes: 8 additions & 0 deletions lib/build/recipe/dashboard/api/userroles/removeUserRole.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,17 @@ var __importDefault =
return mod && mod.__esModule ? mod : { default: mod };
};
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 removeUserRole = async (_, tenantId, options, __) => {
try {
recipe_1.default.getInstanceOrThrowError();
} catch (_) {
return {
status: "FEATURE_NOT_ENABLED_ERROR",
};
}
const userId = options.req.getKeyValueFromQuery("userId");
const role = options.req.getKeyValueFromQuery("role");
if (role === undefined || typeof role !== "string") {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,6 @@ declare const createRole: (
options: APIOptions,
___: any
) => Promise<{
status: "OK" | "ROLE_ALREADY_EXITS";
status: "OK" | "ROLE_ALREADY_EXITS" | "FEATURE_NOT_ENABLED_ERROR";
}>;
export default createRole;
18 changes: 9 additions & 9 deletions lib/build/recipe/dashboard/api/userroles/roles/createRole.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,17 @@ var __importDefault =
return mod && mod.__esModule ? mod : { default: mod };
};
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 createRole = async (_, __, options, ___) => {
try {
recipe_1.default.getInstanceOrThrowError();
} catch (_) {
return {
status: "FEATURE_NOT_ENABLED_ERROR",
};
}
const requestBody = await options.req.getJSONBody();
const permissions = requestBody.permissions;
const role = requestBody.role;
Expand All @@ -23,15 +31,7 @@ const createRole = async (_, __, options, ___) => {
type: error_1.default.BAD_INPUT_ERROR,
});
}
//TODO: check for a is this role already exists or not before creating the new role.
const response = await userroles_1.default.createNewRoleOrAddPermissions(role, permissions);
if (response.status === "OK" && response.createdNewRole === false) {
return {
status: "ROLE_ALREADY_EXITS",
};
}
return {
status: "OK",
};
return response;
};
exports.default = createRole;
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,6 @@ declare const deleteRole: (
options: APIOptions,
__: any
) => Promise<{
status: "OK" | "ROLE_DO_NOT_EXISTS";
status: "OK" | "ROLE_DO_NOT_EXISTS" | "FEATURE_NOT_ENABLED_ERROR";
}>;
export default deleteRole;
17 changes: 9 additions & 8 deletions lib/build/recipe/dashboard/api/userroles/roles/deleteRole.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,17 @@ var __importDefault =
return mod && mod.__esModule ? mod : { default: mod };
};
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 deleteRole = async (_, ___, options, __) => {
try {
recipe_1.default.getInstanceOrThrowError();
} catch (_) {
return {
status: "FEATURE_NOT_ENABLED_ERROR",
};
}
const role = options.req.getKeyValueFromQuery("role");
if (role === undefined || typeof role !== "string") {
throw new error_1.default({
Expand All @@ -16,13 +24,6 @@ const deleteRole = async (_, ___, options, __) => {
});
}
const response = await userroles_1.default.deleteRole(role);
if (response.status === "OK" && response.didRoleExist === false) {
return {
status: "ROLE_DO_NOT_EXISTS",
};
}
return {
status: "OK",
};
return response;
};
exports.default = deleteRole;
27 changes: 11 additions & 16 deletions lib/ts/recipe/dashboard/api/userroles/addRoleToUser.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import { APIInterface, APIOptions } from "../../types";
import UserRolesRecipe from "../../../userroles/recipe";
import UserRoles from "../../../userroles";

import STError from "../../../../error";
Expand All @@ -9,8 +10,16 @@ const addRoleToUser = async (
options: APIOptions,
__: any
): Promise<{
status: "OK" | "UNKNOWN_ROLE_ERROR" | "ROLE_ALREADY_ASSIGNED";
status: "OK" | "UNKNOWN_ROLE_ERROR" | "FEATURE_NOT_ENABLED_ERROR";
}> => {
try {
UserRolesRecipe.getInstanceOrThrowError();
} catch (_) {
return {
status: "FEATURE_NOT_ENABLED_ERROR",
};
}

const requestBody = await options.req.getJSONBody();

const userId = requestBody.userId;
Expand All @@ -32,21 +41,7 @@ const addRoleToUser = async (

const response = await UserRoles.addRoleToUser(tenantId, userId, role);

if (response.status === "OK" && response.didUserAlreadyHaveRole === true) {
return {
status: "ROLE_ALREADY_ASSIGNED",
};
}

if (response.status === "UNKNOWN_ROLE_ERROR") {
return {
status: "UNKNOWN_ROLE_ERROR",
};
}

return {
status: "OK",
};
return response;
};

export default addRoleToUser;
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import UserRolesRecipe from "../../../../userroles/recipe";
import UserRoles from "../../../../userroles";
import { APIInterface, APIOptions } from "../../../types";

Expand All @@ -8,10 +9,21 @@ const addPermissions = async (
___: string,
options: APIOptions,
__: any
): Promise<{
status: "OK";
createdNewRole: boolean;
}> => {
): Promise<
| {
status: "OK";
createdNewRole: boolean;
}
| { status: "FEATURE_NOT_ENABLED_ERROR" }
> => {
try {
UserRolesRecipe.getInstanceOrThrowError();
} catch (_) {
return {
status: "FEATURE_NOT_ENABLED_ERROR",
};
}

const requestBody = await options.req.getJSONBody();
const role = requestBody.role;
const permissions = requestBody.permissions;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import UserRolesRecipe from "../../../../userroles/recipe";
import UserRoles from "../../../../userroles";
import { APIInterface, APIOptions } from "../../../types";

Expand All @@ -8,10 +9,21 @@ const getPermissionsForRole = async (
___: string,
options: APIOptions,
__: any
): Promise<{
status: "OK";
permissions: string[];
}> => {
): Promise<
| {
status: "OK";
permissions: string[];
}
| { status: "FEATURE_NOT_ENABLED_ERROR" | "UNKNOWN_ROLE_ERROR" }
> => {
try {
UserRolesRecipe.getInstanceOrThrowError();
} catch (_) {
return {
status: "FEATURE_NOT_ENABLED_ERROR",
};
}

const role = options.req.getKeyValueFromQuery("role");

if (role === undefined || typeof role !== "string") {
Expand All @@ -23,13 +35,6 @@ const getPermissionsForRole = async (

const response = await UserRoles.getPermissionsForRole(role);

if (response.status === "UNKNOWN_ROLE_ERROR") {
return {
status: "OK",
permissions: [],
};
}

return response;
};

Expand Down
Loading

0 comments on commit 5811643

Please sign in to comment.