Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 8 additions & 0 deletions packages/user/src/constants.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,12 @@ const ROUTE_ME = "/me";
const ROUTE_USERS = "/users";
const ROUTE_USERS_DISABLE = "/users/:id/disable";
const ROUTE_USERS_ENABLE = "/users/:id/enable";

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

// Roles
const ROUTE_ROLES = "/roles";
Expand Down Expand Up @@ -75,4 +80,7 @@ export {
ROUTE_USERS_ENABLE,
TABLE_INVITATIONS,
TABLE_USERS,
TABLE_ROLES,
TABLE_ROLE_PERMISSIONS,
TABLE_USER_ROLES,
};
1 change: 1 addition & 0 deletions packages/user/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -105,6 +105,7 @@ 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";
export { default as ownRoleRoutes } from "./model/own-roles/controller";
// [DU 2023-AUG-07] use formatDate from "@dzangolab/fastify-slonik" package
export { formatDate } from "@dzangolab/fastify-slonik";
export { default as computeInvitationExpiresAt } from "./lib/computeInvitationExpiresAt";
Expand Down
58 changes: 58 additions & 0 deletions packages/user/src/model/own-roles/controller.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
import handlers from "./handlers";
import { ROUTE_ROLES, ROUTE_ROLES_PERMISSIONS } from "../../constants";

import type { FastifyInstance } from "fastify";

const plugin = async (
fastify: FastifyInstance,
options: unknown,
done: () => void
) => {
const OWN_ROUTE_ROLES = `/own${ROUTE_ROLES}`;

const OWN_ROUTE_ROLES_PERMISSIONS = `/own${ROUTE_ROLES_PERMISSIONS}`;

fastify.delete(
OWN_ROUTE_ROLES,
{
preHandler: [fastify.verifySession()],
},
handlers.deleteRole
);

fastify.get(
OWN_ROUTE_ROLES,
{
preHandler: [fastify.verifySession()],
},
handlers.getRoles
);

fastify.get(
OWN_ROUTE_ROLES_PERMISSIONS,
{
preHandler: [fastify.verifySession()],
},
handlers.getPermissions
);

fastify.post(
OWN_ROUTE_ROLES,
{
preHandler: [fastify.verifySession()],
},
handlers.createRole
);

fastify.put(
OWN_ROUTE_ROLES_PERMISSIONS,
{
preHandler: [fastify.verifySession()],
},
handlers.updatePermissions
);

done();
};

export default plugin;
38 changes: 38 additions & 0 deletions packages/user/src/model/own-roles/handlers/createRole.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
import CustomApiError from "../../../customApiError";
import RoleService from "../service";

import type { FastifyReply } from "fastify";
import type { QueryResultRow } from "slonik";
import type { SessionRequest } from "supertokens-node/framework/fastify";

const createRole = async (request: SessionRequest, reply: FastifyReply) => {
const { body, log, dbSchema, config, slonik } = request;

try {
const service = new RoleService(config, slonik, dbSchema);

const response = await service.create(body as QueryResultRow);

return reply.send(response);
} catch (error) {
if (error instanceof CustomApiError) {
reply.status(error.statusCode);

return reply.send({
message: error.message,
name: error.name,
statusCode: error.statusCode,
});
}

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

return reply.send({
status: "ERROR",
message: "Oops! Something went wrong",
});
}
};

export default createRole;
61 changes: 61 additions & 0 deletions packages/user/src/model/own-roles/handlers/deleteRole.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
import CustomApiError from "../../../customApiError";
import RoleService from "../service";

import type { FastifyReply } from "fastify";
import type { SessionRequest } from "supertokens-node/framework/fastify";

const deleteRole = async (request: SessionRequest, reply: FastifyReply) => {
const { config, slonik, log, query, dbSchema } = request;

try {
let { role } = query as { role?: string };

if (role) {
try {
role = JSON.parse(role) as string;
} catch {
/* empty */
}

if (typeof role != "string") {
throw new CustomApiError({
name: "UNKNOWN_ROLE_ERROR",
message: `Invalid role`,
statusCode: 422,
});
}

const service = new RoleService(config, slonik, dbSchema);

const deleteResponse = await service.delete(role);

return reply.send(deleteResponse);
}

throw new CustomApiError({
name: "UNKNOWN_ROLE_ERROR",
message: `Invalid role`,
statusCode: 422,
});
} catch (error) {
if (error instanceof CustomApiError) {
reply.status(error.statusCode);

return reply.send({
message: error.message,
name: error.name,
statusCode: error.statusCode,
});
}

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

return reply.send({
status: "ERROR",
message: "Oops! Something went wrong",
});
}
};

export default deleteRole;
39 changes: 39 additions & 0 deletions packages/user/src/model/own-roles/handlers/getPermissions.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
import RoleService from "../service";

import type { FastifyReply } from "fastify";
import type { SessionRequest } from "supertokens-node/framework/fastify";

const getPermissions = async (request: SessionRequest, reply: FastifyReply) => {
const { config, dbSchema, slonik, log, query } = request;
try {
let { role } = query as { role?: number };

if (role) {
try {
role = JSON.parse(role as unknown as string) as number;
} catch {
/* empty */
}

if (typeof role != "number") {
throw new TypeError("Invalid input");
}

const service = new RoleService(config, slonik, dbSchema);

const permissions = await service.getPermissionsForRole(role);

return permissions;
}
} catch (error) {
log.error(error);
reply.status(500);

return reply.send({
status: "ERROR",
message: "Oops! Something went wrong",
});
}
};

export default getPermissions;
26 changes: 26 additions & 0 deletions packages/user/src/model/own-roles/handlers/getRoles.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
import RoleService from "../service";

import type { FastifyReply } from "fastify";
import type { SessionRequest } from "supertokens-node/framework/fastify";

const getRoles = async (request: SessionRequest, reply: FastifyReply) => {
const { config, dbSchema, log, slonik } = request;

try {
const service = new RoleService(config, slonik, dbSchema);

const roles = await service.getRoles();

return reply.send({ roles });
} catch (error) {
log.error(error);
reply.status(500);

return reply.send({
status: "ERROR",
message: "Oops! Something went wrong",
});
}
};

export default getRoles;
13 changes: 13 additions & 0 deletions packages/user/src/model/own-roles/handlers/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
import createRole from "./createRole";
import deleteRole from "./deleteRole";
import getPermissions from "./getPermissions";
import getRoles from "./getRoles";
import updatePermissions from "./updatePermissions";

export default {
deleteRole,
createRole,
getRoles,
getPermissions,
updatePermissions,
};
48 changes: 48 additions & 0 deletions packages/user/src/model/own-roles/handlers/updatePermissions.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
import CustomApiError from "../../../customApiError";
import RoleService from "../service";

import type { FastifyReply } from "fastify";
import type { SessionRequest } from "supertokens-node/framework/fastify";

const updatePermissions = async (
request: SessionRequest,
reply: FastifyReply
) => {
const { body, config, dbSchema, log, slonik } = request;

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

const service = new RoleService(config, slonik, dbSchema);

const updatedPermissionsResponse = await service.updateRolePermissions(
roleId,
permissions
);

return reply.send(updatedPermissionsResponse);
} catch (error) {
if (error instanceof CustomApiError) {
reply.status(error.statusCode);

return reply.send({
message: error.message,
name: error.name,
statusCode: error.statusCode,
});
}

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

return reply.send({
status: "ERROR",
message: "Oops! Something went wrong",
});
}
};

export default updatePermissions;
Loading