@@ -14,6 +14,8 @@ pub struct Subscription {
1414 pub id : uuid:: Uuid ,
1515 pub expires_at : Option < DateTime < Utc > > ,
1616 pub referred_by : Option < String > ,
17+ pub refer_code : Option < String > ,
18+ pub bonus_days : Option < i32 > ,
1719 pub created_at : DateTime < Utc > ,
1820 pub updated_at : DateTime < Utc > ,
1921 pub is_deleted : bool ,
@@ -30,6 +32,8 @@ impl Subscription {
3032 id : id,
3133 expires_at : exp_at,
3234 referred_by : ref_by,
35+ refer_code : Some ( get_uuid_last_octet_simple ( & id) ) ,
36+ bonus_days : None ,
3337 created_at : now,
3438 updated_at : now,
3539 is_deleted : false ,
@@ -40,11 +44,16 @@ impl Subscription {
4044impl Default for Subscription {
4145 fn default ( ) -> Self {
4246 let now = Utc :: now ( ) ;
47+ let id = uuid:: Uuid :: new_v4 ( ) ;
48+
49+ let refer_code = get_uuid_last_octet_simple ( & id) ;
4350
4451 Self {
45- id : uuid :: Uuid :: new_v4 ( ) ,
52+ id : id ,
4653 expires_at : None ,
4754 referred_by : None ,
55+ refer_code : Some ( refer_code) ,
56+ bonus_days : None ,
4857 created_at : now,
4958 updated_at : now,
5059 is_deleted : false ,
@@ -62,6 +71,8 @@ impl From<tokio_postgres::Row> for Subscription {
6271 id : row. get ( "id" ) ,
6372 expires_at,
6473 referred_by : row. get ( "referred_by" ) ,
74+ refer_code : row. get ( "refer_code" ) ,
75+ bonus_days : row. get ( "bonus_days" ) ,
6576 created_at,
6677 updated_at,
6778 is_deleted : row. get :: < _ , bool > ( "is_deleted" ) ,
@@ -113,6 +124,7 @@ pub struct UpdateSubscription {
113124pub struct SubscriptionStats {
114125 pub id : uuid:: Uuid ,
115126 pub expires_at : Option < DateTime < Utc > > ,
127+ pub bonus_days : i32 ,
116128 pub days_remaining : i64 ,
117129 pub is_active : bool ,
118130}
@@ -126,9 +138,16 @@ impl Subscription {
126138 99999
127139 } ;
128140
141+ let bonus_days = if let Some ( days) = self . bonus_days {
142+ days
143+ } else {
144+ 0
145+ } ;
146+
129147 SubscriptionStats {
130148 id : self . id ,
131149 expires_at : self . expires_at ,
150+ bonus_days,
132151 days_remaining,
133152 is_active : days_remaining > 0 && !self . is_deleted ,
134153 }
@@ -141,10 +160,13 @@ pub trait Operations {
141160 fn expires_at ( & self ) -> Option < DateTime < Utc > > ;
142161 fn referral_code ( & self ) -> String ;
143162 fn referred_by ( & self ) -> Option < String > ;
163+ fn set_referred_by ( & mut self , code : String ) ;
144164 fn is_active ( & self ) -> bool ;
145165 fn days_remaining ( & self ) -> Option < i64 > ;
146166 fn set_expires_at ( & mut self , expires_at : DateTime < Utc > ) -> Result < ( ) , String > ;
147167 fn mark_deleted ( & mut self ) ;
168+ fn bonus_days ( & self ) -> Option < i32 > ;
169+ fn set_bonus_days ( & mut self , days : i32 ) ;
148170}
149171
150172impl Operations for Subscription {
@@ -171,6 +193,10 @@ impl Operations for Subscription {
171193 self . referred_by . clone ( )
172194 }
173195
196+ fn set_referred_by ( & mut self , code : String ) {
197+ self . referred_by = Some ( code) ;
198+ }
199+
174200 fn is_active ( & self ) -> bool {
175201 !self . is_deleted && self . expires_at > Some ( Utc :: now ( ) )
176202 }
@@ -184,6 +210,14 @@ impl Operations for Subscription {
184210 }
185211 }
186212
213+ fn bonus_days ( & self ) -> Option < i32 > {
214+ self . bonus_days . clone ( )
215+ }
216+
217+ fn set_bonus_days ( & mut self , days : i32 ) {
218+ self . bonus_days = Some ( days) ;
219+ }
220+
187221 fn set_expires_at ( & mut self , expires_at : DateTime < Utc > ) -> Result < ( ) , String > {
188222 if expires_at <= Utc :: now ( ) {
189223 return Err ( "Expiration date must be in the future" . to_string ( ) ) ;
0 commit comments