@latentsearch/timemachine-cli
Version:
CLI tool for TimeMachine API. Generates time entries, lists users/projects, and features an enhanced dry-run output for generation.
55 lines (54 loc) • 1.99 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
exports.resolveUser = resolveUser;
exports.resolveProject = resolveProject;
/**
* Resolves a user input (name or ID) to a user ID
*/
async function resolveUser(api, userInput) {
// If userInput is a number, assume it's an ID
if (!isNaN(parseInt(userInput, 10))) {
return parseInt(userInput, 10);
}
// Otherwise, search for user by name
try {
const response = await api.get("/api/users");
const users = response.data;
// Search for a user whose name contains the input (case-insensitive)
const user = users.find(u => {
const fullName = `${u.given_name || ""} ${u.surname || ""}`.trim().toLowerCase();
const username = (u.username || "").toLowerCase();
return fullName.includes(userInput.toLowerCase()) || username.includes(userInput.toLowerCase());
});
if (user) {
return user.id;
}
throw new Error(`User not found: ${userInput}`);
}
catch (error) {
throw new Error(`Error resolving user: ${error.message}`);
}
}
/**
* Resolves a project input (name or ID) to a project ID
*/
async function resolveProject(api, projectInput) {
// If projectInput is a number, assume it's an ID
if (!isNaN(parseInt(projectInput, 10))) {
return parseInt(projectInput, 10);
}
// Otherwise, search for project by name
try {
const response = await api.get("/api/projects");
const projects = response.data;
// Search for a project whose name contains the input (case-insensitive)
const project = projects.find(p => (p.name || "").toLowerCase().includes(projectInput.toLowerCase()));
if (project) {
return project.id;
}
throw new Error(`Project not found: ${projectInput}`);
}
catch (error) {
throw new Error(`Error resolving project: ${error.message}`);
}
}