Updated Mult-Company Support and added Tax ID Fields
This commit is contained in:
@@ -16,15 +16,17 @@ function assignmentFromRow(row) {
|
||||
};
|
||||
}
|
||||
|
||||
function listEmployees(query = {}) {
|
||||
function listEmployees(query = {}, companyId) {
|
||||
return all(`
|
||||
SELECT *
|
||||
FROM employees
|
||||
WHERE (? IS NULL OR status = ?)
|
||||
WHERE (? IS NULL OR entity_id = ?)
|
||||
AND (? IS NULL OR status = ?)
|
||||
AND (? IS NULL OR name LIKE '%' || ? || '%' OR email LIKE '%' || ? || '%' OR employee_number LIKE '%' || ? || '%')
|
||||
ORDER BY name
|
||||
LIMIT 500
|
||||
`, [
|
||||
companyId || null, companyId || null,
|
||||
query.status || null,
|
||||
query.status || null,
|
||||
query.search || null,
|
||||
@@ -34,12 +36,14 @@ function listEmployees(query = {}) {
|
||||
]).map(employeeFromRow);
|
||||
}
|
||||
|
||||
function upsertEmployee(employee) {
|
||||
// Manual creates use the active company; Workday sync (no companyId) lands in the default company.
|
||||
function upsertEmployee(employee, companyId) {
|
||||
const timestamp = now();
|
||||
const entityId = companyId || (one("SELECT id FROM entities WHERE status = 'active' ORDER BY id LIMIT 1")?.id) || null;
|
||||
const workdayId = employee.workday_worker_id || employee.workdayWorkerId || employee.workerId || null;
|
||||
const existing = workdayId
|
||||
? one('SELECT id FROM employees WHERE workday_worker_id = ?', [workdayId])
|
||||
: one('SELECT id FROM employees WHERE employee_number = ? OR lower(email) = lower(?)', [employee.employee_number || '', employee.email || '']);
|
||||
: one('SELECT id FROM employees WHERE entity_id = ? AND (employee_number = ? OR lower(email) = lower(?))', [entityId, employee.employee_number || '', employee.email || '']);
|
||||
|
||||
const values = [
|
||||
workdayId,
|
||||
@@ -79,10 +83,10 @@ function upsertEmployee(employee) {
|
||||
|
||||
const result = run(
|
||||
`INSERT INTO employees (
|
||||
workday_worker_id, employee_number, name, email, department, location, manager_name,
|
||||
entity_id, workday_worker_id, employee_number, name, email, department, location, manager_name,
|
||||
status, source, source_payload, last_synced_at, created_at, updated_at
|
||||
) VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)`,
|
||||
[...values, timestamp]
|
||||
) VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)`,
|
||||
[entityId, ...values, timestamp]
|
||||
);
|
||||
return one('SELECT * FROM employees WHERE id = ?', [result.lastInsertRowid]);
|
||||
}
|
||||
@@ -116,7 +120,8 @@ function assignAsset(body, userId, companyId) {
|
||||
const asset = one('SELECT * FROM assets WHERE id = ? AND (? IS NULL OR entity_id = ?)', [body.asset_id, companyId || null, companyId || null]);
|
||||
if (!asset) throw Object.assign(new Error('Asset not found'), { status: 404 });
|
||||
|
||||
const employee = body.employee_id ? one('SELECT * FROM employees WHERE id = ?', [body.employee_id]) : null;
|
||||
// Only an employee in the same company may be assigned (foreign ids resolve to no employee).
|
||||
const employee = body.employee_id ? one('SELECT * FROM employees WHERE id = ? AND (? IS NULL OR entity_id = ?)', [body.employee_id, companyId || null, companyId || null]) : null;
|
||||
const department = body.department ?? employee?.department ?? asset.department ?? null;
|
||||
const location = body.location ?? employee?.location ?? asset.location ?? null;
|
||||
|
||||
@@ -134,7 +139,7 @@ function assignAsset(body, userId, companyId) {
|
||||
) VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?)`,
|
||||
[
|
||||
body.asset_id,
|
||||
body.employee_id || null,
|
||||
employee ? employee.id : null,
|
||||
department,
|
||||
location,
|
||||
'assigned',
|
||||
|
||||
Reference in New Issue
Block a user