Validates the event is one of the recognized types
Enqueues process_envelope_updated background job
The job finds the DocusignEnvelope by envelopeId
Updates the appropriate timestamp field (sent_at, delivered_at, signed_at, voided_at, declined_at)
On envelope-completed: downloads the signed documents archive from DocuSign and uploads to Google Drive
flowchart TD
A[POST /webhooks/docusign] --> B{Known event?}
B -->|No| C[Ignore]
B -->|Yes| D[Enqueue process_envelope_updated]
D --> E[Find DocusignEnvelope by ID]
E --> F[Update timestamp field]
F --> G{envelope-completed?}
G -->|No| H[Done]
G -->|Yes| I[Download signed docs from DocuSign]
I --> J[Upload to Google Drive]
J --> H
@enqueueabledefprocess_envelope_updated(event:str,envelope_summary:dict)->None:# type: ignore[type-arg]docusign_envelope=(current_session.execute(select(DocusignEnvelope).filter(DocusignEnvelope.docusign_envelope_id==envelope_summary["envelopeId"])).scalars().unique().one_or_none())ifdocusign_envelopeisNone:current_logger.info("Could not find DocusignEnvelope",docusign_envelope_id=envelope_summary["envelopeId"],)returnif(docusign_envelope.signed_atisnotNoneordocusign_envelope.voided_atisnotNoneordocusign_envelope.declined_atisnotNone):current_logger.info("DocusignEnvelope is already signed or voided or declined",docusign_envelope_id=envelope_summary["envelopeId"],)returnifevent=="envelope-sent":envelope_sent_at=datetime.fromisoformat(envelope_summary["envelopeSummary"]["sentDateTime"])docusign_envelope.sent_at=envelope_sent_atelifevent=="envelope-delivered":envelope_delivered_at=datetime.fromisoformat(envelope_summary["envelopeSummary"]["deliveredDateTime"])docusign_envelope.delivered_at=envelope_delivered_atelifevent=="envelope-completed":envelope_completed_at=datetime.fromisoformat(envelope_summary["envelopeSummary"]["completedDateTime"])docusign_envelope.signed_at=envelope_completed_atelifevent=="envelope-voided":envelope_voided_at=datetime.fromisoformat(envelope_summary["envelopeSummary"]["voidedDateTime"])docusign_envelope.voided_at=envelope_voided_atelifevent=="envelope-declined":envelope_declined_at=datetime.fromisoformat(envelope_summary["envelopeSummary"]["declinedDateTime"])docusign_envelope.declined_at=envelope_declined_atcurrent_session.commit()ifevent=="envelope-completed":alaner_profile=(current_session.execute(select(AlanerProfile).join(AlanerProfile.signed_documents).join(SignedDocument.docusign_envelope).filter(DocusignEnvelope.id==docusign_envelope.id)).scalars().unique().one_or_none())ifalaner_profileisNone:current_logger.error("Could not find AlanerProfile linked to DocusignEnvelope",docusign_envelope_id=docusign_envelope.id,)returnarchive_data=DocusignClient().get_envelope_documents_archive(envelope_summary["envelopeId"])drive_service=get_drive_service()withBytesIO(archive_data)asarchive_file:create_file(drive_service,mandatory(alaner_profile.restricted_drive_directory_id),f"{envelope_summary['envelopeSummary']['emailSubject']}.zip",archive_file,"application/zip",ignore_default_visibility=False,)current_logger.info(f"Processed completed envelope {envelope_summary['envelopeId']} - {event}")