Skip to content

Instantly share code, notes, and snippets.

@SergioGomez321
Last active February 7, 2026 06:27
Show Gist options
  • Select an option

  • Save SergioGomez321/3a6141f1ff49a8989aa419ca84481938 to your computer and use it in GitHub Desktop.

Select an option

Save SergioGomez321/3a6141f1ff49a8989aa419ca84481938 to your computer and use it in GitHub Desktop.
Timesheet generator file to save time every week. by Sergio Gomez
const headers = {
"x-csrf-token": "REPLACE_THIS_VALUE",
"cookie": "REPLACE_THIS_VALUE",
};
const timesheetId = 111090;
const dailyActivity = 154668;
const queryMigrationActivity = 213531;
const awsCourseActivity = 169760;
const holidayActivity = 353;
// --- HOLIDAYS (MX) using Nager.Date ---
const holidayCache = new Map(); // year -> Set("YYYY-MM-DD")
async function getMxHolidaySet(year) {
if (holidayCache.has(year)) return holidayCache.get(year);
// this is a public API
const res = await fetch(`https://date.nager.at/api/v3/PublicHolidays/${year}/MX`);
const holidays = await res.json();
const set = new Set(holidays.map(h => h.date));
holidayCache.set(year, set);
return set;
}
async function isHolidayMX(dateStr /* "YYYY-MM-DD" */) {
const year = dateStr.slice(0, 4);
const set = await getMxHolidaySet(year);
return set.has(dateStr);
}
// --- Generic POST ---
async function postActivity({ timesheetId, taskId, start, end }) {
return fetch(`https://company.domain/timesheets/${timesheetId}/activities`, {
method: "POST",
headers,
body: new URLSearchParams({
taskId: String(taskId),
start,
end,
}),
});
}
// --- Dates: Mon-Fri of this week -> ["YYYY-MM-DDT", ...] ---
function weekdaysMonToFriThisWeekPrefix() {
const now = new Date();
const dow = now.getDay(); // 0=Dom,1=Lun,...6=Sáb
const diffToMonday = (dow + 6) % 7;
const monday = new Date(now);
monday.setHours(0, 0, 0, 0);
monday.setDate(now.getDate() - diffToMonday);
const pad = (n) => String(n).padStart(2, "0");
return Array.from({ length: 5 }, (_, i) => {
const d = new Date(monday);
d.setDate(monday.getDate() + i);
return `${d.getFullYear()}-${pad(d.getMonth() + 1)}-${pad(d.getDate())}`;
});
}
async function createWeekActivities() {
const days = weekdaysMonToFriThisWeekPrefix();
// Pre-calc holidays for the week (re-usable + also tells us if the week had any holiday)
const holidayFlags = await Promise.all(days.map(day => isHolidayMX(day)));
const weekHasHoliday = holidayFlags.some(Boolean);
console.log("Week has holiday?", weekHasHoliday);
for (let i = 0; i < days.length; i++) {
const day = days[i];
const dateStr = day.slice(0, 10);
// Holiday day: add Holiday activity 08:00–14:00 and 15:00–17:00
if (holidayFlags[i]) {
await postActivity({
timesheetId,
taskId: holidayActivity,
start: `${day}T08:00:00`,
end: `${day}T14:00:00`,
});
await postActivity({
timesheetId,
taskId: holidayActivity,
start: `${day}T15:00:00`,
end: `${day}T17:00:00`,
});
console.log("Holiday creado para", dateStr);
continue;
}
// Normal day blocks
await postActivity({
timesheetId,
taskId: queryMigrationActivity,
start: `${day}T08:00:00`,
end: `${day}T09:30:00`,
});
await postActivity({
timesheetId,
taskId: dailyActivity,
start: `${day}T09:30:00`,
end: `${day}T10:00:00`,
});
await postActivity({
timesheetId,
taskId: queryMigrationActivity,
start: `${day}T10:00:00`,
end: `${day}T14:00:00`,
});
// Afternoon logic:
// If week had any holiday: NO AWSCourse for the entire week, extend Query Migration to 18:00
if (weekHasHoliday) {
await postActivity({
timesheetId,
taskId: queryMigrationActivity,
start: `${day}T15:00:00`,
end: `${day}T18:00:00`,
});
} else {
await postActivity({
timesheetId,
taskId: queryMigrationActivity,
start: `${day}T15:00:00`,
end: `${day}T17:00:00`,
});
// AWSCourse 17:00–18:00 except Friday (last day)
if (i !== days.length - 1) {
await postActivity({
timesheetId,
taskId: awsCourseActivity,
start: `${day}T17:00:00`,
end: `${day}T18:00:00`,
});
}
}
}
}
createWeekActivities().catch(console.error);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment