Skip to content

Commit 0b66bdd

Browse files
committed
Time entries
1 parent 6078c99 commit 0b66bdd

File tree

9 files changed

+583
-1
lines changed

9 files changed

+583
-1
lines changed

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
Tests/config.json
Lines changed: 97 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,97 @@
1+
Import-Module -Name Pester -Force
2+
Import-Module .\Toggl.API\Toggl.API.psm1 -Force
3+
4+
$configPath = Join-Path -Path $PSScriptRoot -ChildPath "..\config.json"
5+
$config = Get-Content -Path $configPath | ConvertFrom-Json
6+
7+
$apiToken = $config.apiToken
8+
$workspaceId = $config.workspaceId
9+
10+
Describe 'TimeEntries Integration Tests' {
11+
$timeEntryId = $null
12+
13+
Context "New-TogglTimeEntry" {
14+
It "should create a new time entry" {
15+
$start = [datetime]::UtcNow
16+
$description = "Test Time Entry"
17+
$createdWith = "PesterTest"
18+
19+
$response = New-TogglTimeEntry `
20+
-ApiToken $apiToken `
21+
-WorkspaceId $workspaceId `
22+
-CreatedWith $createdWith `
23+
-Start $start `
24+
-Description $description
25+
26+
$response | Should Not BeNullOrEmpty
27+
$response.description | Should -BeExactly $description
28+
$Script:timeEntryId = $response.id
29+
}
30+
}
31+
32+
Context "Get-TogglTimeEntryById" {
33+
It "should retrieve the created time entry by ID" -Skip:($Script:timeEntryId -eq $null) {
34+
$response = Get-TogglTimeEntryById `
35+
-ApiToken $apiToken `
36+
-TimeEntryId $Script:timeEntryId
37+
38+
$response | Should Not BeNullOrEmpty
39+
$response.id | Should -BeExactly $Script:timeEntryId
40+
}
41+
42+
It "should return null for a non-existing time entry ID" {
43+
$nonExistingTimeEntryId = 999999999
44+
45+
$response = Get-TogglTimeEntryById `
46+
-ApiToken $apiToken `
47+
-TimeEntryId $nonExistingTimeEntryId
48+
49+
$response | Should BeNullOrEmpty
50+
}
51+
}
52+
53+
Context "Update-TogglTimeEntry" {
54+
It "should update the time entry" -Skip:($Script:timeEntryId -eq $null) {
55+
$newDescription = "Updated Test Time Entry"
56+
57+
$response = Update-TogglTimeEntry `
58+
-ApiToken $apiToken `
59+
-WorkspaceId $workspaceId `
60+
-TimeEntryId $Script:timeEntryId `
61+
-Description $newDescription
62+
63+
$response | Should Not BeNullOrEmpty
64+
$response.description | Should -BeExactly $newDescription
65+
}
66+
}
67+
68+
Context "Get-TogglTimeEntries" {
69+
It "should retrieve the list of time entries" {
70+
$response = Get-TogglTimeEntries `
71+
-ApiToken $apiToken `
72+
-Since ([int][double]::Parse((Get-Date).AddDays(-7).ToUniversalTime().Subtract([datetime]'1970-01-01').TotalSeconds))
73+
74+
$response | Should Not BeNullOrEmpty
75+
$response.GetType().Name | Should -Be "Object[]"
76+
77+
$filteredResponse = $response | Where-Object { $_.server_deleted_at -ne $null }
78+
$filteredResponse | Should Not BeNullOrEmpty
79+
$filteredResponse.Count | Should -BeGreaterThan 0
80+
}
81+
}
82+
83+
Context "Remove-TogglTimeEntry" {
84+
It "should delete the time entry" -Skip:($Script:timeEntryId -eq $null) {
85+
Remove-TogglTimeEntry `
86+
-ApiToken $apiToken `
87+
-WorkspaceId $workspaceId `
88+
-TimeEntryId $Script:timeEntryId
89+
90+
$response = Get-TogglTimeEntryById `
91+
-ApiToken $apiToken `
92+
-TimeEntryId $Script:timeEntryId
93+
94+
$response | Should BeNullOrEmpty
95+
}
96+
}
97+
}

Tests/Toggl.API.Tests.ps1

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,3 @@
1-
Clear-Host
21
Import-Module -Name Pester -Force
32
Import-Module .\Toggl.API\Toggl.API.psm1 -Force
43

