Skip to content

Commit 72ec4f1

Browse files
authored
Updates to OpCon_UpdateCalendar
Added logic to support new calendars that don't have dates or dates that already exist.
1 parent 6a6caff commit 72ec4f1

File tree

1 file changed

+85
-26
lines changed

1 file changed

+85
-26
lines changed

Module/OpConModule.psm1

Lines changed: 85 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -1082,8 +1082,6 @@ New-Alias "opc-getcalendar" OpCon_GetCalendar
10821082
#Updates a calendar
10831083
function OpCon_UpdateCalendar($url,$token,$name,$id,$date)
10841084
{
1085-
$hdr = @{"authorization" = $token}
1086-
10871085
if($name -or $id)
10881086
{
10891087
$counter = 0
@@ -1096,35 +1094,96 @@ function OpCon_UpdateCalendar($url,$token,$name,$id,$date)
10961094
$calendar | ForEach-Object{ $counter++ }
10971095

10981096
if($counter -ne 1)
1097+
{ Write-Host "More than 1 or no calendars returned!" }
1098+
else
10991099
{
1100-
Write-Host "More than 1 or no calendars returned!"
1101-
}
1102-
}
1103-
else
1104-
{
1105-
Write-Host "No name or id specified!"
1106-
}
1107-
1108-
if($date)
1109-
{
1110-
$uriput = $url + "/api/calendars/" + $calendar[0].id
1111-
$calendar[0].dates += "$date"
1112-
$body = $calendar[0] | ConvertTo-JSON -Depth 7
1100+
if($date)
1101+
{
1102+
if($calendar[0].dates)
1103+
{
1104+
if($date.IndexOf(";") -ge 0)
1105+
{
1106+
$date.Split(";") | ForEach-Object{
1107+
if($_ -notin $calendar[0].dates)
1108+
{
1109+
if($null -eq $dateList)
1110+
{ $dateList = $_ }
1111+
else
1112+
{ $dateList = $dateList + ";" + $_ }
1113+
}
1114+
}
1115+
}
1116+
else
1117+
{
1118+
if($date -notin $calendar[0].dates)
1119+
{ $dateList = $date }
1120+
}
1121+
1122+
if($null -ne $dateList )
1123+
{
1124+
$calendar[0].dates += $dateList
1125+
$body = $calendar[0]
1126+
1127+
try
1128+
{ $calendaradd = Invoke-RestMethod -Method PUT -Uri ($url + "/api/calendars/" + $calendar[0].id) -Body ($body | ConvertTo-JSON -Depth 7) -Headers @{"authorization" = $token} -ContentType "application/json" }
1129+
catch [Exception]
1130+
{
1131+
Write-Host $_
1132+
Write-Host $_.Exception.Message
1133+
}
1134+
1135+
return $calendaradd
1136+
}
1137+
else
1138+
{ Write-Host "Date/s $date already in calendar $name !" }
1139+
}
1140+
else
1141+
{
1142+
if(!$calendar[0].description)
1143+
{ $description = "" }
1144+
else
1145+
{ $description = $calendar[0].description }
1146+
1147+
if(!$calendar[0].schedule)
1148+
{
1149+
$body = @{
1150+
"id" = $calendar[0].id;
1151+
"type" = $calendar[0].type;
1152+
"name" = $calendar[0].Name;
1153+
"dates" = @( $date );
1154+
"description" = $description
1155+
}
1156+
}
1157+
else
1158+
{
1159+
$schedule = $calendar[0].schedule
1160+
$body = @{
1161+
"id" = $calendar[0].id;
1162+
"type" = $calendar[0].type;
1163+
"schedule" = $schedule;
1164+
"name" = $calendar[0].Name;
1165+
"dates" = @( $date );
1166+
"description" = $description
1167+
}
1168+
}
11131169

1114-
try
1115-
{
1116-
$calendaradd = (Invoke-RestMethod -Method PUT -Uri $uriput -Body $body -Headers $hdr -ContentType "application/json")
1117-
}
1118-
catch [Exception]
1119-
{
1120-
Write-Host $_
1121-
Write-Host $_.Exception.Message
1170+
try
1171+
{ $calendaradd = Invoke-RestMethod -Method PUT -Uri ($url + "/api/calendars/" + $calendar[0].id) -Body ($body | ConvertTo-JSON -Depth 7) -Headers @{"authorization" = $token} -ContentType "application/json" }
1172+
catch [Exception]
1173+
{
1174+
Write-Host $_
1175+
Write-Host $_.Exception.Message
1176+
}
1177+
1178+
return $calendaradd
1179+
}
1180+
}
1181+
else
1182+
{ Write-Host "No date specified!" }
11221183
}
1123-
1124-
return $calendaradd
11251184
}
11261185
else
1127-
{ Write-Host "No date specified!" }
1186+
{ Write-Host "No name or id specified!" }
11281187
}
11291188
New-Alias "opc-updatecalendar" OpCon_UpdateCalendar
11301189

0 commit comments

Comments
 (0)