Skip to content

Commit

Permalink
fix: handle multiple cycle log dates (#370)
Browse files Browse the repository at this point in the history
* fix: 5% service charge for order and genetic analysis order

* fix: handle multiple cycle log dates
  • Loading branch information
abdulhakim2902 authored Nov 9, 2022
1 parent e97d1cf commit 33cd3e3
Show file tree
Hide file tree
Showing 10 changed files with 121 additions and 137 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -186,14 +186,11 @@ impl<T: Config> GeneticAnalysisOrderInterface<T> for Pallet<T> {
let asset_id = genetic_analysis_order.asset_id;
let account_id = Self::account_id();

// Calculate 5% of the price_component_value
let mut price_component_substracted_value: BalanceOf<T> = 0u128.saturated_into();
for analysis_order in genetic_analysis_order.prices.iter() {
price_component_substracted_value += analysis_order.value / 20u128.saturated_into();
}
// Calculate 5% of the total price
let price_substracted_value: BalanceOf<T> = total_price / 20u128.saturated_into();

// 5% of the price_component_value is substracted
let total_price_paid = total_price - price_component_substracted_value;
// 5% of the total price is substracted
let total_price_paid = total_price - price_substracted_value;

// Withhold 5% for DBIO
Self::do_transfer(
Expand All @@ -209,7 +206,7 @@ impl<T: Config> GeneticAnalysisOrderInterface<T> for Pallet<T> {
&genetic_analysis_order.currency,
&account_id,
&TreasuryKey::<T>::get().unwrap(),
price_component_substracted_value,
price_substracted_value,
asset_id,
)?;

Expand Down
38 changes: 2 additions & 36 deletions pallets/genetic-analysis-orders/src/tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -621,8 +621,8 @@ fn fulfill_genetic_analysis_order_works() {
_genetic_analysis_order_id
));

assert_eq!(Balances::free_balance(1), 9975);
assert_eq!(Balances::free_balance(2), 25);
assert_eq!(Balances::free_balance(1), 9950);
assert_eq!(Balances::free_balance(2), 50);

assert_eq!(
GeneticAnalysisOrders::genetic_analysis_order_by_id(&_genetic_analysis_order_id),
Expand Down Expand Up @@ -1902,40 +1902,6 @@ fn call_event_should_work() {
updated_at: 0,
}),
));

// assert_ok!(GeneticAnalysis::process_genetic_analysis(
// Origin::signed(1),
// _genetic_analysis[0].clone(),
// GeneticAnalysisStatus::Rejected,
// ));

// assert_ok!(GeneticAnalysisOrders::set_genetic_analysis_order_refunded(
// Origin::signed(3),
// _genetic_analysis_order_id
// ));

// System::assert_last_event(Event::GeneticAnalysisOrders(
// crate::Event::GeneticAnalysisOrderRefunded(GeneticAnalysisOrder {
// id: _genetic_analysis_order_id,
// genetic_data_id: _genetic_data_ids[0],
// service_id: _genetic_analyst.services[0],
// customer_id: 1,
// customer_box_public_key: Keccak256::hash(
// "0xhJ7TRe456FADD2726A132ABJK5RCc9E6fC5869F4".as_bytes(),
// ),
// seller_id: 1,
// genetic_analysis_tracking_id: _genetic_analysis[0].clone(),
// genetic_link: "DeBio Genetic Genetic Link".as_bytes().to_vec(),
// asset_id: None,
// currency: CurrencyType::default(),
// prices: PriceByCurrency::default().price_components,
// additional_prices: PriceByCurrency::default().additional_prices,
// total_price: PriceByCurrency::default().total_price,
// status: GeneticAnalysisOrderStatus::Refunded,
// created_at: 0,
// updated_at: 0,
// }),
// ));
});
}

Expand Down
32 changes: 22 additions & 10 deletions pallets/menstrual-calendar/src/benchmarking.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
use super::*;

#[allow(unused)]
use crate::{Pallet as MenstrualCalendar, Symptom};
use crate::{MenstrualInfo, Pallet as MenstrualCalendar, Symptom};
use frame_benchmarking::{benchmarks, impl_benchmark_test_suite, whitelisted_caller};
use frame_support::sp_runtime::SaturatedConversion;
use frame_system::RawOrigin;
Expand Down Expand Up @@ -37,12 +37,16 @@ benchmarks! {
let menstrual_ids = MenstrualCalendar::<T>::menstrual_calendar_by_owner(
caller.clone()
).unwrap();

let menstrual_info = MenstrualInfo {
date: 0u128.saturated_into(),
symptoms: vec![Symptom::from(b"pain")],
menstruation: true,
};
}: add_menstrual_cycle_log(
RawOrigin::Signed(caller),
menstrual_ids[0],
0u128.saturated_into(),
vec![Symptom::from(b"pain")],
true
vec![menstrual_info],
)

