In this challenge, use the HTTP GET method to retrieve information from a database of Card Transactions records for users. Query https://jsonmock.hackerrank.com/api/transactions/search?txnType=txn where txn is the transaction type of the record passed to the function. This will return all records that have the given transaction type. The query response is paginated and can be further accessed by appending to the query string &page=num where num is the page number.
The query response from the API is a JSON response with the following five fields:
The data field in the response contains a list of the transaction records, with each transaction record following the below-described schema:
Given the locationId, locationld, and the provided transaction type, txnType, return a 2d array containing the total amount transacted by each user at the given locationld.
The array will be in the format [[1, 1200], [2, 2333] ] where the item at index 0 in the inner array denotes the id of the user and item at index 1 denotes the total amount transacted (either debit or credit based on input txnType). The items in the outer array should be sorted by the ids of the user. Note that the search is not case sensitive.
getTransactions has the following parameter(s):
int locationld: the id of the location by which record will be fetched, to be matched with the property location.id
string txnType: the transaction type to filter the records on
Returns:
[int[]]: a 2d array containing the total amount transacted by each user at the given location id. If no records are found matching the filter criteria, it should return [[-1, -1]].
Note: The total amount should be rounded off to 2 places after the decimal. It can also be returned as a string.
Input Format For Custom Testing
The first line contains an integer, locationld, the id of the location of interest.
The second line contains a string, txn Type, the transaction type to filter for
Sample Input For Custom Testing
STDIN Function
----- --------
1 -> locationId = 1
debit -> txType = 'debit'
Sample Output
1 13200.08
2 16745.72
3 18859.77
4 10360.32
Given txn Type = debit the query is https://jsonmock hackerrank.com/api/transactions/search?txnType=debit and the response includes:
[
{ id: 1,
userId: 1,
userName: 'John Oliver',
timestamp: 1549536882071,
txnType: 'debit',
amount: '$1,670.57',
location: { id: 7,
address: '770, Deepends, Stockton Street',
city: 'Ripley',
zipCode: 44139
},
ip: '212.215.115.165'
}...
]
Given the response from the API for txn Type = 'debit', filter the records. After filtering the data to find all the records which belong to location.id = 1, find the total amount spent by each user. The final values for user 1 is 11590.28, for user 2 is 17410.38 and so on. Finally create the 2d array containing the answer [[1, 13200.08], [2, 16745.72], [3, 18859.77], [4, 10360.32]] which is returned.