<automation name="Deactivate old users" version="11.0.0.0" description="" id="3b7178ca397e4b4eb8e4bc31665f732a" executeAsServiceUser="false" trigger="Job" schedule="1m" lastModified="2019-06-24 14:43:00Z" provider="31f486cc-03b5-42b6-b5df-81cdd976aa59">
	<executionScript language="JavaScript"><![CDATA[var unit = core_v2_widget.GetStringValue('ageUnit', '');
if(unit === '') {
    return;
}

var age = core_v2_widget.GetIntValue('age', 0);
if(age === 0) {
    return;
}

var currentDate = core_v2_utility.CurrentDate;
var cutoff = new Date(currentDate.getFullYear(), currentDate.getMonth(), currentDate.getDate(), 0, 0, 0);

if(unit === 'y')
{
    cutoff.setFullYear(cutoff.getFullYear() - age);
}
else if (unit === 'd')
{
    cutoff.setDate(cutoff.getDate() - age);
}
else return;

var pageIndex = 0;
var done = false;
while(!done) {
    var users = core_v2_user.List({
       SortBy: 'LastVisitedDate',
       SortOrder: 'Ascending',
       PageSize: 100,
       PageIndex: pageIndex
    });
    
    for(var i = 0; i < users.Count; i++) {  
        if(!users[i].IsSystemAccount && users[i].JoinDate !== null && users[i].JoinDate <= cutoff) {
            core_v2_user.Update({ AccountStatus: 'Disapproved', Id: users[i].Id });
        }
    }
    
    if(users.TotalCount <= 100 * (pageIndex + 1)) {
        done = true;
    }
    
    pageIndex++;
}]]></executionScript>
	<configuration><![CDATA[<propertyGroup id="options" labelText="Options">
    <property id="age" labelText="Age" descriptionText="Number of units to measure" dataType="Int" defaultValue="1" />
    <property id="ageUnit" labelText="Unit of Measure" descriptionText="Calculate in days or years" dataType="String" defaultValue="y">
        <propertyValue value="y" labelText="Years" />
        <propertyValue value="d" labelText="Days" />
    </property>
</propertyGroup>]]></configuration>
</automation>