# Site 6.2.x updates

function Execute6.2SiteUpdates
{
	SiteUpdate_6.2_01 6200
	SiteUpdate_6.2_02 6200
	SiteUpdate_6.2_03 6200
	#SiteUpdate_6.2_04 6210 Skip because of SiteUpdate_6.3_01
	SiteUpdate_6.2_05 6200
	SiteUpdate_6.2_06 6200
	SiteUpdate_6.2_07 6210
	SetTemplateVersion "6.2.1.0" -IgnoreWhenHigherVersion
}

# Add list: EncryptedTokens
# Add content type to 'EncryptedTokens' list: EncryptedToken
# Add fields to 'EncryptedToken' content type: wsp_ucc_et_key, wsp_ucc_et_value
# Hide 'Title' field from 'EncryptedTokens' list
function SiteUpdate_6.2_01($patchVersion)
{
	if ($patchCheck -ge $patchVersion) 
	{
		return
	}

	# EncryptedTokens
	$listTitle = "EncryptedTokens"
	$ctName = "EncryptedToken"
	$added = AddPnPList $listTitle -RemoveDefaultView
	if ($added)
	{
		AddPnPFieldFromXml '<Field ID="{310bbdf3-b36c-4a2a-bdd9-146bfd23a44f}" Type="Text" Name="wsp_ucc_et_key" DisplayName="Key" Required="TRUE" Description="The key to substitute with the decrypted value" Group="AnywhereNow" />' $null "wsp_ucc_et_value"
		AddPnPFieldFromXml '<Field ID="{76a7781f-e9aa-42a4-bc7e-170a2f21f351}" Type="Text" Name="wsp_ucc_et_value" DisplayName="Value" Required="TRUE" Description="The encrypted value as generated by the Anywhere365 Encryption Tool" Group="AnywhereNow" />' $null "wsp_ucc_et_value"
		AddPnPContentType $ctName "0x0100217d299c0b291042a79ee1dc54bf9938" "Add a EncryptedToken item to the EncryptedTokens list"
		AddPnPFieldsToContentType $ctName @("Title", "!wsp_ucc_et_key", "!wsp_ucc_et_value")
		AddPnPContentTypesToList $listTitle @($ctName)
		AddPnPView $listTitle "AllItems" "AllItems" 30 '<OrderBy><FieldRef Name="ID" Ascending="True" /></OrderBy>' @("LinkTitle", "wsp_ucc_et_key", "wsp_ucc_et_value") -Default -RemoveIfExists
		AddForms $updateSite_RelativeURL $listTitle
	}
}

