feat: customer e-mails in the Site's look, and an order confirmation
All checks were successful
Build and deploy / Validate source (push) Successful in 49s
Build and deploy / Integration suite on a real stack (push) Successful in 3m23s
Build and deploy / Secret scan and release gate (push) Successful in 10s
Build and deploy / Publish images (push) Successful in 1m17s
All checks were successful
Build and deploy / Validate source (push) Successful in 49s
Build and deploy / Integration suite on a real stack (push) Successful in 3m23s
Build and deploy / Secret scan and release gate (push) Successful in 10s
Build and deploy / Publish images (push) Successful in 1m17s
The account e-mails (signup and e-mail change codes, new password, an address that already has an account, the changed-e-mail notice) gain an HTML version beside their plain text: the DROPSTAR header, a white card, orange buttons, the customer greeted by first name. Built with tables and inline styles only, so Gmail, Outlook and phone apps show it the same. A paid order now also e-mails the customer "Recebemos seu pedido #N": the items, delivery and total, the stages to come, and a link to the order in the account. Not for an operator's test order. A code's or a recovery link's HTML is erased with its text once sent. The team's alerts stay plain text. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
@@ -16,7 +16,7 @@ from ..core.auth import (DUMMY_PASSWORD_HASH, audit, client_ip, new_session, own
|
||||
password_matches, rate_limit, session_row, throttle, transfer_guest)
|
||||
from ..core.models import (ArtworkSubmission, EmailChange, EmailCode, EmailResend, ForgotPassword, Login,
|
||||
PasswordChange, PasswordReset, ProfileUpdate, Register, SignupCode, SignupResend)
|
||||
from .. import mail
|
||||
from .. import mail, mail_html
|
||||
from ..runtime import ENVIRONMENT, PUBLIC_ORIGIN, STATES, file_rows, order_code, owned_order, storage
|
||||
from ..scanning import require_clean
|
||||
|
||||
@@ -37,25 +37,42 @@ CODE_SENDS = 5
|
||||
RESEND_SECONDS = 60
|
||||
|
||||
|
||||
def signup_code(c, signup_id, email, exists):
|
||||
def signup_code(c, signup_id, email, name):
|
||||
"""A fresh code for a signup (or the notice for an address that already has
|
||||
an account), queued by e-mail. Returns the code's hash, None for the notice."""
|
||||
if exists:
|
||||
an account, greeting that account's owner), queued by e-mail. Returns the
|
||||
code's hash, None for the notice."""
|
||||
existing = c.execute('SELECT profile FROM dtf_local.accounts WHERE email=%s', (email,)).fetchone()
|
||||
if existing:
|
||||
owner = existing['profile'].get('name')
|
||||
login = f'{PUBLIC_ORIGIN}/conta/entrar'
|
||||
mail.queue(c, 'account', email, 'Você já tem conta · DTF Dropstar',
|
||||
'Olá!\n\nAlguém tentou criar uma conta no DTF Dropstar com este e-mail, mas ele já tem conta.\n\n'
|
||||
f'Para entrar: {PUBLIC_ORIGIN}/conta/entrar\n'
|
||||
f'{mail_html.greeting(owner)}\n\nAlguém tentou criar uma conta no DTF Dropstar com este e-mail, '
|
||||
'mas ele já tem conta.\n\n'
|
||||
f'Para entrar: {login}\n'
|
||||
'Esqueceu a senha? Na mesma página, clique em “Esqueci minha senha”.\n\n'
|
||||
'Se não foi você, ignore este e-mail: nada muda na sua conta.')
|
||||
'Se não foi você, ignore este e-mail: nada muda na sua conta.',
|
||||
mail_html.account_exists(owner, login))
|
||||
return None
|
||||
return send_code(c, signup_id, email, 'criar a conta no DTF Dropstar')
|
||||
return send_code(c, signup_id, email, 'signup', name)
|
||||
|
||||
|
||||
def send_code(c, key, email, purpose):
|
||||
"""A fresh 6-digit code by e-mail for `purpose`; returns its hash."""
|
||||
# What a code is for: the text, the e-mail's heading, and what stays the same
|
||||
# if it was not the customer who asked.
|
||||
CODE_PURPOSES = {
|
||||
'signup': ('criar a sua conta no DTF Dropstar', 'Confirme seu e-mail', 'nenhuma conta é criada sem ele.'),
|
||||
'email': ('trocar o e-mail da sua conta no DTF Dropstar', 'Confirme seu novo e-mail',
|
||||
'o e-mail da conta continua o mesmo.'),
|
||||
}
|
||||
|
||||
|
||||
def send_code(c, key, email, purpose, name=''):
|
||||
"""A fresh 6-digit code by e-mail for `purpose` (CODE_PURPOSES); returns its hash."""
|
||||
text, heading, unchanged = CODE_PURPOSES[purpose]
|
||||
code = f'{secrets.randbelow(10 ** 6):06d}'
|
||||
mail.queue(c, 'code', email, f'{code} é o seu código · DTF Dropstar',
|
||||
f'Olá!\n\nO seu código para {purpose} é:\n\n{code}\n\n'
|
||||
f'Ele vale por {mail.CODE_MINUTES} minutos. Se não foi você, ignore este e-mail.')
|
||||
f'{mail_html.greeting(name)}\n\nO seu código para {text} é:\n\n{code}\n\n'
|
||||
f'Ele vale por {mail.CODE_MINUTES} minutos. Se não foi você, ignore este e-mail: {unchanged}',
|
||||
mail_html.code(name, heading, text, code, mail.CODE_MINUTES, unchanged))
|
||||
return code_digest(key, code)
|
||||
|
||||
|
||||
@@ -108,8 +125,7 @@ def register(body: Register, request: Request, response: Response):
|
||||
rate_limit('signup-account', email, CODE_SENDS, 3600)
|
||||
signup_id = uuid4()
|
||||
with db.connect() as c:
|
||||
exists = c.execute('SELECT 1 FROM dtf_local.accounts WHERE email=%s', (email,)).fetchone()
|
||||
digest = signup_code(c, signup_id, email, exists)
|
||||
digest = signup_code(c, signup_id, email, profile.get('name'))
|
||||
c.execute('''INSERT INTO dtf_local.account_signups(id,email,profile,password_hash,code_hash,expires_at)
|
||||
VALUES(%s,%s,%s,%s,%s,now() + %s * interval '1 minute')''',
|
||||
(signup_id, email, Jsonb(profile), password_hash(body.password), digest, mail.CODE_MINUTES))
|
||||
@@ -197,8 +213,7 @@ def register_resend(body: SignupResend, request: Request):
|
||||
raise HTTPException(410, 'Este cadastro venceu. Preencha os dados de novo.')
|
||||
if refusal := resend_wait(signup):
|
||||
raise refusal
|
||||
exists = c.execute('SELECT 1 FROM dtf_local.accounts WHERE email=%s', (signup['email'],)).fetchone()
|
||||
digest = signup_code(c, signup['id'], signup['email'], exists)
|
||||
digest = signup_code(c, signup['id'], signup['email'], signup['profile'].get('name'))
|
||||
c.execute('''UPDATE dtf_local.account_signups SET code_hash=%s, attempts=0, sends=sends+1, sent_at=now(),
|
||||
expires_at=now() + %s * interval '1 minute' WHERE id=%s''', (digest, mail.CODE_MINUTES, signup['id']))
|
||||
audit('account_signup_code_resent', ip=client_ip(request))
|
||||
@@ -286,7 +301,7 @@ def change_email(body: EmailChange, request: Request, identity=Depends(owner)):
|
||||
with db.connect() as c:
|
||||
if c.execute('SELECT 1 FROM dtf_local.accounts WHERE email=%s', (body.email,)).fetchone():
|
||||
raise HTTPException(409, 'Este e-mail já é de outra conta.')
|
||||
digest = send_code(c, change, body.email, 'trocar o e-mail da sua conta no DTF Dropstar')
|
||||
digest = send_code(c, change, body.email, 'email', row['profile'].get('name'))
|
||||
c.execute('''INSERT INTO dtf_local.email_changes(id,account_id,new_email,code_hash,expires_at)
|
||||
VALUES(%s,%s,%s,%s,now() + %s * interval '1 minute')''', (change, identity, body.email, digest, mail.CODE_MINUTES))
|
||||
audit('account_email_change_started', account=str(identity))
|
||||
@@ -301,10 +316,12 @@ def apply_email(identity, row, email):
|
||||
c.execute('UPDATE dtf_local.accounts SET email=%s, profile=%s, email_verified=true WHERE id=%s',
|
||||
(email, Jsonb(profile), identity))
|
||||
claim_orders(c, identity, profile)
|
||||
name = row['profile'].get('name')
|
||||
mail.queue(c, 'notice', row['email'], 'O e-mail da sua conta foi trocado · DTF Dropstar',
|
||||
f'Olá!\n\nO e-mail da sua conta no DTF Dropstar foi trocado para {email}. '
|
||||
f'{mail_html.greeting(name)}\n\nO e-mail da sua conta no DTF Dropstar foi trocado para {email}. '
|
||||
'A partir de agora, entre com ele.\n\n'
|
||||
'Se não foi você, fale com a Dropstar pelo WhatsApp 16 99428-0056.')
|
||||
'Se não foi você, fale com a Dropstar pelo WhatsApp 16 99428-0056.',
|
||||
mail_html.email_changed(name, row['email'], email))
|
||||
except UniqueViolation:
|
||||
raise HTTPException(409, 'Este e-mail já é de outra conta.')
|
||||
audit('account_email_changed', account=str(identity))
|
||||
@@ -333,7 +350,8 @@ def change_email_resend(body: EmailResend, request: Request, identity=Depends(ow
|
||||
raise HTTPException(410, 'Esta troca venceu. Comece de novo.')
|
||||
if refusal := resend_wait(change):
|
||||
raise refusal
|
||||
digest = send_code(c, change['id'], change['new_email'], 'trocar o e-mail da sua conta no DTF Dropstar')
|
||||
name = c.execute('SELECT profile FROM dtf_local.accounts WHERE id=%s', (identity,)).fetchone()['profile'].get('name')
|
||||
digest = send_code(c, change['id'], change['new_email'], 'email', name)
|
||||
c.execute('''UPDATE dtf_local.email_changes SET code_hash=%s, attempts=0, sends=sends+1, sent_at=now(),
|
||||
expires_at=now() + %s * interval '1 minute' WHERE id=%s''', (digest, mail.CODE_MINUTES, change['id']))
|
||||
return {'ok': True, 'minutes': mail.CODE_MINUTES, 'resend_seconds': RESEND_SECONDS}
|
||||
@@ -368,7 +386,7 @@ def forgot_password(body: ForgotPassword, request: Request):
|
||||
rate_limit('reset-account', email, 3, 3600)
|
||||
token = secrets.token_urlsafe(32)
|
||||
with db.connect() as c:
|
||||
account = c.execute('SELECT id FROM dtf_local.accounts WHERE email=%s', (email,)).fetchone()
|
||||
account = c.execute('SELECT id,profile FROM dtf_local.accounts WHERE email=%s', (email,)).fetchone()
|
||||
if account:
|
||||
c.execute('''INSERT INTO dtf_local.password_resets(token_hash,account_id,expires_at)
|
||||
VALUES(%s,%s,now() + %s * interval '1 minute')''',
|
||||
@@ -376,10 +394,12 @@ def forgot_password(body: ForgotPassword, request: Request):
|
||||
# In the fragment, not the query: it is never sent to a server,
|
||||
# nor in the Referer of the fonts and the logo the page loads.
|
||||
link = f'{PUBLIC_ORIGIN}/conta/redefinir#{token}'
|
||||
name = account['profile'].get('name')
|
||||
mail.queue(c, 'reset', email, 'Crie uma nova senha · DTF Dropstar',
|
||||
'Olá!\n\nAlguém pediu para criar uma nova senha na sua conta do DTF Dropstar. '
|
||||
f'{mail_html.greeting(name)}\n\nAlguém pediu para criar uma nova senha na sua conta do DTF Dropstar. '
|
||||
f'Para criar, abra o link abaixo. Ele vale por 1 hora e funciona uma vez só.\n\n{link}\n\n'
|
||||
'Se não foi você, ignore este e-mail: a sua senha continua a mesma.')
|
||||
'Se não foi você, ignore este e-mail: a sua senha continua a mesma.',
|
||||
mail_html.reset(name, link, mail.RESET_MINUTES))
|
||||
audit('password_reset_requested', account=str(account['id']) if account else None, ip=client_ip(request))
|
||||
return {'ok': True, 'message': RESET_SENT}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user