breakhours-transform/services/PauzeManager.js

208 lines
6.1 KiB
JavaScript
Raw Normal View History

2024-10-03 11:55:18 +00:00
import { query } from './db.js';
export class PauzeManager {
constructor(identifier = 'Pauze: ', suffix = '') {
this.identifier = identifier;
this.suffix = suffix;
this.pauzeRegex = new RegExp(`${this.identifier}\\d+\\s*min\\.?\\s*(--\\s*)?`);
}
parseTime(timeString) {
const [hours, minutes] = timeString.split(':').map(Number);
return { hours, minutes };
}
isMatchingTime(jobStart, jobEnd, entryStart, entryEnd) {
const jobStartTime = this.parseTime(jobStart);
const jobEndTime = this.parseTime(jobEnd);
const entryStartTime = this.parseTime(entryStart);
const entryEndTime = this.parseTime(entryEnd);
const jobStartMinutes = jobStartTime.hours * 60 + jobStartTime.minutes;
const jobEndMinutes = jobEndTime.hours * 60 + jobEndTime.minutes;
const entryStartMinutes = entryStartTime.hours * 60 + entryStartTime.minutes;
const entryEndMinutes = entryEndTime.hours * 60 + entryEndTime.minutes;
const normalizedJobEndMinutes = jobEndMinutes < jobStartMinutes ? jobEndMinutes + 24 * 60 : jobEndMinutes;
const normalizedEntryEndMinutes = entryEndMinutes < entryStartMinutes ? entryEndMinutes + 24 * 60 : entryEndMinutes;
return jobStartMinutes === entryStartMinutes && normalizedJobEndMinutes === normalizedEntryEndMinutes;
}
convertBreakHoursToPauze(breakHours) {
const hours = parseFloat(breakHours);
const minutes = Math.round(hours * 60);
return `${this.identifier}${minutes} min${this.suffix}`;
}
updateFunctionDescription(description, pauzeText) {
if (!description) return pauzeText;
const match = description.match(this.pauzeRegex);
if (match) {
const trailingDashes = match[0].includes('--') ? ' -- ' : '';
return description.replace(this.pauzeRegex, `${pauzeText}${trailingDashes}`);
} else {
return `${pauzeText}${description.trim() ? ' -- ' + description : ''}`;
}
}
// updateFunctionDescription(description, pauzeText) {
// if (!description) return pauzeText;
// if (this.pauzeRegex.test(description)) {
// return description.replace(this.pauzeRegex, pauzeText);
// } else {
// return `${pauzeText}${description.trim() ? ' -- ' + description : ''}`;
// }
// }
updateJobsWithPauze(jobs, timeTable, convertToAmsterdamTime) {
return jobs.map(job => {
const startDate = new Date(job.start_date);
const endDate = new Date(job.end_date);
const startAmsterdamTime = convertToAmsterdamTime(startDate);
const endAmsterdamTime = convertToAmsterdamTime(endDate);
const formattedStartTime = startAmsterdamTime.slice(0, 5);
const formattedEndTime = endAmsterdamTime.slice(0, 5);
const matchedTime = timeTable.find(entry =>
this.isMatchingTime(formattedStartTime, formattedEndTime, entry.start_time, entry.end_time)
);
const oldBreakDecimal = parseFloat(job.break_hours);
const oldBreakMinutes = Math.round(oldBreakDecimal * 60);
const newBreakMinutes = matchedTime ? Math.round(parseFloat(matchedTime.break_hours) * 60) : "N/A";
let updatedDescription = job.function_description;
if (matchedTime) {
const pauzeText = this.convertBreakHoursToPauze(matchedTime.break_hours);
updatedDescription = this.updateFunctionDescription(job.function_description, pauzeText);
}
return {
job_id: job.job_id,
job_uuid: job.job_uuid,
project_title: job.project_title,
start_time: startAmsterdamTime,
end_time: endAmsterdamTime,
function_description: updatedDescription,
break_minutes: {
old: oldBreakMinutes,
new: newBreakMinutes
}
};
});
}
async updateJobsInDatabase(tenantId, jobs) {
if (!tenantId) {
throw new Error("Tenant ID is required");
}
const updatePromises = jobs.map(job => {
const queryText = `
UPDATE public.jobs_jobs
SET function_description = $2
WHERE uuid = $3
AND tenant_id = $1
`;
const queryValues = [
tenantId,
job.function_description,
job.job_uuid
];
return query(queryText, queryValues);
});
await Promise.all(updatePromises);
}
async getAllJobs(tenantId) {
const queryText = `
SELECT *
FROM public.jobs_jobs
WHERE tenant_id = $1
`;
const queryValues = [tenantId];
const res = await query(queryText, queryValues);
return res.rows;
}
async getJobByUUID(jobUUID) {
const queryText = `
SELECT *
FROM public.jobs_jobs
WHERE uuid = $1
`;
const queryValues = [jobUUID];
const res = await query(queryText, queryValues);
return res.rows[0];
}
async getAllJobsWithPauze() {
const queryText = `
SELECT *
FROM public.jobs_jobs
WHERE function_description LIKE $1
`;
const queryValues = [`%${this.identifier}%`];
const res = await query(queryText, queryValues);
return res.rows;
}
async getJobsWithPauze(tenantId) {
const queryText = `
SELECT *
FROM public.jobs_jobs
WHERE tenant_id = $1 AND function_description LIKE '%Pauze%'
`;
const queryValues = [tenantId];
const res = await query(queryText, queryValues);
return res.rows;
}
async removePauzeFromDatabase(tenantId) {
if (!tenantId) {
throw new Error("Tenant ID is required");
}
const jobsWithPauze = await this.getAllJobsWithPauze();
console.log({ jobspauze: jobsWithPauze[0] })
const jobsWithoutPauze = jobsWithPauze.map(job => {
let description = job.function_description;
if (!description) return job;
// Remove the pauze text
let updatedDescription = description.replace(this.pauzeRegex, '').trim();
if (updatedDescription.startsWith('--')) {
updatedDescription = updatedDescription.slice(2).trim();
}
if (updatedDescription.endsWith('--')) {
updatedDescription = updatedDescription.slice(0, -2).trim();
}
return {
...job,
function_description: updatedDescription
};
});
await this.updateJobsInDatabase(tenantId, jobsWithoutPauze);
}
}