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,
};
6 changes: 3 additions & 3 deletions packages/user/src/model/roles/handlers/createRole.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,17 +5,17 @@ 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 { body, log, dbSchema, config, slonik } = request;

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

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

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

return reply.send(createResponse);
} catch (error) {
Expand Down
6 changes: 3 additions & 3 deletions packages/user/src/model/roles/handlers/deleteRole.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import type { FastifyReply } from "fastify";
import type { SessionRequest } from "supertokens-node/framework/fastify";

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

try {
let { role } = query as { role?: string };
Expand All @@ -25,9 +25,9 @@ const deleteRole = async (request: SessionRequest, reply: FastifyReply) => {
});
}

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

const deleteResponse = await service.deleteRole(role);
const deleteResponse = await service.delete(role);

return reply.send(deleteResponse);
}
Expand Down
20 changes: 9 additions & 11 deletions packages/user/src/model/roles/handlers/getPermissions.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,29 +4,27 @@ import type { FastifyReply } from "fastify";
import type { SessionRequest } from "supertokens-node/framework/fastify";

const getPermissions = async (request: SessionRequest, reply: FastifyReply) => {
const { log, query } = request;
let permissions: string[] = [];

const { config, dbSchema, slonik, log, query } = request;
try {
let { role } = query as { role?: string };
let { role } = query as { role?: number };

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

if (typeof role != "string") {
return reply.send({ permissions });
if (typeof role != "number") {
throw new TypeError("Invalid input");
}

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

permissions = await service.getPermissionsForRole(role);
}
const permissions = await service.getPermissionsForRole(role);

return reply.send({ permissions });
return permissions;
}
} catch (error) {
log.error(error);
reply.status(500);
Expand Down
5 changes: 3 additions & 2 deletions packages/user/src/model/roles/handlers/getRoles.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,11 @@ import type { FastifyReply } from "fastify";
import type { SessionRequest } from "supertokens-node/framework/fastify";

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

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

const roles = await service.getRoles();

return reply.send({ roles });
Expand Down
11 changes: 6 additions & 5 deletions packages/user/src/model/roles/handlers/updatePermissions.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,17 +8,18 @@ const updatePermissions = async (
request: SessionRequest,
reply: FastifyReply
) => {
const { log, body } = request;
const { body, config, dbSchema, log, slonik } = request;

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

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

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

Expand Down
47 changes: 24 additions & 23 deletions packages/user/src/model/roles/resolver.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,15 +14,15 @@ const Mutation = {
},
context: MercuriusContext
) => {
const { app } = context;
const { app, config, dbSchema, database } = context;

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

const createResponse = await service.createRole(
arguments_.role,
arguments_.permissions
);
const createResponse = await service.create({
role: arguments_.role,
permissions: arguments_.permissions,
});

return createResponse;
} catch (error) {
Expand Down Expand Up @@ -53,14 +53,14 @@ const Mutation = {
},
context: MercuriusContext
) => {
const { app } = context;
const { app, config, dbSchema, database } = context;

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

const { role } = arguments_;

const deleteResponse = await service.deleteRole(role);
const deleteResponse = await service.delete(role);

return deleteResponse;
} catch (error) {
Expand All @@ -87,18 +87,19 @@ const Mutation = {
updateRolePermissions: async (
parent: unknown,
arguments_: {
role: string;
roleId: number;
permissions: string[];
},
context: MercuriusContext
) => {
const { app } = context;
const { permissions, role } = arguments_;
const { app, config, database, dbSchema } = context;
const { permissions, roleId } = arguments_;

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

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

Expand Down Expand Up @@ -131,10 +132,11 @@ const Query = {
arguments_: Record<string, never>,
context: MercuriusContext
) => {
const { app } = context;
const { app, config, database, dbSchema } = context;

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

const roles = await service.getRoles();

return roles;
Expand All @@ -153,22 +155,21 @@ const Query = {
rolePermissions: async (
parent: unknown,
arguments_: {
role: string;
role: number;
},
context: MercuriusContext
) => {
const { app } = context;
const { app, config, database, dbSchema } = context;
const { role } = arguments_;
let permissions: string[] = [];

try {
if (role) {
const service = new RoleService();
const service = new RoleService(config, database, dbSchema);

permissions = await service.getPermissionsForRole(role);
}
const permissions = await service.getPermissionsForRole(role);

return permissions;
return permissions;
}
} catch (error) {
app.log.error(error);

Expand Down
Loading