Skip to content

Commit

Permalink
chore: code abstraction clean up
Browse files Browse the repository at this point in the history
  • Loading branch information
potts99 committed Nov 13, 2024
1 parent be1f585 commit 77f68d6
Show file tree
Hide file tree
Showing 10 changed files with 801 additions and 1,049 deletions.
469 changes: 198 additions & 271 deletions apps/api/src/controllers/auth.ts

Large diffs are not rendered by default.

117 changes: 48 additions & 69 deletions apps/api/src/controllers/clients.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
import { FastifyInstance, FastifyReply, FastifyRequest } from "fastify";
import { track } from "../lib/hog";
import { checkToken } from "../lib/jwt";
import { prisma } from "../prisma";

export function clientRoutes(fastify: FastifyInstance) {
Expand All @@ -9,32 +8,27 @@ export function clientRoutes(fastify: FastifyInstance) {
"/api/v1/client/create",

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

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

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

const hog = track();

hog.capture({
event: "client_created",
distinctId: client.id,
});

reply.send({
success: true,
});
}
const { name, email, number, contactName }: any = request.body;

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

const hog = track();

hog.capture({
event: "client_created",
distinctId: client.id,
});

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

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

async (request: FastifyRequest, reply: FastifyReply) => {
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,
});
}
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 @@ -71,17 +60,12 @@ export function clientRoutes(fastify: FastifyInstance) {
"/api/v1/clients/all",

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

if (token) {
const clients = await prisma.client.findMany({});

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

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

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

if (token) {
const { id }: any = request.params;
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,
});
}
);
}
Loading

0 comments on commit 77f68d6

Please sign in to comment.