diff --git a/projekt-matching/projektmatch/stages.py b/projekt-matching/projektmatch/stages.py index 46389a7..dcfcfda 100644 --- a/projekt-matching/projektmatch/stages.py +++ b/projekt-matching/projektmatch/stages.py @@ -133,9 +133,11 @@ def stage_crm(ctx, cfg, espo=None): problems.append("cProjektlink") if back.get("description") != ctx["description"]: problems.append("description") - if account_id and agency and not back.get("cAccount1Id"): + if team not in (back.get("teamsIds") or []): + problems.append("teamsIds") + if account_id and agency and back.get("cAccount1Id") != account_id: problems.append("cAccount1Id") - if account_id and not agency and not back.get("accountId"): + if account_id and not agency and back.get("accountId") != account_id: problems.append("accountId") if problems: raise RuntimeError(f"CRM-Verifikation fehlgeschlagen: {problems}") @@ -145,16 +147,23 @@ def stage_crm(ctx, cfg, espo=None): def stage_notify(ctx, cfg): - if ctx.get("status") == "created": - subject = (f"[Projekt-Match] Freelancermap — {ctx['mustMatch']} % — " - f"{ctx['projectName']}") - body = (f"Neues passendes Projekt gefunden.\n\n" - f"Projekt: {ctx['projectName']}\n" - f"Must-have-Match: {rules.fmt_match(ctx['mustMatch'])}\n" - f"Nice-to-have-Match: {rules.fmt_match(ctx.get('niceMatch'))}\n\n" - f"CRM-Verkaufschance: {ctx['crmUrl']}\n") - mailer.send_mail(cfg.imap_user, cfg.imap_password, cfg.notify_to, - subject, body) + """Send notification on created; ALWAYS post the Langfuse trace. + + Called directly by the component (not via run_stage), so failed runs + still get their trace and an SMTP error cannot lose it.""" + try: + if ctx.get("status") == "created": + subject = (f"[Projekt-Match] Freelancermap — {ctx['mustMatch']} % — " + f"{ctx['projectName']}") + body = (f"Neues passendes Projekt gefunden.\n\n" + f"Projekt: {ctx['projectName']}\n" + f"Must-have-Match: {rules.fmt_match(ctx['mustMatch'])}\n" + f"Nice-to-have-Match: {rules.fmt_match(ctx.get('niceMatch'))}\n\n" + f"CRM-Verkaufschance: {ctx['crmUrl']}\n") + mailer.send_mail(cfg.imap_user, cfg.imap_password, cfg.notify_to, + subject, body) + except Exception as exc: # noqa: BLE001 + ctx.update(status="failed", error=f"notify: {exc}") pm_trace.post_trace(ctx) return ctx diff --git a/projekt-matching/tests/test_stages.py b/projekt-matching/tests/test_stages.py index 91767e2..ebb038f 100644 --- a/projekt-matching/tests/test_stages.py +++ b/projekt-matching/tests/test_stages.py @@ -100,3 +100,10 @@ def test_stage_notify_only_on_created(): sent.clear() stages.stage_notify({"status": "rejected"}, CFG) assert not sent + traced = [] + with mock.patch("projektmatch.stages.mailer.send_mail", + lambda *a: sent.append(a)), \ + mock.patch("projektmatch.stages.pm_trace.post_trace", + lambda c: traced.append(c["status"])): + stages.stage_notify({"status": "failed", "error": "x"}, CFG) + assert traced == ["failed"] and not sent