Skip to content

Commit

Permalink
Merge pull request #27 from Degenics/dev-theo
Browse files Browse the repository at this point in the history
feat: added uploaded_at property inside EMR.
  • Loading branch information
kevinjanada authored Jun 26, 2021
2 parents 8ae5a03 + bf274cd commit 3956ab4
Show file tree
Hide file tree
Showing 6 changed files with 73 additions and 45 deletions.
2 changes: 2 additions & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions pallets/electronic-medical-record/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ version = '2.0.0'
frame-support = { default-features = false, version = '3.0.0' }
frame-system = { default-features = false, version = '3.0.0' }
sp-std = { default-features = false, version = '3.0.0' }
pallet-timestamp = { default-features = false, version = '3.0.0' }
traits-electronic-medical-record = { path = '../../traits/electronic-medical-record', default-features = false }

[dev-dependencies]
Expand Down
104 changes: 62 additions & 42 deletions pallets/electronic-medical-record/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,93 +13,103 @@ pub mod interface;
pub use interface::ElectronicMedicalRecordInterface;
use sp_std::prelude::*;

/// ElectronicMedicalRecordInfo struct
/// Information that is mutable by user
#[derive(Encode, Decode, Clone, Default, RuntimeDebug, PartialEq, Eq)]
pub struct ElectronicMedicalRecordInfo<AccountId, Hash>
pub struct ElectronicMedicalRecord<AccountId, Hash>
where Hash: PartialEq + Eq
{
pub id: Hash,
pub owner_id: AccountId,
pub title: Vec<u8>,
pub description: Vec<u8>, // TODO: limit the length
pub record_link: Vec<u8>
pub info: Vec<Hash>,
}

impl<AccountId, Hash> ElectronicMedicalRecordInfo<AccountId, Hash>
impl<AccountId, Hash> ElectronicMedicalRecord<AccountId, Hash>
where Hash: PartialEq + Eq
{

pub fn new (
id: Hash,
owner_id: AccountId,
title: Vec<u8>,
description: Vec<u8>,
record_link: Vec<u8>
owner_id: AccountId
) -> Self {
Self {
id,
owner_id,
title,
description,
record_link
info: Vec::<Hash>::new(),
}
}

pub fn get_id(&self) -> &Hash {
&self.id
}

pub fn get_owner_id(&self) -> &AccountId {
&self.owner_id
}

pub fn add_info(&mut self, emr_info_id: Hash) -> () {
&self.info.push(emr_info_id);
}

pub fn remove_info(&mut self, emr_info_id: Hash) -> () {
if let Some(pos) = &self.info.iter().position(|x| *x == emr_info_id) {
&self.info.remove(*pos);
}
}
}

/// ElectronicMedicalRecordInfo struct
/// Information that is mutable by user
#[derive(Encode, Decode, Clone, Default, RuntimeDebug, PartialEq, Eq)]
pub struct ElectronicMedicalRecord<AccountId, Hash>
pub struct ElectronicMedicalRecordInfo<AccountId, Hash, Moment>
where Hash: PartialEq + Eq
{
pub id: Hash,
pub owner_id: AccountId,
pub info: Vec<Hash>,
pub title: Vec<u8>,
pub description: Vec<u8>, // TODO: limit the length
pub record_link: Vec<u8>,
pub uploaded_at: Moment,
}

