Skip to content

Commit

Permalink
ProducerDelivery scaffolded
Browse files Browse the repository at this point in the history
  • Loading branch information
guillemcordoba committed Jul 8, 2024
1 parent eea069e commit c15ddc2
Show file tree
Hide file tree
Showing 16 changed files with 1,509 additions and 0 deletions.
1 change: 1 addition & 0 deletions dnas/plenty/zomes/coordinator/orders/src/lib.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
pub mod producer_delivery;
pub mod household_order;
pub mod order;
use hdk::prelude::*;
Expand Down
223 changes: 223 additions & 0 deletions dnas/plenty/zomes/coordinator/orders/src/producer_delivery.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,223 @@
use hdk::prelude::*;
use orders_integrity::*;

#[hdk_extern]
pub fn create_producer_delivery(
producer_delivery: ProducerDelivery,
) -> ExternResult<Record> {
let producer_delivery_hash = create_entry(
&EntryTypes::ProducerDelivery(producer_delivery.clone()),
)?;
create_link(
producer_delivery.order_hash.clone(),
producer_delivery_hash.clone(),
LinkTypes::OrderToProducerDeliveries,
(),
)?;
let record = get(producer_delivery_hash.clone(), GetOptions::default())?
.ok_or(
wasm_error!(
WasmErrorInner::Guest("Could not find the newly created ProducerDelivery"
.to_string())
),
)?;
Ok(record)
}

#[hdk_extern]
pub fn get_original_producer_delivery(
original_producer_delivery_hash: ActionHash,
) -> ExternResult<Option<Record>> {
let Some(details) = get_details(
original_producer_delivery_hash,
GetOptions::default(),
)? else {
return Ok(None);
};
match details {
Details::Record(details) => Ok(Some(details.record)),
_ => {
Err(
wasm_error!(
WasmErrorInner::Guest("Malformed get details response".to_string())
),
)
}
}
}

#[hdk_extern]
pub fn get_latest_producer_delivery(
original_producer_delivery_hash: ActionHash,
) -> ExternResult<Option<Record>> {
let Some(details) = get_details(
original_producer_delivery_hash,
GetOptions::default(),
)? else {
return Ok(None);
};
let record_details = match details {
Details::Entry(_) => {
Err(wasm_error!(WasmErrorInner::Guest("Malformed details".into())))
}
Details::Record(record_details) => Ok(record_details),
}?;
match record_details.updates.last() {
Some(update) => get_latest_producer_delivery(update.action_address().clone()),
None => Ok(Some(record_details.record)),
}
}

#[hdk_extern]
pub fn get_all_revisions_for_producer_delivery(
original_producer_delivery_hash: ActionHash,
) -> ExternResult<Vec<Record>> {
let Some(Details::Record(details)) = get_details(
original_producer_delivery_hash,
GetOptions::default(),
)? else {
return Ok(vec![]);
};
let mut records = vec![details.record];
for update in details.updates {
let mut update_records = get_all_revisions_for_producer_delivery(
update.action_address().clone(),
)?;
records.append(&mut update_records);
}
Ok(records)
}

#[derive(Serialize, Deserialize, Debug)]
pub struct UpdateProducerDeliveryInput {
pub previous_producer_delivery_hash: ActionHash,
pub updated_producer_delivery: ProducerDelivery,
}

#[hdk_extern]
pub fn update_producer_delivery(
input: UpdateProducerDeliveryInput,
) -> ExternResult<Record> {
let updated_producer_delivery_hash = update_entry(
input.previous_producer_delivery_hash,
&input.updated_producer_delivery,
)?;
let record = get(updated_producer_delivery_hash.clone(), GetOptions::default())?
.ok_or(
wasm_error!(
WasmErrorInner::Guest("Could not find the newly updated ProducerDelivery"
.to_string())
),
)?;
Ok(record)
}

