|
| 1 | +// periphery:ignore:all |
| 2 | +import Codegen |
| 3 | +import Foundation |
| 4 | + |
| 5 | +/// Represents a Booking Entity. |
| 6 | +/// |
| 7 | +public struct Booking: Codable, GeneratedCopiable, Equatable, GeneratedFakeable { |
| 8 | + public let siteID: Int64 |
| 9 | + public let bookingID: Int64 |
| 10 | + public let allDay: Bool |
| 11 | + public let cost: String |
| 12 | + public let customerID: Int64 |
| 13 | + public let dateCreated: Date |
| 14 | + public let dateModified: Date |
| 15 | + public let endDate: Date |
| 16 | + public let googleCalendarEventID: String? |
| 17 | + public let orderID: Int64 |
| 18 | + public let orderItemID: Int64 |
| 19 | + public let parentID: Int64 |
| 20 | + public let productID: Int64 |
| 21 | + public let resourceID: Int64 |
| 22 | + public let startDate: Date |
| 23 | + public let statusKey: String |
| 24 | + public let localTimezone: String |
| 25 | + |
| 26 | + /// Computed Properties |
| 27 | + /// |
| 28 | + public var bookingStatus: BookingStatus { |
| 29 | + return BookingStatus(rawValue: statusKey) ?? .unknown |
| 30 | + } |
| 31 | + |
| 32 | + /// Booking struct initializer. |
| 33 | + /// |
| 34 | + public init(siteID: Int64, |
| 35 | + bookingID: Int64, |
| 36 | + allDay: Bool, |
| 37 | + cost: String, |
| 38 | + customerID: Int64, |
| 39 | + dateCreated: Date, |
| 40 | + dateModified: Date, |
| 41 | + endDate: Date, |
| 42 | + googleCalendarEventID: String?, |
| 43 | + orderID: Int64, |
| 44 | + orderItemID: Int64, |
| 45 | + parentID: Int64, |
| 46 | + productID: Int64, |
| 47 | + resourceID: Int64, |
| 48 | + startDate: Date, |
| 49 | + statusKey: String, |
| 50 | + localTimezone: String) { |
| 51 | + self.siteID = siteID |
| 52 | + self.bookingID = bookingID |
| 53 | + self.allDay = allDay |
| 54 | + self.cost = cost |
| 55 | + self.customerID = customerID |
| 56 | + self.dateCreated = dateCreated |
| 57 | + self.dateModified = dateModified |
| 58 | + self.endDate = endDate |
| 59 | + self.googleCalendarEventID = googleCalendarEventID |
| 60 | + self.orderID = orderID |
| 61 | + self.orderItemID = orderItemID |
| 62 | + self.parentID = parentID |
| 63 | + self.productID = productID |
| 64 | + self.resourceID = resourceID |
| 65 | + self.startDate = startDate |
| 66 | + self.statusKey = statusKey |
| 67 | + self.localTimezone = localTimezone |
| 68 | + } |
| 69 | + |
| 70 | + /// The public initializer for Booking. |
| 71 | + /// |
| 72 | + public init(from decoder: Decoder) throws { |
| 73 | + guard let siteID = decoder.userInfo[.siteID] as? Int64 else { |
| 74 | + throw BookingDecodingError.missingSiteID |
| 75 | + } |
| 76 | + |
| 77 | + let container = try decoder.container(keyedBy: CodingKeys.self) |
| 78 | + |
| 79 | + let bookingID = try container.decode(Int64.self, forKey: .bookingID) |
| 80 | + let allDay = try container.decode(Bool.self, forKey: .allDay) |
| 81 | + |
| 82 | + // Cost may come as string or number |
| 83 | + let cost = container.failsafeDecodeIfPresent(targetType: String.self, |
| 84 | + forKey: .cost, |
| 85 | + alternativeTypes: [.decimal(transform: { NSDecimalNumber(decimal: $0).stringValue })]) ?? "" |
| 86 | + |
| 87 | + let customerID = try container.decode(Int64.self, forKey: .customerID) |
| 88 | + let dateCreated = Date(timeIntervalSince1970: try container.decode(Double.self, forKey: .dateCreated)) |
| 89 | + let dateModified = Date(timeIntervalSince1970: try container.decode(Double.self, forKey: .dateModified)) |
| 90 | + let endDate = Date(timeIntervalSince1970: try container.decode(Double.self, forKey: .endDate)) |
| 91 | + let googleCalendarEventID = try container.decodeIfPresent(String.self, forKey: .googleCalendarEventID) |
| 92 | + let orderID = try container.decode(Int64.self, forKey: .orderID) |
| 93 | + let orderItemID = try container.decode(Int64.self, forKey: .orderItemID) |
| 94 | + let parentID = try container.decode(Int64.self, forKey: .parentID) |
| 95 | + let productID = try container.decode(Int64.self, forKey: .productID) |
| 96 | + let resourceID = try container.decode(Int64.self, forKey: .resourceID) |
| 97 | + let startDate = Date(timeIntervalSince1970: try container.decode(Double.self, forKey: .startDate)) |
| 98 | + let statusKey = try container.decode(String.self, forKey: .statusKey) |
| 99 | + let localTimezone = try container.decode(String.self, forKey: .localTimezone) |
| 100 | + |
| 101 | + self.init(siteID: siteID, |
| 102 | + bookingID: bookingID, |
| 103 | + allDay: allDay, |
| 104 | + cost: cost, |
| 105 | + customerID: customerID, |
| 106 | + dateCreated: dateCreated, |
| 107 | + dateModified: dateModified, |
| 108 | + endDate: endDate, |
| 109 | + googleCalendarEventID: googleCalendarEventID, |
| 110 | + orderID: orderID, |
| 111 | + orderItemID: orderItemID, |
| 112 | + parentID: parentID, |
| 113 | + productID: productID, |
| 114 | + resourceID: resourceID, |
| 115 | + startDate: startDate, |
| 116 | + statusKey: statusKey, |
| 117 | + localTimezone: localTimezone) |
| 118 | + } |
| 119 | + |
| 120 | + public func encode(to encoder: Encoder) throws { |
| 121 | + var container = encoder.container(keyedBy: CodingKeys.self) |
| 122 | + |
| 123 | + try container.encode(bookingID, forKey: .bookingID) |
| 124 | + try container.encode(allDay, forKey: .allDay) |
| 125 | + try container.encode(cost, forKey: .cost) |
| 126 | + try container.encode(customerID, forKey: .customerID) |
| 127 | + try container.encode(dateCreated, forKey: .dateCreated) |
| 128 | + try container.encode(dateModified, forKey: .dateModified) |
| 129 | + try container.encode(endDate, forKey: .endDate) |
| 130 | + try container.encode(googleCalendarEventID, forKey: .googleCalendarEventID) |
| 131 | + try container.encode(orderID, forKey: .orderID) |
| 132 | + try container.encode(orderItemID, forKey: .orderItemID) |
| 133 | + try container.encode(parentID, forKey: .parentID) |
| 134 | + try container.encode(productID, forKey: .productID) |
| 135 | + try container.encode(resourceID, forKey: .resourceID) |
| 136 | + try container.encode(startDate, forKey: .startDate) |
| 137 | + try container.encode(statusKey, forKey: .statusKey) |
| 138 | + try container.encode(localTimezone, forKey: .localTimezone) |
| 139 | + } |
| 140 | +} |
| 141 | + |
| 142 | +/// Defines all of the Booking CodingKeys |
| 143 | +/// |
| 144 | +private extension Booking { |
| 145 | + |
| 146 | + enum CodingKeys: String, CodingKey { |
| 147 | + case bookingID = "id" |
| 148 | + case allDay = "all_day" |
| 149 | + case cost |
| 150 | + case customerID = "customer_id" |
| 151 | + case dateCreated = "date_created" |
| 152 | + case dateModified = "date_modified" |
| 153 | + case endDate = "end" |
| 154 | + case googleCalendarEventID = "google_calendar_event_id" |
| 155 | + case orderID = "order_id" |
| 156 | + case orderItemID = "order_item_id" |
| 157 | + case parentID = "parent_id" |
| 158 | + case personCounts = "person_counts" |
| 159 | + case productID = "product_id" |
| 160 | + case resourceID = "resource_id" |
| 161 | + case startDate = "start" |
| 162 | + case statusKey = "status" |
| 163 | + case localTimezone = "local_timezone" |
| 164 | + } |
| 165 | +} |
| 166 | + |
| 167 | +// MARK: - Decoding Errors |
| 168 | +// |
| 169 | +enum BookingDecodingError: Error { |
| 170 | + case missingSiteID |
| 171 | +} |
| 172 | + |
| 173 | +// MARK: - Supporting Types |
| 174 | +// |
| 175 | + |
| 176 | +/// Represents a Booking Status. |
| 177 | +/// |
| 178 | +public enum BookingStatus: String, CaseIterable { |
| 179 | + case complete |
| 180 | + case paid |
| 181 | + case unpaid |
| 182 | + case cancelled |
| 183 | + case pendingConfirmation = "pending-confirmation" |
| 184 | + case confirmed |
| 185 | + case inCart = "in-cart" |
| 186 | + case unknown |
| 187 | +} |
0 commit comments