Skip to content

Commit 4f902a4

Browse files
authored
fix(SignRequest): Fix encoding date_value to yyyy-mm-dd format in prefillTag (#806)
1 parent 8e9e11f commit 4f902a4

File tree

7 files changed

+124
-20
lines changed

7 files changed

+124
-20
lines changed

Sources/Core/Date+Convenience.swift

Lines changed: 16 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -17,14 +17,24 @@ extension ISO8601DateFormatter {
1717
}
1818
}
1919

20+
extension DateFormatter {
21+
// swiftlint:disable:next force_unwrapping
22+
convenience init(dateFormat: String, timeZone: TimeZone = TimeZone(secondsFromGMT: 0)!) {
23+
self.init()
24+
self.dateFormat = dateFormat
25+
self.timeZone = timeZone
26+
}
27+
}
28+
2029
extension Formatter {
21-
static let iso8601 = ISO8601DateFormatter()
22-
static let iso8601WithMilliseconds = ISO8601DateFormatter([.withInternetDateTime, .withFractionalSeconds])
30+
static let iso8601WithDateOnly = DateFormatter(dateFormat: "yyyy-MM-dd")
31+
static let iso8601WithSeconds = ISO8601DateFormatter()
32+
static let iso8601WithMilliseconds = ISO8601DateFormatter([.withInternetDateTime, .withFractionalSeconds, .withDashSeparatorInDate])
2333
}
2434

