1
- <<<<<<< HEAD
2
- use crate :: { success_response , Application , ResponseError } ;
3
- use hyper:: { Body , Request , Response } ;
4
- use primitives:: {
5
- adapter:: Adapter ,
6
- sentry:: { campaign_create:: CreateCampaign , SuccessResponse } ,
7
- =======
8
1
use crate :: {
9
- access:: { self , check_access } ,
10
2
db:: {
11
- accounting:: get_accounting_spent ,
3
+ accounting:: { get_accounting , Side } ,
12
4
campaign:: { get_campaigns_by_channel, insert_campaign, update_campaign} ,
13
5
spendable:: fetch_spendable,
14
6
CampaignRemaining , DbPool , RedisError ,
15
7
} ,
16
- success_response , Application , Auth , ResponseError , Session ,
8
+ success_response, Application , Auth , ResponseError ,
17
9
} ;
18
- use chrono:: Utc ;
19
10
use deadpool_postgres:: PoolError ;
20
11
use hyper:: { Body , Request , Response } ;
21
12
use primitives:: {
22
13
adapter:: Adapter ,
23
14
campaign_validator:: Validator ,
24
- sentry:: {
25
- campaign_create:: { CreateCampaign , ModifyCampaign } ,
26
- Event , SuccessResponse ,
27
- } ,
15
+ sentry:: campaign_create:: { CreateCampaign , ModifyCampaign } ,
28
16
Address , Campaign , UnifiedNum ,
29
17
} ;
30
18
use slog:: error;
31
- use std:: {
32
- cmp:: { max , Ordering } ,
33
- collections:: HashMap ,
34
- >>>>>>> issue-382 -campaign-routes
35
- } ;
19
+ use std:: cmp:: { max, Ordering } ;
36
20
use thiserror:: Error ;
37
21
use tokio_postgres:: error:: SqlState ;
38
22
@@ -88,9 +72,15 @@ pub async fn create_campaign<A: Adapter>(
88
72
89
73
let total_remaining =
90
74
{
91
- let accounting_spent =
92
- get_accounting_spent( app. pool. clone( ) , & campaign. creator, & campaign. channel. id( ) )
93
- . await ?;
75
+ let accounting_spent = get_accounting (
76
+ app. pool . clone ( ) ,
77
+ campaign. channel . id ( ) ,
78
+ campaign. creator ,
79
+ Side :: Spender ,
80
+ )
81
+ . await ?
82
+ . map ( |accounting| accounting. amount )
83
+ . unwrap_or_default ( ) ;
94
84
95
85
let latest_spendable =
96
86
fetch_spendable ( app. pool . clone ( ) , & campaign. creator , & campaign. channel . id ( ) )
@@ -168,7 +158,7 @@ pub async fn create_campaign<A: Adapter>(
168
158
}
169
159
170
160
pub mod update_campaign {
171
- use crate:: db:: CampaignRemaining ;
161
+ use crate :: db:: { accounting :: Side , CampaignRemaining } ;
172
162
173
163
use super :: * ;
174
164
@@ -221,9 +211,15 @@ pub mod update_campaign {
221
211
// sum(AllChannelCampaigns.map(getRemaining)) + DeltaBudgetForMutatedCampaign <= totalDeposited - totalSpent
222
212
// sum(AllChannelCampaigns.map(getRemaining)) - DeltaBudgetForMutatedCampaign <= totalDeposited - totalSpent
223
213
if let Some ( delta_budget) = delta_budget {
224
- let accounting_spent =
225
- get_accounting_spent( pool. clone( ) , & campaign. creator, & campaign. channel. id( ) )
226
- . await ?;
214
+ let accounting_spent = get_accounting (
215
+ pool. clone ( ) ,
216
+ campaign. channel . id ( ) ,
217
+ campaign. creator ,
218
+ Side :: Spender ,
219
+ )
220
+ . await ?
221
+ . map ( |accounting| accounting. amount )
222
+ . unwrap_or_default ( ) ;
227
223
228
224
let latest_spendable =
229
225
fetch_spendable ( pool. clone ( ) , & campaign. creator , & campaign. channel . id ( ) )
@@ -779,12 +775,11 @@ pub mod insert_events {
779
775
mod test {
780
776
use super :: { update_campaign:: modify_campaign, * } ;
781
777
use crate :: {
782
- db:: { accounting:: insert_accounting , spendable:: insert_spendable} ,
778
+ db:: { accounting:: update_accounting , spendable:: insert_spendable} ,
783
779
test_util:: setup_dummy_app,
784
780
} ;
785
781
use hyper:: StatusCode ;
786
782
use primitives:: {
787
- sentry:: accounting:: { Balances, CheckedState} ,
788
783
spender:: { Deposit , Spendable } ,
789
784
util:: tests:: prep_db:: DUMMY_CAMPAIGN ,
790
785
ValidatorId ,
@@ -831,13 +826,15 @@ mod test {
831
826
. await
832
827
. expect( "Should insert Spendable for Campaign creator" ) ) ;
833
828
834
- let mut balances = Balances :: < CheckedState > :: default( ) ;
835
- balances. add_spender( create. creator) ;
836
-
837
- // TODO: Replace this once https://github.com/AdExNetwork/adex-validator-stack-rust/pull/413 is merged
838
- let _accounting = insert_accounting( app. pool. clone( ) , create. channel. clone( ) , balances)
839
- . await
840
- . expect( "Should create Accounting" ) ;
829
+ let _accounting = update_accounting (
830
+ app. pool . clone ( ) ,
831
+ create. channel . id ( ) ,
832
+ create. creator ,
833
+ Side :: Spender ,
834
+ UnifiedNum :: default ( ) ,
835
+ )
836
+ . await
837
+ . expect ( "Should create Accounting" ) ;
841
838
842
839
let create_response = create_campaign ( build_request ( create) , & app)
843
840
. await
@@ -928,7 +925,12 @@ mod test {
928
925
. await
929
926
. expect_err ( "Should return Error response" ) ;
930
927
931
- assert_eq ! ( ResponseError :: BadRequest ( "Not enough deposit left for the new campaign's budget" . to_string( ) ) , create_err) ;
928
+ assert_eq ! (
929
+ ResponseError :: BadRequest (
930
+ "Not enough deposit left for the new campaign's budget" . to_string( )
931
+ ) ,
932
+ create_err
933
+ ) ;
932
934
}
933
935
934
936
// modify first campaign, by lowering the budget from 1000 to 900
@@ -967,7 +969,7 @@ mod test {
967
969
. await
968
970
. expect ( "Should return create campaign" ) ;
969
971
970
- let json = hyper:: body:: to_bytes( create_response. into_body( ) )
972
+ let json = hyper:: body:: to_bytes ( create_response. into_body ( ) )
971
973
. await
972
974
. expect ( "Should get json" ) ;
973
975
@@ -991,12 +993,13 @@ mod test {
991
993
targeting_rules : None ,
992
994
} ;
993
995
994
- let modify_err =
995
- modify_campaign( & app. pool, & app. campaign_remaining, modified, modify)
996
- . await
997
- . expect_err( "Should return Error response" ) ;
996
+ let modify_err = modify_campaign ( & app. pool , & app. campaign_remaining , modified, modify)
997
+ . await
998
+ . expect_err ( "Should return Error response" ) ;
998
999
999
- assert ! ( matches!( modify_err, Error :: NewBudget ( string) if string == "Not enough deposit left for the campaign's new budget" ) ) ;
1000
+ assert ! (
1001
+ matches!( modify_err, Error :: NewBudget ( string) if string == "Not enough deposit left for the campaign's new budget" )
1002
+ ) ;
1000
1003
}
1001
1004
}
1002
1005
}
0 commit comments