# List: Skills
# Add 'wsp_ucc_sla_goal_in_seconds' field
# Add 'wsp_ucc_sla_goal_in_seconds' field to 'Skill', 'CountDownSkill', 'AvailabilitySkill', 'ChatSkill', 'ForwardSkill', 'ChatForwardSkill' and 'CountdownAvailabilitySkill' contenttypes
# Update 'NormalSkillView', 'CountDownSkillView', 'AvailabilitySkillView', 'CountdownAvailabilitySkillView', 'ForwardSkillView', 'ChatSkillView' and 'ChatForwardSkillView' views
function SiteUpdate_6.2_02($patchVersion)
{
	if ($patchCheck -ge $patchVersion) 
	{
		return
	}

	$listTitle = "Skills"
	AddPnPFieldFromXml '<Field ID="{a2730c27-02c8-43ff-bc6b-2166f45ef372}" Type="Number" Name="wsp_ucc_sla_goal_in_seconds" DisplayName="SLA Goal" Required="FALSE" Group="AnywhereNow" />' $null "wsp_ucc_sla_goal_in_seconds"
	AddPnPFieldsToContentType "Skill" @("wsp_ucc_sla_goal_in_seconds") @("Title", "wsp_ucc_ShowOnWallboard", "wsp_ucc_sla_goal_in_seconds")
	AddPnPFieldsToContentType "CountDownSkill" @("wsp_ucc_sla_goal_in_seconds") @("Title", "wsp_ucc_ShowOnWallboard", "wsp_ucc_sla_goal_in_seconds", "StartCountDownSeconds", "EndCountDownSeconds", "wsp_ucc_skills_escapeskill")
	AddPnPFieldsToContentType "AvailabilitySkill" @("wsp_ucc_sla_goal_in_seconds") @("Title", "wsp_ucc_ShowOnWallboard", "wsp_ucc_sla_goal_in_seconds", "Availability", "wsp_ucc_skills_escapeskill")
	AddPnPFieldsToContentType "ChatSkill" @("wsp_ucc_sla_goal_in_seconds") @("Title", "wsp_ucc_ShowOnWallboard", "wsp_ucc_sla_goal_in_seconds")
	AddPnPFieldsToContentType "ForwardSkill" @("wsp_ucc_sla_goal_in_seconds") @("Title", "wsp_ucc_ForwardToSip", "ForwardWithDTMF", "wsp_ucc_sla_goal_in_seconds")
	AddPnPFieldsToContentType "ChatForwardSkill" @("wsp_ucc_sla_goal_in_seconds") @("Title", "wsp_ucc_ForwardToSip", "wsp_ucc_ShowOnWallboard", "wsp_ucc_sla_goal_in_seconds")
	AddPnPFieldsToContentType "CountdownAvailabilitySkill" @("wsp_ucc_sla_goal_in_seconds") @("Title", "wsp_ucc_ShowOnWallboard", "wsp_ucc_sla_goal_in_seconds", "Availability", "StartCountDownSeconds", "EndCountDownSeconds", "wsp_ucc_skills_escapeskill")	
	AddPnPView $listTitle "NormalSkillView" "NormalSkillView" 30 '<Where><Eq><FieldRef Name="ContentType" /><Value Type="Text">Skill</Value></Eq></Where><OrderBy><FieldRef Name="ID" /></OrderBy>' @("LinkTitle", "wsp_ucc_ShowOnWallboard", "wsp_ucc_sla_goal_in_seconds") -RemoveIfExists
	AddPnPView $listTitle "CountDownSkillView" "CountDownSkillView" 30 '<Where><Eq><FieldRef Name="ContentType" /><Value Type="Text">CountDownSkill</Value></Eq></Where><OrderBy><FieldRef Name="ID" /></OrderBy>' @("LinkTitle", "wsp_ucc_ShowOnWallboard", "wsp_ucc_sla_goal_in_seconds", "StartCountDownSeconds", "EndCountDownSeconds", "wsp_ucc_skills_escapeskill") -RemoveIfExists
	AddPnPView $listTitle "AvailabilitySkillView" "AvailabilitySkillView" 30 '<Where><Eq><FieldRef Name="ContentType" /><Value Type="Text">AvailabilitySkill</Value></Eq></Where><OrderBy><FieldRef Name="ID" /></OrderBy>' @("LinkTitle", "wsp_ucc_ShowOnWallboard", "wsp_ucc_sla_goal_in_seconds", "Availability", "wsp_ucc_skills_escapeskill") -RemoveIfExists
	AddPnPView $listTitle "CountdownAvailabilitySkillView" "CountdownAvailabilitySkillView" 30 '<Where><Eq><FieldRef Name="ContentType" /><Value Type="Text">CountdownAvailabilitySkill</Value></Eq></Where><OrderBy><FieldRef Name="ID" /></OrderBy>' @("LinkTitle", "wsp_ucc_ShowOnWallboard", "wsp_ucc_sla_goal_in_seconds", "Availability", "StartCountDownSeconds", "EndCountDownSeconds", "wsp_ucc_skills_escapeskill") -RemoveIfExists
	AddPnPView $listTitle "ForwardSkillView" "ForwardSkillView" 30 '<Where><Eq><FieldRef Name="ContentType" /><Value Type="Text">ForwardSkill</Value></Eq></Where><OrderBy><FieldRef Name="ID" /></OrderBy>' @("LinkTitle", "wsp_ucc_ForwardToSip", "ForwardWithDTMF", "wsp_ucc_sla_goal_in_seconds") -RemoveIfExists
	AddPnPView $listTitle "ChatSkillView" "ChatSkillView" 30 '<Where><Eq><FieldRef Name="ContentType" /><Value Type="Text">ChatSkill</Value></Eq></Where><OrderBy><FieldRef Name="ID" /></OrderBy>' @("LinkTitle", "wsp_ucc_ShowOnWallboard", "wsp_ucc_sla_goal_in_seconds") -RemoveIfExists
	AddPnPView $listTitle "ChatForwardSkillView" "ChatForwardSkillView" 30 '<Where><Eq><FieldRef Name="ContentType" /><Value Type="Text">ChatForwardSkill</Value></Eq></Where><OrderBy><FieldRef Name="ID" /></OrderBy>' @("LinkTitle", "wsp_ucc_ForwardToSip", "wsp_ucc_ShowOnWallboard", "wsp_ucc_sla_goal_in_seconds") -RemoveIfExists			
}

