Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: added interaface for processing product coupon assignment sheets #3406

Open
wants to merge 5 commits into
base: master
Choose a base branch
from

Conversation

Anas12091101
Copy link
Contributor

@Anas12091101 Anas12091101 commented Feb 19, 2025

What are the relevant tickets?

https://github.com/mitodl/hq/issues/6582

Description (What does it do?)

This PR adds the interface for processing product coupon assignment sheets in ecommerce admin. The UI will only be accessible to people having permissions to add and update ProductCouponAssignment model.

Screenshots (if appropriate):

Users with only adding and deleting a coupon permissions

Screenshot 2025-02-19 at 4 58 47 PM

Screenshot 2025-02-19 
at 5 00 01 PM

Screenshot 2025-02-19 at 5 01 26 PM

Users with adding and changing ProductCouponAssignment model permissions

Screenshot 2025-02-19 at 5 04 07 PM Screenshot 2025-02-19 at 5 05 08 PM Screenshot 2025-02-19 at 5 04 45 PM

Form Validations when using ID

Screenshot 2025-02-19 at 5 06 42 PM Screenshot 2025-02-19 at 5 07 04 PM

Form Validations when using Title

Screenshot 2025-02-19 at 5 08 37 PM Screenshot 2025-02-19 at 5 09 20 PM Screenshot 2025-02-19 at 5 09 35 PM

Success response from API

Screenshot 2025-02-19 at 5 10 30 PM

Error response from API

Screenshot 2025-02-19 at 5 11 28 PM

How can this be tested?

  • Create some test coupons on http://localhost:8053/ecommerce/admin/coupons/. Make sure you select only one product at the time of coupon creation to automatically create CouponEligibility object which is required for processing sheets.
  • Follow https://pygsheets.readthedocs.io/en/stable/authorization.html to get the Service Account Key. Set this key in DRIVE_SERVICE_ACCOUNT_CREDS environment variable
  • Grant your service account owner permissions
  • Now, create a Shared Drive folder in your google drive.
  • Create a folder inside the shared drive. I named it Sheets xPRO when creating one for local testing.
  • In the Sheets xPRO folder, create a spread sheet with name prefixed by Enrollment Codes - . See this shared drive I created when testing for reference. I would still need to give you access so feel free to request one.
  • In the .env file add the following variables:
    SHEETS_ADMIN_EMAILS = "[email protected], [email protected]" # Your main and service account emails
    DRIVE_OUTPUT_FOLDER_ID=1vz6ZepeCX4lGMrFQmy8PB3Kk6IDGbfQ2 # Id of the folder Sheets xPRO (Can be found in URL)
    DRIVE_SHARED_ID=0AIbxGdFEImI3Uk9PVA # Id of the shared drive (Can be found in URL)
    
  • Give editor access to service account in both spreadsheet and parent folder(Sheets xPRO in our case)
  • Your sheets setup is complete and ready to use.
  • On a private window login with a test account with no permissions assigned
  • From the test account go to http://localhost:8053/ecommerce/admin/. You should get a 403
  • Grant the test account permissions to add and update ProductCouponAssignment.
  • Now reload the ecommerce/admin page. Click the Process Coupon AssignmentSheet link.
  • In the spreadsheet you created in the above steps, add the following row:
| Coupon Code | Email (Assignee) | Status   | Status Date         | URL                | Redeemed |
|-------------|-----------------|----------|----------------------|---------------------|----------|
| {Code you created above} | [email protected] | enrolled | 2/18/2025 6:50:18 | [xpro.mit.e](https://xpro.mit.e) | 0 |

  • Copy the spreadsheet ID from the spreadsheet URL and paste it in the text box at /ecommerce/admin/process-coupon-assignment-sheets/
  • Click the Process Coupon Sheet button
  • You should be able to see a success page (see the screenshot above)
  • You should also receive an email:
Screenshot 2025-02-19 at 7 46 08 PM
  • Clicking the email link should open the checkout page with the coupon applied.
  • For processing the sheet with title, add another row with the code (created for a different product) in the sheet
  • Select the Use Title Sheet radio button and enter the title of the sheet in the text box
  • You should be able to see a success page (see the screenshot above). You should receive an enrollment email.
  • Clicking the email link should open the checkout page with the coupon applied.
  • In the form, inputting an invalid ID or title should result in the error page as shown in the screenshots above.

Additional Context

@Anas12091101 Anas12091101 changed the title Anas/add process coupon sheet interface feat: added interaface for processing product coupon assignment sheets Feb 19, 2025
)

except CouponAssignmentError as e:
return Response({"error": str(e)}, status=status.HTTP_400_BAD_REQUEST)

Check warning

Code scanning / CodeQL

Information exposure through an exception Medium

Stack trace information
flows to this location and may be exposed to an external user.

Copilot Autofix AI 5 days ago

To fix the problem, we need to ensure that detailed error messages are logged on the server side, and a generic error message is returned to the user. This can be achieved by modifying the exception handling block to log the exception message and return a generic error message.

  1. Import the logging module if not already imported.
  2. Log the detailed error message using the log object.
  3. Return a generic error message to the user.
Suggested changeset 1
sheets/views.py

Autofix patch

Autofix patch
Run the following command in your local git repository to apply this patch
cat << 'EOF' | git apply
diff --git a/sheets/views.py b/sheets/views.py
--- a/sheets/views.py
+++ b/sheets/views.py
@@ -203,3 +203,4 @@
         except CouponAssignmentError as e:
-            return Response({"error": str(e)}, status=status.HTTP_400_BAD_REQUEST)
+            log.error(f"CouponAssignmentError: {str(e)}")
+            return Response({"error": "An error occurred while processing the coupon sheet."}, status=status.HTTP_400_BAD_REQUEST)
 
EOF
@@ -203,3 +203,4 @@
except CouponAssignmentError as e:
return Response({"error": str(e)}, status=status.HTTP_400_BAD_REQUEST)
log.error(f"CouponAssignmentError: {str(e)}")
return Response({"error": "An error occurred while processing the coupon sheet."}, status=status.HTTP_400_BAD_REQUEST)

Copilot is powered by AI and may make mistakes. Always verify output.
Positive Feedback
Negative Feedback

Provide additional feedback

Please help us improve GitHub Copilot by sharing more details about this comment.

Please select one or more of the options
@Anas12091101 Anas12091101 force-pushed the anas/add-process-coupon-sheet-interface branch from cb7910c to bcd9eac Compare February 19, 2025 17:05
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants