Skip to content

Commit

Permalink
Add DN endpoint for audio transactions count (#4451)
Browse files Browse the repository at this point in the history
* Add transactions count endpoint

* fixup! Add transactions count endpoint

* forgot a generated file

* Add args for count endpoint
  • Loading branch information
dharit-tan committed Dec 13, 2022
1 parent b803064 commit bc53abc
Show file tree
Hide file tree
Showing 6 changed files with 205 additions and 3 deletions.
46 changes: 44 additions & 2 deletions discovery-provider/src/api/v1/transactions.py
Original file line number Diff line number Diff line change
@@ -1,14 +1,18 @@
import logging

from flask_restx import Namespace, Resource, fields
from flask_restx import Namespace, Resource, fields, reqparse
from src.api.v1.helpers import (
DescriptiveArgument,
abort_bad_request_param,
extend_transaction_details,
make_full_response,
pagination_parser,
success_response,
)
from src.queries.get_audio_transactions_history import get_audio_transactions_history
from src.queries.get_audio_transactions_history import (
get_audio_transactions_history,
get_audio_transactions_history_count,
)
from src.queries.query_helpers import SortDirection, TransactionSortMethod
from src.utils.auth_middleware import MESSAGE_HEADER, SIGNATURE_HEADER, auth_middleware

Expand Down Expand Up @@ -81,3 +85,41 @@ def get(self, authed_user_id=None):
}
)
return success_response(list(map(extend_transaction_details, transactions)))


transaction_history_count_response = make_full_response(
"transaction_history_count_response", full_ns, fields.Integer()
)

transaction_history_count_parser = reqparse.RequestParser(
argument_class=DescriptiveArgument
)
transaction_history_count_parser.add_argument(
MESSAGE_HEADER,
required=True,
description="The data that was signed by the user for signature recovery",
location="headers",
)
transaction_history_count_parser.add_argument(
SIGNATURE_HEADER,
required=True,
description="The signature of data, used for signature recovery",
location="headers",
)


@full_ns.route("/count")
class GetTransactionHistoryCount(Resource):
@full_ns.doc(
id="""Get Audio Transaction History Count""",
description="""Gets the count of the user's $AUDIO transaction history within the App""",
)
@full_ns.expect(transaction_history_count_parser)
@full_ns.marshal_with(transaction_history_count_response)
@auth_middleware()
def get(self, authed_user_id=None):
if authed_user_id is None:
abort_bad_request_param(None, full_ns)
transactions_count = get_audio_transactions_history_count(authed_user_id)
response = success_response(transactions_count)
return response
34 changes: 33 additions & 1 deletion discovery-provider/src/queries/get_audio_transactions_history.py
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,35 @@ class GetAudioTransactionsArgs(TypedDict):
offset: int


# SELECT count(*)
# FROM users
# JOIN user_bank_accounts ON user_bank_accounts.ethereum_address = users.wallet
# JOIN audio_transactions_history ON audio_transactions_history.user_bank = user_bank_accounts.bank_account
# WHERE users.user_id = <user_id> AND users.is_current = TRUE


def _get_audio_transactions_history_count(session: Session, user_id: int):
query: Query = (
session.query(AudioTransactionsHistory)
.select_from(User)
.filter(User.user_id == user_id, User.is_current == True)
.join(UserBankAccount, UserBankAccount.ethereum_address == User.wallet)
.join(
AudioTransactionsHistory,
AudioTransactionsHistory.user_bank == UserBankAccount.bank_account,
)
)
count: int = query.count()
return count


def get_audio_transactions_history_count(user_id: int):
db = get_db_read_replica()
with db.scoped_session() as session:
count = _get_audio_transactions_history_count(session, user_id)
return count


