111 lines
3.4 KiB
JavaScript
111 lines
3.4 KiB
JavaScript
import { login } from './auth.js'
|
|
import { convertToAmsterdamTime } from "./utils.js";
|
|
|
|
export const getShifts = async (req, res) => {
|
|
const apiKey = req.body.apiKey;
|
|
// const apiProdUrl = 'https://api.fleks.works/v1/shifts/?limit=100000&page=1&isArchived=false&is_approved=false&is_exported=false&is_invoiced=false&is_paid_out=true&noShiftComments=true';
|
|
const apiProdUrl = 'https://api.fleks.works/v1/shifts/?limit=100000&page=1&isArchived=false&is_approved=false&noShiftComments=true';
|
|
|
|
try {
|
|
const response = await fetch(apiProdUrl, {
|
|
headers: {
|
|
'x-api-key': apiKey
|
|
}
|
|
});
|
|
|
|
const data = await response.json();
|
|
|
|
const shifts = data.results.map(shift => {
|
|
const startDate = new Date(shift.start_date);
|
|
const endDate = new Date(shift.end_date);
|
|
const startAmsterdamTime = convertToAmsterdamTime(startDate);
|
|
const endAmsterdamTime = convertToAmsterdamTime(endDate);
|
|
|
|
return {
|
|
...shift,
|
|
start_time: startAmsterdamTime,
|
|
end_time: endAmsterdamTime
|
|
};
|
|
});
|
|
|
|
res.json({ ...data, results: shifts });
|
|
} catch (error) {
|
|
console.log(error)
|
|
res.status(500).json({ error: 'Error fetching data' });
|
|
}
|
|
}
|
|
|
|
export const updateShifts = async (req, res) => {
|
|
const { config, shifts } = req.body; // Expected type = { config , shifts }
|
|
|
|
if (!Array.isArray(shifts)) {
|
|
console.error('Request body should be an array of updates');
|
|
return res.status(400).json({ error: 'Request body should be an array of updates' });
|
|
}
|
|
|
|
const results = {
|
|
success: [],
|
|
failed: []
|
|
};
|
|
|
|
const getToken = async () => {
|
|
return await login(config?.userName, config?.passWord);
|
|
}
|
|
|
|
let token = await getToken();
|
|
|
|
if (!token) {
|
|
const error = `token not obtained`;
|
|
console.log(error);
|
|
return res.status(403).send({ error });
|
|
}
|
|
|
|
const updateShift = async (update) => {
|
|
const { uuid, break_hours } = update;
|
|
const apiProdUrl = `https://backend.fleks.works/api/jobs/workflow-fields/${uuid}/`;
|
|
|
|
try {
|
|
const response = await fetch(apiProdUrl, {
|
|
method: 'PUT',
|
|
headers: {
|
|
'Content-Type': 'application/json',
|
|
'Authorization': `JWT ${token}`
|
|
},
|
|
body: JSON.stringify({ break_hours })
|
|
});
|
|
|
|
const data = await response.json();
|
|
|
|
if (response.ok) {
|
|
console.log(`Successfully updated break hours for UUID: ${uuid}`);
|
|
results.success.push({ uuid, message: data.message });
|
|
} else {
|
|
if (response.status === 401 || response.status === 403) {
|
|
console.log(`Token invalid. Reattempting login for UUID: ${uuid}`);
|
|
token = await getToken();
|
|
if (token) {
|
|
return updateShift(update);
|
|
} else {
|
|
console.error(`Failed to update break hours for UUID: ${uuid}`, data);
|
|
results.failed.push({ uuid, error: 'Failed to reauthenticate and obtain new token' });
|
|
}
|
|
} else {
|
|
console.error(`Failed to update break hours for UUID: ${uuid}`, data);
|
|
results.failed.push({ uuid, error: data.message || data });
|
|
}
|
|
}
|
|
} catch (error) {
|
|
console.error(`Error updating break hours for UUID: ${uuid}`, error);
|
|
results.failed.push({ uuid, error: error.message });
|
|
}
|
|
}
|
|
|
|
for (const update of shifts) {
|
|
await updateShift(update);
|
|
}
|
|
|
|
console.log(`Finished processing updates. Success: ${results.success.length}, Failed: ${results.failed.length}`);
|
|
|
|
res.json(results);
|
|
}
|