Skip to content

Commit c6e56c3

Browse files
author
shawon-majid
committed
employeee list added and logging added
1 parent e3e8458 commit c6e56c3

File tree

10 files changed

+226
-2
lines changed

10 files changed

+226
-2
lines changed

server/src/controllers/auth.ts

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,8 @@ import CustomError from "../services/CustomError";
1212
import { randomOTPGenerator, randomPasswordGenerator } from "../services/utils";
1313
import { sendMail, sendOTPMail } from "../services/mailService";
1414
import { PERMISSIONS, getPermittedRoleNames } from "../permissions/permissions";
15-
import { adminLog } from "../services/logdata";
15+
import { adminLog, contractorLog } from "../services/logdata";
16+
import { RoleName } from "../types/rolesTypes";
1617

1718
const prisma = new PrismaClient();
1819

@@ -136,6 +137,7 @@ const createEmployee = errorWrapper(
136137
);
137138

138139
await adminLog(`Employee Entry`, `Employee ${user.username} added`);
140+
await contractorLog(`Employee Entry`, `Employee ${user.username} added`);
139141

140142
res.status(201).json({ user, token });
141143
},
@@ -153,6 +155,7 @@ const login = errorWrapper(
153155
include: {
154156
landfill: true,
155157
sts: true,
158+
Contractor: true,
156159
},
157160
});
158161