#[hdk_extern]
pub fn delete_producer_delivery(
original_producer_delivery_hash: ActionHash,
) -> ExternResult<ActionHash> {
let details = get_details(
original_producer_delivery_hash.clone(),
GetOptions::default(),
)?
.ok_or(
wasm_error!(WasmErrorInner::Guest("ProducerDelivery not found".to_string())),
)?;
let record = match details {
Details::Record(details) => Ok(details.record),
_ => {
Err(
wasm_error!(
WasmErrorInner::Guest("Malformed get details response".to_string())
),
)
}
}?;
let entry = record
.entry()
.as_option()
.ok_or(
wasm_error!(
WasmErrorInner::Guest("ProducerDelivery record has no entry".to_string())
),
)?;
let producer_delivery = <ProducerDelivery>::try_from(entry)?;
let links = get_links(
GetLinksInputBuilder::try_new(
producer_delivery.order_hash.clone(),
LinkTypes::OrderToProducerDeliveries,
)?
.build(),
)?;
for link in links {
if let Some(action_hash) = link.target.into_action_hash() {
if action_hash == original_producer_delivery_hash {
delete_link(link.create_link_hash)?;
}
}
}
delete_entry(original_producer_delivery_hash)
}

#[hdk_extern]
pub fn get_all_deletes_for_producer_delivery(
original_producer_delivery_hash: ActionHash,
) -> ExternResult<Option<Vec<SignedActionHashed>>> {
let Some(details) = get_details(
original_producer_delivery_hash,
GetOptions::default(),
)? else {
return Ok(None);
};
match details {
Details::Entry(_) => {
Err(wasm_error!(WasmErrorInner::Guest("Malformed details".into())))
}
Details::Record(record_details) => Ok(Some(record_details.deletes)),
}
}

#[hdk_extern]
pub fn get_oldest_delete_for_producer_delivery(
original_producer_delivery_hash: ActionHash,
) -> ExternResult<Option<SignedActionHashed>> {
let Some(mut deletes) = get_all_deletes_for_producer_delivery(
original_producer_delivery_hash,
)? else {
return Ok(None);
};
deletes
.sort_by(|delete_a, delete_b| {
delete_a.action().timestamp().cmp(&delete_b.action().timestamp())
});
Ok(deletes.first().cloned())
}

#[hdk_extern]
pub fn get_producer_deliveries_for_order(
order_hash: ActionHash,
) -> ExternResult<Vec<Link>> {
get_links(
GetLinksInputBuilder::try_new(order_hash, LinkTypes::OrderToProducerDeliveries)?
.build(),
)
}

#[hdk_extern]
pub fn get_deleted_producer_deliveries_for_order(
order_hash: ActionHash,
) -> ExternResult<Vec<(SignedActionHashed, Vec<SignedActionHashed>)>> {
let details = get_link_details(
order_hash,
LinkTypes::OrderToProducerDeliveries,
None,
GetOptions::default(),
)?;
Ok(
details
.into_inner()
.into_iter()
.filter(|(_link, deletes)| !deletes.is_empty())
.collect(),
)
}
23 changes: 23 additions & 0 deletions dnas/plenty/zomes/coordinator/orders/tests/common/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -50,3 +50,26 @@ pub async fn create_household_order(conductor: &SweetConductor, zome: &SweetZome
record
}



pub async fn sample_producer_delivery_1(conductor: &SweetConductor, zome: &SweetZome) -> ProducerDelivery {
ProducerDelivery {
order_hash: create_order(conductor, zome, sample_order_1(conductor, zome).await).await.signed_action.hashed.hash,
products: vec![::fixt::fixt!(ActionHash)],
}
}

pub async fn sample_producer_delivery_2(conductor: &SweetConductor, zome: &SweetZome) -> ProducerDelivery {
ProducerDelivery {
order_hash: create_order(conductor, zome, sample_order_2(conductor, zome).await).await.signed_action.hashed.hash,
products: vec![::fixt::fixt!(ActionHash)],
}
}

pub async fn create_producer_delivery(conductor: &SweetConductor, zome: &SweetZome, producer_delivery: ProducerDelivery) -> Record {
let record: Record = conductor
.call(zome, "create_producer_delivery", producer_delivery)
.await;
record
}

Loading

0 comments on commit c15ddc2

Please sign in to comment.