115 lines
4.2 KiB
JavaScript
115 lines
4.2 KiB
JavaScript
/**
|
|
* TasGrid SDK
|
|
*
|
|
* A headless utility for creating tasks in the TasGrid PocketBase database
|
|
* from sister applications, ensuring all TasGrid defaults and date calculations
|
|
* are applied consistently.
|
|
*/
|
|
|
|
const URGENCY_HOURS = {
|
|
10: 12,
|
|
9: 24,
|
|
8: 36,
|
|
7: 72,
|
|
6: 120,
|
|
5: 240,
|
|
4: 504,
|
|
3: 1080,
|
|
2: 2160,
|
|
1: 4320
|
|
};
|
|
|
|
/**
|
|
* Calculates a future ISO date string based on the TasGrid urgency decay algorithm.
|
|
* @param {number} level Urgency level (1-10)
|
|
* @returns {string} ISO Date string
|
|
*/
|
|
export const calculateDateFromUrgency = (level) => {
|
|
const roundedLevel = Math.max(1, Math.min(10, Math.round(level)));
|
|
const hours = URGENCY_HOURS[roundedLevel] || 24;
|
|
return new Date(Date.now() + hours * 60 * 60 * 1000).toISOString();
|
|
};
|
|
|
|
/**
|
|
* Creates a new task in the TasGrid database using the provided PocketBase instance.
|
|
*
|
|
* @param {object} pb - An authenticated PocketBase JS SDK instance.
|
|
* @param {object} payload - The task payload.
|
|
* @param {string} [payload.title="New Task"] - The task title.
|
|
* @param {string} [payload.owner] - PocketBase User ID. Defaults to pb.authStore.model.id.
|
|
* @param {number} [payload.priority=5] - Priority (1-10).
|
|
* @param {number} [payload.urgency=5] - Urgency (1-10). Used to calculate dueDate if omitted.
|
|
* @param {number} [payload.size=3] - Size (0-10).
|
|
* @param {number} [payload.status=0] - Status (0-10).
|
|
* @param {boolean} [payload.completed=false] - Completion status.
|
|
* @param {string[]} [payload.tags=[]] - Array of tag strings.
|
|
* @param {string} [payload.content=""] - HTML string for task description/notes.
|
|
* @param {string} [payload.dueDate] - Strict ISO Date string. Overrides urgency calculation.
|
|
* @param {string} [payload.startDate=""] - ISO Date string.
|
|
* @returns {Promise<object>} The created PocketBase task record.
|
|
*/
|
|
export const createTasgridTask = async (pb, payload = {}) => {
|
|
if (!pb || !pb.authStore || !pb.authStore.isValid || !pb.authStore.model) {
|
|
throw new Error("Invalid or unauthenticated PocketBase instance provided.");
|
|
}
|
|
|
|
const urgencyVal = payload.urgency ?? 5;
|
|
|
|
const taskData = {
|
|
title: payload.title || "New Task",
|
|
priority: payload.priority ?? 5,
|
|
size: payload.size ?? 3,
|
|
status: payload.status ?? 0,
|
|
completed: payload.completed ?? false,
|
|
tags: payload.tags || [],
|
|
content: payload.content || "",
|
|
user: payload.owner || pb.authStore.model.id,
|
|
dueDate: payload.dueDate || calculateDateFromUrgency(urgencyVal),
|
|
startDate: payload.startDate || "",
|
|
};
|
|
|
|
try {
|
|
const record = await pb.collection('TasGrid').create(taskData);
|
|
return record;
|
|
} catch (err) {
|
|
console.error("TasGrid SDK: Failed to create task", err);
|
|
throw err;
|
|
}
|
|
};
|
|
|
|
/**
|
|
* Creates a new note in the TasGrid database using the provided PocketBase instance.
|
|
*
|
|
* @param {object} pb - An authenticated PocketBase JS SDK instance.
|
|
* @param {object} payload - The note payload.
|
|
* @param {string} [payload.title="New Note"] - The note title.
|
|
* @param {string} [payload.content=""] - HTML string for note content.
|
|
* @param {string[]} [payload.tags=[]] - Array of tag strings.
|
|
* @param {boolean} [payload.isPrivate=false] - Whether the note is private.
|
|
* @param {string[]} [payload.tasks=[]] - Array of task IDs to link.
|
|
* @param {string} [payload.owner] - PocketBase User ID. Defaults to pb.authStore.model.id.
|
|
* @returns {Promise<object>} The created PocketBase note record.
|
|
*/
|
|
export const createTasgridNote = async (pb, payload = {}) => {
|
|
if (!pb || !pb.authStore || !pb.authStore.isValid || !pb.authStore.model) {
|
|
throw new Error("Invalid or unauthenticated PocketBase instance provided.");
|
|
}
|
|
|
|
const noteData = {
|
|
title: payload.title || "New Note",
|
|
content: payload.content || "",
|
|
tags: payload.tags || [],
|
|
isPrivate: payload.isPrivate || false,
|
|
tasks: payload.tasks || [],
|
|
user: payload.owner || pb.authStore.model.id,
|
|
};
|
|
|
|
try {
|
|
const record = await pb.collection('TasGrid_Notes').create(noteData);
|
|
return record;
|
|
} catch (err) {
|
|
console.error("TasGrid SDK: Failed to create note", err);
|
|
throw err;
|
|
}
|
|
};
|