Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(user): rename old roles model and add new roles model #640

Open
wants to merge 10 commits into
base: main
Choose a base branch
from
10 changes: 9 additions & 1 deletion packages/user/src/constants.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,6 @@ const ROUTE_ME = "/me";
const ROUTE_USERS = "/users";
const ROUTE_USERS_DISABLE = "/users/:id/disable";
const ROUTE_USERS_ENABLE = "/users/:id/enable";
const TABLE_USERS = "users";

// Roles
const ROUTE_ROLES = "/roles";
Expand All @@ -29,6 +28,12 @@ const ROUTE_ROLES_PERMISSIONS = "/roles/permissions";
// Permissions
const ROUTE_PERMISSIONS = "/permissions";

// Tables
const TABLE_ROLES = "roles";
const TABLE_USERS = "users";
const TABLE_ROLE_PERMISSIONS = "role_permissions";
const TABLE_USER_ROLES = "user_roles";

// Email verification
const EMAIL_VERIFICATION_MODE = "REQUIRED";
const EMAIL_VERIFICATION_PATH = "/verify-email";
Expand Down Expand Up @@ -75,4 +80,7 @@ export {
ROUTE_USERS_ENABLE,
TABLE_INVITATIONS,
TABLE_USERS,
TABLE_ROLES,
TABLE_ROLE_PERMISSIONS,
TABLE_USER_ROLES,
};
6 changes: 5 additions & 1 deletion packages/user/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -103,7 +103,6 @@ export { default as invitationRoutes } from "./model/invitations/controller";
export { default as permissionResolver } from "./model/permissions/resolver";
export { default as permissionRoutes } from "./model/permissions/controller";
export { default as RoleService } from "./model/roles/service";
export { default as roleResolver } from "./model/roles/resolver";
export { default as roleRoutes } from "./model/roles/controller";
// [DU 2023-AUG-07] use formatDate from "@dzangolab/fastify-slonik" package
export { formatDate } from "@dzangolab/fastify-slonik";
Expand All @@ -118,6 +117,8 @@ export { default as areRolesExist } from "./supertokens/utils/areRolesExist";
export { default as validateEmail } from "./validator/email";
export { default as validatePassword } from "./validator/password";
export { default as hasUserPermission } from "./lib/hasUserPermission";
export { default as CustomApiError } from "./customApiError";
export { emailSchema, passwordSchema, roleSchema } from "./schemas";

export * from "./constants";

