Compare commits
2 Commits
eb97317b1c
...
7a8ab80a84
| Author | SHA1 | Date | |
|---|---|---|---|
| 7a8ab80a84 | |||
| 0e61b32467 |
@ -14,7 +14,8 @@ export const getEmployees = async (ctx: any) => {
|
||||
JOIN partners p ON e.partner_id = p.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`
|
||||
JOIN job_positions j ON c.job_id = j.job_id
|
||||
WHERE e.is_active = true`
|
||||
);
|
||||
|
||||
ctx.response.body = {
|
||||
@ -101,4 +102,175 @@ export const createEmployee = async (ctx: any) => {
|
||||
// 6. Release the connection back to the pool
|
||||
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,12 +1,26 @@
|
||||
import { Router } from "@oak/oak";
|
||||
import { getEmployees, createEmployee } from "./controllers/employee.controller.ts";
|
||||
import {
|
||||
getEmployees,
|
||||
createEmployee,
|
||||
getEmployeeById,
|
||||
updateEmployee,
|
||||
deleteEmployee } from "./controllers/employee.controller.ts";
|
||||
|
||||
const router = new Router();
|
||||
|
||||
// Route definitions are now clean and easy to read
|
||||
// Retrieve all employees
|
||||
router.get("/api/v1/employees", getEmployees);
|
||||
|
||||
// Retrieve a single employee's comprehensive profile
|
||||
router.get("/api/v1/employees/:id", getEmployeeById);
|
||||
|
||||
// Create a new employee
|
||||
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;
|
||||
Loading…
x
Reference in New Issue
Block a user