Skip to content

Commit

Permalink
fix: add method to change opinion status (#386)
Browse files Browse the repository at this point in the history
  • Loading branch information
abdulhakim2902 authored Jan 2, 2023
1 parent 2d4e42b commit ccbc8e2
Show file tree
Hide file tree
Showing 7 changed files with 230 additions and 114 deletions.
45 changes: 40 additions & 5 deletions pallets/opinion/benchmarking/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ use opinion_requestor::{
};

#[allow(unused)]
use opinion::{Config as OpinionConfig, OpinionAdminKey, OpinionInfo, Pallet as Opinion};
use opinion::{Config as OpinionConfig, OpinionAdminKey, OpinionInfo, Pallet as Opinion, Status};

use frame_benchmarking::{account, benchmarks, whitelisted_caller};
use frame_system::RawOrigin;
Expand Down Expand Up @@ -84,7 +84,7 @@ benchmarks! {

let _ = Opinion::<T>::create(caller_origin, requestor_id, doctor.clone(), info);

let opinion_ids = Opinion::<T>::opinion_by_owner(doctor.clone());
let opinion_ids = Opinion::<T>::opinion_by_owner(doctor);
let opinion_id = opinion_ids[0];

let updated_info = OpinionInfo::new(
Expand All @@ -94,7 +94,42 @@ benchmarks! {
CurrencyType::DBIO,
1000,
);
}: update(RawOrigin::Signed(caller), opinion_id, doctor, updated_info)
}: update(RawOrigin::Signed(caller), opinion_id, updated_info)

update_status {
let caller: T::AccountId = OpinionAdminKey::<T>::get().unwrap();
let doctor: T::AccountId = account("doctor", 0, SEED);
let customer: T::AccountId = whitelisted_caller();

let caller_origin = T::Origin::from(RawOrigin::Signed(caller.clone()));
let customer_origin = T::Origin::from(RawOrigin::Signed(customer.clone()));

let info = RequestorInfo::new(
b"category",
b"description",
&Vec::new(),
&Vec::new(),
b"myriad_url",
);

let _ = OpinionRequestor::<T>::request_opinion(customer_origin, info);

let requestor_ids = OpinionRequestor::<T>::opinion_requestor_by_owner(customer);
let requestor_id = requestor_ids[0];

let info = OpinionInfo::new(
b"description".to_vec(),
b"myriad_url".to_vec(),
None,
CurrencyType::DBIO,
1000,
);

let _ = Opinion::<T>::create(caller_origin, requestor_id, doctor.clone(), info);

let opinion_ids = Opinion::<T>::opinion_by_owner(doctor);
let opinion_id = opinion_ids[0];
}: update_status(RawOrigin::Signed(caller), opinion_id, Status::Paid)

delete {
let caller: T::AccountId = OpinionAdminKey::<T>::get().unwrap();
Expand Down Expand Up @@ -127,9 +162,9 @@ benchmarks! {

let _ = Opinion::<T>::create(caller_origin, requestor_id, doctor.clone(), info);

let opinion_ids = Opinion::<T>::opinion_by_owner(doctor.clone());
let opinion_ids = Opinion::<T>::opinion_by_owner(doctor);
let opinion_id = opinion_ids[0];
}: delete(RawOrigin::Signed(caller), doctor, opinion_id)
}: delete(RawOrigin::Signed(caller), opinion_id)

update_admin_key {
let caller: T::AccountId = OpinionAdminKey::<T>::get().unwrap();
Expand Down
44 changes: 25 additions & 19 deletions pallets/opinion/src/impl_opinion.rs
Original file line number Diff line number Diff line change
Expand Up @@ -43,17 +43,13 @@ impl<T: Config> OpinionInterface<T> for Pallet<T> {
}

fn update_opinion(
admin: &T::AccountId,
opinion_id: &T::Hash,
account_id: &T::AccountId,
opinion_id: &T::Hash,
info: &OpinionInfo,
) -> Result<Self::Opinion, Self::Error> {
Self::is_admin(admin)?;
Self::is_admin(account_id)?;

let mut opinion = Opinions::<T>::get(opinion_id)
.ok_or(Error::<T>::NotFound)?
.is_authorized_owner(account_id)
.ok_or(Error::<T>::Unauthorized)?;
let mut opinion = Opinions::<T>::get(opinion_id).ok_or(Error::<T>::NotFound)?;

let asset_id = *info.asset_id();
let currency = info.currency();
Expand All @@ -67,27 +63,37 @@ impl<T: Config> OpinionInterface<T> for Pallet<T> {
Ok(opinion)
}

fn remove_opinion(
admin: &T::AccountId,
opinion_id: &T::Hash,
account_id: &T::AccountId,
) -> Result<(), Self::Error> {
Self::is_admin(admin)?;
fn remove_opinion(account_id: &T::AccountId, opinion_id: &T::Hash) -> Result<(), Self::Error> {
Self::is_admin(account_id)?;

let opinion = Opinions::<T>::get(opinion_id)
.ok_or(Error::<T>::NotFound)?
.is_authorized_owner(account_id)
.ok_or(Error::<T>::Unauthorized)?;
let opinion = Opinions::<T>::get(opinion_id).ok_or(Error::<T>::NotFound)?;

let requestor_id = opinion.requestor_id();
let owner = opinion.professional_id();

Opinions::<T>::remove(opinion_id);

T::OpinionRequestor::disassociate(requestor_id, opinion_id);

Self::substract_opinion_count(1);
Self::substract_opinion_count_by_owner(account_id, 1);
Self::remove_opinion_id(account_id, opinion_id);
Self::substract_opinion_count_by_owner(owner, 1);
Self::remove_opinion_id(owner, opinion_id);

Ok(())
}

fn update_status(
account_id: &T::AccountId,
opinion_id: &T::Hash,
status: &Status,
) -> Result<(), Self::Error> {
Self::is_admin(account_id)?;

let mut opinion = Opinions::<T>::get(opinion_id).ok_or(Error::<T>::NotFound)?;

opinion.update_status(status);

Opinions::<T>::insert(opinion_id, opinion);

Ok(())
}
Expand Down
13 changes: 8 additions & 5 deletions pallets/opinion/src/interface.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
use crate::Status;

pub trait OpinionInterface<T: frame_system::Config> {
type Error;
type Opinion;
Expand All @@ -11,16 +13,17 @@ pub trait OpinionInterface<T: frame_system::Config> {
) -> Result<Self::Opinion, Self::Error>;

fn update_opinion(
admin: &T::AccountId,
opinion_id: &T::Hash,
account_id: &T::AccountId,
opinion_id: &T::Hash,
info: &Self::OpinionInfo,
) -> Result<Self::Opinion, Self::Error>;

fn remove_opinion(
admin: &T::AccountId,
opinion_id: &T::Hash,
fn remove_opinion(account_id: &T::AccountId, opinion_id: &T::Hash) -> Result<(), Self::Error>;

fn update_status(
account_id: &T::AccountId,
opinion_id: &T::Hash,
status: &Status,
) -> Result<(), Self::Error>;

fn update_admin_key(admin: &T::AccountId, account_id: &T::AccountId)
Expand Down
32 changes: 20 additions & 12 deletions pallets/opinion/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -84,6 +84,7 @@ pub mod pallet {
OpinionAdded(AccountIdOf<T>, OpinionOf<T>),
OpinionUpdated(AccountIdOf<T>, OpinionOf<T>),
OpinionRemoved(AccountIdOf<T>, HashOf<T>),
OpinionStatusUpdated(AccountIdOf<T>, HashOf<T>, Status),
AdminKeyUpdated(AccountIdOf<T>),
}

Expand Down Expand Up @@ -143,17 +144,11 @@ pub mod pallet {
pub fn update(
origin: OriginFor<T>,
opinion_id: HashOf<T>,
account_id: AccountIdOf<T>,
info: OpinionInfo,
) -> DispatchResultWithPostInfo {
let who = ensure_signed(origin)?;

match <Self as OpinionInterface<T>>::update_opinion(
&who,
&opinion_id,
&account_id,
&info,
) {
match <Self as OpinionInterface<T>>::update_opinion(&who, &opinion_id, &info) {
Ok(opinion) => {
Self::deposit_event(Event::OpinionUpdated(who, opinion));
Ok(().into())
Expand All @@ -163,16 +158,29 @@ pub mod pallet {
}

#[pallet::weight(T::OpinionWeightInfo::delete())]
pub fn delete(
pub fn delete(origin: OriginFor<T>, opinion_id: HashOf<T>) -> DispatchResultWithPostInfo {
let who = ensure_signed(origin)?;

match <Self as OpinionInterface<T>>::remove_opinion(&who, &opinion_id) {
Ok(()) => {
Self::deposit_event(Event::OpinionRemoved(who, opinion_id));
Ok(().into())
},
Err(error) => Err(error.into()),
}
}

#[pallet::weight(T::OpinionWeightInfo::update_status())]
pub fn update_status(
origin: OriginFor<T>,
account_id: AccountIdOf<T>,
opinion_id: HashOf<T>,
status: Status,
) -> DispatchResultWithPostInfo {
let who = ensure_signed(origin)?;

match <Self as OpinionInterface<T>>::remove_opinion(&who, &opinion_id, &account_id) {
match <Self as OpinionInterface<T>>::update_status(&who, &opinion_id, &status) {
Ok(()) => {
Self::deposit_event(Event::OpinionRemoved(who, opinion_id));
Self::deposit_event(Event::OpinionStatusUpdated(who, opinion_id, status));
Ok(().into())
},
Err(error) => Err(error.into()),
Expand All @@ -188,7 +196,7 @@ pub mod pallet {

match <Self as OpinionInterface<T>>::update_admin_key(&who, &account_id) {
Ok(_) => {
Self::deposit_event(Event::AdminKeyUpdated(who));
Self::deposit_event(Event::AdminKeyUpdated(account_id));
Ok(().into())
},
Err(error) => Err(error.into()),
Expand Down
4 changes: 4 additions & 0 deletions pallets/opinion/src/types.rs
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,10 @@ where
self.info = info.clone();
}

pub fn update_status(&mut self, status: &Status) {
self.status = status.clone();
}

pub fn update_asset_id(&mut self, asset_id: Option<u32>) {
self.info.asset_id = asset_id;
}
Expand Down
33 changes: 24 additions & 9 deletions pallets/opinion/src/weights.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
//! Autogenerated weights for opinion_benchmarking
//!
//! THIS FILE WAS AUTO-GENERATED USING THE SUBSTRATE BENCHMARK CLI VERSION 4.0.0-dev
//! DATE: 2022-12-16, STEPS: `20`, REPEAT: 10, LOW RANGE: `[]`, HIGH RANGE: `[]`
//! DATE: 2022-12-31, STEPS: `20`, REPEAT: 10, LOW RANGE: `[]`, HIGH RANGE: `[]`
//! EXECUTION: Some(Wasm), WASM-EXECUTION: Compiled, CHAIN: Some("dev"), DB CACHE: 1024
// Executed Command:
Expand Down Expand Up @@ -29,6 +29,7 @@ use sp_std::marker::PhantomData;
pub trait WeightInfo {
fn create() -> Weight;
fn update() -> Weight;
fn update_status() -> Weight;
fn delete() -> Weight;
fn update_admin_key() -> Weight;
}
Expand All @@ -45,14 +46,21 @@ impl<T: frame_system::Config> WeightInfo for SubstrateWeight<T> {
// Storage: Opinion OpinionByOwner (r:1 w:1)
// Storage: Opinion Opinions (r:0 w:1)
fn create() -> Weight {
122_600_000_u64
282_200_000_u64
.saturating_add(T::DbWeight::get().reads(7_u64))
.saturating_add(T::DbWeight::get().writes(5_u64))
}
// Storage: Opinion OpinionAdminKey (r:1 w:0)
// Storage: Opinion Opinions (r:1 w:1)
fn update() -> Weight {
59_900_000_u64
54_600_000_u64
.saturating_add(T::DbWeight::get().reads(2_u64))
.saturating_add(T::DbWeight::get().writes(1_u64))
}
// Storage: Opinion OpinionAdminKey (r:1 w:0)
// Storage: Opinion Opinions (r:1 w:1)
fn update_status() -> Weight {
41_700_000_u64
.saturating_add(T::DbWeight::get().reads(2_u64))
.saturating_add(T::DbWeight::get().writes(1_u64))
}
Expand All @@ -63,13 +71,13 @@ impl<T: frame_system::Config> WeightInfo for SubstrateWeight<T> {
// Storage: Opinion OpinionCountByOwner (r:1 w:1)
// Storage: Opinion OpinionByOwner (r:1 w:1)
fn delete() -> Weight {
99_100_000_u64
257_900_000_u64
.saturating_add(T::DbWeight::get().reads(6_u64))
.saturating_add(T::DbWeight::get().writes(5_u64))
}
// Storage: Opinion OpinionAdminKey (r:1 w:1)
fn update_admin_key() -> Weight {
35_700_000_u64
53_100_000_u64
.saturating_add(T::DbWeight::get().reads(1_u64))
.saturating_add(T::DbWeight::get().writes(1_u64))
}
Expand All @@ -86,14 +94,21 @@ impl WeightInfo for () {
// Storage: Opinion OpinionByOwner (r:1 w:1)
// Storage: Opinion Opinions (r:0 w:1)
fn create() -> Weight {
122_600_000_u64
282_200_000_u64
.saturating_add(RocksDbWeight::get().reads(7_u64))
.saturating_add(RocksDbWeight::get().writes(5_u64))
}
// Storage: Opinion OpinionAdminKey (r:1 w:0)
// Storage: Opinion Opinions (r:1 w:1)
fn update() -> Weight {
59_900_000_u64
54_600_000_u64
.saturating_add(RocksDbWeight::get().reads(2_u64))
.saturating_add(RocksDbWeight::get().writes(1_u64))
}
// Storage: Opinion OpinionAdminKey (r:1 w:0)
// Storage: Opinion Opinions (r:1 w:1)
fn update_status() -> Weight {
41_700_000_u64
.saturating_add(RocksDbWeight::get().reads(2_u64))
.saturating_add(RocksDbWeight::get().writes(1_u64))
}
Expand All @@ -104,13 +119,13 @@ impl WeightInfo for () {
// Storage: Opinion OpinionCountByOwner (r:1 w:1)
// Storage: Opinion OpinionByOwner (r:1 w:1)
fn delete() -> Weight {
99_100_000_u64
257_900_000_u64
.saturating_add(RocksDbWeight::get().reads(6_u64))
.saturating_add(RocksDbWeight::get().writes(5_u64))
}
// Storage: Opinion OpinionAdminKey (r:1 w:1)
fn update_admin_key() -> Weight {
35_700_000_u64
53_100_000_u64
.saturating_add(RocksDbWeight::get().reads(1_u64))
.saturating_add(RocksDbWeight::get().writes(1_u64))
}
Expand Down
Loading

0 comments on commit ccbc8e2

Please sign in to comment.