pub async fn create_pickup(db: &Database) {
        // We should only do this for USPS, OR if we use DHL in the future.
        let shipments = outbound_shipments::dsl::outbound_shipments
            .filter(
                outbound_shipments::dsl::status
                    .eq("Label printed".to_string())
                    .and(outbound_shipments::dsl::carrier.eq("USPS".to_string()))
                    .and(outbound_shipments::dsl::pickup_date.is_null()),
            )
            .load::<OutboundShipment>(&db.conn())
            .unwrap();

        if shipments.is_empty() {
            // We can return early.
            return;
        }

        // Get the transaction ids, these should be the same as the shippo_id.
        let mut transaction_ids: Vec<String> = Default::default();
        let mut link_to_outbound_shipments: Vec<String> = Default::default();
        for shipment in shipments.clone() {
            println!("Adding {} shipment to our pickup", shipment.name);
            transaction_ids.push(shipment.shippo_id.to_string());
            link_to_outbound_shipments.push(shipment.airtable_record_id.to_string());
        }

        if transaction_ids.is_empty() {
            // We can return early.
            return;
        }

        // Get the carrier ID for USPS.
        // Create the shippo client.
        let shippo_client = Shippo::new_from_env();
        let carrier_accounts = shippo_client.list_carrier_accounts().await.unwrap();
        let mut carrier_account_id = "".to_string();
        for ca in carrier_accounts {
            if ca.carrier.to_lowercase() == "usps" {
                // Shippo docs say this is the object ID.
                carrier_account_id = ca.object_id;
                break;
            }
        }

        if carrier_account_id.is_empty() {
            // We can return early.
            // This should not happen.
            println!("[create_pickup] carrier account id for usps cannot be empty.");
            return;
        }

        // Get the next buisness day for pickup.
        let (start_time, end_time) = get_next_business_day();

        let pickup_date = start_time.date().naive_utc();

        let new_pickup = shippo::NewPickup {
            carrier_account: carrier_account_id.to_string(),
            location: shippo::Location {
                building_location_type: "Office".to_string(),
                building_type: "building".to_string(),
                instructions: "Knock on the glass door and someone will come open it.".to_string(),
                address: oxide_hq_address(),
            },
            transactions: transaction_ids.clone(),
            requested_start_time: start_time,
            requested_end_time: end_time,
            metadata: "".to_string(),
        };
        println!("{}", json!(new_pickup).to_string());

        let pickup = shippo_client.create_pickup(&new_pickup).await.unwrap();
        println!("{:?}", pickup);

        // Let's create the new pickup in the database.
        let np = NewPackagePickup {
            shippo_id: pickup.object_id.to_string(),
            confirmation_code: pickup.confirmation_code.to_string(),
            carrier: "USPS".to_string(),
            status: pickup.status.to_string(),
            location: "Oxide HQ".to_string(),
            transactions: transaction_ids,
            link_to_outbound_shipments,
            requested_start_time: start_time,
            requested_end_time: end_time,
            confirmed_start_time: pickup.confirmed_start_time,
            confirmed_end_time: pickup.confirmed_end_time,
            cancel_by_time: pickup.cancel_by_time,
            messages: format!("{:?}", pickup.messages),
        };

        // Insert the new pickup into the database.
        np.upsert(&db).await;

        // For each of the shipments, let's set the pickup date.
        for mut shipment in shipments {
            shipment.pickup_date = Some(pickup_date);
            shipment.update(&db).await;
        }
    }
// Always schedule the pickup for the next business day.
// It will create a pickup for all the shipments that have "Label printed"
// status and no pickup date currently.
Always schedule the pickup for the next business day.
It will create a pickup for all the shipments that have "Label printed"
status and no pickup date currently.
Always schedule the pickup for the next business day . It will create a pickup for all the shipments that have " Label printed " status and no pickup date currently .
pub async fn create_pickup(db: &Database) {
        // We should only do this for USPS, OR if we use DHL in the future.
        let shipments = outbound_shipments::dsl::outbound_shipments
            .filter(
                outbound_shipments::dsl::status
                    .eq("Label printed".to_string())
                    .and(outbound_shipments::dsl::carrier.eq("USPS".to_string()))
                    .and(outbound_shipments::dsl::pickup_date.is_null()),
            )
            .load::<OutboundShipment>(&db.conn())
            .unwrap();
        if shipments.is_empty() {
            // We can return early.
            return;
        }
        // Get the transaction ids, these should be the same as the shippo_id.
        let mut transaction_ids: Vec<String> = Default::default();
        let mut link_to_outbound_shipments: Vec<String> = Default::default();
        for shipment in shipments.clone() {
            println!("Adding {} shipment to our pickup", shipment.name);
            transaction_ids.push(shipment.shippo_id.to_string());
            link_to_outbound_shipments.push(shipment.airtable_record_id.to_string());
        }
        if transaction_ids.is_empty() {
            // We can return early.
            return;
        }
        // Get the carrier ID for USPS.
        // Create the shippo client.
        let shippo_client = Shippo::new_from_env();
        let carrier_accounts = shippo_client.list_carrier_accounts().await.unwrap();
        let mut carrier_account_id = "".to_string();
        for ca in carrier_accounts {
            if ca.carrier.to_lowercase() == "usps" {
                // Shippo docs say this is the object ID.
                carrier_account_id = ca.object_id;
                break;
            }
        }
        if carrier_account_id.is_empty() {
            // We can return early.
            // This should not happen.
            println!("[create_pickup] carrier account id for usps cannot be empty.");
            return;
        }
        // Get the next buisness day for pickup.
        let (start_time, end_time) = get_next_business_day();
        let pickup_date = start_time.date().naive_utc();
        let new_pickup = shippo::NewPickup {
            carrier_account: carrier_account_id.to_string(),
            location: shippo::Location {
                building_location_type: "Office".to_string(),
                building_type: "building".to_string(),
                instructions: "Knock on the glass door and someone will come open it.".to_string(),
                address: oxide_hq_address(),
            },
            transactions: transaction_ids.clone(),
            requested_start_time: start_time,
            requested_end_time: end_time,
            metadata: "".to_string(),
        };
        println!("{}", json!(new_pickup).to_string());
        let pickup = shippo_client.create_pickup(&new_pickup).await.unwrap();
        println!("{:?}", pickup);
        // Let's create the new pickup in the database.
        let np = NewPackagePickup {
            shippo_id: pickup.object_id.to_string(),
            confirmation_code: pickup.confirmation_code.to_string(),
            carrier: "USPS".to_string(),
            status: pickup.status.to_string(),
            location: "Oxide HQ".to_string(),
            transactions: transaction_ids,
            link_to_outbound_shipments,
            requested_start_time: start_time,
            requested_end_time: end_time,
            confirmed_start_time: pickup.confirmed_start_time,
            confirmed_end_time: pickup.confirmed_end_time,
            cancel_by_time: pickup.cancel_by_time,
            messages: format!("{:?}", pickup.messages),
        };
        // Insert the new pickup into the database.
        np.upsert(&db).await;
        // For each of the shipments, let's set the pickup date.
        for mut shipment in shipments {
            shipment.pickup_date = Some(pickup_date);
            shipment.update(&db).await;
        }
    }