impl<AccountId, Hash> ElectronicMedicalRecord<AccountId, Hash>
impl<AccountId, Hash, Moment> ElectronicMedicalRecordInfo<AccountId, Hash, Moment>
where Hash: PartialEq + Eq
{

{
pub fn new (
owner_id: AccountId
id: Hash,
owner_id: AccountId,
title: Vec<u8>,
description: Vec<u8>,
record_link: Vec<u8>,
uploaded_at: Moment
) -> Self {
Self {
id,
owner_id,
info: Vec::<Hash>::new(),
title,
description,
record_link,
uploaded_at,
}
}

pub fn get_owner_id(&self) -> &AccountId {
&self.owner_id
pub fn get_id(&self) -> &Hash {
&self.id
}

pub fn add_info(&mut self, emr_info_id: Hash) -> () {
&self.info.push(emr_info_id);
pub fn get_uploaded_at(&self) -> &Moment {
&self.uploaded_at
}

pub fn remove_info(&mut self, emr_info_id: Hash) -> () {
if let Some(pos) = &self.info.iter().position(|x| *x == emr_info_id) {
&self.info.remove(*pos);
}
pub fn get_owner_id(&self) -> &AccountId {
&self.owner_id
}
}

impl<T, AccountId, Hash> ElectronicMedicalRecordInfoT<T> for ElectronicMedicalRecordInfo<AccountId, Hash>
impl<T, AccountId, Hash, Moment> ElectronicMedicalRecordInfoT<T> for ElectronicMedicalRecordInfo<AccountId, Hash, Moment>
where
Hash: PartialEq + Eq,
T: frame_system::Config<AccountId = AccountId, Hash = Hash>
T: frame_system::Config<AccountId = AccountId, Hash = Hash> + pallet_timestamp::Config<Moment = Moment>
{
fn get_id(&self) -> &Hash {
self.get_id()
}

fn get_uploaded_at(&self) -> &Moment {
self.get_uploaded_at()
}

fn get_owner_id(&self) -> &AccountId {
self.get_owner_id()
}
Expand All @@ -116,7 +126,7 @@ pub mod pallet {
use crate::interface::ElectronicMedicalRecordInterface;

#[pallet::config]
pub trait Config: frame_system::Config {
pub trait Config: frame_system::Config + pallet_timestamp::Config {
type Event: From<Event<Self>> + IsType<<Self as frame_system::Config>::Event>;
type ElectronicMedicalRecord: ElectronicMedicalRecordInfoOwner<Self>;
}
Expand All @@ -134,8 +144,9 @@ pub mod pallet {
// ----- Types -------
pub type AccountIdOf<T> = <T as frame_system::Config>::AccountId;
pub type HashOf<T> = <T as frame_system::Config>::Hash;
pub type MomentOf<T> = <T as pallet_timestamp::Config>::Moment;
pub type ElectronicMedicalRecordOf<T> = ElectronicMedicalRecord<AccountIdOf<T>, HashOf<T>>;
pub type ElectronicMedicalRecordInfoOf<T> = ElectronicMedicalRecordInfo<AccountIdOf<T>, HashOf<T>>;
pub type ElectronicMedicalRecordInfoOf<T> = ElectronicMedicalRecordInfo<AccountIdOf<T>, HashOf<T>, MomentOf<T>>;
pub type ElectronicMedicalRecordInfoIdOf<T> = HashOf<T>;

// ------- Storage -------------
Expand Down Expand Up @@ -289,10 +300,18 @@ impl<T: Config> ElectronicMedicalRecordInterface<T> for Pallet<T> {
}

fn add_electronic_medical_record_info(owner_id: &T::AccountId, title: &mut Vec<u8>, description: &mut Vec<u8>, record_link: &mut Vec<u8>) -> Result<Self::ElectronicMedicalRecordInfo, Self::Error> {
let owner_electronic_medical_record_info_count = <Self as ElectronicMedicalRecordInterface<T>>::electronic_medical_record_info_count_by_owner(owner_id);
let owner_electronic_medical_record_info_count = <Self as ElectronicMedicalRecordInterface<T>>::electronic_medical_record_info_count_by_owner(owner_id);
let electronic_medical_record_info_id = Self::generate_electronic_medical_record_info_id(owner_id, owner_electronic_medical_record_info_count);
let now = pallet_timestamp::Pallet::<T>::get();

let electronic_medical_record_info = ElectronicMedicalRecordInfo::new(electronic_medical_record_info_id.clone(), owner_id.clone(), title.clone(), description.clone(), record_link.clone());
let electronic_medical_record_info = ElectronicMedicalRecordInfo::new(
electronic_medical_record_info_id.clone(),
owner_id.clone(),
title.clone(),
description.clone(),
record_link.clone(),
now
);
// Store to ElectronicMedicalRecordInfos storage
ElectronicMedicalRecordInfoById::<T>::insert(&electronic_medical_record_info_id, &electronic_medical_record_info);

Expand Down Expand Up @@ -372,6 +391,7 @@ impl<T: Config> Pallet<T> {
/// ElectronicMedicalRecordInfosProvider Trait Implementation
impl<T: Config> ElectronicMedicalRecordInfosProvider<T> for Pallet<T> {
type Error = Error<T>;
type Moment = MomentOf<T>;
type ElectronicMedicalRecordInfo = ElectronicMedicalRecordInfoOf<T>;

fn electronic_medical_record_info_by_id(electronic_medical_record_info_id: &T::Hash) -> Option<ElectronicMedicalRecordInfoOf<T>> {
Expand Down
1 change: 1 addition & 0 deletions traits/electronic-medical-record/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ version = '2.0.0'

[dependencies]
sp-std = { default-features = false, version = '3.0.0' }
pallet-timestamp = { default-features = false, version = '3.0.0' }
frame-system = { default-features = false, version = '3.0.0' }
frame-support = { default-features = false, version = '3.0.0' }

Expand Down
7 changes: 5 additions & 2 deletions traits/electronic-medical-record/src/lib.rs
Original file line number Diff line number Diff line change
@@ -1,15 +1,18 @@
#![cfg_attr(not(feature = "std"), no_std)]

use frame_system::Config;
use pallet_timestamp::Config as TimeConfig;
//use sp_std::prelude::*;

pub trait ElectronicMedicalRecordInfo<T: Config> {
pub trait ElectronicMedicalRecordInfo<T: Config + TimeConfig> {
fn get_id(&self) -> &T::Hash;
fn get_uploaded_at(&self) -> &T::Moment;
fn get_owner_id(&self) -> &T::AccountId;
}

pub trait ElectronicMedicalRecordInfosProvider<T: Config> {
pub trait ElectronicMedicalRecordInfosProvider<T: Config + TimeConfig> {
type Error;
type Moment;
type ElectronicMedicalRecordInfo: ElectronicMedicalRecordInfo<T> + sp_std::fmt::Debug;

fn remove_electronic_medical_record_info(owner_id: &T::AccountId, electronic_medical_record_info_id: &T::Hash) -> Result<Self::ElectronicMedicalRecordInfo, Self::Error>;
Expand Down
3 changes: 2 additions & 1 deletion types.json
Original file line number Diff line number Diff line change
Expand Up @@ -143,7 +143,8 @@
"owner_id": "AccountId",
"title": "Text",
"description": "Text",
"record_link": "Text"
"record_link": "Text",
"uploaded_at": "Moment"
},
"ElectronicMedicalRecord": {
"owner_id": "AccountId",
Expand Down

0 comments on commit 3956ab4

Please sign in to comment.