Expand All @@ -127,6 +128,9 @@ export type { ThirdPartyEmailPasswordRecipe } from "./supertokens/types/thirdPar
export type {
AuthUser,
ChangePasswordInput,
Role,
RoleCreateInput,
RoleUpdateInput,
UserCreateInput,
UserUpdateInput,
User,
Expand Down
35 changes: 17 additions & 18 deletions packages/user/src/model/roles/controller.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
import handlers from "./handlers";
import { ROUTE_ROLES, ROUTE_ROLES_PERMISSIONS } from "../../constants";

import type { FastifyInstance } from "fastify";

Expand All @@ -8,44 +7,44 @@ const plugin = async (
options: unknown,
done: () => void
) => {
fastify.delete(
ROUTE_ROLES,
fastify.get(
"/roles",
{
preHandler: [fastify.verifySession()],
preHandler: fastify.verifySession(),
},
handlers.deleteRole
handlers.roles
);

fastify.get(
ROUTE_ROLES,
"/roles/:id(^\\d+)",
{
preHandler: [fastify.verifySession()],
preHandler: fastify.verifySession(),
},
handlers.getRoles
handlers.role
);

fastify.get(
ROUTE_ROLES_PERMISSIONS,
fastify.delete(
"/roles/:id(^\\d+)",
{
preHandler: [fastify.verifySession()],
preHandler: fastify.verifySession(),
},
handlers.getPermissions
handlers.deleteRole
);

fastify.post(
ROUTE_ROLES,
"/roles",
{
preHandler: [fastify.verifySession()],
preHandler: fastify.verifySession(),
},
handlers.createRole
handlers.create
);

fastify.put(
ROUTE_ROLES_PERMISSIONS,
"/roles/:id(^\\d+)",
{
preHandler: [fastify.verifySession()],
preHandler: fastify.verifySession(),
},
handlers.updatePermissions
handlers.update
);

done();
Expand Down
Original file line number Diff line number Diff line change
@@ -1,28 +1,22 @@
import CustomApiError from "../../../customApiError";
import RoleService from "../service";
import Service from "../service";

import type { Role, RoleCreateInput, RoleUpdateInput } from "../../../types";
import type { FastifyReply } from "fastify";
import type { SessionRequest } from "supertokens-node/framework/fastify";

const updatePermissions = async (
request: SessionRequest,
reply: FastifyReply
) => {
const { log, body } = request;
const create = async (request: SessionRequest, reply: FastifyReply) => {
const service = new Service<Role, RoleCreateInput, RoleUpdateInput>(
request.config,
request.slonik
);

const input = request.body as RoleCreateInput;

try {
const { role, permissions } = body as {
role: string;
permissions: string[];
};

const service = new RoleService();
const updatedPermissionsResponse = await service.updateRolePermissions(
role,
permissions
);

return reply.send(updatedPermissionsResponse);
const data = await service.create(input);

return reply.send(data);
} catch (error) {
if (error instanceof CustomApiError) {
reply.status(error.statusCode);
Expand All @@ -34,7 +28,7 @@ const updatePermissions = async (
});
}

log.error(error);
request.log.error(error);
reply.status(500);

return reply.send({
Expand All @@ -44,4 +38,4 @@ const updatePermissions = async (
}
};

export default updatePermissions;
export default create;
Original file line number Diff line number Diff line change
@@ -1,23 +1,22 @@
import CustomApiError from "../../../customApiError";
import RoleService from "../service";
import Service from "../service";

import type { Role, RoleCreateInput, RoleUpdateInput } from "../../../types";
import type { FastifyReply } from "fastify";
import type { SessionRequest } from "supertokens-node/framework/fastify";

const createRole = async (request: SessionRequest, reply: FastifyReply) => {
const { body, log } = request;
const deleteRole = async (request: SessionRequest, reply: FastifyReply) => {
const service = new Service<Role, RoleCreateInput, RoleUpdateInput>(
request.config,
request.slonik
);

const { role, permissions } = body as {
role: string;
permissions: string[];
};
const { id } = request.params as { id: number };

try {
const service = new RoleService();
const data = await service.delete(id);

const createResponse = await service.createRole(role, permissions);

return reply.send(createResponse);
reply.send(data);
} catch (error) {
if (error instanceof CustomApiError) {
reply.status(error.statusCode);
Expand All @@ -29,7 +28,7 @@ const createRole = async (request: SessionRequest, reply: FastifyReply) => {
});
}

log.error(error);
request.log.error(error);
reply.status(500);

return reply.send({
Expand All @@ -39,4 +38,4 @@ const createRole = async (request: SessionRequest, reply: FastifyReply) => {
}
};

export default createRole;
export default deleteRole;
61 changes: 0 additions & 61 deletions packages/user/src/model/roles/handlers/deleteRole.ts

This file was deleted.

41 changes: 0 additions & 41 deletions packages/user/src/model/roles/handlers/getPermissions.ts

This file was deleted.

25 changes: 0 additions & 25 deletions packages/user/src/model/roles/handlers/getRoles.ts

This file was deleted.

18 changes: 9 additions & 9 deletions packages/user/src/model/roles/handlers/index.ts
Original file line number Diff line number Diff line change
@@ -1,13 +1,13 @@
import createRole from "./createRole";
import deleteRole from "./deleteRole";
import getPermissions from "./getPermissions";
import getRoles from "./getRoles";
import updatePermissions from "./updatePermissions";
import create from "./create";
import deleteRole from "./delete";
import role from "./role";
import roles from "./roles";
import update from "./update";

export default {
create,
deleteRole,
createRole,
getRoles,
getPermissions,
updatePermissions,
role,
roles,
update,
};
Loading
Loading