Skip to content

Commit

Permalink
Merge pull request #5 from ajilderda/fix/only-list-current-user-entries
Browse files Browse the repository at this point in the history
fix: Save user id + list current user entries only
  • Loading branch information
andrejilderda authored May 21, 2020
2 parents dfb8c8b + 043ca80 commit 698ff4f
Show file tree
Hide file tree
Showing 6 changed files with 20 additions and 32 deletions.
2 changes: 1 addition & 1 deletion info.plist
Original file line number Diff line number Diff line change
Expand Up @@ -1558,7 +1558,7 @@ https://github.com/ajilderda/alfred-harvest-v2</string>
<key>variablesdontexport</key>
<array/>
<key>version</key>
<string>1.1.0</string>
<string>1.1.1</string>
<key>webaddress</key>
<string>https://github.com/ajilderda/alfred-harvest-v2</string>
</dict>
Expand Down
17 changes: 4 additions & 13 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 2 additions & 1 deletion src/list-todays-timers.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,9 @@ import { getToday, roundTime } from './utils/time';
const alfy = require('alfy');

const today = getToday();
const userId = alfy.config.get('userId') || '';

const url = `https://api.harvestapp.com/v2/time_entries?from=${today}&to=${today}`
const url = `https://api.harvestapp.com/v2/time_entries?user_id=${userId}&from=${today}&to=${today}`;

const action = process.argv[3] || ''; // toggle|note
const actionPrefix = action === 'note' ? 'Add note: ' : '';
Expand Down
4 changes: 2 additions & 2 deletions src/start-project-task.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,9 @@ const vars = process.env;
const { projectId, projectName, taskId, taskName } = vars;

const today = getToday();
const url = `https://api.harvestapp.com/v2/time_entries?project_id=${projectId}&task_id=${taskId}&spent_date=${today}`;

const userId = alfy.config.get('userId') || '';

const url = `https://api.harvestapp.com/v2/time_entries?user_id=${userId}&project_id=${projectId}&task_id=${taskId}&spent_date=${today}`;

await apiCall(url, 'POST')
.then(response => {
Expand Down
16 changes: 6 additions & 10 deletions src/store-credentials.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
import { notify } from './utils/notifications';
const util = require('util');
const exec = util.promisify(require('child_process').exec);
import { apiCall, execute } from './utils/helpers';
const alfy = require('alfy');

const token = process.env.token;
Expand All @@ -9,18 +8,15 @@ const accountId = process.env.account_id;
// store the Harvest account id (6 digits)
alfy.config.set('accountId', accountId);

const execute = async (command) => {
const { stdout, stderr } = await exec(command);
return stdout;
};

const successMessage = 'All set!';

// delete previous API token the user might have saved before
execute(`security delete-generic-password -a 'apitoken' -s 'com.andrejilderda.harvest'`);

execute(`security add-generic-password -a 'apitoken' -s 'com.andrejilderda.harvest' -w '${token}'`)
.then(token => {
.then(async (token) => {
// store user id
await apiCall('https://api.harvestapp.com/v2/users/me', 'GET')
.then((user) => alfy.config.set('userId', user.id));

notify(
'All setup!',
'Type ‘hvn’ to list projects and start a new timer.'
Expand Down
10 changes: 5 additions & 5 deletions src/utils/helpers.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,11 @@ const util = require('util');
const exec = util.promisify(require('child_process').exec);
const alfy = require('alfy');

export const execute = async (command) => {
const { stdout } = await exec(command);
return stdout;
};

/**
* call the Harvest API and returns a promise with the result
*
Expand All @@ -15,11 +20,6 @@ export const apiCall = async (url, method) => {

if (!accountId) throw new Error('Missing account id. Have you setup correctly?');

const execute = async (command) => {
const { stdout, stderr } = await exec(command);
return stdout;
};

const fetchApi = async (token) => {
return alfy.fetch(url, {
method: method,
Expand Down

0 comments on commit 698ff4f

Please sign in to comment.