|
11 | 11 | from databases import Database
|
12 | 12 | from jinja2 import Environment, PackageLoader, exceptions
|
13 | 13 | from sendgrid import Mail, SendGridAPIClient
|
14 |
| -from sqlalchemy import asc, func, insert, or_, select |
| 14 | +from sqlalchemy import asc, desc, func, insert, or_, select |
15 | 15 | from sqlalchemy.sql import Select
|
16 | 16 |
|
17 | 17 | from rctab.constants import (
|
@@ -725,9 +725,13 @@ async def get_emails_sent_since(
|
725 | 725 |
|
726 | 726 | emails_by_subscription = []
|
727 | 727 | for key, value in groupby(all_emails_sent, extract_sub_id):
|
728 |
| - name_query = select([subscription_details.c.display_name.label("name")]).where( |
729 |
| - subscription_details.c.subscription_id == key |
| 728 | + name_query = ( |
| 729 | + select([subscription_details.c.display_name.label("name")]) |
| 730 | + .where(subscription_details.c.subscription_id == key) |
| 731 | + .order_by(desc(subscription_details.c.time_created)) |
| 732 | + .limit(1) |
730 | 733 | )
|
| 734 | + |
731 | 735 | name_rows = await database.fetch_all(name_query)
|
732 | 736 | if name_rows:
|
733 | 737 | name = name_rows[0]["name"]
|
@@ -766,9 +770,13 @@ async def get_finance_entries_since(
|
766 | 770 | all_new_entries = [{**row._mapping} for row in rows]
|
767 | 771 | entries_by_subscription = []
|
768 | 772 | for key, value in groupby(all_new_entries, extract_sub_id):
|
769 |
| - name_query = select([subscription_details.c.display_name.label("name")]).where( |
770 |
| - subscription_details.c.subscription_id == key |
| 773 | + name_query = ( |
| 774 | + select([subscription_details.c.display_name.label("name")]) |
| 775 | + .where(subscription_details.c.subscription_id == key) |
| 776 | + .order_by(desc(subscription_details.c.time_created)) |
| 777 | + .limit(1) |
771 | 778 | )
|
| 779 | + |
772 | 780 | name_rows = await database.fetch_all(name_query)
|
773 | 781 | if name_rows:
|
774 | 782 | name = name_rows[0]["name"]
|
|
0 commit comments