lib/Classes/Public/TMTask.ps1


class TMTask {

    #region Non-Static Properties

    [System.Int64]$Id

    [System.Int64]$TaskNumber

    [System.String]$Title

    [ValidateSet('Hold', 'Planned', 'Ready', 'Pending', 'Started', 'Completed', 'Terminated')]
    [System.String]$Status

    [Nullable[System.DateTime]]$StatusUpdated

    [System.String]$StatusUpdatedElapsed

    [Nullable[System.DateTime]]$LastUpdated

    [System.String]$LastUpdatedElapsed

    [TMTaskAction]$Action

    [TMTaskAsset]$Asset

    [TMReference]$AssignedTo

    [TMReference]$CreatedBy

    [System.String]$Category
    
    [Nullable[System.DateTime]]$DateCreated
    
    [ValidateRange(0, 1)]
    [System.Int64]$HardAssigned
    
    [System.Int64]$EstDurationMinutes
    
    [String]$EstStart
    
    [String]$EstFinish
    
    [System.Object]$Slack
    
    [System.Boolean]$IsCriticalPath
    
    [Nullable[System.DateTime]]$ActStart
    
    [Nullable[System.DateTime]]$ActFinish
    
    [System.String]$Team
    
    [System.Boolean]$IsPublished
    
    [ValidateRange(0, 100)]
    [System.Int64]$PercentageComplete
    
    [TMReference]$Project
    
    [System.Boolean]$IsActionInvocableLocally
    
    [System.Boolean]$IsActionInvocableRemotely
    
    [System.Boolean]$IsAutomatic
    
    [System.Int64]$Duration
    
    [System.Boolean]$SendNotification
    
    [ValidateRange(1, 5)]
    [System.Int64]$Priority
    
    [TMReference]$Event
    
    [Nullable[System.DateTime]]$DueDate
    
    [System.String]$InstructionsLink
    
    [TMTaskDependency[]]$Predecessors
    
    [TMTaskDependency[]]$Successors

    #endregion Non-Static Properties

    #region Static Properties

    static [String[]]$ValidStatuses = @(
        'Hold', 
        'Planned', 
        'Ready', 
        'Pending', 
        'Started', 
        'Completed', 
        'Terminated'
    )

    static [String[]]$ValidCategories = @(
        'general',
        'discovery',
        'analysis',
        'design',
        'planning',
        'buildout',
        'walkthru',
        'premove',
        'moveday',
        'shutdown',
        'physical',
        'transport',
        'startup',
        'verify',
        'postmove',
        'closeout',
        'learning'
    )

    #endregion Static Properties

    #region Constructors

    TMTask() {}

    TMTask([Object]$_object) {
        $this.Id = $_object.id
        $this.TaskNumber = $_object.taskNumber
        $this.Title = $_object.title
        $this.Status = $_object.status
        $this.StatusUpdated = $_object.statusUpdated
        $this.StatusUpdatedElapsed = $_object.statusUpdatedElapsed
        $this.LastUpdated = $_object.lastUpdated
        $this.LastUpdatedElapsed = $_object.lastUpdatedElapsed
        $this.Action = [TMTaskAction]::new($_object.action)
        $this.Asset = [TMTaskAsset]::new($_object.asset)
        $this.AssignedTo = [TMReference]::new($_object.assignedTo)
        $this.CreatedBy = [TMReference]::new($_object.createdBy)
        $this.Category = $_object.category
        $this.DateCreated = $_object.dateCreated
        $this.HardAssigned = $_object.hardAssigned
        $this.EstDurationMinutes = $_object.estDurationMinutes
        $this.EstStart = $_object.estStart
        $this.EstFinish = $_object.estFinish
        $this.Slack = $_object.slack
        $this.IsCriticalPath = $_object.isCriticalPath
        $this.ActStart = $_object.actStart
        $this.ActFinish = $_object.actFinish
        $this.Team = $_object.team
        $this.IsPublished = $_object.isPublished
        $this.PercentageComplete = $_object.percentageComplete
        $this.Project = [TMReference]::new($_object.project)
        $this.IsActionInvocableLocally = $_object.isActionInvocableLocally
        $this.IsActionInvocableRemotely = $_object.isActionInvocableRemotely
        $this.IsAutomatic = $_object.isAutomatic
        $this.Duration = $_object.duration
        $this.SendNotification = $_object.sendNotification
        $this.Priority = $_object.priority
        $this.Event = [TMReference]::new($_object.event)
        $this.DueDate = $_object.dueDate
        $this.InstructionsLink = $_object.instructionsLink
        $this.Predecessors = $_object.predecessors | ForEach-Object { [TMTaskDependency]::new($_) }
        $this.Successors = $_object.successors | ForEach-Object { [TMTaskDependency]::new($_) }
    }

    #endregion Constructors

    #region Non-Static Methods

