GET /api/aliases/:alias_id/contacts
This commit is contained in:
parent
bfd729b889
commit
d7cb5ed26e
36
README.md
36
README.md
|
@ -913,6 +913,42 @@ Input:
|
|||
Output:
|
||||
If success, return 200
|
||||
|
||||
#### GET /api/aliases/:alias_id/contacts
|
||||
|
||||
Get contacts for a given alias.
|
||||
|
||||
Input:
|
||||
- `Authentication` header that contains the api key
|
||||
- `alias_id`: the alias id, passed in url.
|
||||
- `page_id` used in request query (`?page_id=0`). The endpoint returns maximum 20 contacts for each page. `page_id` starts at 0.
|
||||
|
||||
Output:
|
||||
If success, 200 with the list of contacts, for example:
|
||||
|
||||
```json
|
||||
{
|
||||
"contacts": [
|
||||
{
|
||||
"contact": "marketing@example.com",
|
||||
"creation_date": "2020-02-21 11:35:00+00:00",
|
||||
"creation_timestamp": 1582284900,
|
||||
"last_email_sent_date": null,
|
||||
"last_email_sent_timestamp": null,
|
||||
"reverse_alias": "marketing at example.com <reply+bzvpazcdedcgcpztehxzgjgzmxskqa@sl.co>"
|
||||
},
|
||||
{
|
||||
"contact": "newsletter@example.com",
|
||||
"creation_date": "2020-02-21 11:35:00+00:00",
|
||||
"creation_timestamp": 1582284900,
|
||||
"last_email_sent_date": "2020-02-21 11:35:00+00:00",,
|
||||
"last_email_sent_timestamp": 1582284900,
|
||||
"reverse_alias": "newsletter at example.com <reply+bzvpazcdedcgcpztehxzgjgzmxskqa@sl.co>"
|
||||
}
|
||||
]
|
||||
}
|
||||
```
|
||||
|
||||
Please note that last_email_sent_timestamp and last_email_sent_date can be null.
|
||||
|
||||
### Database migration
|
||||
|
||||
|
|
|
@ -3,10 +3,11 @@ from flask import jsonify, request
|
|||
from flask_cors import cross_origin
|
||||
|
||||
from app.api.base import api_bp, verify_api_key
|
||||
from app.config import PAGE_LIMIT
|
||||
from app.dashboard.views.alias_log import get_alias_log
|
||||
from app.dashboard.views.index import get_alias_info, AliasInfo
|
||||
from app.extensions import db
|
||||
from app.models import GenEmail
|
||||
from app.models import GenEmail, ForwardEmail, ForwardEmailLog
|
||||
|
||||
|
||||
@api_bp.route("/aliases")
|
||||
|
@ -157,7 +158,7 @@ def get_alias_activities(alias_id):
|
|||
|
||||
activities.append(activity)
|
||||
|
||||
return (jsonify(activities=activities), 200)
|
||||
return jsonify(activities=activities), 200
|
||||
|
||||
|
||||
@api_bp.route("/aliases/<int:alias_id>", methods=["PUT"])
|
||||
|
@ -189,3 +190,71 @@ def update_alias(alias_id):
|
|||
db.session.commit()
|
||||
|
||||
return jsonify(note=new_note), 200
|
||||
|
||||
|
||||
def serialize_forward_email(fe: ForwardEmail) -> dict:
|
||||
|
||||
res = {
|
||||
"creation_date": fe.created_at.format(),
|
||||
"creation_timestamp": fe.created_at.timestamp,
|
||||
"last_email_sent_date": None,
|
||||
"last_email_sent_timestamp": None,
|
||||
"contact": fe.website_from or fe.website_email,
|
||||
"reverse_alias": fe.website_send_to(),
|
||||
}
|
||||
|
||||
fel: ForwardEmailLog = fe.last_reply()
|
||||
if fel:
|
||||
res["last_email_sent_date"] = fel.created_at.format()
|
||||
res["last_email_sent_timestamp"] = fel.created_at.timestamp
|
||||
|
||||
return res
|
||||
|
||||
|
||||
def get_alias_contacts(gen_email, page_id: int) -> [dict]:
|
||||
q = (
|
||||
ForwardEmail.query.filter_by(gen_email_id=gen_email.id)
|
||||
.order_by(ForwardEmail.id.desc())
|
||||
.limit(PAGE_LIMIT)
|
||||
.offset(page_id * PAGE_LIMIT)
|
||||
)
|
||||
|
||||
res = []
|
||||
for fe in q.all():
|
||||
res.append(serialize_forward_email(fe))
|
||||
|
||||
return res
|
||||
|
||||
|
||||
@api_bp.route("/aliases/<int:alias_id>/contacts")
|
||||
@cross_origin()
|
||||
@verify_api_key
|
||||
def get_alias_contacts_route(alias_id):
|
||||
"""
|
||||
Get alias contacts
|
||||
Input:
|
||||
page_id: in query
|
||||
Output:
|
||||
- contacts: list of contacts:
|
||||
- creation_date
|
||||
- creation_timestamp
|
||||
- last_email_sent_date
|
||||
- last_email_sent_timestamp
|
||||
- contact
|
||||
- reverse_alias
|
||||
|
||||
"""
|
||||
user = g.user
|
||||
try:
|
||||
page_id = int(request.args.get("page_id"))
|
||||
except (ValueError, TypeError):
|
||||
return jsonify(error="page_id must be provided in request query"), 400
|
||||
|
||||
gen_email: GenEmail = GenEmail.get(alias_id)
|
||||
|
||||
if gen_email.user_id != user.id:
|
||||
return jsonify(error="Forbidden"), 403
|
||||
|
||||
contacts = get_alias_contacts(gen_email, page_id)
|
||||
|
||||
return jsonify(contacts=contacts), 200
|
||||
|
|
|
@ -696,7 +696,7 @@ class ClientUser(db.Model, ModelMixin):
|
|||
|
||||
class ForwardEmail(db.Model, ModelMixin):
|
||||
"""
|
||||
Emails that are forwarded through SL: email that is sent by website to user via SL alias
|
||||
Store configuration of sender (website-email) and alias.
|
||||
"""
|
||||
|
||||
__table_args__ = (
|
||||
|
|
|
@ -183,3 +183,51 @@ def test_update_alias(flask_client):
|
|||
|
||||
assert r.status_code == 200
|
||||
assert r.json == {"note": "test note"}
|
||||
|
||||
|
||||
def test_alias_contacts(flask_client):
|
||||
user = User.create(
|
||||
email="a@b.c", password="password", name="Test User", activated=True
|
||||
)
|
||||
db.session.commit()
|
||||
|
||||
# create api_key
|
||||
api_key = ApiKey.create(user.id, "for test")
|
||||
db.session.commit()
|
||||
|
||||
gen_email = GenEmail.create_new_random(user)
|
||||
db.session.commit()
|
||||
|
||||
# create some alias log
|
||||
for i in range(PAGE_LIMIT + 1):
|
||||
forward_email = ForwardEmail.create(
|
||||
website_email=f"marketing-{i}@example.com",
|
||||
reply_email=f"reply-{i}@a.b",
|
||||
gen_email_id=gen_email.id,
|
||||
)
|
||||
db.session.commit()
|
||||
|
||||
ForwardEmailLog.create(forward_id=forward_email.id, is_reply=True)
|
||||
db.session.commit()
|
||||
|
||||
r = flask_client.get(
|
||||
url_for("api.get_alias_contacts_route", alias_id=gen_email.id, page_id=0),
|
||||
headers={"Authentication": api_key.code},
|
||||
)
|
||||
|
||||
assert r.status_code == 200
|
||||
assert len(r.json["contacts"]) == PAGE_LIMIT
|
||||
for ac in r.json["contacts"]:
|
||||
assert ac["creation_date"]
|
||||
assert ac["creation_timestamp"]
|
||||
assert ac["last_email_sent_date"]
|
||||
assert ac["last_email_sent_timestamp"]
|
||||
assert ac["contact"]
|
||||
assert ac["reverse_alias"]
|
||||
|
||||
# second page, should return 1 result only
|
||||
r = flask_client.get(
|
||||
url_for("api.get_alias_contacts_route", alias_id=gen_email.id, page_id=1),
|
||||
headers={"Authentication": api_key.code},
|
||||
)
|
||||
assert len(r.json["contacts"]) == 1
|
||||
|
|
Loading…
Reference in New Issue