Skip to content

Commit bcf3d22

Browse files
committed
Add DTOs
1 parent 5c5ddee commit bcf3d22

File tree

8 files changed

+204
-5
lines changed

8 files changed

+204
-5
lines changed

Package.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ let package = Package(
1212
],
1313
dependencies: [
1414
.package(url: "https://github.com/vapor/fluent-kit.git", from: "1.49.0"),
15-
.package(url: "https://github.com/fpseverino/swift-wallet.git", branch: "main"),
15+
.package(url: "https://github.com/fpseverino/swift-wallet.git", from: "0.2.0"),
1616
],
1717
targets: [
1818
.target(
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
/// An object that contains an array of messages.
2+
///
3+
/// See: [`LogEntries`](https://developer.apple.com/documentation/walletpasses/logentries)
4+
public struct LogEntriesDTO: Codable {
5+
/// An array of log messages.
6+
public let logs: [String]
7+
8+
public init(logs: [String]) {
9+
self.logs = logs
10+
}
11+
}
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
/// An object that contains the push notification token for a registered pass on a device.
2+
///
3+
/// See: [`PushToken`](https://developer.apple.com/documentation/walletpasses/pushtoken)
4+
public struct PushTokenDTO: Codable {
5+
/// A push token the server uses to send update notifications for a registered pass to a device.
6+
public let pushToken: String
7+
8+
public init(pushToken: String) {
9+
self.pushToken = pushToken
10+
}
11+
}
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
import struct Foundation.Date
2+
3+
/// The unique identifiers associated with orders.
4+
///
5+
/// See: [`OrderIdentifiers`](https://developer.apple.com/documentation/walletorders/orderidentifiers)
6+
public struct OrderIdentifiersDTO: Codable {
7+
/// An array of order identifer strings.
8+
public let orderIdentifiers: [String]
9+
10+
/// The date and time of when an order was last changed.
11+
public let lastModified: String
12+
13+
public init(with orderIdentifiers: [String], maxDate: Date) {
14+
self.orderIdentifiers = orderIdentifiers
15+
self.lastModified = String(maxDate.timeIntervalSince1970)
16+
}
17+
}
Lines changed: 79 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,79 @@
1+
/// An object that contains the information you use to personalize a pass.
2+
///
3+
/// See: [`PersonalizationDictionary`](https://developer.apple.com/documentation/walletpasses/personalizationdictionary)
4+
public struct PersonalizationDictionaryDTO: Codable {
5+
/// The personalization token for this request. The server must sign and return the token.
6+
public let personalizationToken: String
7+
8+
/// An object that contains the user-entered information for a personalized pass.
9+
public let requiredPersonalizationInfo: RequiredPersonalizationInfo
10+
11+
public init(
12+
personalizationToken: String,
13+
requiredPersonalizationInfo: RequiredPersonalizationInfo
14+
) {
15+
self.personalizationToken = personalizationToken
16+
self.requiredPersonalizationInfo = requiredPersonalizationInfo
17+
}
18+
19+
/// An object that contains the user-entered information for a personalized pass.
20+
///
21+
/// See: [`RequiredPersonalizationInfo`](https://developer.apple.com/documentation/walletpasses/personalizationdictionary/requiredpersonalizationinfo-data.dictionary)
22+
public struct RequiredPersonalizationInfo: Codable {
23+
/// The user-entered email address for the user of the personalized pass.
24+
public let emailAddress: String?
25+
26+
/// The family name for the user of the personalized pass, parsed from the full name.
27+
///
28+
/// The name can indicate membership in a group.
29+
public let familyName: String?
30+
31+
/// The user-entered full name for the user of the personalized pass.
32+
public let fullName: String?
33+
34+
/// The given name for the user of the personalized pass, parsed from the full name.
35+
///
36+
/// The system uses the name to differentiate the individual from other members who share the same family name.
37+
/// In some locales, the given name is also known as a _forename_ or _first name_.
38+
public let givenName: String?
39+
40+
/// The ISO country code.
41+
///
42+
/// The system sets this key when it’s known; otherwise, it doesn’t include the key.
43+
public let isoCountryCode: String?
44+
45+
/// The user-entered phone number for the user of the personalized pass.
46+
public let phoneNumber: String?
47+
48+
/// The user-entered postal code for the user of the personalized pass.
49+
public let postalCode: String?
50+
51+
public init(
52+
emailAddress: String? = nil,
53+
familyName: String? = nil,
54+
fullName: String? = nil,
55+
givenName: String? = nil,
56+
isoCountryCode: String? = nil,
57+
phoneNumber: String? = nil,
58+
postalCode: String? = nil
59+
) {
60+
self.emailAddress = emailAddress
61+
self.familyName = familyName
62+
self.fullName = fullName
63+
self.givenName = givenName
64+
self.isoCountryCode = isoCountryCode
65+
self.phoneNumber = phoneNumber
66+
self.postalCode = postalCode
67+
}
68+
69+
enum CodingKeys: String, CodingKey {
70+
case emailAddress
71+
case familyName
72+
case fullName
73+
case givenName
74+
case isoCountryCode = "ISOCountryCode"
75+
case phoneNumber
76+
case postalCode
77+
}
78+
}
79+
}
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
import struct Foundation.Date
2+
3+
/// An object that contains serial numbers for the updatable passes on a device.
4+
///
5+
/// See: [`SerialNumbers`](https://developer.apple.com/documentation/walletpasses/serialnumbers)
6+
public struct SerialNumbersDTO: Codable {
7+
/// A developer-defined string that contains a tag that indicates the modification time for the returned passes.
8+
public let lastUpdated: String
9+
10+
/// An array of serial numbers for the updated passes.
11+
public let serialNumbers: [String]
12+
13+
public init(with serialNumbers: [String], maxDate: Date) {
14+
lastUpdated = String(maxDate.timeIntervalSince1970)
15+
self.serialNumbers = serialNumbers
16+
}
17+
}

Tests/FluentWalletOrdersTests/FluentWalletOrdersTests.swift

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,8 @@
11
import FluentWalletOrders
2+
import Foundation
23
import Testing
34
import XCTFluent
45

5-
import struct Foundation.UUID
6-
76
@Suite("FluentWalletOrders Tests")
87
struct FluentWalletOrdersTests {
98
let test = ArrayTestDatabase()
@@ -107,4 +106,13 @@ struct FluentWalletOrdersTests {
107106

108107
try await migration.revert(on: test.db)
109108
}
109+
110+
@Test("OrderIdentifiersDTO")
111+
func orderIdentifiersDTO() {
112+
let orderIdentifiers = ["Test Order Identifier"]
113+
let maxDate = Date.now
114+
let orderIdentifiersDTO = OrderIdentifiersDTO(with: orderIdentifiers, maxDate: maxDate)
115+
#expect(orderIdentifiersDTO.orderIdentifiers == orderIdentifiers)
116+
#expect(orderIdentifiersDTO.lastModified == String(maxDate.timeIntervalSince1970))
117+
}
110118
}

Tests/FluentWalletPassesTests/FluentWalletPassesTests.swift

Lines changed: 58 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,8 @@
11
import FluentWalletPasses
2+
import Foundation
23
import Testing
34
import XCTFluent
45

5-
import struct Foundation.UUID
6-
76
@Suite("FluentWalletPasses Tests")
87
struct FluentWalletPassesTests {
98
let test = ArrayTestDatabase()
@@ -154,4 +153,61 @@ struct FluentWalletPassesTests {
154153

155154
try await migration.revert(on: test.db)
156155
}
156+
157+
@Test("SerialNumbersDTO")
158+
func serialNumbersDTO() {
159+
let serialNumbers = ["Test Serial Number 1", "Test Serial Number 2"]
160+
let maxDate = Date.now
161+
let serialNumbersDTO = SerialNumbersDTO(with: serialNumbers, maxDate: maxDate)
162+
#expect(serialNumbersDTO.lastUpdated == String(maxDate.timeIntervalSince1970))
163+
#expect(serialNumbersDTO.serialNumbers == serialNumbers)
164+
}
165+
166+
@Test("PersonalizationDictionaryDTO")
167+
func personalizationDictionaryDTO() throws {
168+
let personalizationToken = "Test Personalization Token"
169+
let requiredPersonalizationInfo = PersonalizationDictionaryDTO.RequiredPersonalizationInfo(
170+
emailAddress: "Test Email Address",
171+
familyName: "Test Family Name",
172+
fullName: "Test Full Name",
173+
givenName: "Test Given Name",
174+
isoCountryCode: "Test ISO Country Code",
175+
phoneNumber: "Test Phone Number",
176+
postalCode: "Test Postal Code"
177+
)
178+
let personalizationDictionaryDTO = PersonalizationDictionaryDTO(
179+
personalizationToken: personalizationToken,
180+
requiredPersonalizationInfo: requiredPersonalizationInfo
181+
)
182+
#expect(personalizationDictionaryDTO.personalizationToken == personalizationToken)
183+
#expect(personalizationDictionaryDTO.requiredPersonalizationInfo.emailAddress == requiredPersonalizationInfo.emailAddress)
184+
#expect(personalizationDictionaryDTO.requiredPersonalizationInfo.familyName == requiredPersonalizationInfo.familyName)
185+
#expect(personalizationDictionaryDTO.requiredPersonalizationInfo.fullName == requiredPersonalizationInfo.fullName)
186+
#expect(personalizationDictionaryDTO.requiredPersonalizationInfo.givenName == requiredPersonalizationInfo.givenName)
187+
#expect(personalizationDictionaryDTO.requiredPersonalizationInfo.isoCountryCode == requiredPersonalizationInfo.isoCountryCode)
188+
#expect(personalizationDictionaryDTO.requiredPersonalizationInfo.phoneNumber == requiredPersonalizationInfo.phoneNumber)
189+
#expect(personalizationDictionaryDTO.requiredPersonalizationInfo.postalCode == requiredPersonalizationInfo.postalCode)
190+
191+
// Test `PersonalizationDictionaryDTO.RequiredPersonalizationInfo` `CodingKeys`
192+
let encoded = try JSONEncoder().encode(requiredPersonalizationInfo)
193+
let jsonString = String(data: encoded, encoding: .utf8)!
194+
#expect(jsonString.contains("ISOCountryCode"))
195+
#expect(!jsonString.contains("isoCountryCode"))
196+
let decoded = try JSONDecoder().decode(PersonalizationDictionaryDTO.RequiredPersonalizationInfo.self, from: encoded)
197+
#expect(decoded.isoCountryCode == requiredPersonalizationInfo.isoCountryCode)
198+
}
199+
200+
@Test("PushTokenDTO")
201+
func pushTokenDTO() {
202+
let pushToken = "Test Push Token"
203+
let pushTokenDTO = PushTokenDTO(pushToken: pushToken)
204+
#expect(pushTokenDTO.pushToken == pushToken)
205+
}
206+
207+
@Test("LogEntriesDTO")
208+
func logEntriesDTO() {
209+
let logEntries = ["Test Log Entry 1", "Test Log Entry 2"]
210+
let logEntriesDTO = LogEntriesDTO(logs: logEntries)
211+
#expect(logEntriesDTO.logs == logEntries)
212+
}
157213
}

0 commit comments

Comments
 (0)