    hidden [PSCustomObject]GetWSUpdateObject() {
        $ReturnObject = [PSCustomObject]@{
            id = $this.Id
            comment = $this.Title
            status = $this.Status
            assignedTo = $this.AssignedTo.id
            apiAction = $this.Action.Id
            apiActionId = $this.Action.Id
            category = $this.Category
            assetEntity = $this.Asset.Id
            hardAssigned = $this.HardAssigned
            moveEvent = $this.Event.id
            priority = $this.Priority
            role = $this.Team
            percentageComplete = $this.PercentageComplete
            sendNotification = $this.SendNotification ? 1 : 0
            instructionsLink = $this.InstructionsLink
            duration = $this.Duration
            durationScale = "M"
            durationLocked = 0
            taskDependency = @()
            taskSuccessor = @()
        }

        foreach ($Predecessor in $this.Predecessors) {
            if (-not $Predecessor.Id -or $Predecessor.Id -eq 0) {
                $Predecessor.Id = -1
            }
            $ReturnObject.taskDependency += "$($Predecessor.Id)_$($Predecessor.TaskId)"
        }

        foreach ($Successor in $this.Successors) {
            if (-not $Successor.Id -or $Successor.Id -eq 0) {
                $Successor.Id = -1
            }
            $ReturnObject.taskSuccessor += "$($Successor.Id)_$($Successor.TaskId)"
        }

        return $ReturnObject
    }
    
    hidden [PSCustomObject]GetApiUpdateObject([String]$_note) {
        $ReturnObject =  [PSCustomObject]@{
            action = @{ id = $this.Action.Id }
            asset = @{ id = $this.Asset.Id }
            event = @{ id = $this.Event.id }
            assignedTo = $this.AssignedTo.id
            category = $this.Category
            title = $this.Title
            hardAssigned = $this.HardAssigned
            instructionsLink = $this.InstructionsLink
            percentageComplete = $this.PercentageComplete
            priority = $this.Priority
            role = $this.Team
            status = $this.Status
            currentStatus = $this.Status
            sendNotification = $this.SendNotification ? 1 : 0
            project = $this.Project.Id
            note = $_note
            predecessors = @()
            successors = @()
        }

        foreach ($Predecessor in $this.Predecessors) {
            if (-not $Predecessor.Id -or $Predecessor.Id -eq 0) {
                $Predecessor.Id = -1
            }
            $ReturnObject.predecessors += @{id = $Predecessor.Id; taskId = $Predecessor.TaskId}
        }
        
        foreach ($Successor in $this.Successors) {
            if (-not $Successor.Id -or $Successor.Id -eq 0) {
                $Successor.Id = -1
            }
            $ReturnObject.successors += @{id = $Successor.Id; taskId = $Successor.TaskId}
        }
        

        return $ReturnObject
    }

    #endregion Non-Static Methods
}


class TMTaskAction {

    #region Non-Static Properties

    [System.Int64]$Id
    [System.String]$Name
    [System.Boolean]$IsRemote
    [TMTaskActionActionType]$ActionType
    [System.String]$Description
    [Nullable[System.DateTime]]$InvokedAt
    [System.Object]$CompletedAt

    #endregion Non-Static Properties

    #region Constructors

    TMTaskAction() {}

    TMTaskAction([Object]$_object) {
        $this.Id = $_object.id
        $this.Name = $_object.name
        $this.IsRemote = $_object.isRemote
        $this.ActionType = [TMTaskActionActionType]::new($_object.actionType)
        $this.Description = $_object.description
        $this.InvokedAt = $_object.invokedAt
        $this.CompletedAt = $_object.completedAt
    }

    #endregion Constructors

}


class TMTaskActionActionType {

    #region Non-Static Properties

    [System.String]$Id
    [System.String]$Name

    #endregion Non-Static Properties

    #region Constructors

    TMTaskActionActionType() {}

    TMTaskActionActionType([System.String]$_id, [System.String]$_name) {
        $this.Id = $_id
        $this.Name = $_name
    }

    TMTaskActionActionType([Object]$_object) {
        $this.Id = $_object.id
        $this.Name = $_object.name
    }

    #endregion Constructors

}


class TMTaskAsset {

    #region Non-Static Properties

    [System.Int64]$Id
    [System.String]$Name
    [System.String]$Class
    [System.String]$Type
    [TMReference]$Bundle

    #endregion Non-Static Properties

    #region Constructors

    TMTaskAsset() {}

    TMTaskAsset([System.Int64]$_id, [System.String]$_name, [System.String]$_class, [System.String]$_type, [TMReference]$_bundle) {
        $this.Id = $_id
        $this.Name = $_name
        $this.Class = $_class
        $this.Type = $_type
        $this.Bundle = [TMReference]::new($_bundle)
    }

    TMTaskAsset([Object]$_object) {
        $this.Id = $_object.id
        $this.Name = $_object.name
        $this.Class = $_object.class
        $this.Type = $_object.type
        $this.Bundle = [TMReference]::new($_object.bundle)
    }

    #endregion Constructors

}


class TMTaskDependency {

    #region Non-Static Properties

    [System.Int64]$Id
    [System.Int64]$TaskId
    [System.Int64]$Number
    [System.String]$Title

    #endregion Non-Static Properties

    #region Constructors

    TMTaskDependency() {}

    TMTaskDependency([System.Int64]$_id, [System.Int64]$_taskId, [System.Int64]$_number, [System.String]$_title) {
        $this.Id = $_id
        $this.TaskId = $_taskId
        $this.Number = $_number
        $this.Title = $_title
    }

    TMTaskDependency([Object]$_object) {
        $this.Id = $_object.id
        $this.TaskId = $_object.taskId
        $this.Number = $_object.number
        $this.Title = $_object.title
    }

    #endregion Constructors

}