Tests/config.example.json

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
{
2+
"apiToken": "your_api_token_here",
3+
"workspaceId": 123456
4+
}
Lines changed: 75 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,75 @@
1+
<#
2+
.SYNOPSIS
3+
Lists latest time entries from Toggl.
4+
5+
.DESCRIPTION
6+
This cmdlet retrieves the latest time entries from Toggl using the provided parameters. It sends a GET request to the Toggl API.
7+
8+
.PARAMETER ApiToken
9+
The API token for authentication. (Mandatory)
10+
11+
.PARAMETER Meta
12+
Should the response contain data for meta entities. (Optional)
13+
14+
.PARAMETER IncludeSharing
15+
Include sharing details in the response. (Optional)
16+
17+
.PARAMETER Since
18+
Get entries modified since this date using UNIX timestamp, including deleted ones. (Optional)
19+
20+
.PARAMETER Before
21+
Get entries with start time, before given date (YYYY-MM-DD) or with time in RFC3339 format. (Optional)
22+
23+
.PARAMETER StartDate
24+
Get entries with start time, from start_date YYYY-MM-DD or with time in RFC3339 format. To be used with end_date. (Optional)
25+
26+
.PARAMETER EndDate
27+
Get entries with start time, until end_date YYYY-MM-DD or with time in RFC3339 format. To be used with start_date. (Optional)
28+
29+
.EXAMPLE
30+
Get-TogglTimeEntries -ApiToken "your_api_token" -Since 1609459200 -Before "2023-12-31" -StartDate "2023-01-01" -EndDate "2023-12-31"
31+
#>
32+
function Get-TogglTimeEntries {
33+
[CmdletBinding()]
34+
param (
35+
[Parameter(Mandatory = $true)]
36+
[string]$ApiToken,
37+
38+
[bool]$Meta = $false,
39+
40+
[bool]$IncludeSharing = $false,
41+
42+
[int]$Since,
43+
44+
[string]$Before,
45+
46+
[string]$StartDate,
47+
48+
[string]$EndDate
49+
)
50+
51+
$params = @{
52+
meta = $Meta
53+
include_sharing = $IncludeSharing
54+
}
55+
56+
if ($PSBoundParameters.ContainsKey('Since')) { $params['since'] = $Since }
57+
if ($PSBoundParameters.ContainsKey('Before')) { $params['before'] = $Before }
58+
if ($PSBoundParameters.ContainsKey('StartDate')) { $params['start_date'] = $StartDate }
59+
if ($PSBoundParameters.ContainsKey('EndDate')) { $params['end_date'] = $EndDate }
60+
61+
$queryString = ($params.GetEnumerator() | ForEach-Object { "$($_.Key)=$($_.Value)" }) -join '&'
62+
$url = "$Global:TogglBaseUrl/me/time_entries?$queryString"
63+
64+
$headers = Get-TogglAuthHeader -ApiToken $ApiToken
65+
66+
try {
67+
$response = Invoke-RestMethod -Uri $url -Headers $headers -Method Get
68+
return $response
69+
}
70+
catch {
71+
Write-Error "Failed to retrieve time entries: $_"
72+
}
73+
}
74+
75+
Export-ModuleMember -Function Get-TogglTimeEntries
Lines changed: 62 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,62 @@
1+
<#
2+
.SYNOPSIS
3+
Retrieves a time entry by ID from Toggl.
4+
5+
.DESCRIPTION
6+
This cmdlet retrieves a time entry by ID from Toggl using the provided parameters. It sends a GET request to the Toggl API.
7+
8+
.PARAMETER ApiToken
9+
The API token for authentication. (Mandatory)
10+
11+
.PARAMETER TimeEntryId
12+
The ID of the time entry to retrieve. (Mandatory)
13+
14+
.PARAMETER Meta
15+
Should the response contain data for meta entities. (Optional)
16+
17+
.PARAMETER IncludeSharing
18+
Include sharing details in the response. (Optional)
19+
20+
.EXAMPLE
21+
Get-TogglTimeEntryById -ApiToken "your_api_token" -TimeEntryId 123456
22+
23+
#>
24+
function Get-TogglTimeEntryById {
25+
[CmdletBinding()]
26+
param (
27+
[Parameter(Mandatory = $true)]
28+
[string]$ApiToken,
29+
30+
[Parameter(Mandatory = $true)]
31+
[Int64]$TimeEntryId,
32+
33+
[bool]$Meta = $false,
34+
35+
[bool]$IncludeSharing = $false
36+
)
37+
38+
$params = @{
39+
meta = $Meta
40+
include_sharing = $IncludeSharing
41+
}
42+
43+
$queryString = ($params.GetEnumerator() | ForEach-Object { "$($_.Key)=$($_.Value)" }) -join '&'
44+
$url = "$Global:TogglBaseUrl/me/time_entries/$TimeEntryId`?$queryString"
45+
46+
$headers = Get-TogglAuthHeader -ApiToken $ApiToken
47+
48+
try {
49+
$response = Invoke-RestMethod -Uri $url -Headers $headers -Method Get
50+
return $response
51+
}
52+
catch {
53+
if ($_.Exception.Response.StatusCode -eq 404) {
54+
return $null
55+
}
56+
else {
57+
Write-Error "Failed to retrieve time entry: $_"
58+
}
59+
}
60+
}
61+
62+
Export-ModuleMember -Function Get-TogglTimeEntryById

0 commit comments

Comments
 (0)