Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: handle multiple update menstrual cycle log #376

Merged
merged 1 commit into from
Nov 22, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 10 additions & 6 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::{MenstrualInfo, Pallet as MenstrualCalendar, Symptom};
use crate::{MenstrualCycleLog, 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 @@ -75,13 +75,17 @@ benchmarks! {
);

let cycle_log_ids = MenstrualCalendar::<T>::menstrual_cycle_log_by_owner_id(menstrual_ids[0]).unwrap();
let menstrual_cycle_log = MenstrualCycleLog::new(
cycle_log_ids[0],
menstrual_ids[0],
1u128.saturated_into(),
false,
vec![Symptom::from(b"headache")],
0u128.saturated_into(),
);
}: update_menstrual_cycle_log(
RawOrigin::Signed(caller),
menstrual_ids[0],
cycle_log_ids[0],
1u128.saturated_into(),
vec![Symptom::from(b"headache")],
false
menstrual_cycle_log
)

remove_menstrual_cycle_log {
Expand Down
60 changes: 35 additions & 25 deletions pallets/menstrual-calendar/src/impl_menstrual_calendar.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +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 MenstrualInfo = MenstrualInfoOf<T>;
type Date = MomentOf<T>;

fn add_menstrual_calendar(
Expand Down Expand Up @@ -102,37 +102,47 @@ impl<T: Config> MenstrualCalendarInterface<T> for Pallet<T> {

fn update_menstrual_cycle_log(
address_id: &T::AccountId,
menstrual_calendar_id: &T::Hash,
menstrual_cycle_log_id: &T::Hash,
date: &Self::Date,
symptoms: &[Symptom],
menstruation: bool,
) -> Result<Self::MenstrualCycleLog, Self::Error> {
let menstrual_calendar = MenstrualCalendarById::<T>::get(menstrual_calendar_id)
.ok_or(Error::<T>::MenstrualCalendarDoesNotExist)?;
menstrual_cycle_logs: &[Self::MenstrualCycleLog],
) -> Result<Vec<Self::MenstrualCycleLog>, Self::Error> {
let now = pallet_timestamp::Pallet::<T>::get();
let mut updated_menstrual_cycle_logs: Vec<MenstrualCycleLogOf<T>> = Vec::new();

if &menstrual_calendar.address_id != address_id {
return Err(Error::<T>::NotMenstrualCalendarOwner)
}
for menstrual_cycle_log in menstrual_cycle_logs.iter() {
let menstrual_calendar_id = &menstrual_cycle_log.menstrual_calendar_id;
let menstrual_cycle_log_id = &menstrual_cycle_log.id;
let date = &menstrual_cycle_log.date;
let symptoms = &menstrual_cycle_log.symptoms;
let menstruation = menstrual_cycle_log.menstruation;

let mut menstrual_cycle_log = MenstrualCycleLogById::<T>::get(menstrual_cycle_log_id)
.ok_or(Error::<T>::MenstrualCycleLogDoesNotExist)?;
let menstrual_calendar = MenstrualCalendarById::<T>::get(menstrual_calendar_id);
let new_menstrual_cycle_log = MenstrualCycleLogById::<T>::get(menstrual_cycle_log_id);

if &menstrual_cycle_log.menstrual_calendar_id != menstrual_calendar_id {
return Err(Error::<T>::NotMenstrualCycleLogOwner)
}
if menstrual_calendar.is_none() || new_menstrual_cycle_log.is_none() {
continue
}

let now = pallet_timestamp::Pallet::<T>::get();
let menstrual_calendar = menstrual_calendar.unwrap();
let mut new_menstrual_cycle_log = new_menstrual_cycle_log.unwrap();

menstrual_cycle_log.date = *date;
menstrual_cycle_log.menstruation = menstruation;
menstrual_cycle_log.symptoms = symptoms.to_vec();
menstrual_cycle_log.updated_at = now;
if &menstrual_calendar.address_id != address_id {
continue
}

// Store to MenstrualCycleLogById storage
MenstrualCycleLogById::<T>::insert(menstrual_cycle_log_id, &menstrual_cycle_log);
if &new_menstrual_cycle_log.menstrual_calendar_id != menstrual_calendar_id {
continue
}

new_menstrual_cycle_log.date = *date;
new_menstrual_cycle_log.menstruation = menstruation;
new_menstrual_cycle_log.symptoms = symptoms.to_vec();
new_menstrual_cycle_log.updated_at = now;

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

updated_menstrual_cycle_logs.push(new_menstrual_cycle_log);
}

Ok(menstrual_cycle_log)
Ok(updated_menstrual_cycle_logs)
}

fn remove_menstrual_cycle_log(
Expand Down
8 changes: 2 additions & 6 deletions pallets/menstrual-calendar/src/interface.rs
Original file line number Diff line number Diff line change
Expand Up @@ -26,12 +26,8 @@ pub trait MenstrualCalendarInterface<T: frame_system::Config> {

fn update_menstrual_cycle_log(
address_id: &T::AccountId,
menstrual_calendar_id: &T::Hash,
menstrual_cycle_log_id: &T::Hash,
date: &Self::Date,
symptoms: &[Symptom],
menstruation: bool,
) -> Result<Self::MenstrualCycleLog, Self::Error>;
menstrual_cycle_logs: &[Self::MenstrualCycleLog],
) -> Result<Vec<Self::MenstrualCycleLog>, Self::Error>;

fn remove_menstrual_cycle_log(
address_id: &T::AccountId,
Expand Down
22 changes: 7 additions & 15 deletions pallets/menstrual-calendar/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +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>>;
pub type MenstrualInfoOf<T> = MenstrualInfo<MomentOf<T>>;

// ------- Storage -------------
#[pallet::storage]
Expand Down Expand Up @@ -124,7 +124,7 @@ pub mod pallet {
MenstrualCycleLogsAdded(Vec<MenstrualCycleLogOf<T>>, AccountIdOf<T>),
//// MenstrualCycleLog updated
/// parameters, [MenstrualCycleLog, who]
MenstrualCycleLogUpdated(MenstrualCycleLogOf<T>, AccountIdOf<T>),
MenstrualCycleLogUpdated(Vec<MenstrualCycleLogOf<T>>, AccountIdOf<T>),
//// MenstrualCycleLog deleted
/// parameters, [MenstrualCycleLogId, who]
MenstrualCycleLogRemoved(HashOf<T>, AccountIdOf<T>),
Expand Down Expand Up @@ -191,7 +191,7 @@ pub mod pallet {
pub fn add_menstrual_cycle_log(
origin: OriginFor<T>,
menstrual_calendar_id: HashOf<T>,
menstrual_infos: Vec<SymptomInfoOf<T>>,
menstrual_infos: Vec<MenstrualInfoOf<T>>,
) -> DispatchResultWithPostInfo {
let who = ensure_signed(origin)?;

Expand All @@ -211,24 +211,16 @@ pub mod pallet {
#[pallet::weight(T::MenstrualCalendarWeightInfo::update_menstrual_cycle_log())]
pub fn update_menstrual_cycle_log(
origin: OriginFor<T>,
menstrual_calendar_id: HashOf<T>,
menstrual_cycle_log_id: HashOf<T>,
date: MomentOf<T>,
symptoms: Vec<Symptom>,
menstruation: bool,
menstrual_cycle_logs: Vec<MenstrualCycleLogOf<T>>,
) -> DispatchResultWithPostInfo {
let who = ensure_signed(origin)?;

match <Self as MenstrualCalendarInterface<T>>::update_menstrual_cycle_log(
&who,
&menstrual_calendar_id,
&menstrual_cycle_log_id,
&date,
&symptoms,
menstruation,
&menstrual_cycle_logs,
) {
Ok(menstrual_cycle_log) => {
Self::deposit_event(Event::MenstrualCycleLogUpdated(menstrual_cycle_log, who));
Ok(menstrual_cycle_logs) => {
Self::deposit_event(Event::MenstrualCycleLogUpdated(menstrual_cycle_logs, who));
Ok(().into())
},
Err(error) => Err(error.into()),
Expand Down
102 changes: 18 additions & 84 deletions pallets/menstrual-calendar/src/tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -105,14 +105,17 @@ fn update_menstrual_cycle_log_works() {

let cycle_log_ids =
MenstrualCalendar::menstrual_cycle_log_by_owner_id(menstrual_ids[0]).unwrap();

assert_ok!(MenstrualCalendar::update_menstrual_cycle_log(
Origin::signed(customer),
menstrual_ids[0],
let menstrual_cycle_log = MenstrualCycleLog::new(
cycle_log_ids[0],
menstrual_ids[0],
1,
vec![Symptom::from(b"headache")],
false,
vec![Symptom::from(b"headache")],
0,
);
assert_ok!(MenstrualCalendar::update_menstrual_cycle_log(
Origin::signed(customer),
vec![menstrual_cycle_log],
));

assert_eq!(
Expand Down Expand Up @@ -240,70 +243,6 @@ fn cant_add_menstrual_cycle_log_when_not_owner() {
})
}

#[test]
fn cant_update_menstrual_cycle_log_when_menstrual_calendar_not_exists() {
ExternalityBuilder::build().execute_with(|| {
assert_noop!(
MenstrualCalendar::update_menstrual_cycle_log(
Origin::signed(1),
Keccak256::hash("0xDb9Af2d1f3ADD2726A132AA7A65Cc9E6fC5761C3".as_bytes()),
Keccak256::hash("0xDb9Af2d1f3ADD2726A132AA7A65Cc9E6fC5761C3".as_bytes()),
0,
vec![Symptom::from(b"pain")],
false
),
Error::<Test>::MenstrualCalendarDoesNotExist
);
})
}

#[test]
fn cant_update_menstrual_cycle_log_when_not_owner() {
ExternalityBuilder::build().execute_with(|| {
let customer = 1;
let other_customer = 2;

assert_ok!(MenstrualCalendar::add_menstrual_calendar(Origin::signed(customer), 16));

let menstrual_ids = MenstrualCalendar::menstrual_calendar_by_owner(customer).unwrap();

assert_noop!(
MenstrualCalendar::update_menstrual_cycle_log(
Origin::signed(other_customer),
menstrual_ids[0],
Keccak256::hash("0xDb9Af2d1f3ADD2726A132AA7A65Cc9E6fC5761C3".as_bytes()),
1,
vec![Symptom::from(b"headache")],
false,
),
Error::<Test>::NotMenstrualCalendarOwner
);
})
}

#[test]
fn cant_update_menstrual_cycle_log_when_menstrual_cycle_log_not_exists() {
ExternalityBuilder::build().execute_with(|| {
let customer = 1;

assert_ok!(MenstrualCalendar::add_menstrual_calendar(Origin::signed(customer), 16));

let menstrual_ids = MenstrualCalendar::menstrual_calendar_by_owner(customer).unwrap();

assert_noop!(
MenstrualCalendar::update_menstrual_cycle_log(
Origin::signed(customer),
menstrual_ids[0],
Keccak256::hash("0xDb9Af2d1f3ADD2726A132AA7A65Cc9E6fC5761C3".as_bytes()),
1,
vec![Symptom::from(b"headache")],
false,
),
Error::<Test>::MenstrualCycleLogDoesNotExist
);
})
}

#[test]
fn cant_remove_menstrual_cycle_log_when_menstrual_calendar_not_exists() {
ExternalityBuilder::build().execute_with(|| {
Expand Down Expand Up @@ -425,27 +364,22 @@ fn call_event_should_works() {
customer,
)));

assert_ok!(MenstrualCalendar::update_menstrual_cycle_log(
Origin::signed(customer),
menstrual_ids[0],
let menstrual_cycle_log = MenstrualCycleLog::new(
cycle_log_ids[0],
menstrual_ids[0],
1,
vec![Symptom::from(b"headache")],
false,
vec![Symptom::from(b"headache")],
0,
);

assert_ok!(MenstrualCalendar::update_menstrual_cycle_log(
Origin::signed(customer),
vec![menstrual_cycle_log.clone()],
));

System::assert_last_event(Event::MenstrualCalendar(
crate::Event::MenstrualCycleLogUpdated(
MenstrualCycleLog::new(
cycle_log_ids[0],
menstrual_ids[0],
1,
false,
vec![Symptom::from(b"headache")],
0,
),
customer,
),
crate::Event::MenstrualCycleLogUpdated(vec![menstrual_cycle_log], customer),
));

assert_ok!(MenstrualCalendar::remove_menstrual_cycle_log(
Expand Down