Enhance service worker with update handling and improve task scheduling format

This commit is contained in:
2025-01-29 21:57:04 +01:00
parent 502938b8cf
commit a9dfb8d54d
3 changed files with 84 additions and 29 deletions

View File

@@ -15,16 +15,30 @@ if (!fs.existsSync(dataDir)) {
// Ensure the tags.json file exists
const tagsFilePath = path.join(dataDir, 'tags.json');
if (!fs.existsSync(tagsFilePath)) {
fs.writeFileSync(tagsFilePath, JSON.stringify([]));}
fs.writeFileSync(tagsFilePath, JSON.stringify([]));
}
// Protect the /add-task endpoint with authentication
router.post('/add-task', auth, async (req, res) => {
const { subject, description, scheduled } = req.body;
const currentDateTime = new Date().toISOString().replace(/T/, ' ').replace(/\..+/, '');
// Format the scheduled date and time for Org mode
const scheduledDate = new Date(scheduled);
const dayNames = ['Sun', 'Mon', 'Tue', 'Wed', 'Thu', 'Fri', 'Sat'];
const dayName = dayNames[scheduledDate.getUTCDay()];
let formattedScheduled;
if (scheduledDate.getUTCHours() === 0 && scheduledDate.getUTCMinutes() === 0) {
// No time provided, format without time
formattedScheduled = `<${scheduledDate.getUTCFullYear()}-${String(scheduledDate.getUTCMonth() + 1).padStart(2, '0')}-${String(scheduledDate.getUTCDate()).padStart(2, '0')} ${dayName}>`;
} else {
// Time provided, format with time
formattedScheduled = `<${scheduledDate.getUTCFullYear()}-${String(scheduledDate.getUTCMonth() + 1).padStart(2, '0')}-${String(scheduledDate.getUTCDate()).padStart(2, '0')} ${dayName} ${String(scheduledDate.getUTCHours()).padStart(2, '0')}:${String(scheduledDate.getUTCMinutes()).padStart(2, '0')}>`;
}
let orgFormattedData = `* TODO ${subject}
SCHEDULED: <${scheduled}>
SCHEDULED: ${formattedScheduled}
:LOGBOOK:
- State "TODO" from "TODO" [${currentDateTime}]
:END:
@@ -33,7 +47,7 @@ router.post('/add-task', auth, async (req, res) => {
if (description) {
orgFormattedData = `* TODO ${subject}
${description}
SCHEDULED: <${scheduled}>
SCHEDULED: ${formattedScheduled}
:LOGBOOK:
- State "TODO" from "TODO" [${currentDateTime}]
:END:
@@ -43,10 +57,10 @@ router.post('/add-task', auth, async (req, res) => {
const filePath = path.join(dataDir, 'tasks.org');
try {
await fs.promises.appendFile(filePath, orgFormattedData);
res.send({ message: 'Task added successfully!' });
} catch (err) {
logger.error('Error writing to file:', err);
res.status(500).send('Error writing to file.');
res.json({ message: 'Task added successfully' });
} catch (error) {
logger.error('Error writing to tasks.org file:', error);
res.status(500).json({ message: 'Error adding task' });
}
});