Compare commits
No commits in common. "7a8ab80a84f2eaf9b43e5fad9008e4f198a46c6e" and "eb97317b1c74ce4bccf0ad0c20f0de4771ee847a" have entirely different histories.
7a8ab80a84
...
eb97317b1c
@ -14,8 +14,7 @@ export const getEmployees = async (ctx: any) => {
|
|||||||
JOIN partners p ON e.partner_id = p.partner_id
|
JOIN partners p ON e.partner_id = p.partner_id
|
||||||
JOIN contracts c ON e.employee_id = c.employee_id
|
JOIN contracts c ON e.employee_id = c.employee_id
|
||||||
JOIN departments d ON c.department_id = d.department_id
|
JOIN departments d ON c.department_id = d.department_id
|
||||||
JOIN job_positions j ON c.job_id = j.job_id
|
JOIN job_positions j ON c.job_id = j.job_id`
|
||||||
WHERE e.is_active = true`
|
|
||||||
);
|
);
|
||||||
|
|
||||||
ctx.response.body = {
|
ctx.response.body = {
|
||||||
@ -103,174 +102,3 @@ export const createEmployee = async (ctx: any) => {
|
|||||||
connection.release();
|
connection.release();
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
export const getEmployeeById = async (ctx: any) => {
|
|
||||||
const id = ctx.params.id;
|
|
||||||
|
|
||||||
try {
|
|
||||||
const [rows] = await pool.query(
|
|
||||||
`SELECT
|
|
||||||
e.employee_id,
|
|
||||||
e.employee_code,
|
|
||||||
e.is_active,
|
|
||||||
p.first_name,
|
|
||||||
p.last_name,
|
|
||||||
p.dob,
|
|
||||||
p.gender,
|
|
||||||
p.personal_email,
|
|
||||||
p.personal_phone,
|
|
||||||
a.address_type,
|
|
||||||
a.door_number,
|
|
||||||
a.landmark,
|
|
||||||
a.address_line,
|
|
||||||
a.pincode,
|
|
||||||
a.district,
|
|
||||||
a.state,
|
|
||||||
c.work_email,
|
|
||||||
c.date_joining,
|
|
||||||
c.probation_days,
|
|
||||||
c.status AS contract_status,
|
|
||||||
d.name AS department,
|
|
||||||
j.title AS designation
|
|
||||||
FROM employees e
|
|
||||||
JOIN partners p ON e.partner_id = p.partner_id
|
|
||||||
LEFT JOIN addresses a ON p.partner_id = a.partner_id
|
|
||||||
JOIN contracts c ON e.employee_id = c.employee_id
|
|
||||||
JOIN departments d ON c.department_id = d.department_id
|
|
||||||
JOIN job_positions j ON c.job_id = j.job_id
|
|
||||||
WHERE e.employee_id = ?`,
|
|
||||||
[id]
|
|
||||||
);
|
|
||||||
|
|
||||||
const data = rows as any[];
|
|
||||||
|
|
||||||
if (data.length === 0) {
|
|
||||||
ctx.response.status = 404;
|
|
||||||
ctx.response.body = {
|
|
||||||
success: false,
|
|
||||||
message: "Employee not found"
|
|
||||||
};
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
|
|
||||||
ctx.response.status = 200;
|
|
||||||
ctx.response.body = {
|
|
||||||
success: true,
|
|
||||||
data: data[0]
|
|
||||||
};
|
|
||||||
} catch (error) {
|
|
||||||
ctx.response.status = 500;
|
|
||||||
ctx.response.body = {
|
|
||||||
success: false,
|
|
||||||
error: (error as Error).message
|
|
||||||
};
|
|
||||||
}
|
|
||||||
};
|
|
||||||
|
|
||||||
export const updateEmployee = async (ctx: any) => {
|
|
||||||
const id = ctx.params.id;
|
|
||||||
const body = ctx.request.body;
|
|
||||||
|
|
||||||
if (body.type() !== "json") {
|
|
||||||
ctx.response.status = 400;
|
|
||||||
ctx.response.body = { success: false, error: "Invalid JSON body" };
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
|
|
||||||
const data = await body.json();
|
|
||||||
const connection = await pool.getConnection();
|
|
||||||
|
|
||||||
try {
|
|
||||||
await connection.beginTransaction();
|
|
||||||
|
|
||||||
// 1. Fetch the partner_id linked to this employee
|
|
||||||
const [employeeRows] = await connection.execute(
|
|
||||||
`SELECT partner_id FROM employees WHERE employee_id = ?`,
|
|
||||||
[id]
|
|
||||||
);
|
|
||||||
|
|
||||||
const employees = employeeRows as any[];
|
|
||||||
if (employees.length === 0) {
|
|
||||||
ctx.response.status = 404;
|
|
||||||
ctx.response.body = { success: false, message: "Employee not found" };
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
|
|
||||||
const partnerId = employees[0].partner_id;
|
|
||||||
|
|
||||||
// 2. Update the Partners table (Personal Data)
|
|
||||||
await connection.execute(
|
|
||||||
`UPDATE partners
|
|
||||||
SET first_name = ?, last_name = ?, personal_email = ?, personal_phone = ?
|
|
||||||
WHERE partner_id = ?`,
|
|
||||||
[data.firstName, data.lastName, data.personalEmail, data.personalPhone, partnerId]
|
|
||||||
);
|
|
||||||
|
|
||||||
// 3. Update the Addresses table
|
|
||||||
await connection.execute(
|
|
||||||
`UPDATE addresses
|
|
||||||
SET door_number = ?, landmark = ?, address_line = ?, pincode = ?, district = ?, state = ?
|
|
||||||
WHERE partner_id = ? AND address_type = ?`,
|
|
||||||
[
|
|
||||||
data.address.doorNumber,
|
|
||||||
data.address.landmark,
|
|
||||||
data.address.line,
|
|
||||||
data.address.pincode,
|
|
||||||
data.address.district,
|
|
||||||
data.address.state,
|
|
||||||
partnerId,
|
|
||||||
data.address.type
|
|
||||||
]
|
|
||||||
);
|
|
||||||
|
|
||||||
await connection.commit();
|
|
||||||
|
|
||||||
ctx.response.status = 200;
|
|
||||||
ctx.response.body = {
|
|
||||||
success: true,
|
|
||||||
message: "Employee profile updated successfully"
|
|
||||||
};
|
|
||||||
|
|
||||||
} catch (error) {
|
|
||||||
await connection.rollback();
|
|
||||||
ctx.response.status = 500;
|
|
||||||
ctx.response.body = {
|
|
||||||
success: false,
|
|
||||||
error: "Update failed: " + (error as Error).message
|
|
||||||
};
|
|
||||||
} finally {
|
|
||||||
connection.release();
|
|
||||||
}
|
|
||||||
};
|
|
||||||
|
|
||||||
export const deleteEmployee = async (ctx: any) => {
|
|
||||||
const id = ctx.params.id;
|
|
||||||
|
|
||||||
try {
|
|
||||||
const [result] = await pool.execute(
|
|
||||||
`UPDATE employees SET is_active = false WHERE employee_id = ?`,
|
|
||||||
[id]
|
|
||||||
);
|
|
||||||
|
|
||||||
const updateResult = result as any;
|
|
||||||
|
|
||||||
if (updateResult.affectedRows === 0) {
|
|
||||||
ctx.response.status = 404;
|
|
||||||
ctx.response.body = { success: false, message: "Employee not found" };
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
|
|
||||||
ctx.response.status = 200;
|
|
||||||
ctx.response.body = {
|
|
||||||
success: true,
|
|
||||||
message: "Employee account deactivated successfully"
|
|
||||||
};
|
|
||||||
|
|
||||||
} catch (error) {
|
|
||||||
ctx.response.status = 500;
|
|
||||||
ctx.response.body = {
|
|
||||||
success: false,
|
|
||||||
error: "Deactivation failed: " + (error as Error).message
|
|
||||||
};
|
|
||||||
}
|
|
||||||
};
|
|
||||||
@ -1,26 +1,12 @@
|
|||||||
import { Router } from "@oak/oak";
|
import { Router } from "@oak/oak";
|
||||||
import {
|
import { getEmployees, createEmployee } from "./controllers/employee.controller.ts";
|
||||||
getEmployees,
|
|
||||||
createEmployee,
|
|
||||||
getEmployeeById,
|
|
||||||
updateEmployee,
|
|
||||||
deleteEmployee } from "./controllers/employee.controller.ts";
|
|
||||||
|
|
||||||
const router = new Router();
|
const router = new Router();
|
||||||
|
|
||||||
// Retrieve all employees
|
// Route definitions are now clean and easy to read
|
||||||
router.get("/api/v1/employees", getEmployees);
|
router.get("/api/v1/employees", getEmployees);
|
||||||
|
|
||||||
// Retrieve a single employee's comprehensive profile
|
|
||||||
router.get("/api/v1/employees/:id", getEmployeeById);
|
|
||||||
|
|
||||||
// Create a new employee
|
// Create a new employee
|
||||||
router.post("/api/v1/employees", createEmployee);
|
router.post("/api/v1/employees", createEmployee);
|
||||||
|
|
||||||
// Update an existing employee
|
|
||||||
router.put("/api/v1/employees/:id", updateEmployee);
|
|
||||||
|
|
||||||
// Deactivate an employee
|
|
||||||
router.delete("/api/v1/employees/:id", deleteEmployee);
|
|
||||||
|
|
||||||
export default router;
|
export default router;
|
||||||
Loading…
x
Reference in New Issue
Block a user