Skip to content

Commit

Permalink
[61539] Draft version not updating after update (#304)
Browse files Browse the repository at this point in the history
When making updates to the draft object on the server side (via `.update`) the version number is incremented upon successful update. However, the local object does not update the value when it's returned from the server. This causes an issue when, the user attempts to re-update the draft a second time and the version number has not been incremented. Now, when calling `.update` the version is updated with the value returned from the payload.

Co-authored-by: Arun Agrawal <[email protected]>
  • Loading branch information
mrashed-dev and arunagw authored Jun 7, 2021
1 parent 40d1a48 commit 4de09f7
Show file tree
Hide file tree
Showing 5 changed files with 67 additions and 22 deletions.
2 changes: 2 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@
* Add `save_all_attributes` and `update_all_attributes` methods to support
nullifying attributes. #299
* Fix issue where files couldn't be attached to drafts #302
* Fix exception raise when content-type is not JSON #303
* Fix issue where draft version wouldn't update after `update` or `save` #304

### 5.0.0 / 2021-05-07

Expand Down
1 change: 1 addition & 0 deletions lib/nylas/draft.rb
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@ class Draft
transfer :api, to: %i[events files folder labels]

def update(**data)
self.files = data[:files] if data[:files]
extract_file_ids!
data[:file_ids] = file_ids

Expand Down
28 changes: 16 additions & 12 deletions lib/nylas/model.rb
Original file line number Diff line number Diff line change
Expand Up @@ -58,12 +58,9 @@ def update(**data)
raise ModelNotUpdatableError, model_class unless updatable?

attributes.merge(**data)
execute(
method: :put,
payload: attributes.serialize_for_api(keys: data.keys),
path: resource_path,
query: query_params
)
payload = attributes.serialize_for_api(keys: data.keys)
update_call(payload)

true
rescue Registry::MissingKeyError => e
raise ModelMissingFieldError.new(e.key, self)
Expand All @@ -89,12 +86,9 @@ def update_all_attributes(**data)
raise ModelNotUpdatableError, model_class unless updatable?

attributes.merge(**data)
execute(
method: :put,
payload: attributes.serialize_all_for_api(keys: data.keys),
path: resource_path,
query: query_params
)
payload = attributes.serialize_all_for_api(keys: data.keys)
update_call(payload)

true
rescue Registry::MissingKeyError => e
raise ModelMissingFieldError.new(e.key, self)
Expand Down Expand Up @@ -134,6 +128,16 @@ def save_call
)
end

def update_call(payload)
result = execute(
method: :put,
payload: payload,
path: resource_path,
query: query_params
)
attributes.merge(result) if result
end

def query_params
{}
end
Expand Down
56 changes: 47 additions & 9 deletions spec/nylas/draft_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -28,26 +28,25 @@
end

describe "update" do
context "when `files` key exists" do
it "removes `files` from the payload and sets the proper file_ids key" do
context "with `files` key" do
it "if `files` present, remove from the payload and sets the proper file_ids key" do
api = instance_double(Nylas::API, execute: JSON.parse("{}"))
file = Nylas::File.new(id: "abc-123")
data = {
id: "draft-1234",
files: [file]
id: "draft-1234"
}
draft = described_class.from_json(
JSON.dump(data),
api: api
)

draft.update(**data)
draft.update(subject: "Updated subject", files: [file])

expect(api).to have_received(:execute).with(
method: :put,
path: "/drafts/draft-1234",
payload: JSON.dump(
id: "draft-1234",
subject: "Updated subject",
file_ids: ["abc-123"]
),
query: {}
Expand All @@ -56,7 +55,7 @@
end

context "when `files` key does not exists" do
it "does nothing" do
it "does not set the file_ids key or make any further changes to the payload" do
api = instance_double(Nylas::API, execute: JSON.parse("{}"))
data = {
id: "draft-1234"
Expand All @@ -66,18 +65,38 @@
api: api
)

draft.update(**data)
draft.update(subject: "Updated subject")

expect(api).to have_received(:execute).with(
method: :put,
path: "/drafts/draft-1234",
payload: JSON.dump(
id: "draft-1234"
subject: "Updated subject"
),
query: {}
)
end
end

it "updates the local draft object version number on update" do
expected_response = {
id: "draft-1234",
version: 1
}
api = instance_double(Nylas::API, execute: expected_response)
data = {
id: "draft-1234",
version: 0
}
draft = described_class.from_json(
JSON.dump(data),
api: api
)

draft.update(**data)

expect(draft.version).to eq(1)
end
end

describe "#create" do
Expand Down Expand Up @@ -184,6 +203,25 @@
)
end
end

it "updates the local draft object version number on save" do
expected_response = {
id: "draft-1234",
version: 1
}
api = instance_double(Nylas::API, execute: expected_response)
data = {
id: "draft-1234"
}
draft = described_class.from_json(
JSON.dump(data),
api: api
)

draft.save

expect(draft.version).to eq(1)
end
end

describe "#send!" do
Expand Down
2 changes: 1 addition & 1 deletion spec/nylas/thread_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -83,7 +83,7 @@

describe "#update" do
it "let's you set the starred, unread, folder, and label ids" do
api = instance_double(Nylas::API, execute: "{}")
api = instance_double(Nylas::API, execute: {})
thread = described_class.from_json('{ "id": "thread-1234" }', api: api)

thread.update(
Expand Down

0 comments on commit 4de09f7

Please sign in to comment.