summaryrefslogtreecommitdiff
path: root/Hutch/Models/TicketBulkAction.swift
blob: dcf767692af9cf8da77f9b687c5dc9cc1de1e927 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
import Foundation

enum TicketBulkActionKind: String, Sendable {
    case close
    case assign

    var displayName: String {
        switch self {
        case .close:
            "Close"
        case .assign:
            "Assign"
        }
    }

    var pastTenseDisplayName: String {
        switch self {
        case .close:
            "Closed"
        case .assign:
            "Assigned"
        }
    }
}

struct TicketBulkActionResult: Identifiable, Sendable {
    let id = UUID()
    let action: TicketBulkActionKind
    let totalCount: Int
    let updatedCount: Int
    let unchangedCount: Int
    let failures: [TicketBulkActionFailure]

    var failedCount: Int {
        failures.count
    }

    var title: String {
        if updatedCount == 0, failedCount > 0 {
            return "\(action.displayName) Failed"
        }
        if failedCount > 0 {
            return "\(action.displayName) Partially Applied"
        }
        return "\(action.displayName) Complete"
    }

    var message: String {
        var components: [String] = []

        if updatedCount > 0 {
            components.append("\(action.pastTenseDisplayName) \(updatedCount) \(ticketWord(for: updatedCount)).")
        }

        if unchangedCount > 0 {
            let unchangedDescription: String
            switch action {
            case .close:
                unchangedDescription = "\(unchangedCount) already closed."
            case .assign:
                unchangedDescription = "\(unchangedCount) already assigned."
            }
            components.append(unchangedDescription)
        }

        if failedCount > 0 {
            let ids = failures
                .map { "#\($0.ticketID)" }
                .joined(separator: ", ")
            components.append("Failed: \(ids).")
        }

        if components.isEmpty {
            components.append("No tickets were selected.")
        }

        return components.joined(separator: " ")
    }

    private func ticketWord(for count: Int) -> String {
        count == 1 ? "ticket" : "tickets"
    }
}

struct TicketBulkActionFailure: Sendable {
    let ticketID: Int
    let message: String
}