@@ -569,8 +569,11 @@ impl<T: Config> Pallet<T> {
569569 ensure ! ( !<ExpendituresInfo <T >>:: contains_key( expenditure_id) , Error :: <T >:: ExpenditureAlreadyExists ) ;
570570 <ExpendituresInfo < T > >:: insert ( expenditure_id, expenditure_data) ;
571571
572- // Create a budget for the expenditure
573- Self :: do_create_budget ( expenditure_id, 0 , project_id) ?;
572+ //Insert expenditure_id into ExpendituresByProject
573+ <ExpendituresByProject < T > >:: try_mutate :: < _ , _ , DispatchError , _ > ( project_id, |expenditures| {
574+ expenditures. try_push ( expenditure_id) . map_err ( |_| Error :: < T > :: MaxExpendituresPerProjectReached ) ?;
575+ Ok ( ( ) )
576+ } ) ?;
574577 }
575578
576579 Self :: deposit_event ( Event :: ExpenditureCreated ) ;
@@ -651,133 +654,16 @@ impl<T: Config> Pallet<T> {
651654 // Delete expenditure data
652655 <ExpendituresInfo < T > >:: remove ( expenditure_id) ;
653656
654- // Delete expenditure budget
655- Self :: do_delete_budget ( project_id, expenditure_id) ?;
656-
657- Self :: deposit_event ( Event :: ExpenditureDeleted ( expenditure_id) ) ;
658- Ok ( ( ) )
659- }
660-
661-
662-
663- // B U D G E T S
664- // --------------------------------------------------------------------------------------------
665- // Buget functions are not exposed to the public. They are only used internally by the module.
666- fn do_create_budget (
667- expenditure_id : [ u8 ; 32 ] ,
668- amount : u64 ,
669- project_id : [ u8 ; 32 ] ,
670- ) -> DispatchResult {
671- // Ensure expenditure_id exists
672- ensure ! ( <ExpendituresInfo <T >>:: contains_key( expenditure_id) , Error :: <T >:: ExpenditureNotFound ) ;
673-
674- // Get timestamp
675- let timestamp = Self :: get_timestamp_in_milliseconds ( ) . ok_or ( Error :: < T > :: TimestampError ) ?;
676-
677- // Create budget id
678- let budget_id = ( expenditure_id, timestamp) . using_encoded ( blake2_256) ;
679-
680- //TOREVIEW: Check if project_id exists.
681-
682- // Create budget data
683- let budget_data = BudgetData {
684- expenditure_id,
685- balance : amount,
686- created_date : timestamp,
687- updated_date : timestamp,
688- } ;
689-
690- // Insert budget data
691- <BudgetsInfo < T > >:: insert ( budget_id, budget_data) ;
692-
693- // Insert budget id into BudgetsByProject
694- <BudgetsByProject < T > >:: try_mutate :: < _ , _ , DispatchError , _ > ( project_id, |budgets| {
695- budgets. try_push ( budget_id) . map_err ( |_| Error :: < T > :: MaxBudgetsPerProjectReached ) ?;
696- Ok ( ( ) )
697- } ) ?;
698-
699- //TOREVIEW: Check if this event is needed
700- Self :: deposit_event ( Event :: BudgetCreated ( budget_id) ) ;
701- Ok ( ( ) )
702- }
703-
704- //For now budgets are not editable.
705- fn _do_edit_budget (
706- admin : T :: AccountId ,
707- budget_id : [ u8 ; 32 ] ,
708- amount : u64 ,
709- ) -> DispatchResult {
710- // Ensure admin permissions
711- Self :: is_superuser ( admin. clone ( ) , & Self :: get_global_scope ( ) , ProxyRole :: Administrator . id ( ) ) ?;
712-
713- //Ensure budget exists
714- ensure ! ( <BudgetsInfo <T >>:: contains_key( budget_id) , Error :: <T >:: BudgetNotFound ) ;
715-
716- // Get timestamp
717- let timestamp = Self :: get_timestamp_in_milliseconds ( ) . ok_or ( Error :: < T > :: TimestampError ) ?;
718-
719- // Mutate budget data
720- <BudgetsInfo < T > >:: try_mutate :: < _ , _ , DispatchError , _ > ( budget_id, |budget_data| {
721- let mod_budget_data = budget_data. as_mut ( ) . ok_or ( Error :: < T > :: BudgetNotFound ) ?;
722- // Update budget data
723- mod_budget_data. balance = amount;
724- mod_budget_data. updated_date = timestamp;
657+ // Delete expenditure_id from ExpendituresByProject
658+ <ExpendituresByProject < T > >:: try_mutate :: < _ , _ , DispatchError , _ > ( project_id, |expenditures| {
659+ expenditures. retain ( |expenditure| expenditure != & expenditure_id) ;
725660 Ok ( ( ) )
726661 } ) ?;
727662
663+ Self :: deposit_event ( Event :: ExpenditureDeleted ( expenditure_id) ) ;
728664 Ok ( ( ) )
729665 }
730666
731- fn do_delete_budget (
732- project_id : [ u8 ; 32 ] ,
733- expenditure_id : [ u8 ; 32 ] ,
734- ) -> DispatchResult {
735- // Get budget id
736- let budget_id = Self :: get_budget_id ( project_id, expenditure_id) ?;
737-
738- // Remove budget data
739- <BudgetsInfo < T > >:: remove ( budget_id) ;
740-
741- // Delete budget_id from BudgetsByProject
742- <BudgetsByProject < T > >:: try_mutate :: < _ , _ , DispatchError , _ > ( project_id, |budgets| {
743- budgets. retain ( |budget| budget != & budget_id) ;
744- Ok ( ( ) )
745- } ) ?;
746-
747- Ok ( ( ) )
748- }
749-
750- fn get_budget_id (
751- project_id : [ u8 ; 32 ] ,
752- expenditure_id : [ u8 ; 32 ] ,
753- ) -> Result < [ u8 ; 32 ] , DispatchError > {
754- // Ensure project exists
755- ensure ! ( ProjectsInfo :: <T >:: contains_key( project_id) , Error :: <T >:: ProjectNotFound ) ;
756-
757- // Get budgets by project (Id's)
758- let budget_ids = Self :: budgets_by_project ( project_id) . into_inner ( ) ;
759-
760- // Check if the project has any budgets
761- if budget_ids. len ( ) == 0 {
762- return Err ( Error :: < T > :: ThereIsNoBudgetsForTheProject . into ( ) ) ;
763- }
764-
765- // Get budget id
766- let budget_id: [ u8 ; 32 ] = budget_ids. iter ( ) . try_fold :: < _ , _ , Result < [ u8 ; 32 ] , DispatchError > > ( [ 0 ; 32 ] , |mut accumulator, & budget_id| {
767- // Get individual budget data
768- let budget_data = BudgetsInfo :: < T > :: get ( budget_id) . ok_or ( Error :: < T > :: BudgetNotFound ) ?;
769-
770- // Check if budget belongs to expenditure
771- if budget_data. expenditure_id == expenditure_id {
772- accumulator = budget_id;
773- }
774- Ok ( accumulator)
775- } ) ?;
776-
777- Ok ( budget_id)
778- }
779-
780-
781667 // D R A W D O W N S
782668 // --------------------------------------------------------------------------------------------
783669 // For now drawdowns functions are private, but in the future they may be public
0 commit comments