Skip to content

[PAY-3612] Add discovery endpoint for retrieving email data#10501

Merged
faridsalau merged 5 commits intomainfrom
farid/email-discovery
Nov 22, 2024
Merged

[PAY-3612] Add discovery endpoint for retrieving email data#10501
faridsalau merged 5 commits intomainfrom
farid/email-discovery

Conversation

@faridsalau
Copy link
Contributor

@faridsalau faridsalau commented Nov 21, 2024

Description

This PR adds the necessary endpoint for accessing email data. This endpoints will be used to retrieve keys for decryption and to retrieve a complete list of emails that are associated with a specific userId

Sample response

{
        "decryption_key": "ABC123XYZ456DefBase64EncodedKey==",
        "sales": [
            {
                "title": "TEST",
                "link": "/reed/test",
                "purchased by": "name 40F0",
                "date": "2024-11-21T21:40:07.943031",
                "sale price": 2.0,
                "network fee": -0.2,
                "pay extra": 0.0,
                "total": 1.8,
                "country": null,
                "encrypted_email": "Base64EncodedEncryptedEmail1Content=="
            },
            {
                "title": "BuyMePlease",
                "link": "/reed/buymeplease",
                "purchased by": "name E769",
                "date": "2024-11-21T21:40:02.697436",
                "sale price": 2.0,
                "network fee": -0.2,
                "pay extra": 0.0,
                "total": 1.8,
                "country": null,
                "encrypted_email": "Base64EncodedEncryptedEmail2Content=="
            }
        ]
    }

How Has This Been Tested?

Locally via running [/v1/users/<userId>/sales/download?json=true](http://audius-protocol-discovery-provider-1/v1/users/15YGgX/sales/download?json=true)

I commented out the auth stuff in the endpoint to make testing easier locally

Sample queries for local encryption db population

-- First, let's insert the encryption key for the primary user
INSERT INTO email_encryption_keys 
(primary_user_id, encrypted_key)
VALUES 
(49174785, 'ABC123XYZ456DefBase64EncodedKey==');  -- Example base64 encoded key

-- Next, let's insert some encrypted emails where this user has primary access
INSERT INTO encrypted_emails 
(email_owner_user_id, primary_user_id, encrypted_email)
VALUES 
(867177347, 49174785, 'Base64EncodedEncryptedEmail1Content=='),
(299108755, 49174785, 'Base64EncodedEncryptedEmail2Content==');

-- Finally, let's give delegated access to some other users
INSERT INTO email_access_keys 
(primary_user_id, delegated_user_id, encrypted_key)
VALUES 
(49174785, 24601, 'DelegatedKey1Base64Encoded=='),
(49174785, 98765, 'DelegatedKey2Base64Encoded=='),
(49174785, 45678, 'DelegatedKey3Base64Encoded==');

Please describe the tests that you ran to verify your changes. Provide repro instructions & any configuration.

@changeset-bot
Copy link

changeset-bot bot commented Nov 21, 2024

⚠️ No Changeset found

Latest commit: cacda8a

Merging this PR will not cause a version bump for any packages. If these changes should not result in a new version, you're good to go. If these changes should result in a version bump, you need to add a changeset.

This PR includes no changesets

When changesets are added to this PR, you'll see the packages that this PR includes changesets for and the associated semver types

Click here to learn what changesets are, and how to add one.

Click here if you're a maintainer who wants to add a changeset to this PR

Copy link
Member

@raymondjacobson raymondjacobson left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't even know if we need a separate API endpoint for this - kind of a nice to have. The CSV comes from its own endpoint and you will probably want to re-use the same queries that you've written in this PR (and break them out into some src/queries/x.py file)

https://github.com/AudiusProject/audius-protocol/blob/main/packages/discovery-provider/src/api/v1/users.py#L2619

decoded_id = decode_with_abort(id, ns)

db = get_db_read_replica()
with db.scoped_session() as session:
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

this is a really simple query and I think probably ok to be untested, but we typically do put queries inside src/queries as their own file so they can get their own tests, .e.g

https://github.com/AudiusProject/audius-protocol/blob/main/packages/discovery-provider/src/queries/get_genre_metrics.py

https://github.com/AudiusProject/audius-protocol/blob/main/packages/discovery-provider/integration_tests/queries/test_get_genre_metrics.py



sales_download_parser = current_user_parser.copy()
sales_download_parser.add_argument(
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

added for backward compatibility sake while we transition to generating csv on FE

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

makes sense

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

probably good to keep this endpoint in general and just let our frontend be nifty. others may want this

@faridsalau faridsalau changed the title [PAY-3612] Add discovery endpoints for retrieving email data [PAY-3612] Add discovery endpoint for retrieving email data Nov 21, 2024
def format_sale_for_csv(result, seller_handle, seller_user_id, include_email=False):
"""Format a sale result into a CSV-friendly dictionary format."""
# Convert datetime to ISO format string
created_at = result.created_at.isoformat() if result.created_at else None
Copy link
Contributor Author

@faridsalau faridsalau Nov 21, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

needed to convert for JSON serialization sakes. seems it was already an iso format though



sales_download_parser = current_user_parser.copy()
sales_download_parser.add_argument(
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

makes sense



sales_download_parser = current_user_parser.copy()
sales_download_parser.add_argument(
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

probably good to keep this endpoint in general and just let our frontend be nifty. others may want this

Copy link
Member

@raymondjacobson raymondjacobson left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

nice

@faridsalau faridsalau merged commit b444ad6 into main Nov 22, 2024
@faridsalau faridsalau deleted the farid/email-discovery branch November 22, 2024 21:27
audius-infra pushed a commit that referenced this pull request Nov 25, 2024
[ccb7082] [PAY-3607] Migrate explore tracks to sdk (#10509) Reed
[b444ad6] [PAY-3612] Add discovery endpoint for retrieving email data (#10501) Farid Salau
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants