-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #4 from tewen/time-entry-utilities
Time entry utilities
- Loading branch information
Showing
13 changed files
with
148 additions
and
14 deletions.
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,6 +1,6 @@ | ||
{ | ||
"name": "xero-hero", | ||
"version": "0.3.0", | ||
"version": "0.4.0", | ||
"description": "", | ||
"files": [ | ||
"dist", | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,8 +1,11 @@ | ||
import { Contact } from 'xero-node'; | ||
import type { Contact } from 'xero-node'; | ||
|
||
import { hasProperty } from '../../utils/properties'; | ||
|
||
export const getContactLink = (contact: Contact | string): string => { | ||
return `https://go.xero.com/app/!kR4N6/contacts/contact/${ | ||
(contact instanceof Contact ? contact.contactID : contact) || | ||
'null-or-empty-contact-id' | ||
(hasProperty(contact, 'contactID') | ||
? (contact as Contact).contactID | ||
: contact) || 'null-or-empty-contact-id' | ||
}`; | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,8 +1,11 @@ | ||
import { Invoice } from 'xero-node'; | ||
import type { Invoice } from 'xero-node'; | ||
|
||
import { hasProperty } from '../../utils/properties'; | ||
|
||
export const getInvoiceLink = (invoice: Invoice | string): string => { | ||
return `https://invoicing.xero.com/view/${ | ||
(invoice instanceof Invoice ? invoice.invoiceID : invoice) || | ||
'null-or-empty-invoice-id' | ||
(hasProperty(invoice, 'invoiceID') | ||
? (invoice as Invoice).invoiceID | ||
: invoice) || 'null-or-empty-invoice-id' | ||
}`; | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,13 +1,15 @@ | ||
import qs from 'qs'; | ||
import { ManualJournal } from 'xero-node'; | ||
import type { ManualJournal } from 'xero-node'; | ||
|
||
import { hasProperty } from '../../utils/properties'; | ||
|
||
export const getManualJournalLink = ( | ||
manualJournal: ManualJournal | string, | ||
): string => { | ||
return `https://go.xero.com/Journal/View.aspx?${qs.stringify({ | ||
invoiceID: | ||
(manualJournal instanceof ManualJournal | ||
? manualJournal.manualJournalID | ||
(hasProperty(manualJournal, 'manualJournalID') | ||
? (manualJournal as ManualJournal).manualJournalID | ||
: manualJournal) || 'null-or-empty-manual-journal-id', | ||
})}`; | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,76 @@ | ||
import { TimeEntry } from 'xero-node/dist/gen/model/projects/timeEntry'; | ||
|
||
import { hoursFromTimeEntries } from '../../'; | ||
|
||
describe('projects/timeEntries', () => { | ||
describe('hoursFromTimeEntries()', () => { | ||
it('should throw an error if passed undefined', () => { | ||
expect(() => { | ||
// @ts-expect-error - This is an invalid type for the function. | ||
return hoursFromTimeEntries(); | ||
}).toThrowError(); | ||
}); | ||
|
||
it('should throw an error if passed null', () => { | ||
expect(() => { | ||
// @ts-expect-error - This is an invalid type for the function. | ||
return hoursFromTimeEntries(null); | ||
}).toThrowError(); | ||
}); | ||
|
||
it('should return 0 if passed an empty array', () => { | ||
expect(hoursFromTimeEntries([])).toBe(0); | ||
}); | ||
|
||
it('should return the total hours from the given TimeEntries, defaulting to the nearest 15 minutes', () => { | ||
const timeEntries = [ | ||
{ | ||
duration: 60, | ||
}, | ||
{ | ||
duration: 30, | ||
}, | ||
{ | ||
duration: 18, | ||
}, | ||
]; | ||
expect(hoursFromTimeEntries(timeEntries)).toBe(1.75); | ||
}); | ||
|
||
it('should play nice with a non-default denominator for rounding', () => { | ||
/* eslint-disable functional/immutable-data */ | ||
const timeEntryA = new TimeEntry(); | ||
timeEntryA.duration = 60; | ||
const timeEntryB = new TimeEntry(); | ||
timeEntryB.duration = 23; | ||
/* eslint-enable functional/immutable-data */ | ||
const timeEntries = [timeEntryA, timeEntryB]; | ||
expect(hoursFromTimeEntries(timeEntries, 10)).toBe(1.4); | ||
}); | ||
|
||
it('should play nice with non-standard decimal places for rounding / fixed numbers', () => { | ||
const timeEntries = [ | ||
{ | ||
duration: 60, | ||
}, | ||
{ | ||
duration: 30, | ||
}, | ||
{ | ||
duration: 18, | ||
}, | ||
]; | ||
expect(hoursFromTimeEntries(timeEntries, 2, 0)).toBe(2); | ||
}); | ||
|
||
it('should sub in 0 when a Time Entry is missing a duration', () => { | ||
/* eslint-disable functional/immutable-data */ | ||
const timeEntryA = new TimeEntry(); | ||
const timeEntryB = new TimeEntry(); | ||
timeEntryB.duration = 23; | ||
/* eslint-enable functional/immutable-data */ | ||
const timeEntries = [timeEntryA, timeEntryB]; | ||
expect(hoursFromTimeEntries(timeEntries, 10)).toBe(0.4); | ||
}); | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1 +1,2 @@ | ||
export { generateProjectAmountUSD } from './generators'; | ||
export { hoursFromTimeEntries } from './timeEntries'; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
import { roundToNearestFraction } from 'deep-cuts'; | ||
import type { TimeEntry } from 'xero-node/dist/gen/model/projects/timeEntry'; | ||
|
||
export const hoursFromTimeEntries = ( | ||
timeEntries: TimeEntry[], | ||
denominator: number = 4, | ||
maxDecimalPlaces: number = 2, | ||
): number | undefined => { | ||
const totalMinutes = timeEntries.reduce((totalMinutes, timeEntry) => { | ||
const duration = timeEntry.duration || 0; | ||
return totalMinutes + duration; | ||
}, 0); | ||
return roundToNearestFraction( | ||
totalMinutes / 60, | ||
denominator, | ||
maxDecimalPlaces, | ||
); | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
export const hasProperty = (object: any, property: string): boolean => { | ||
if (Boolean(object) && typeof object === 'object') { | ||
return property in object; | ||
} | ||
|
||
return false; | ||
}; |