Tech_Tracker_Web/src/server/functions.ts

35 lines
1.0 KiB
TypeScript
Raw Normal View History

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
2024-07-20 09:12:29 -05:00
export const updateEmployeeStatus = async (employeeIds: string[], newStatus: string) => {
2024-07-19 17:00:36 -05:00
try {
// Convert array of ids to a format suitable for SQL query (comma-separated string)
2024-07-20 09:12:29 -05:00
const idList = employeeIds.map(id => parseInt(id, 10));
const updatedAt = new Date();
2024-07-19 15:58:24 -05:00
2024-07-20 09:12:29 -05:00
// Prepare the query using drizzle-orm's template-like syntax for escaping variables
const query = sql`
2024-07-19 17:00:36 -05:00
UPDATE users
2024-07-20 09:12:29 -05:00
SET status = ${newStatus}, updatedAt = ${updatedAt}
WHERE id IN ${idList}
2024-07-19 17:00:36 -05:00
`;
2024-07-20 09:12:29 -05:00
// Execute the query
await db.execute(query);
2024-07-19 17:00:36 -05:00
return { success: true };
} catch (error) {
console.error("Error updating employee status:", error);
throw new Error("Failed to update status");
}
};