2024-07-19 15:58:24 -05:00
|
|
|
import "server-only";
|
|
|
|
import { db } from "~/server/db";
|
2024-07-19 17:00:36 -05:00
|
|
|
import { sql } from "drizzle-orm";
|
2024-07-19 15:58:24 -05:00
|
|
|
|
2024-07-19 17:00:36 -05:00
|
|
|
// Function to Get Employees
|
2024-07-19 15:58:24 -05:00
|
|
|
export const getEmployees = async () => {
|
|
|
|
return await db.query.users.findMany({
|
2024-07-19 17:00:36 -05:00
|
|
|
orderBy: (model, { asc }) => asc(model.id),
|
2024-07-19 15:58:24 -05:00
|
|
|
});
|
|
|
|
};
|
|
|
|
|
2024-07-19 17:00:36 -05:00
|
|
|
// Function to Update Employee Status using Raw SQL
|
|
|
|
export const updateEmployeeStatus = async (employeeIds: number[], newStatus: string) => {
|
|
|
|
try {
|
|
|
|
// Convert array of ids to a format suitable for SQL query (comma-separated string)
|
|
|
|
const idString = employeeIds.join(",");
|
2024-07-19 15:58:24 -05:00
|
|
|
|
2024-07-19 17:00:36 -05:00
|
|
|
// Prepare the raw SQL query with embedded variables
|
|
|
|
const query = `
|
|
|
|
UPDATE users
|
|
|
|
SET status = '${newStatus}', updatedAt = '${new Date().toISOString()}'
|
|
|
|
WHERE id IN (${idString})
|
|
|
|
`;
|
|
|
|
|
|
|
|
// Execute the raw SQL query using the execute method
|
|
|
|
await db.execute(sql`${query}`);
|
|
|
|
|
|
|
|
return { success: true };
|
|
|
|
} catch (error) {
|
|
|
|
console.error("Error updating employee status:", error);
|
|
|
|
throw new Error("Failed to update status");
|
|
|
|
}
|
|
|
|
};
|