From a3cf46f377682902e64820c3adc575f9212474b5 Mon Sep 17 00:00:00 2001 From: Shivashankar P Date: Mon, 20 Apr 2020 20:45:14 +0530 Subject: [PATCH] Find user donations by email --- lib/actionkit_connector/rest/user.rb | 5 + .../rest_user_filter_user_orders_by_email.yml | 200 ++++++++++++++++++ ..._user_orders_by_email_with_0_donations.yml | 42 ++++ spec/lib/rest/user_spec.rb | 44 ++++ 4 files changed, 291 insertions(+) create mode 100644 spec/fixtures/cassettes/rest_user_filter_user_orders_by_email.yml create mode 100644 spec/fixtures/cassettes/rest_user_filter_user_orders_by_email_with_0_donations.yml diff --git a/lib/actionkit_connector/rest/user.rb b/lib/actionkit_connector/rest/user.rb index 4296200..8f54c88 100644 --- a/lib/actionkit_connector/rest/user.rb +++ b/lib/actionkit_connector/rest/user.rb @@ -4,6 +4,11 @@ module User def update_user(id, data) self.class.put("/user/#{id}/", prep_options(data)) end + + def filter_user_orders_by(field: 'user__email', value: '') + return [] if field.to_s.empty? || value.to_s.empty? + self.class.get("/order/?#{field}=#{value}", prep_options({})) + end end end end diff --git a/spec/fixtures/cassettes/rest_user_filter_user_orders_by_email.yml b/spec/fixtures/cassettes/rest_user_filter_user_orders_by_email.yml new file mode 100644 index 0000000..a47fe1a --- /dev/null +++ b/spec/fixtures/cassettes/rest_user_filter_user_orders_by_email.yml @@ -0,0 +1,200 @@ +--- +http_interactions: +- request: + method: get + uri: https://DummyUsername:DummyPassword@act.sumofus.org/rest/v1/order/?user__email=test@example.com + body: + encoding: UTF-8 + string: "{}" + headers: + Content-Type: + - application/json + Charset: + - UTF-8 + Authorization: + - Basic 1245689z1GFua2FyOmFjdGlvbmtpdFF3ZXJ0eTEyIw== + response: + status: + code: 200 + message: OK + headers: + Date: + - Mon, 20 Apr 2020 14:01:14 GMT + Content-Type: + - application/json + Transfer-Encoding: + - chunked + Connection: + - keep-alive + Server: + - openresty + Vary: + - Accept, Cookie, Origin + - Accept-Encoding + Cache-Control: + - no-cache + body: + encoding: UTF-8 + string: '{"meta": {"limit": 20, "next": "/rest/v1/order/?user__email=test%40example.com&_limit=20&_offset=20", + "offset": 0, "previous": null, "total_count": 35}, "objects": [{"account": + "Braintree EUR", "action": "/rest/v1/donationaction/67248325/", "card_num_last_four": + "1111", "created_at": "2016-05-10T09:29:43", "currency": "GBP", "id": 869740, + "import_id": null, "orderdetails": [], "orderrecurrings": [], "payment_method": + "cc", "resource_uri": "/rest/v1/order/869740/", "reverse": "/rest/v1/order/869740/reverse/", + "shipping_address": null, "status": "completed", "total": "34.00", "total_converted": + "49.12", "transactions": ["/rest/v1/transaction/1044000/"], "updated_at": + "2016-05-10T09:29:43", "user": "/rest/v1/user/991564/", "user_detail": "/rest/v1/orderuserdetail/869868/"}, + {"account": "Braintree EUR", "action": "/rest/v1/donationaction/67253135/", + "card_num_last_four": "1111", "created_at": "2016-05-10T10:44:51", "currency": + "GBP", "id": 869761, "import_id": null, "orderdetails": [], "orderrecurrings": + [], "payment_method": "cc", "resource_uri": "/rest/v1/order/869761/", "reverse": + "/rest/v1/order/869761/reverse/", "shipping_address": null, "status": "completed", + "total": "1.00", "total_converted": "1.44", "transactions": ["/rest/v1/transaction/1044021/"], + "updated_at": "2016-05-10T10:44:51", "user": "/rest/v1/user/991564/", "user_detail": + "/rest/v1/orderuserdetail/869889/"}, {"account": "Braintree EUR", "action": + "/rest/v1/donationaction/67253314/", "card_num_last_four": "1111", "created_at": + "2016-05-10T10:47:42", "currency": "GBP", "id": 869763, "import_id": null, + "orderdetails": [], "orderrecurrings": [], "payment_method": "cc", "resource_uri": + "/rest/v1/order/869763/", "reverse": "/rest/v1/order/869763/reverse/", "shipping_address": + null, "status": "completed", "total": "1.00", "total_converted": "1.44", "transactions": + ["/rest/v1/transaction/1044023/"], "updated_at": "2016-05-10T10:47:42", "user": + "/rest/v1/user/991564/", "user_detail": "/rest/v1/orderuserdetail/869891/"}, + {"account": "Braintree EUR", "action": "/rest/v1/donationaction/67253869/", + "card_num_last_four": "1111", "created_at": "2016-05-10T10:56:03", "currency": + "GBP", "id": 869766, "import_id": null, "orderdetails": [], "orderrecurrings": + [], "payment_method": "cc", "resource_uri": "/rest/v1/order/869766/", "reverse": + "/rest/v1/order/869766/reverse/", "shipping_address": null, "status": "completed", + "total": "1.00", "total_converted": "1.44", "transactions": ["/rest/v1/transaction/1044026/"], + "updated_at": "2016-05-10T10:56:03", "user": "/rest/v1/user/991564/", "user_detail": + "/rest/v1/orderuserdetail/869894/"}, {"account": "Braintree EUR", "action": + "/rest/v1/donationaction/67254009/", "card_num_last_four": "1111", "created_at": + "2016-05-10T10:57:59", "currency": "GBP", "id": 869769, "import_id": "BxU2H1J5bEqzkQy05CnhHA==", + "orderdetails": [], "orderrecurrings": [], "payment_method": "cc", "resource_uri": + "/rest/v1/order/869769/", "reverse": "/rest/v1/order/869769/reverse/", "shipping_address": + null, "status": "completed", "total": "1.00", "total_converted": "1.44", "transactions": + ["/rest/v1/transaction/1044029/"], "updated_at": "2016-05-11T10:21:40", "user": + "/rest/v1/user/991564/", "user_detail": "/rest/v1/orderuserdetail/869897/"}, + {"account": "Braintree EUR", "action": "/rest/v1/donationaction/67492940/", + "card_num_last_four": "1111", "created_at": "2016-05-13T10:22:40", "currency": + "GBP", "id": 872947, "import_id": null, "orderdetails": [], "orderrecurrings": + ["/rest/v1/orderrecurring/10411/"], "payment_method": "cc", "resource_uri": + "/rest/v1/order/872947/", "reverse": "/rest/v1/order/872947/reverse/", "shipping_address": + null, "status": "completed", "total": "1.00", "total_converted": "1.44", "transactions": + ["/rest/v1/transaction/1047856/", "/rest/v1/transaction/1047857/", "/rest/v1/transaction/1047873/", + "/rest/v1/transaction/1047874/", "/rest/v1/transaction/1047875/", "/rest/v1/transaction/1047876/", + "/rest/v1/transaction/1606926/"], "updated_at": "2016-05-13T10:22:40", "user": + "/rest/v1/user/991564/", "user_detail": "/rest/v1/orderuserdetail/873075/"}, + {"account": "Braintree EUR", "action": "/rest/v1/donationaction/67496447/", + "card_num_last_four": "1111", "created_at": "2016-05-13T10:56:25", "currency": + "GBP", "id": 872964, "import_id": null, "orderdetails": [], "orderrecurrings": + ["/rest/v1/orderrecurring/10412/"], "payment_method": "cc", "resource_uri": + "/rest/v1/order/872964/", "reverse": "/rest/v1/order/872964/reverse/", "shipping_address": + null, "status": "completed", "total": "1.00", "total_converted": "1.44", "transactions": + ["/rest/v1/transaction/1047878/", "/rest/v1/transaction/1047879/", "/rest/v1/transaction/1047880/", + "/rest/v1/transaction/1047881/", "/rest/v1/transaction/1606927/"], "updated_at": + "2016-05-13T10:56:25", "user": "/rest/v1/user/991564/", "user_detail": "/rest/v1/orderuserdetail/873092/"}, + {"account": "Braintree EUR", "action": "/rest/v1/donationaction/67503908/", + "card_num_last_four": "1111", "created_at": "2016-05-13T12:16:31", "currency": + "GBP", "id": 872987, "import_id": null, "orderdetails": [], "orderrecurrings": + ["/rest/v1/orderrecurring/10413/"], "payment_method": "cc", "resource_uri": + "/rest/v1/order/872987/", "reverse": "/rest/v1/order/872987/reverse/", "shipping_address": + null, "status": "completed", "total": "1.00", "total_converted": "1.44", "transactions": + ["/rest/v1/transaction/1047904/", "/rest/v1/transaction/1047905/", "/rest/v1/transaction/1049537/", + "/rest/v1/transaction/1606929/"], "updated_at": "2016-05-13T12:16:31", "user": + "/rest/v1/user/991564/", "user_detail": "/rest/v1/orderuserdetail/873115/"}, + {"account": "Braintree EUR", "action": "/rest/v1/donationaction/67834993/", + "card_num_last_four": "1111", "created_at": "2016-05-17T11:07:23", "currency": + "GBP", "id": 873962, "import_id": null, "orderdetails": [], "orderrecurrings": + [], "payment_method": "cc", "resource_uri": "/rest/v1/order/873962/", "reverse": + "/rest/v1/order/873962/reverse/", "shipping_address": null, "status": "completed", + "total": "1.00", "total_converted": "1.44", "transactions": ["/rest/v1/transaction/1049536/"], + "updated_at": "2016-05-17T11:07:23", "user": "/rest/v1/user/991564/", "user_detail": + "/rest/v1/orderuserdetail/874090/"}, {"account": "GoCardless GBP", "action": + "/rest/v1/donationaction/67996554/", "card_num_last_four": "1111", "created_at": + "2016-05-18T17:35:30", "currency": "GBP", "id": 874265, "import_id": null, + "orderdetails": [], "orderrecurrings": ["/rest/v1/orderrecurring/10416/"], + "payment_method": "cc", "resource_uri": "/rest/v1/order/874265/", "reverse": + "/rest/v1/order/874265/reverse/", "shipping_address": null, "status": "completed", + "total": "3.00", "total_converted": "4.36", "transactions": ["/rest/v1/transaction/1050077/", + "/rest/v1/transaction/1050078/", "/rest/v1/transaction/1050079/", "/rest/v1/transaction/1606930/"], + "updated_at": "2016-05-18T17:35:30", "user": "/rest/v1/user/991564/", "user_detail": + "/rest/v1/orderuserdetail/874393/"}, {"account": "GoCardless GBP", "action": + "/rest/v1/donationaction/67997863/", "card_num_last_four": "1111", "created_at": + "2016-05-18T17:41:24", "currency": "GBP", "id": 874266, "import_id": null, + "orderdetails": [], "orderrecurrings": [], "payment_method": "cc", "resource_uri": + "/rest/v1/order/874266/", "reverse": "/rest/v1/order/874266/reverse/", "shipping_address": + null, "status": "completed", "total": "3.00", "total_converted": "4.36", "transactions": + ["/rest/v1/transaction/1050080/"], "updated_at": "2016-05-18T17:41:24", "user": + "/rest/v1/user/991564/", "user_detail": "/rest/v1/orderuserdetail/874394/"}, + {"account": "Braintree EUR", "action": "/rest/v1/donationaction/68237639/", + "card_num_last_four": "1111", "created_at": "2016-05-23T11:33:19", "currency": + "GBP", "id": 879746, "import_id": null, "orderdetails": [], "orderrecurrings": + [], "payment_method": "cc", "resource_uri": "/rest/v1/order/879746/", "reverse": + "/rest/v1/order/879746/reverse/", "shipping_address": null, "status": "completed", + "total": "1.00", "total_converted": "1.45", "transactions": ["/rest/v1/transaction/1056421/"], + "updated_at": "2016-05-23T11:33:19", "user": "/rest/v1/user/991564/", "user_detail": + "/rest/v1/orderuserdetail/879874/"}, {"account": "Braintree EUR", "action": + "/rest/v1/donationaction/68237640/", "card_num_last_four": "1111", "created_at": + "2016-05-23T11:33:20", "currency": "GBP", "id": 879747, "import_id": null, + "orderdetails": [], "orderrecurrings": [], "payment_method": "cc", "resource_uri": + "/rest/v1/order/879747/", "reverse": "/rest/v1/order/879747/reverse/", "shipping_address": + null, "status": "completed", "total": "1.00", "total_converted": "1.45", "transactions": + ["/rest/v1/transaction/1056422/"], "updated_at": "2016-05-23T11:33:20", "user": + "/rest/v1/user/991564/", "user_detail": "/rest/v1/orderuserdetail/879875/"}, + {"account": "Braintree EUR", "action": "/rest/v1/donationaction/68237642/", + "card_num_last_four": "1111", "created_at": "2016-05-23T11:33:21", "currency": + "GBP", "id": 879748, "import_id": null, "orderdetails": [], "orderrecurrings": + [], "payment_method": "cc", "resource_uri": "/rest/v1/order/879748/", "reverse": + "/rest/v1/order/879748/reverse/", "shipping_address": null, "status": "completed", + "total": "1.00", "total_converted": "1.45", "transactions": ["/rest/v1/transaction/1056423/"], + "updated_at": "2016-05-23T11:33:21", "user": "/rest/v1/user/991564/", "user_detail": + "/rest/v1/orderuserdetail/879876/"}, {"account": "Braintree EUR", "action": + "/rest/v1/donationaction/68238075/", "card_num_last_four": "1111", "created_at": + "2016-05-23T11:51:19", "currency": "GBP", "id": 879752, "import_id": null, + "orderdetails": [], "orderrecurrings": [], "payment_method": "cc", "resource_uri": + "/rest/v1/order/879752/", "reverse": "/rest/v1/order/879752/reverse/", "shipping_address": + null, "status": "completed", "total": "1.00", "total_converted": "1.45", "transactions": + ["/rest/v1/transaction/1056427/"], "updated_at": "2016-05-23T11:51:19", "user": + "/rest/v1/user/991564/", "user_detail": "/rest/v1/orderuserdetail/879880/"}, + {"account": "Braintree EUR", "action": "/rest/v1/donationaction/68238077/", + "card_num_last_four": "1111", "created_at": "2016-05-23T11:51:21", "currency": + "GBP", "id": 879753, "import_id": "", "orderdetails": [], "orderrecurrings": + [], "payment_method": "cc", "resource_uri": "/rest/v1/order/879753/", "reverse": + "/rest/v1/order/879753/reverse/", "shipping_address": null, "status": "completed", + "total": "1.00", "total_converted": "1.45", "transactions": ["/rest/v1/transaction/1056428/"], + "updated_at": "2016-05-23T12:41:40", "user": "/rest/v1/user/991564/", "user_detail": + "/rest/v1/orderuserdetail/879881/"}, {"account": "Braintree GBP", "action": + "/rest/v1/donationaction/68963214/", "card_num_last_four": "1615", "created_at": + "2015-06-02T10:22:26", "currency": "GBP", "id": 889155, "import_id": null, + "orderdetails": [], "orderrecurrings": ["/rest/v1/orderrecurring/10423/"], + "payment_method": "cc", "resource_uri": "/rest/v1/order/889155/", "reverse": + "/rest/v1/order/889155/reverse/", "shipping_address": null, "status": "completed", + "total": "1.00", "total_converted": "1.44", "transactions": ["/rest/v1/transaction/1068459/", + "/rest/v1/transaction/1068533/", "/rest/v1/transaction/1606931/"], "updated_at": + "2016-06-02T10:22:26", "user": "/rest/v1/user/991564/", "user_detail": "/rest/v1/orderuserdetail/889283/"}, + {"account": "Braintree USD", "action": "/rest/v1/donationaction/69409039/", + "card_num_last_four": "3831", "created_at": "2016-06-09T20:30:55", "currency": + "USD", "id": 893914, "import_id": null, "orderdetails": [], "orderrecurrings": + [], "payment_method": "cc", "resource_uri": "/rest/v1/order/893914/", "reverse": + "/rest/v1/order/893914/reverse/", "shipping_address": null, "status": "completed", + "total": "1.00", "total_converted": "1.00", "transactions": ["/rest/v1/transaction/1074845/"], + "updated_at": "2016-06-09T20:30:56", "user": "/rest/v1/user/991564/", "user_detail": + "/rest/v1/orderuserdetail/894042/"}, {"account": "Braintree USD", "action": + "/rest/v1/donationaction/69409056/", "card_num_last_four": "3831", "created_at": + "2016-06-09T20:31:29", "currency": "USD", "id": 893915, "import_id": null, + "orderdetails": [], "orderrecurrings": [], "payment_method": "cc", "resource_uri": + "/rest/v1/order/893915/", "reverse": "/rest/v1/order/893915/reverse/", "shipping_address": + null, "status": "completed", "total": "1.00", "total_converted": "1.00", "transactions": + ["/rest/v1/transaction/1074846/"], "updated_at": "2016-06-09T20:31:31", "user": + "/rest/v1/user/991564/", "user_detail": "/rest/v1/orderuserdetail/894043/"}, + {"account": "Braintree USD", "action": "/rest/v1/donationaction/69409108/", + "card_num_last_four": "3831", "created_at": "2016-06-09T20:33:20", "currency": + "USD", "id": 893917, "import_id": null, "orderdetails": [], "orderrecurrings": + [], "payment_method": "cc", "resource_uri": "/rest/v1/order/893917/", "reverse": + "/rest/v1/order/893917/reverse/", "shipping_address": null, "status": "completed", + "total": "1.00", "total_converted": "1.00", "transactions": ["/rest/v1/transaction/1074848/"], + "updated_at": "2016-06-09T20:33:21", "user": "/rest/v1/user/991564/", "user_detail": + "/rest/v1/orderuserdetail/894045/"}]}' + http_version: + recorded_at: Mon, 20 Apr 2020 14:01:15 GMT +recorded_with: VCR 3.0.3 diff --git a/spec/fixtures/cassettes/rest_user_filter_user_orders_by_email_with_0_donations.yml b/spec/fixtures/cassettes/rest_user_filter_user_orders_by_email_with_0_donations.yml new file mode 100644 index 0000000..82077ee --- /dev/null +++ b/spec/fixtures/cassettes/rest_user_filter_user_orders_by_email_with_0_donations.yml @@ -0,0 +1,42 @@ +--- + http_interactions: + - request: + method: get + uri: https://DummyUsername:DummyPassword@act.sumofus.org/rest/v1/order/?user__email=test1@example.com + body: + encoding: UTF-8 + string: "{}" + headers: + Content-Type: + - application/json + Charset: + - UTF-8 + Authorization: + - Basic U2hpdmFzaGFua2FyOmFjdGlvbmtpdFF3ZXJ0eTEyIw== + response: + status: + code: 200 + message: OK + headers: + Date: + - Mon, 20 Apr 2020 14:10:44 GMT + Content-Type: + - application/json + Transfer-Encoding: + - chunked + Connection: + - keep-alive + Server: + - openresty + Vary: + - Accept, Cookie, Origin + - Accept-Encoding + Cache-Control: + - no-cache + body: + encoding: UTF-8 + string: '{"meta": {"limit": 20, "next": null, "offset": 0, "previous": null, + "total_count": 0}, "objects": []}' + http_version: + recorded_at: Mon, 20 Apr 2020 14:10:44 GMT + recorded_with: VCR 3.0.3 diff --git a/spec/lib/rest/user_spec.rb b/spec/lib/rest/user_spec.rb index 50f6662..2d092fc 100644 --- a/spec/lib/rest/user_spec.rb +++ b/spec/lib/rest/user_spec.rb @@ -47,5 +47,49 @@ end end end + + describe "#filter_user_orders_by" do + context 'with an email that already exists' do + it 'gets back all the orders associated with the user email' do + VCR.use_cassette('rest_user_filter_user_orders_by_email') do + resp = client.filter_user_orders_by(field: "user__email", value: 'test@example.com') + expect(resp.response.code.to_i).to eq 200 + expect(resp.parsed_response["objects"].size).to eql 20 + expect(resp.parsed_response["meta"]["total_count"]).to eql 35 + end + end + + it 'should return 0 records' do + VCR.use_cassette('rest_user_filter_user_orders_by_email_with_0_donations') do + resp = client.filter_user_orders_by(field: "user__email", value: 'test1@example.com') + expect(resp.response.code.to_i).to eq 200 + expect(resp.parsed_response["objects"].size).to eql 0 + expect(resp.parsed_response["meta"]["total_count"]).to eql 0 + end + end + end + + context 'with non existing user' do + it 'gets back all the orders associated with the user email' do + VCR.use_cassette('rest_user_filter_user_orders_by_email') do + resp = client.filter_user_orders_by(field: "user__email", value: 'test@example.com') + expect(resp.response.code.to_i).to eq 200 + expect(resp.parsed_response["objects"].size).to eql 20 + expect(resp.parsed_response["meta"]["total_count"]).to eql 35 + end + end + + it 'should return 0 records' do + VCR.use_cassette('rest_user_filter_user_orders_by_email_with_0_donations') do + resp = client.filter_user_orders_by(field: "user__email", value: 'test1@example.com') + expect(resp.response.code.to_i).to eq 200 + expect(resp.parsed_response["objects"].size).to eql 0 + expect(resp.parsed_response["meta"]["total_count"]).to eql 0 + end + end + end + end end + +