update_menstrual_cycle_log {
Expand All @@ -58,12 +62,16 @@ benchmarks! {
caller.clone()
).unwrap();

let menstrual_info = MenstrualInfo {
date: 0u128.saturated_into(),
symptoms: vec![Symptom::from(b"pain")],
menstruation: true,
};

let _ = MenstrualCalendar::<T>::add_menstrual_cycle_log(
caller_origin.clone(),
menstrual_ids[0],
0u128.saturated_into(),
vec![Symptom::from(b"pain")],
true,
vec![menstrual_info],
);

let cycle_log_ids = MenstrualCalendar::<T>::menstrual_cycle_log_by_owner_id(menstrual_ids[0]).unwrap();
Expand All @@ -89,12 +97,16 @@ benchmarks! {
caller.clone()
).unwrap();

let menstrual_info = MenstrualInfo {
date: 0u128.saturated_into(),
symptoms: vec![Symptom::from(b"pain")],
menstruation: true,
};

let _ = MenstrualCalendar::<T>::add_menstrual_cycle_log(
caller_origin.clone(),
menstrual_ids[0],
0u128.saturated_into(),
vec![Symptom::from(b"pain")],
true,
vec![menstrual_info],
);

let cycle_log_ids = MenstrualCalendar::<T>::menstrual_cycle_log_by_owner_id(menstrual_ids[0]).unwrap();
Expand Down
69 changes: 38 additions & 31 deletions pallets/menstrual-calendar/src/impl_menstrual_calendar.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ impl<T: Config> MenstrualCalendarInterface<T> for Pallet<T> {
type Error = Error<T>;
type MenstrualCycleLog = MenstrualCycleLogOf<T>;
type MenstrualCalendar = MenstrualCalendarOf<T>;
type MenstrualInfo = SymptomInfoOf<T>;
type Date = MomentOf<T>;

fn add_menstrual_calendar(
Expand Down Expand Up @@ -52,45 +53,51 @@ impl<T: Config> MenstrualCalendarInterface<T> for Pallet<T> {
fn add_menstrual_cycle_log(
address_id: &T::AccountId,
menstrual_calendar_id: &T::Hash,
date: &Self::Date,
symptoms: &[Symptom],
menstruation: bool,
) -> Result<Self::MenstrualCycleLog, Self::Error> {
menstrual_infos: &[Self::MenstrualInfo],
) -> Result<Vec<Self::MenstrualCycleLog>, Self::Error> {
let menstrual_calendar = MenstrualCalendarById::<T>::get(menstrual_calendar_id)
.ok_or(Error::<T>::MenstrualCalendarDoesNotExist)?;

if &menstrual_calendar.address_id != address_id {
return Err(Error::<T>::NotMenstrualCalendarOwner)
}

let owner_menstrual_cycle_log_count =
MenstrualCycleLogCountByOwner::<T>::get(menstrual_calendar_id).unwrap_or(0);

let menstrual_cycle_log_id = Self::generate_id(
address_id,
owner_menstrual_cycle_log_count,
Some(*menstrual_calendar_id),
);

let now = pallet_timestamp::Pallet::<T>::get();

// Store to MenstrualCycleLogById storage
let _menstrual_cycle_log = MenstrualCycleLog::new(
menstrual_cycle_log_id,
*menstrual_calendar_id,
*date,
menstruation,
symptoms.to_vec(),
now,
);

MenstrualCycleLogById::<T>::insert(menstrual_cycle_log_id, &_menstrual_cycle_log);

Self::add_menstrual_cycle_log_by_owner(menstrual_calendar_id, &menstrual_cycle_log_id);
Self::add_menstrual_cycle_log_count();
Self::add_menstrual_cycle_log_count_by_owner(menstrual_calendar_id);

Ok(_menstrual_cycle_log)
let mut menstrual_cycle_logs: Vec<MenstrualCycleLogOf<T>> = Vec::new();

for menstrual_info in menstrual_infos.iter() {
let owner_menstrual_cycle_log_count =
MenstrualCycleLogCountByOwner::<T>::get(menstrual_calendar_id).unwrap_or(0);

let menstrual_cycle_log_id = Self::generate_id(
address_id,
owner_menstrual_cycle_log_count,
Some(*menstrual_calendar_id),
);

let now = pallet_timestamp::Pallet::<T>::get();
let date = &menstrual_info.date;
let symptoms = &menstrual_info.symptoms;
let menstruation = menstrual_info.menstruation;
let menstrual_cycle_log = MenstrualCycleLog::new(
menstrual_cycle_log_id,
*menstrual_calendar_id,
*date,
menstruation,
symptoms.to_vec(),
now,
);

MenstrualCycleLogById::<T>::insert(menstrual_cycle_log_id, &menstrual_cycle_log);

Self::add_menstrual_cycle_log_by_owner(menstrual_calendar_id, &menstrual_cycle_log_id);
Self::add_menstrual_cycle_log_count();
Self::add_menstrual_cycle_log_count_by_owner(menstrual_calendar_id);

menstrual_cycle_logs.push(menstrual_cycle_log);
}

Ok(menstrual_cycle_logs)
}

fn update_menstrual_cycle_log(
Expand Down
7 changes: 3 additions & 4 deletions pallets/menstrual-calendar/src/interface.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ pub trait MenstrualCalendarInterface<T: frame_system::Config> {
type Error;
type MenstrualCycleLog;
type MenstrualCalendar;
type MenstrualInfo;
type Date;

fn add_menstrual_calendar(
Expand All @@ -20,10 +21,8 @@ pub trait MenstrualCalendarInterface<T: frame_system::Config> {
fn add_menstrual_cycle_log(
address_id: &T::AccountId,
menstrual_calendar_id: &T::Hash,
date: &Self::Date,
symptoms: &[Symptom],
menstruation: bool,
) -> Result<Self::MenstrualCycleLog, Self::Error>;
menstrual_infos: &[Self::MenstrualInfo],
) -> Result<Vec<Self::MenstrualCycleLog>, Self::Error>;

fn update_menstrual_cycle_log(
address_id: &T::AccountId,
Expand Down
15 changes: 6 additions & 9 deletions pallets/menstrual-calendar/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,7 @@ pub mod pallet {
pub type MenstrualCalendarIdOf<T> = HashOf<T>;
pub type MenstrualCycleLogOf<T> = MenstrualCycleLog<HashOf<T>, MomentOf<T>>;
pub type MenstrualCycleLogIdOf<T> = HashOf<T>;
pub type SymptomInfoOf<T> = MenstrualInfo<MomentOf<T>>;

// ------- Storage -------------
#[pallet::storage]
Expand Down Expand Up @@ -120,7 +121,7 @@ pub mod pallet {
MenstrualCalendarRemoved(HashOf<T>, AccountIdOf<T>),
/// Event documentation should end with an array that provides descriptive names for event
/// parameters, [MenstrualCycleLog, who]
MenstrualCycleLogAdded(MenstrualCycleLogOf<T>, AccountIdOf<T>),
MenstrualCycleLogsAdded(Vec<MenstrualCycleLogOf<T>>, AccountIdOf<T>),
//// MenstrualCycleLog updated
/// parameters, [MenstrualCycleLog, who]
MenstrualCycleLogUpdated(MenstrualCycleLogOf<T>, AccountIdOf<T>),
Expand Down Expand Up @@ -190,21 +191,17 @@ pub mod pallet {
pub fn add_menstrual_cycle_log(
origin: OriginFor<T>,
menstrual_calendar_id: HashOf<T>,
date: MomentOf<T>,
symptoms: Vec<Symptom>,
menstruation: bool,
menstrual_infos: Vec<SymptomInfoOf<T>>,
) -> DispatchResultWithPostInfo {
let who = ensure_signed(origin)?;

match <Self as MenstrualCalendarInterface<T>>::add_menstrual_cycle_log(
&who,
&menstrual_calendar_id,
&date,
&symptoms,
menstruation,
&menstrual_infos,
) {
Ok(menstrual_cycle_log) => {
Self::deposit_event(Event::MenstrualCycleLogAdded(menstrual_cycle_log, who));
Ok(menstrual_cycle_logs) => {
Self::deposit_event(Event::MenstrualCycleLogsAdded(menstrual_cycle_logs, who));
Ok(().into())
},
Err(error) => Err(error.into()),
Expand Down
Loading

0 comments on commit 33cd3e3

Please sign in to comment.