def export()

in src/dispatch/forms/service.py [0:0]


def export(*, db_session: Session, ids: List[int]) -> List[str]:
    """Exports forms."""
    folders = []
    # get all the forms given the ids
    forms = db_session.query(Forms).filter(Forms.id.in_(ids)).all()
    # from the forms, get all unique project ids
    project_ids = list({form.project_id for form in forms})

    for project_id in project_ids:
        # ensure there is a document plugin active
        document_plugin = plugin_service.get_active_instance(
            db_session=db_session, project_id=project_id, plugin_type="document"
        )
        if not document_plugin:
            log.warning(
                f"Forms for project id ${project_id} not exported. No document plugin enabled."
            )
            continue

        storage_plugin = plugin_service.get_active_instance(
            db_session=db_session, project_id=project_id, plugin_type="storage"
        )
        if not storage_plugin:
            log.warning(
                f"Forms for project id ${project_id} not exported. No storage plugin enabled."
            )
            continue

        # create a storage folder for the forms in the root project folder
        external_storage_root_id = storage_plugin.configuration.root_id

        if not external_storage_root_id:
            log.warning(
                f"Forms for project id ${project_id} not exported. No external storage root id configured."
            )
            continue

        project = project_service.get(db_session=db_session, project_id=project_id)
        if not project:
            log.warning(f"Forms for project id ${project_id} not exported. Project not found.")
            continue

        form_export_template = document_service.get_project_forms_export_template(
            db_session=db_session, project_id=project_id
        )
        if not form_export_template:
            log.warning(
                f"Forms for project id ${project_id} not exported. No form export template document configured."
            )
            continue

        # create a folder name that includes the date and time
        folder_name = f"Exported forms {datetime.now().strftime('%Y-%m-%d %H:%M:%S')}"
        folder = storage_plugin.instance.create_file(
            parent_id=external_storage_root_id, name=folder_name
        )
        folders.append(folder["weblink"])

        # get the subset of forms that have this project id
        project_forms = [form for form in forms if form.project_id == project_id]

        # for each form, create a document from the template and update it with the form data
        for form in project_forms:
            export_document_name = f"{form.incident.name}-{form.form_type.name}-{form.id}"
            export_document = storage_plugin.instance.copy_file(
                folder_id=folder["id"],
                file_id=form_export_template.resource_id,
                name=export_document_name,
            )
            storage_plugin.instance.move_file(
                new_folder_id=folder["id"], file_id=export_document["id"]
            )
            document_kwargs = {
                "commander_fullname": form.incident.commander.individual.name,
                "conference_challenge": resolve_attr(form.incident, "conference.challenge"),
                "conference_weblink": resolve_attr(form.incident, "conference.weblink"),
                "conversation_weblink": resolve_attr(form.incident, "conversation.weblink"),
                "description": form.incident.description,
                "document_weblink": resolve_attr(form.incident, "incident_document.weblink"),
                "name": form.incident.name,
                "priority": form.incident.incident_priority.name,
                "reported_at": form.incident.reported_at.strftime("%m/%d/%Y %H:%M:%S"),
                "closed_at": (
                    form.incident.closed_at.strftime("%m/%d/%Y %H:%M:%S")
                    if form.incident.closed_at
                    else ""
                ),
                "resolution": form.incident.resolution,
                "severity": form.incident.incident_severity.name,
                "status": form.incident.status,
                "storage_weblink": resolve_attr(form.incident, "storage.weblink"),
                "ticket_weblink": resolve_attr(form.incident, "ticket.weblink"),
                "title": form.incident.title,
                "type": form.incident.incident_type.name,
                "summary": form.incident.summary,
                "form_status": form.status,
                "form_type": form.form_type.name,
                "form_data": build_form_doc(form.form_type.form_schema, form.form_data),
                "attorney_form_data": form.attorney_form_data,
                "attorney_status": form.attorney_status,
                "attorney_questions": form.attorney_questions,
                "attorney_analysis": form.attorney_analysis,
            }
            document_plugin.instance.update(export_document["id"], **document_kwargs)

    return folders