Skip to content

Commit

Permalink
checks
Browse files Browse the repository at this point in the history
  • Loading branch information
potts99 committed Nov 26, 2023
1 parent ddb196a commit 948eefa
Show file tree
Hide file tree
Showing 9 changed files with 425 additions and 281 deletions.
36 changes: 19 additions & 17 deletions apps/api/src/controllers/auth.ts
Original file line number Diff line number Diff line change
Expand Up @@ -141,25 +141,25 @@ export function authRoutes(fastify: FastifyInstance) {
fastify.delete(
"/api/v1/auth/user/:id",
async (request: FastifyRequest, reply: FastifyReply) => {
const { id } = request.params as { id: string };
const bearer = request.headers.authorization!.split(" ")[1];
const token = checkToken(bearer);

await prisma.user.delete({
where: { id },
});
if (token) {
const { id } = request.params as { id: string };

reply.send({ success: true });
await prisma.user.delete({
where: { id },
});

reply.send({ success: true });
}
}
);

// User Profile
fastify.get(
"/api/v1/auth/profile",
async (request: FastifyRequest, reply: FastifyReply) => {
// check token
// see if token exists on session table
// if not, return 401
// if yes, return user data

const bearer = request.headers.authorization!.split(" ")[1];

const token = checkToken(bearer);
Expand Down Expand Up @@ -211,8 +211,6 @@ export function authRoutes(fastify: FastifyInstance) {
};

const bearer = request.headers.authorization!.split(" ")[1];

//checks if token is valid and returns valid token
const token = checkToken(bearer);

if (token) {
Expand Down Expand Up @@ -288,13 +286,17 @@ export function authRoutes(fastify: FastifyInstance) {
fastify.get(
"/api/v1/auth/user/:id/logout",
async (request: FastifyRequest, reply: FastifyReply) => {
const { id } = request.params as { id: string };
const bearer = request.headers.authorization!.split(" ")[1];
const token = checkToken(bearer);
if (token) {
const { id } = request.params as { id: string };

await prisma.session.deleteMany({
where: { userId: id },
});
await prisma.session.deleteMany({
where: { userId: id },
});

reply.send({ success: true });
reply.send({ success: true });
}
}
);
}
103 changes: 62 additions & 41 deletions apps/api/src/controllers/clients.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import { FastifyInstance, FastifyReply, FastifyRequest } from "fastify";
import { checkToken } from "../lib/jwt";
import { prisma } from "../prisma";

export function clientRoutes(fastify: FastifyInstance) {
Expand All @@ -7,20 +8,25 @@ export function clientRoutes(fastify: FastifyInstance) {
"/api/v1/client/create",

async (request: FastifyRequest, reply: FastifyReply) => {
const { name, email, number, contactName }: any = request.body;

await prisma.client.create({
data: {
name,
contactName,
email,
number: String(number),
},
});

reply.send({
success: true,
});
const bearer = request.headers.authorization!.split(" ")[1];
const token = checkToken(bearer);

if (token) {
const { name, email, number, contactName }: any = request.body;

await prisma.client.create({
data: {
name,
contactName,
email,
number: String(number),
},
});

reply.send({
success: true,
});
}
}
);

Expand All @@ -29,21 +35,26 @@ export function clientRoutes(fastify: FastifyInstance) {
"/api/v1/client/update",

async (request: FastifyRequest, reply: FastifyReply) => {
const { name, email, number, contactName, id }: any = request.body;

await prisma.client.update({
where: { id: id },
data: {
name,
contactName,
email,
number: String(number),
},
});

reply.send({
success: true,
});
const bearer = request.headers.authorization!.split(" ")[1];
const token = checkToken(bearer);

if (token) {
const { name, email, number, contactName, id }: any = request.body;

await prisma.client.update({
where: { id: id },
data: {
name,
contactName,
email,
number: String(number),
},
});

reply.send({
success: true,
});
}
}
);

Expand All @@ -52,12 +63,17 @@ export function clientRoutes(fastify: FastifyInstance) {
"/api/v1/clients/all",

async (request: FastifyRequest, reply: FastifyReply) => {
const clients = await prisma.client.findMany({});
const bearer = request.headers.authorization!.split(" ")[1];
const token = checkToken(bearer);

reply.send({
success: true,
clients: clients,
});
if (token) {
const clients = await prisma.client.findMany({});

reply.send({
success: true,
clients: clients,
});
}
}
);

Expand All @@ -66,15 +82,20 @@ export function clientRoutes(fastify: FastifyInstance) {
"/api/v1/clients/:id/delete-client",

async (request: FastifyRequest, reply: FastifyReply) => {
const { id }: any = request.params;
const bearer = request.headers.authorization!.split(" ")[1];
const token = checkToken(bearer);

if (token) {
const { id }: any = request.params;

await prisma.client.delete({
where: { id: id },
});
await prisma.client.delete({
where: { id: id },
});

reply.send({
success: true,
});
reply.send({
success: true,
});
}
}
);
}
50 changes: 36 additions & 14 deletions apps/api/src/controllers/data.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import { FastifyInstance, FastifyReply, FastifyRequest } from "fastify";
import { checkToken } from "../lib/jwt";
import { prisma } from "../prisma";

export function dataRoutes(fastify: FastifyInstance) {
Expand All @@ -7,8 +8,14 @@ export function dataRoutes(fastify: FastifyInstance) {
"/api/v1/data/tickets/all",

async (request: FastifyRequest, reply: FastifyReply) => {
// check jwt is valid
// check user is admin
const bearer = request.headers.authorization!.split(" ")[1];
const token = checkToken(bearer);

if (token) {
const result = await prisma.ticket.count();

reply.send({ count: result });
}
}
);

Expand All @@ -17,11 +24,16 @@ export function dataRoutes(fastify: FastifyInstance) {
"/api/v1/data/tickets/completed",

async (request: FastifyRequest, reply: FastifyReply) => {
const result = await prisma.ticket.count({
where: { isComplete: true },
});
const bearer = request.headers.authorization!.split(" ")[1];
const token = checkToken(bearer);

if (token) {
const result = await prisma.ticket.count({
where: { isComplete: true },
});

reply.send({ count: result });
reply.send({ count: result });
}
}
);

Expand All @@ -30,11 +42,16 @@ export function dataRoutes(fastify: FastifyInstance) {
"/api/v1/data/tickets/open",

async (request: FastifyRequest, reply: FastifyReply) => {
const result = await prisma.ticket.count({
where: { isComplete: false },
});
const bearer = request.headers.authorization!.split(" ")[1];
const token = checkToken(bearer);

reply.send({ count: result });
if (token) {
const result = await prisma.ticket.count({
where: { isComplete: false },
});

reply.send({ count: result });
}
}
);

Expand All @@ -43,11 +60,16 @@ export function dataRoutes(fastify: FastifyInstance) {
"/api/v1/data/tickets/unassigned",

async (request: FastifyRequest, reply: FastifyReply) => {
const result = await prisma.ticket.count({
where: { userId: null },
});
const bearer = request.headers.authorization!.split(" ")[1];
const token = checkToken(bearer);

if (token) {
const result = await prisma.ticket.count({
where: { userId: null },
});

reply.send({ count: result });
reply.send({ count: result });
}
}
);
}
Loading

0 comments on commit 948eefa

Please sign in to comment.