@@ -171,6 +174,18 @@ const login = errorWrapper(
171174
// console.log(roles);
172175
// console.log(user.roleName);
173176

177+
adminLog(
178+
`Login`,
179+
`User ${user.username}, Role: ${user.roleName} logged in`
180+
);
181+
182+
if (user.roleName === RoleName.CONTRACTOR_EMPLOYEE) {
183+
contractorLog(
184+
`Login`,
185+
`User ${user.username}, Role: ${user.roleName} logged in`
186+
);
187+
}
188+
174189
if (!roles.includes(user.roleName)) {
175190
throw new CustomError("You are not allowed to login", 403);
176191
}

server/src/controllers/employee.ts

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
import { PrismaClient } from "@prisma/client";
2+
import { Request, Response } from "express";
3+
import { RoleName } from "../types/rolesTypes";
4+
5+
const prisma = new PrismaClient();
6+
7+
const getAllEmployees = async (req: Request, res: Response) => {
8+
const employees = await prisma.user.findMany({
9+
where: {
10+
roleName: RoleName.CONTRACTOR_EMPLOYEE,
11+
},
12+
});
13+
res.status(200).json(employees);
14+
};
15+
16+
const getEmployeeById = async (req: Request, res: Response) => {
17+
const { employeeId } = req.params;
18+
const employee = await prisma.user.findUnique({
19+
where: {
20+
id: employeeId,
21+
},
22+
});
23+
24+
if (!employee) {
25+
res.status(404).json({ message: "Employee not found" });
26+
}
27+
28+
res.status(200).json(employee);
29+
};
30+
31+
export { getAllEmployees, getEmployeeById };

server/src/controllers/logs.ts

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,15 @@ const getAdminLogs = errorWrapper(
1515
);
1616

1717
// get all sts manager logs
18+
1819
// get all contractor manager logs
1920

20-
export { getAdminLogs };
21+
const getContractorManagerLogs = errorWrapper(
22+
async (req: Request, res: Response) => {
23+
const logs = await prisma.contractorLogs.findMany();
24+
res.status(200).json(logs);
25+
},
26+
{ statusCode: 500, message: "Couldn't get contractor manager logs" }
27+
);
28+
29+
export { getAdminLogs, getContractorManagerLogs };
Lines changed: 100 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,100 @@
1+
import { PrismaClient } from "@prisma/client";
2+
import { Request, Response } from "express";
3+
4+
const prisma = new PrismaClient();
5+
6+
const addRoute = async (req: Request, res: Response) => {
7+
const { name, description } = req.body;
8+
const newRoute = await prisma.route.create({
9+
data: {
10+
name,
11+
description,
12+
},
13+
});
14+
15+
res.status(201).json(newRoute);
16+
};
17+
18+
const getAllRoutes = async (req: Request, res: Response) => {
19+
const routes = await prisma.route.findMany();
20+
res.status(200).json(routes);
21+
};
22+
23+
const getRouteById = async (req: Request, res: Response) => {
24+
const { routeId } = req.params;
25+
const route = await prisma.route.findUnique({
26+
where: {
27+
id: routeId,
28+
},
29+
});
30+
31+
if (!route) {
32+
res.status(404).json({ message: "Route not found" });
33+
}
34+
35+
res.status(200).json(route);
36+
};
37+
38+
const addArea = async (req: Request, res: Response) => {
39+
const { name, description } = req.body;
40+
const newArea = await prisma.area.create({
41+
data: {
42+
name,
43+
},
44+
});
45+
46+
res.status(201).json(newArea);
47+
};
48+
49+
const addRouteToArea = async (req: Request, res: Response) => {
50+
const { areaId, routeId } = req.params;
51+
52+
const area = await prisma.area.findUnique({
53+
where: {
54+
id: areaId,
55+
},
56+
});
57+
58+
if (!area) {
59+
res.status(404).json({ message: "Area not found" });
60+
}
61+
62+
const route = await prisma.route.findUnique({
63+
where: {
64+
id: routeId,
65+
},
66+
});
67+
68+
if (!route) {
69+
res.status(404).json({ message: "Route not found" });
70+
}
71+
72+
const updatedArea = await prisma.area.update({
73+
where: {
74+
id: areaId,
75+
},
76+
data: {
77+
routes: {
78+
connect: {
79+
id: routeId,
80+
},
81+
},
82+
},
83+
});
84+
85+
res.status(200).json(updatedArea);
86+
};
87+
88+
const getAllAreas = async (req: Request, res: Response) => {
89+
const areas = await prisma.area.findMany();
90+
res.status(200).json(areas);
91+
};
92+
93+
export {
94+
addRoute,
95+
getAllRoutes,
96+
getRouteById,
97+
addArea,
98+
addRouteToArea,
99+
getAllAreas,
100+
};
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
import { PrismaClient } from "@prisma/client";
2+
import { Request, Response } from "express";
3+
4+
const prisma = new PrismaClient();
5+
6+
// const addSTSContractorEntry = async (req: Request, res: Response) => {

server/src/prisma/schema.prisma

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -189,6 +189,10 @@ model Landfill {
189189
Vehicle Vehicle[]
190190
}
191191

192+
// model STSContractorEntry{
193+
194+
// }
195+
192196
model STSVehicleEntry {
193197
id String @id @default(uuid())
194198
sts STS @relation(fields: [stsId], references: [id], onDelete: Cascade)

server/src/prisma/seed.ts

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -280,6 +280,26 @@ const userData: Prisma.UserCreateInput[] = [
280280
},
281281
];
282282

283+
const contractorData: Prisma.ContractorCreateInput[] = [
284+
{
285+
name: "Contractor 2 Name",
286+
registrationId: "ABSDC123456",
287+
registrationDate: "2024-05-10T00:00:00Z",
288+
tinNumber: "34334",
289+
contactNumber: "+929323",
290+
workforceSize: 100,
291+
paymentPerTon: 10.5,
292+
requiredWastePerDay: 500.0,
293+
contractDuration: "1 year",
294+
area: "City XYZ",
295+
assignedSTS: {
296+
connect: {
297+
id: "sts1",
298+
},
299+
},
300+
},
301+
];
302+
283303
const vehicleData: Prisma.VehicleCreateInput[] = [
284304
{
285305
id: "vid1",
@@ -579,6 +599,14 @@ async function main() {
579599
console.log(newRoute);
580600
}
581601

602+
console.log("Seeding contractors...");
603+
for (const contractor of contractorData) {
604+
const newContractor = await prisma.contractor.create({
605+
data: contractor,
606+
});
607+
console.log(newContractor);
608+
}
609+
582610
console.log("Seeding users...");
583611
for (const user of userData) {
584612
const newUser = await prisma.user.create({

server/src/routes/index.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ import scheduleRoute from "./schedule";
1616
import authChecker from "../middlewares/auth";
1717
import contractorRoute from "./contractor";
1818
import logRouter from "./logs";
19+
import routeAreaRouter from "./routeArea";
1920

2021
router.use("/auth", authRoute);
2122
router.use("/users", userRoute);
@@ -33,5 +34,6 @@ router.use("/schedules", scheduleRoute);
3334
// auth checker needed
3435
router.use("/contractors", contractorRoute);
3536
router.use("/logs", logRouter);
37+
router.use("/route-areas", routeAreaRouter);
3638

3739
export default router;

server/src/routes/routeArea.ts

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
import express from "express";
2+
const router = express.Router();
3+
4+
import {
5+
addArea,
6+
getAllAreas,
7+
addRoute,
8+
getAllRoutes,
9+
getRouteById,
10+
addRouteToArea,
11+
} from "../controllers/routeArea";
12+
13+
router.route("/create").post(addRoute);
14+
router.route("/").get(getAllRoutes);
15+
router.route("/area").post(addArea);
16+
router.route("/area").get(getAllAreas);
17+
router.route("/:routeId").get(getRouteById);
18+
router.route("/area/:areaId/route/:routeId").post(addRouteToArea);
19+
20+
export default router;

server/src/services/logdata.ts

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,3 +10,12 @@ export const adminLog = async (logType: string, description: string) => {
1010
},
1111
});
1212
};
13+
14+
export const contractorLog = async (logType: string, description: string) => {
15+
await prisma.contractorLogs.create({
16+
data: {
17+
type: logType,
18+
description: description,
19+
},
20+
});
21+
};

0 commit comments

Comments
 (0)