2535
extension Date {
2636
var iso8601: String {
27-
return Formatter.iso8601.string(from: self)
37+
return Formatter.iso8601WithSeconds.string(from: self)
2838
}
2939

3040
init?(fromISO8601String dateValue: String) {
@@ -37,6 +47,8 @@ extension Date {
3747

3848
extension String {
3949
var iso8601: Date? {
40-
return Formatter.iso8601.date(from: self) ?? Formatter.iso8601WithMilliseconds.date(from: self)
50+
return Formatter.iso8601WithSeconds.date(from: self)
51+
?? Formatter.iso8601WithMilliseconds.date(from: self)
52+
?? Formatter.iso8601WithDateOnly.date(from: self)
4153
}
4254
}

Sources/Responses/SignRequestPrefillTag.swift

Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,13 @@ import Foundation
1212
/// Only one value field can be included.
1313
public class SignRequestPrefillTag: BoxInnerModel {
1414

15+
private enum CodingKeys: String, CodingKey {
16+
case documentTagId
17+
case textValue
18+
case checkboxValue
19+
case dateValue
20+
}
21+
1522
// MARK: - Properties
1623

1724
/// This references the ID of a specific tag contained in a file of the sign request
@@ -38,6 +45,8 @@ public class SignRequestPrefillTag: BoxInnerModel {
3845
dateValue = nil
3946
}
4047

48+
/// Initializer.
49+
///
4150
/// - Parameters:
4251
/// - documentTagId: Id of the tag.
4352
/// - checkboxValue: The checkbox prefill value.
@@ -51,6 +60,8 @@ public class SignRequestPrefillTag: BoxInnerModel {
5160
dateValue = nil
5261
}
5362

63+
/// Initializer.
64+
///
5465
/// - Parameters:
5566
/// - documentTagId: Id of the tag.
5667
/// - dateValue: The date prefill value.
@@ -63,4 +74,44 @@ public class SignRequestPrefillTag: BoxInnerModel {
6374
textValue = nil
6475
checkboxValue = nil
6576
}
77+
78+
/// Creates a new instance by decoding from the given decoder.
79+
/// This initializer throws an error if reading from the decoder fails, or
80+
/// if the data read is corrupted or otherwise invalid.
81+
///
82+
/// - Parameters:
83+
/// - decoder: The decoder to read data from.
84+
public required init(from decoder: Decoder) throws {
85+
let container = try decoder.container(keyedBy: CodingKeys.self)
86+
documentTagId = try container.decodeIfPresent(String.self, forKey: .documentTagId)
87+
textValue = try container.decodeIfPresent(String.self, forKey: .textValue)
88+
checkboxValue = try container.decodeIfPresent(Bool.self, forKey: .checkboxValue)
89+
90+
if let dateString = try? container.decodeIfPresent(String.self, forKey: .dateValue),
91+
let date = Formatter.iso8601WithDateOnly.date(from: dateString) {
92+
dateValue = date
93+
}
94+
else {
95+
dateValue = nil
96+
}
97+
}
98+
99+
/// Encodes this value into the given encoder.
100+
/// If the value fails to encode anything, `encoder` will encode an empty
101+
/// keyed container in its place.
102+
/// This function throws an error if any values are invalid for the given
103+
/// encoder's format.
104+
///
105+
/// - Parameters:
106+
/// - encoder: The encoder to write data to.
107+
public func encode(to encoder: Encoder) throws {
108+
var container = encoder.container(keyedBy: CodingKeys.self)
109+
try container.encodeIfPresent(documentTagId, forKey: .documentTagId)
110+
try container.encodeIfPresent(textValue, forKey: .textValue)
111+
try container.encodeIfPresent(checkboxValue, forKey: .checkboxValue)
112+
113+
if let date = dateValue {
114+
try container.encodeIfPresent(Formatter.iso8601WithDateOnly.string(from: date), forKey: .dateValue)
115+
}
116+
}
66117
}

Tests/Modules/SignRequestsModuleSpecs.swift

Lines changed: 19 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,10 @@ class SignRequestsModuleSpecs: QuickSpec {
3939
"is_document_preparation_needed": true,
4040
"are_text_signatures_enabled": true,
4141
"are_reminders_enabled": true,
42-
"prefill_tags": [["document_tag_id": "1234", "text_value": "text"]],
42+
"prefill_tags": [
43+
["document_tag_id": "1234", "text_value": "text"],
44+
["document_tag_id": "4567", "date_value": "2021-12-03"]
45+
],
4346
"email_subject": "Sign Request from Acme",
4447
"email_message": "Hello! Please sign the document below",
4548
"external_id": "123",
@@ -56,7 +59,10 @@ class SignRequestsModuleSpecs: QuickSpec {
5659
let signers = [SignRequestCreateSigner(email: "[email protected]", role: .signer)]
5760
let sourceFiles = [SignRequestCreateSourceFile(id: "12345")]
5861
let parentFolder = SignRequestCreateParentFolder(id: "12345")
59-
let tags = [SignRequestPrefillTag(documentTagId: "1234", textValue: "text")]
62+
let tags = [
63+
SignRequestPrefillTag(documentTagId: "1234", textValue: "text"),
64+
SignRequestPrefillTag(documentTagId: "4567", dateValue: "2021-12-03T08:12:13.982Z".iso8601)
65+
]
6066
let params = SignRequestCreateParameters(
6167
isDocumentPreparationNeeded: true,
6268
areTextSignaturesEnabled: true,
@@ -83,6 +89,14 @@ class SignRequestsModuleSpecs: QuickSpec {
8389
expect(signRequest.sourceFiles.first?.name).to(equal("Contract.pdf"))
8490
expect(signRequest.parentFolder.id).to(equal("12345"))
8591
expect(signRequest.parentFolder.name).to(equal("Contracts"))
92+
expect(signRequest.signers[0].inputs?[0].documentTagId).to(equal("1234"))
93+
expect(signRequest.signers[0].inputs?[0].textValue).to(equal("text"))
94+
expect(signRequest.signers[0].inputs?[1].documentTagId).to(equal("4567"))
95+
expect(signRequest.signers[0].inputs?[1].dateValue).to(equal("2021-12-03".iso8601))
96+
expect(signRequest.prefillTags?[0].documentTagId).to(equal("1234"))
97+
expect(signRequest.prefillTags?[0].textValue).to(equal("text"))
98+
expect(signRequest.prefillTags?[1].documentTagId).to(equal("4567"))
99+
expect(signRequest.prefillTags?[1].dateValue).to(equal("2021-12-03".iso8601))
86100
expect(signRequest.emailSubject).to(equal("Sign Request from Acme"))
87101
expect(signRequest.emailMessage).to(equal("Hello! Please sign the document below"))
88102
expect(signRequest.externalId).to(equal("123"))
@@ -119,6 +133,9 @@ class SignRequestsModuleSpecs: QuickSpec {
119133
expect(firstSignRequest).toNot(beNil())
120134
expect(firstSignRequest.id).to(equal("12345"))
121135
expect(firstSignRequest.signers.first?.email).to(equal("[email protected]"))
136+
let firstInput = firstSignRequest.signers[0].inputs?[0]
137+
expect(firstInput?.dateValue).to(equal("2021-04-26".iso8601))
138+
expect(firstInput?.textValue).to(equal("April 26, 2021"))
122139
expect(firstSignRequest.sourceFiles.first?.id).to(equal("12345"))
123140
expect(firstSignRequest.sourceFiles.first?.name).to(equal("Contract.pdf"))
124141
expect(firstSignRequest.parentFolder.id).to(equal("12345"))

Tests/Responses/SignRequestPrefillTagSpecs.swift

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -34,9 +34,9 @@ class SignRequestPrefillTagSpecs: QuickSpec {
3434
}
3535

3636
it("should correctly create an object using init with dateValue parameter") {
37-
let sut = SignRequestPrefillTag(documentTagId: "1", dateValue: "2021-04-26T08:12:13Z".iso8601)
37+
let sut = SignRequestPrefillTag(documentTagId: "1", dateValue: "2021-04-26".iso8601)
3838
expect(sut.documentTagId).to(equal("1"))
39-
expect(sut.dateValue).to(equal("2021-04-26T08:12:13Z".iso8601))
39+
expect(sut.dateValue).to(equal("2021-04-26".iso8601))
4040
expect(sut.checkboxValue).to(beNil())
4141
expect(sut.textValue).to(beNil())
4242
}

Tests/Stubs/Resources/SignRequests/CreateSignRequest.json

Lines changed: 20 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -17,12 +17,22 @@
1717
"finalized_at": "2021-04-26T08:12:13.982Z"
1818
},
1919
"inputs": [
20-
{
21-
"document_tag_id": "1234",
22-
"text_value": "text",
23-
"type": "text",
24-
"page_index": 4
25-
}
20+
{
21+
"document_tag_id": "1234",
22+
"text_value": "text",
23+
"checkbox_value": null,
24+
"date_value": null,
25+
"type": "text",
26+
"page_index": 4
27+
},
28+
{
29+
"document_tag_id": "4567",
30+
"text_value": "Dec 3, 2021",
31+
"checkbox_value": null,
32+
"date_value": "2021-12-03",
33+
"type": "date",
34+
"page_index": 4
35+
}
2636
],
2737
"embed_url": "https://example.com"
2838
}
@@ -53,6 +63,10 @@
5363
{
5464
"document_tag_id": "1234",
5565
"text_value": "text",
66+
},
67+
{
68+
"document_tag_id": "4567",
69+
"date_value": "2021-12-03",
5670
}
5771
],
5872
"days_valid": 2,

Tests/Stubs/Resources/SignRequests/GetSignRequests.json

Lines changed: 14 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -22,10 +22,20 @@
2222
},
2323
"inputs": [
2424
{
25-
"document_tag_id": "1234",
26-
"text_value": "text",
27-
"type": "text",
28-
"page_index": 4
25+
"document_tag_id": null,
26+
"text_value": "April 26, 2021",
27+
"checkbox_value": null,
28+
"date_value": "2021-04-26",
29+
"type": "date",
30+
"page_index": 0
31+
},
32+
{
33+
"document_tag_id": null,
34+
"text_value": "",
35+
"checkbox_value": null,
36+
"date_value": null,
37+
"type": "signature",
38+
"page_index": 0
2939
}
3040
],
3141
"embed_url": "https://example.com"
Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"watermark": {
3-
"created_at":"2019-08-26",
4-
"modified_at":"2019-08-26"
3+
"created_at":"26-08-2019",
4+
"modified_at":"26-08-2019"
55
}
66
}

0 commit comments

Comments
 (0)