Skip to content

Commit

Permalink
feat(user): add roles controller
Browse files Browse the repository at this point in the history
  • Loading branch information
dipendraupreti committed Apr 10, 2024
1 parent b72e70b commit b9fb06d
Show file tree
Hide file tree
Showing 4 changed files with 182 additions and 1 deletion.
3 changes: 2 additions & 1 deletion packages/user/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -103,7 +103,8 @@ 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/supertokens-roles/service";
export { default as roleResolver } from "./model/supertokens-roles/resolver";
export { default as supertokensRoleResolver } from "./model/supertokens-roles/resolver";
export { default as roleResolver } from "./model/roles/controller";
export { default as roleRoutes } from "./model/supertokens-roles/controller";
// [DU 2023-AUG-07] use formatDate from "@dzangolab/fastify-slonik" package
export { formatDate } from "@dzangolab/fastify-slonik";
Expand Down
167 changes: 167 additions & 0 deletions packages/user/src/model/roles/controller.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,167 @@
import Service from "./service";
import CustomApiError from "../../customApiError";

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

const plugin = async (
fastify: FastifyInstance,
options: unknown,
done: () => void
) => {
fastify.get(
"/roles",
{
preHandler: fastify.verifySession(),
},
async (request: SessionRequest, reply: FastifyReply) => {
const service = new Service(request.config, request.slonik);

const { limit, offset, filters, sort } = request.query as {
limit: number;
offset?: number;
filters?: string;
sort?: string;
};

const data = await service.list(
limit,
offset,
filters ? JSON.parse(filters) : undefined,
sort ? JSON.parse(sort) : undefined
);

reply.send(data);
}
);

fastify.get(
"/roles/:id(^\\d+)",
{
preHandler: fastify.verifySession(),
},
async (request: SessionRequest, reply) => {
const service = new Service(request.config, request.slonik);

const { id } = request.params as { id: number };

const data = await service.findById(id);

reply.send(data);
}
);

fastify.delete(
"/roles/:id(^\\d+)",
{
preHandler: fastify.verifySession(),
},
async (request: SessionRequest, reply: FastifyReply) => {
const service = new Service(request.config, request.slonik);

const { id } = request.params as { id: number };

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

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

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

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

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

fastify.post(
"/roles",
{
preHandler: fastify.verifySession(),
},
async (request: SessionRequest, reply: FastifyReply) => {
const service = new Service(request.config, request.slonik);
const input = request.body as RoleUpdateInput;

try {
const data = await service.create(input);

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

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

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

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

fastify.put(
"/roles/:id(^\\d+)",
{
preHandler: fastify.verifySession(),
},
async (request: SessionRequest, reply: FastifyReply) => {
const service = new Service(request.config, request.slonik);

const { id } = request.params as { id: number };

const input = request.body as RoleUpdateInput;

try {
const data = await service.update(id, input);

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

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

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

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

done();
};

export default plugin;
2 changes: 2 additions & 0 deletions packages/user/src/types/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -67,3 +67,5 @@ export type {
export type { IsEmailOptions } from "./isEmailOptions";

export type { StrongPasswordOptions } from "./strongPasswordOptions";

export type { Role, RoleCreateInput, RoleUpdateInput } from "./roles";
11 changes: 11 additions & 0 deletions packages/user/src/types/roles.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
import { z } from "zod";

import { roleSchema } from "../schemas";

type Role = z.infer<typeof roleSchema>;

type RoleCreateInput = Omit<Role, "id">;

type RoleUpdateInput = Partial<Omit<Role, "id" | "role">>;

export type { Role, RoleCreateInput, RoleUpdateInput };

0 comments on commit b9fb06d

Please sign in to comment.