# List: Routing
# Add 'wsp_ucc_routing_referredby_calle' field
# Add 'wsp_ucc_routing_referredby_calle' field to 'InboundRouting' contenttype
# Update 'InboundRoutingView' view
function SiteUpdate_6.2_03($patchVersion)
{
	if ($patchCheck -ge $patchVersion) 
	{
		return
	}

	AddPnPFieldFromXml '<Field ID="{ca2e4f90-e8bb-4a9a-9f8b-3edc7aa3d931}" Type="Boolean" Name="wsp_ucc_routing_referredby_calle" DisplayName="ReferredBy Callee" Required="FALSE" Group="AnywhereNow"><Default>0</Default></Field>' $null "wsp_ucc_routing_referredby_calle"
	AddPnPFieldsToContentType "InboundRouting" @("wsp_ucc_routing_referredby_calle") @("Title", "wsp_ucc_routing_match_from", "wsp_ucc_routing_match_to", "wsp_ucc_routing_action", "wsp_ucc_routing_skill", "wsp_ucc_routing_parentquestion", "wsp_ucc_routing_name", "wsp_ucc_routing_order", "wsp_ucc_routing_referredby_calle", "wsp_ucc_routing_active")
	AddPnPView "Routing" "InboundRoutingView" "InboundRoutingView" 100 '<Where><Eq><FieldRef Name="ContentType" /><Value Type="Text">InboundRouting</Value></Eq></Where><OrderBy><FieldRef Name="ID" /></OrderBy>' @("LinkTitle", "wsp_ucc_routing_match_from", "wsp_ucc_routing_match_to", "wsp_ucc_routing_action", "wsp_ucc_routing_order", "wsp_ucc_routing_referredby_calle", "wsp_ucc_routing_active") -RemoveIfExists
}

# List: SettingValues
# Remove items (if any)
# Import new data
function SiteUpdate_6.2_04($patchVersion)
{
	if ($patchCheck -ge $patchVersion -or $newSite -eq $true) 
	{
		return
	}

	UpdateSettingValues
}

