diff --git a/lib/client.coffee b/lib/client.coffee index 04fbbc2..c06fd3f 100644 --- a/lib/client.coffee +++ b/lib/client.coffee @@ -3,6 +3,16 @@ Request = require "./request" BASE_URL = "https://financialdatafeed.platform.intuit.com/v1" +zeroBased = (number) -> + ("0" + number).slice(-2) + +# 2015-07-29 +formatDate = (date) -> + year = date.getFullYear() + month = zeroBased date.getMonth() + 1 + day = zeroBased date.getDate() + "#{year}-#{month}-#{day}" + module.exports = class IntuitClient constructor: (@options) -> @@ -36,9 +46,26 @@ module.exports = class IntuitClient return done err if err done err, _.first response.accounts - getAccountTransactions: (userId, accountId, startDate, endDate) -> + # Download last week of transactions unless a date range is specified + # If no endDate is specified, startDate to current is downloaded + getAccountTransactions: (userId, accountId, startDate, endDate, done) -> + oneWeekAgo = -10080 + defaultStartDate = formatDate new Date(new Date().getTime() + oneWeekAgo * 60000) + + if typeof(startDate) is "function" + [startDate, endDate, done] = [defaultStartDate, null, startDate] + else if typeof(endDate) is "function" + [startDate, endDate, done] = [formatDate(startDate), null, endDate] + else + [startDate, endDate, done] = [formatDate(startDate), formatDate(endDate), done] + + url = "/accounts/#{accountId}/transactions?txnStartDate=#{startDate}" + url += "&txnEndDate=#{endDate}" if endDate + @options.userId = userId - @request "get", "/accounts/#{accountId}/transactions", done + @request "get", url, (err, response) -> + return done err if err + done err, response.bankingTransactions updateInstitutionLogin: (userId, institutionId, loginId, credentials, done) -> @options.userId = userId diff --git a/package.json b/package.json index afd3796..d26fa73 100644 --- a/package.json +++ b/package.json @@ -18,7 +18,8 @@ "devDependencies": { "bondjs": "^1.2.3", "mocha": "^2.2.5", - "nock": "^2.9.1" + "nock": "^2.9.1", + "timekeeper": "0.0.5" }, "engines": { "node": "0.12.5", diff --git a/test/api.coffee b/test/api.coffee index 4233d4d..6f86317 100644 --- a/test/api.coffee +++ b/test/api.coffee @@ -1,4 +1,5 @@ assert = require "assert" +timekeeper = require "timekeeper" request = require "request" bond = require "bondjs" request = require "request" @@ -44,7 +45,32 @@ describe "Intuit Client", -> done err describe "getAccountTransactions", -> - it "should return transactions" + describe "default parameters", -> + before -> timekeeper.freeze new Date "2015-07-30" + before -> fixture.load "getAccountTransactions" + after -> timekeeper.reset() + it "should return transactions", (done) -> + intuit.getAccountTransactions "userId", 400107846787, (err, transactions) -> + assert.equal transactions.length, 8 + done err + + describe "Specific start date", -> + before -> timekeeper.freeze new Date "2015-07-30" + before -> fixture.load "getAccountTransactions_startDate" + after -> timekeeper.reset() + it "should return transactions", (done) -> + intuit.getAccountTransactions "userId", 400107846787, new Date("2015-05-15"), (err, transactions) -> + assert.equal transactions.length, 8 + done err + + describe "Specific date range", -> + before -> timekeeper.freeze new Date "2015-07-30" + before -> fixture.load "getAccountTransactions_dateRange" + after -> timekeeper.reset() + it "should return transactions", (done) -> + intuit.getAccountTransactions "userId", 400107846787, new Date("2015-05-15"), new Date("2015-05-22"), (err, transactions) -> + assert.equal transactions.length, 8 + done err describe "updateInstitutionLogin", -> it "should update bank credentials" diff --git a/test/fixtures/getAccountTransactions.json b/test/fixtures/getAccountTransactions.json new file mode 100644 index 0000000..095a57f --- /dev/null +++ b/test/fixtures/getAccountTransactions.json @@ -0,0 +1,159 @@ +[ + { + "scope": "https://financialdatafeed.platform.intuit.com:443", + "method": "GET", + "path": "/v1/accounts/400107846787/transactions?txnStartDate=2015-07-22", + "body": "", + "status": 200, + "response": { + "bankingTransactions": [ + { + "id": 403657885582, + "currencyType": "USD", + "postedDate": 1437634800000, + "payeeName": "CHECKINGD debit 204", + "amount": -7.23, + "pending": false, + "institutionTransactionId": "INTUIT-403657885582", + "userDate": 1437634800000, + "categorization": { + "context": [ + + ], + "common": { + + } + } + }, + { + "id": 403657885585, + "currencyType": "USD", + "postedDate": 1437894000000, + "payeeName": "CHECKINGD credit 207", + "amount": 7.26, + "pending": false, + "institutionTransactionId": "INTUIT-403657885585", + "userDate": 1437894000000, + "categorization": { + "context": [ + + ], + "common": { + + } + } + }, + { + "id": 403657885595, + "currencyType": "USD", + "postedDate": 1437548400000, + "payeeName": "CHECKINGD credit 203", + "amount": 7.22, + "pending": false, + "institutionTransactionId": "INTUIT-403657885595", + "userDate": 1437548400000, + "categorization": { + "context": [ + + ], + "common": { + + } + } + }, + { + "id": 403657885902, + "currencyType": "USD", + "postedDate": 1437807600000, + "payeeName": "CHECKINGD debit 206", + "amount": -7.25, + "pending": false, + "institutionTransactionId": "INTUIT-403657885902", + "userDate": 1437807600000, + "categorization": { + "context": [ + + ], + "common": { + + } + } + }, + { + "id": 403657885905, + "currencyType": "USD", + "postedDate": 1437721200000, + "payeeName": "CHECKINGD credit 205", + "amount": 7.24, + "pending": false, + "institutionTransactionId": "INTUIT-403657885905", + "userDate": 1437721200000, + "categorization": { + "context": [ + + ], + "common": { + + } + } + }, + { + "id": 403663363252, + "currencyType": "USD", + "postedDate": 1437980400000, + "payeeName": "CHECKINGD debit 208", + "amount": -7.27, + "pending": false, + "institutionTransactionId": "INTUIT-403663363252", + "userDate": 1437980400000, + "categorization": { + "context": [ + + ], + "common": { + + } + } + }, + { + "id": 403664091017, + "currencyType": "USD", + "payeeName": "CHECKINGD credit 209", + "amount": 7.28, + "pending": true, + "userDate": 1438153200000, + "categorization": { + "context": [ + + ], + "common": { + + } + } + }, + { + "id": 403664091018, + "currencyType": "USD", + "payeeName": "CHECKINGD debit 210", + "amount": -7.29, + "pending": true, + "userDate": 1438153200000, + "categorization": { + "context": [ + + ], + "common": { + + } + } + } + ]}, + "headers": { + "date": "Tue, 28 Jul 2015 18:32:49 GMT", + "content-type": "text/plain", + "content-length": "0", + "intuit_tid": "gw-1e6def67-b82b-4317-981c-d2e7bed343c8", + "connection": "close" + } + } +] diff --git a/test/fixtures/getAccountTransactions_dateRange.json b/test/fixtures/getAccountTransactions_dateRange.json new file mode 100644 index 0000000..81bec2c --- /dev/null +++ b/test/fixtures/getAccountTransactions_dateRange.json @@ -0,0 +1,159 @@ +[ + { + "scope": "https://financialdatafeed.platform.intuit.com:443", + "method": "GET", + "path": "/v1/accounts/400107846787/transactions?txnStartDate=2015-05-14&txnEndDate=2015-05-21", + "body": "", + "status": 200, + "response": { + "bankingTransactions": [ + { + "id": 403657885582, + "currencyType": "USD", + "postedDate": 1437634800000, + "payeeName": "CHECKINGD debit 204", + "amount": -7.23, + "pending": false, + "institutionTransactionId": "INTUIT-403657885582", + "userDate": 1437634800000, + "categorization": { + "context": [ + + ], + "common": { + + } + } + }, + { + "id": 403657885585, + "currencyType": "USD", + "postedDate": 1437894000000, + "payeeName": "CHECKINGD credit 207", + "amount": 7.26, + "pending": false, + "institutionTransactionId": "INTUIT-403657885585", + "userDate": 1437894000000, + "categorization": { + "context": [ + + ], + "common": { + + } + } + }, + { + "id": 403657885595, + "currencyType": "USD", + "postedDate": 1437548400000, + "payeeName": "CHECKINGD credit 203", + "amount": 7.22, + "pending": false, + "institutionTransactionId": "INTUIT-403657885595", + "userDate": 1437548400000, + "categorization": { + "context": [ + + ], + "common": { + + } + } + }, + { + "id": 403657885902, + "currencyType": "USD", + "postedDate": 1437807600000, + "payeeName": "CHECKINGD debit 206", + "amount": -7.25, + "pending": false, + "institutionTransactionId": "INTUIT-403657885902", + "userDate": 1437807600000, + "categorization": { + "context": [ + + ], + "common": { + + } + } + }, + { + "id": 403657885905, + "currencyType": "USD", + "postedDate": 1437721200000, + "payeeName": "CHECKINGD credit 205", + "amount": 7.24, + "pending": false, + "institutionTransactionId": "INTUIT-403657885905", + "userDate": 1437721200000, + "categorization": { + "context": [ + + ], + "common": { + + } + } + }, + { + "id": 403663363252, + "currencyType": "USD", + "postedDate": 1437980400000, + "payeeName": "CHECKINGD debit 208", + "amount": -7.27, + "pending": false, + "institutionTransactionId": "INTUIT-403663363252", + "userDate": 1437980400000, + "categorization": { + "context": [ + + ], + "common": { + + } + } + }, + { + "id": 403664091017, + "currencyType": "USD", + "payeeName": "CHECKINGD credit 209", + "amount": 7.28, + "pending": true, + "userDate": 1438153200000, + "categorization": { + "context": [ + + ], + "common": { + + } + } + }, + { + "id": 403664091018, + "currencyType": "USD", + "payeeName": "CHECKINGD debit 210", + "amount": -7.29, + "pending": true, + "userDate": 1438153200000, + "categorization": { + "context": [ + + ], + "common": { + + } + } + } + ]}, + "headers": { + "date": "Tue, 28 Jul 2015 18:32:49 GMT", + "content-type": "text/plain", + "content-length": "0", + "intuit_tid": "gw-1e6def67-b82b-4317-981c-d2e7bed343c8", + "connection": "close" + } + } +] diff --git a/test/fixtures/getAccountTransactions_startDate.json b/test/fixtures/getAccountTransactions_startDate.json new file mode 100644 index 0000000..043b0ce --- /dev/null +++ b/test/fixtures/getAccountTransactions_startDate.json @@ -0,0 +1,159 @@ +[ + { + "scope": "https://financialdatafeed.platform.intuit.com:443", + "method": "GET", + "path": "/v1/accounts/400107846787/transactions?txnStartDate=2015-05-14", + "body": "", + "status": 200, + "response": { + "bankingTransactions": [ + { + "id": 403657885582, + "currencyType": "USD", + "postedDate": 1437634800000, + "payeeName": "CHECKINGD debit 204", + "amount": -7.23, + "pending": false, + "institutionTransactionId": "INTUIT-403657885582", + "userDate": 1437634800000, + "categorization": { + "context": [ + + ], + "common": { + + } + } + }, + { + "id": 403657885585, + "currencyType": "USD", + "postedDate": 1437894000000, + "payeeName": "CHECKINGD credit 207", + "amount": 7.26, + "pending": false, + "institutionTransactionId": "INTUIT-403657885585", + "userDate": 1437894000000, + "categorization": { + "context": [ + + ], + "common": { + + } + } + }, + { + "id": 403657885595, + "currencyType": "USD", + "postedDate": 1437548400000, + "payeeName": "CHECKINGD credit 203", + "amount": 7.22, + "pending": false, + "institutionTransactionId": "INTUIT-403657885595", + "userDate": 1437548400000, + "categorization": { + "context": [ + + ], + "common": { + + } + } + }, + { + "id": 403657885902, + "currencyType": "USD", + "postedDate": 1437807600000, + "payeeName": "CHECKINGD debit 206", + "amount": -7.25, + "pending": false, + "institutionTransactionId": "INTUIT-403657885902", + "userDate": 1437807600000, + "categorization": { + "context": [ + + ], + "common": { + + } + } + }, + { + "id": 403657885905, + "currencyType": "USD", + "postedDate": 1437721200000, + "payeeName": "CHECKINGD credit 205", + "amount": 7.24, + "pending": false, + "institutionTransactionId": "INTUIT-403657885905", + "userDate": 1437721200000, + "categorization": { + "context": [ + + ], + "common": { + + } + } + }, + { + "id": 403663363252, + "currencyType": "USD", + "postedDate": 1437980400000, + "payeeName": "CHECKINGD debit 208", + "amount": -7.27, + "pending": false, + "institutionTransactionId": "INTUIT-403663363252", + "userDate": 1437980400000, + "categorization": { + "context": [ + + ], + "common": { + + } + } + }, + { + "id": 403664091017, + "currencyType": "USD", + "payeeName": "CHECKINGD credit 209", + "amount": 7.28, + "pending": true, + "userDate": 1438153200000, + "categorization": { + "context": [ + + ], + "common": { + + } + } + }, + { + "id": 403664091018, + "currencyType": "USD", + "payeeName": "CHECKINGD debit 210", + "amount": -7.29, + "pending": true, + "userDate": 1438153200000, + "categorization": { + "context": [ + + ], + "common": { + + } + } + } + ]}, + "headers": { + "date": "Tue, 28 Jul 2015 18:32:49 GMT", + "content-type": "text/plain", + "content-length": "0", + "intuit_tid": "gw-1e6def67-b82b-4317-981c-d2e7bed343c8", + "connection": "close" + } + } +] diff --git a/test/fixtures/index.coffee b/test/fixtures/index.coffee index 101daab..a1ec632 100644 --- a/test/fixtures/index.coffee +++ b/test/fixtures/index.coffee @@ -56,6 +56,18 @@ fixtures = oauth() load "getAccount" + getAccountTransactions: -> + oauth() + load "getAccountTransactions" + + getAccountTransactions_startDate: -> + oauth() + load "getAccountTransactions_startDate" + + getAccountTransactions_dateRange: -> + oauth() + load "getAccountTransactions_dateRange" + module.exports = record: -> nock.recorder.rec