pub async fn create_pickup ( db : & Database ) { // We should only do this for USPS, OR if we use DHL in the future. let shipments = outbound_shipments :: dsl :: outbound_shipments . filter ( outbound_shipments :: dsl :: status . eq ( " " . to_string ( ) ) . and ( outbound_shipments :: dsl :: carrier . eq ( " " . to_string ( ) ) ) . and ( outbound_shipments :: dsl :: pickup_date . is_null ( ) ) , ) . load :: < OutboundShipment > ( & db . conn ( ) ) . unwrap ( ) ; if shipments . is_empty ( ) { // We can return early. return ; } // Get the transaction ids, these should be the same as the shippo_id. let mut transaction_ids : Vec < String > = Default :: default ( ) ; let mut link_to_outbound_shipments : Vec < String > = Default :: default ( ) ; for shipment in shipments . clone ( ) { println ! ( " " shipment name ) ; transaction_ids . push ( shipment . shippo_id . to_string ( ) ) ; link_to_outbound_shipments . push ( shipment . airtable_record_id . to_string ( ) ) ; } if transaction_ids . is_empty ( ) { // We can return early. return ; } // Get the carrier ID for USPS. // Create the shippo client. let shippo_client = Shippo :: new_from_env ( ) ; let carrier_accounts = shippo_client . list_carrier_accounts ( ) . await . unwrap ( ) ; let mut carrier_account_id = " " . to_string ( ) ; for ca in carrier_accounts { if ca . carrier . to_lowercase ( ) == " " { // Shippo docs say this is the object ID. carrier_account_id = ca . object_id ; break ; } } if carrier_account_id . is_empty ( ) { // We can return early. // This should not happen. println ! ( " " ) ; return ; } // Get the next buisness day for pickup. let ( start_time , end_time ) = get_next_business_day ( ) ; let pickup_date = start_time . date ( ) . naive_utc ( ) ; let new_pickup = shippo :: NewPickup { carrier_account : carrier_account_id . to_string ( ) , location : shippo :: Location { building_location_type : " " . to_string ( ) , building_type : " " . to_string ( ) , instructions : " " . to_string ( ) , address : oxide_hq_address ( ) , } , transactions : transaction_ids . clone ( ) , requested_start_time : start_time , requested_end_time : end_time , metadata : " " . to_string ( ) , } ; println ! ( " " json ( new_pickup ) to_string ( ) ) ; let pickup = shippo_client . create_pickup ( & new_pickup ) . await . unwrap ( ) ; println ! ( " " pickup ) ; // Let's create the new pickup in the database. let np = NewPackagePickup { shippo_id : pickup . object_id . to_string ( ) , confirmation_code : pickup . confirmation_code . to_string ( ) , carrier : " " . to_string ( ) , status : pickup . status . to_string ( ) , location : " " . to_string ( ) , transactions : transaction_ids , link_to_outbound_shipments , requested_start_time : start_time , requested_end_time : end_time , confirmed_start_time : pickup . confirmed_start_time , confirmed_end_time : pickup . confirmed_end_time , cancel_by_time : pickup . cancel_by_time , messages : format ! ( " " pickup messages ) , } ; // Insert the new pickup into the database. np . upsert ( & db ) . await ; // For each of the shipments, let's set the pickup date. for mut shipment in shipments { shipment . pickup_date = Some ( pickup_date ) ; shipment . update ( & db ) . await ; } }
Always schedule the pickup for the next business day.
Always schedule the pickup for the next business day .
db
&Database
db
&Database