# SELECT audio_transactions_history.*
# FROM users
# JOIN user_bank_accounts ON user_bank_accounts.ethereum_address = users.wallet
Expand All @@ -51,7 +80,10 @@ def _get_audio_transactions_history(session: Session, args: GetAudioTransactions
if sort_method == TransactionSortMethod.date:
query = query.order_by(sort_fn(AudioTransactionsHistory.transaction_created_at))
elif sort_method == TransactionSortMethod.transaction_type:
query = query.order_by(sort_fn(AudioTransactionsHistory.transaction_type))
query = query.order_by(
sort_fn(AudioTransactionsHistory.transaction_type),
desc(AudioTransactionsHistory.transaction_created_at),
)
query = paginate_query(query)
results: List[AudioTransactionsHistory] = query.all()
return results
Expand Down
1 change: 1 addition & 0 deletions libs/src/sdk/api/generated/full/.openapi-generator/FILES
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,7 @@ models/TrackId.ts
models/TrackRepostsResponseFull.ts
models/TrackSegment.ts
models/TransactionDetails.ts
models/TransactionHistoryCountResponse.ts
models/TransactionHistoryResponse.ts
models/TrendingIdsResponse.ts
models/TrendingTimesIds.ts
Expand Down
46 changes: 46 additions & 0 deletions libs/src/sdk/api/generated/full/apis/TransactionsApi.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,9 @@

import * as runtime from '../runtime';
import {
TransactionHistoryCountResponse,
TransactionHistoryCountResponseFromJSON,
TransactionHistoryCountResponseToJSON,
TransactionHistoryResponse,
TransactionHistoryResponseFromJSON,
TransactionHistoryResponseToJSON,
Expand Down Expand Up @@ -48,6 +51,17 @@ export interface GetAudioTransactionHistoryRequest {
sortDirection?: GetAudioTransactionHistorySortDirectionEnum;
}

export interface GetAudioTransactionHistoryCountRequest {
/**
* The data that was signed by the user for signature recovery
*/
encodedDataMessage: string;
/**
* The signature of data, used for signature recovery
*/
encodedDataSignature: string;
}

/**
*
*/
Expand Down Expand Up @@ -101,6 +115,38 @@ export class TransactionsApi extends runtime.BaseAPI {
}) as Promise<NonNullable<TransactionHistoryResponse["data"]>>;
}

/**
* Gets the count of the user\'s $AUDIO transaction history within the App
*/
async getAudioTransactionHistoryCount(requestParameters: GetAudioTransactionHistoryCountRequest): Promise<NonNullable<TransactionHistoryCountResponse["data"]>> {
if (requestParameters.encodedDataMessage === null || requestParameters.encodedDataMessage === undefined) {
throw new runtime.RequiredError('encodedDataMessage','Required parameter requestParameters.encodedDataMessage was null or undefined when calling getAudioTransactionHistoryCount.');
}

if (requestParameters.encodedDataSignature === null || requestParameters.encodedDataSignature === undefined) {
throw new runtime.RequiredError('encodedDataSignature','Required parameter requestParameters.encodedDataSignature was null or undefined when calling getAudioTransactionHistoryCount.');
}

const queryParameters: any = {};

const headerParameters: runtime.HTTPHeaders = {};

if (requestParameters.encodedDataMessage !== undefined && requestParameters.encodedDataMessage !== null) {
headerParameters['Encoded-Data-Message'] = String(requestParameters.encodedDataMessage);
}

if (requestParameters.encodedDataSignature !== undefined && requestParameters.encodedDataSignature !== null) {
headerParameters['Encoded-Data-Signature'] = String(requestParameters.encodedDataSignature);
}

return this.request({
path: `/transactions/count`,
method: 'GET',
headers: headerParameters,
query: queryParameters,
}) as Promise<NonNullable<TransactionHistoryCountResponse["data"]>>;
}

}

/**
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,80 @@
// @ts-nocheck
/* tslint:disable */
/* eslint-disable */
/**
* API
* No description provided (generated by Openapi Generator https://github.com/openapitools/openapi-generator)
*
* The version of the OpenAPI document: 1.0
*
*
* NOTE: This class is auto generated by OpenAPI Generator (https://openapi-generator.tech).
* https://openapi-generator.tech
* Do not edit the class manually.
*/

import {
VersionMetadata,
VersionMetadataFromJSON,
VersionMetadataFromJSONTyped,
VersionMetadataToJSON,
} from './VersionMetadata';

/**
*
* @export
* @interface TransactionHistoryCountResponse
*/
export interface TransactionHistoryCountResponse
{
/**
*
* @type {number}
* @memberof TransactionHistoryCountResponse
*/
latest_chain_block: number;
/**
*
* @type {number}
* @memberof TransactionHistoryCountResponse
*/
latest_indexed_block: number;
/**
*
* @type {number}
* @memberof TransactionHistoryCountResponse
*/
latest_chain_slot_plays: number;
/**
*
* @type {number}
* @memberof TransactionHistoryCountResponse
*/
latest_indexed_slot_plays: number;
/**
*
* @type {string}
* @memberof TransactionHistoryCountResponse
*/
signature: string;
/**
*
* @type {string}
* @memberof TransactionHistoryCountResponse
*/
timestamp: string;
/**
*
* @type {VersionMetadata}
* @memberof TransactionHistoryCountResponse
*/
version: VersionMetadata;
/**
*
* @type {number}
* @memberof TransactionHistoryCountResponse
*/
data?: number;
}


1 change: 1 addition & 0 deletions libs/src/sdk/api/generated/full/models/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,7 @@ export * from './TrackId';
export * from './TrackRepostsResponseFull';
export * from './TrackSegment';
export * from './TransactionDetails';
export * from './TransactionHistoryCountResponse';
export * from './TransactionHistoryResponse';
export * from './TrendingIdsResponse';
export * from './TrendingTimesIds';
Expand Down

0 comments on commit bc53abc

Please sign in to comment.