# Add list: CannedResponses
# Add content type to 'CannedResponses' list: CannedResponse
# Add fields to 'CannedResponse' content type: wsp_ucc_cr_text, wsp_ucc_cr_skill
function SiteUpdate_6.2_05($patchVersion)
{
	if ($patchCheck -ge $patchVersion) 
	{
		return
	}

	# CannedResponses
	$listTitle = "CannedResponses"
	$ctName = "CannedResponse"
	$added = AddPnPList $listTitle -EnableAttachments -RemoveDefaultView
	if ($added)
	{
		AddPnPFieldFromXml '<Field ID="{7728e9a2-a7b9-4662-8eab-63a646dc49f7}" Type="Note" Name="wsp_ucc_cr_text" DisplayName="Response" Required="TRUE" RichText="TRUE" RichTextMode="FullHtml" NumLines="6"  Group="AnywhereNow" />' $null "wsp_ucc_cr_text"
		AddPnPFieldFromXml '<Field ID="{f58eabd8-b0df-4743-af13-9fef4f170a27}" Type="LookupMulti" Name="wsp_ucc_cr_skill" DisplayName="Skill" Required="TRUE" EnforceUniqueValues="FALSE" List="{$listId}" ShowField="Title" Mult="TRUE" Group="AnywhereNow" />' "Skills" "wsp_ucc_cr_skill"
		AddPnPContentType $ctName "0x01000e1f6a943b6a6e41a71001fa24df8372" "Add a CannedResponse item to the CannedResponses list"
		AddPnPFieldsToContentType $ctName @("!wsp_ucc_cr_text", "!wsp_ucc_cr_skill")
		AddPnPContentTypesToList $listTitle @($ctName)
		AddPnPView $listTitle "AllItems" "AllItems" 30 '<OrderBy><FieldRef Name="ID" Ascending="True" /></OrderBy>' @("LinkTitle", "wsp_ucc_cr_text", "wsp_ucc_cr_skill") -Default -RemoveIfExists
		AddForms $updateSite_RelativeURL $listTitle
	}
}

# Change title and description of field 'wsp_ucc_PointsPerSession'
# Add 'wsp_ucc_PointsPerSession' and 'wsp_ucc_sla_goal_in_seconds' to 'Modality' content type
# Update default view of 'Modalities' list
# Remove 'ReservationPointSystemRules' list when there are no items
function SiteUpdate_6.2_06($patchVersion)
{
	if ($patchCheck -ge $patchVersion) 
	{
		return
	}

	# wsp_ucc_PointsPerSession
	$field = GetPnPField "wsp_ucc_PointsPerSession"
	if ($field -ne $null)
	{
		$field.Title = "ReservationPointSystemPoints"
		$field.Description = "The number of points a Session of the given Modality type should count as in the ReservationPointSystem. Only applicable if Settings.ReservationSystem = PointSystem."
		$field.Required = $false
		$field.Update()
		Invoke-PnPQuery
	}

	# Modalities
	$listTitle = "Modalities"
	$ctName = "Modality"
	AddPnPFieldsToContentType $ctName @("wsp_ucc_PointsPerSession", "wsp_ucc_sla_goal_in_seconds")
	AddPnPView $listTitle "AllItems" "AllItems" 30 '<OrderBy><FieldRef Name="ID" Ascending="True" /></OrderBy>' @("LinkTitle", "wsp_ucc_PointsPerSession", "wsp_ucc_sla_goal_in_seconds") -Default -RemoveIfExists

	# ReservationPointSystemRules
	$listTitle = "ReservationPointSystemRules"
	$checkList = GetPnPList $listTitle
	if ($checkList -ne $null)
	{
		$listItems = GetPnPListItem -List $listTitle
		if ($listItems.Count -eq 0) 
		{
			RemovePnPList $listTitle
		}
	}
}

# Update 'AudioRecordingFormat' setting value
function SiteUpdate_6.2_07($patchVersion)
{
	if ($patchCheck -ge $patchVersion) 
	{
		return
	}

	$listTitle = "Settings"
	$updateItem = Get-PnPListItem -List $listTitle -Query '<View><Query><Where><Eq><FieldRef Name="wsp_ucc_Key" /><Value Type="Text">AudioRecordingFormat</Value></Eq></Where></Query></View>'
	if ($updateItem -ne $null) 
	{
		$checkValue = $updateItem["wsp_ucc_Value"]
		if ($checkValue -eq "Wma48Kbs")
		{
			SetPnPListItem $listTitle $updateItem @{"wsp_ucc_Value" = "Wma48Kbps"}
		}
	}
}