github.com/resonatecoop/user-api@v1.0.0-13.0.20220915120639-05dc9c04014a/model/credit.go (about) 1 package model 2 3 import ( 4 uuid "github.com/google/uuid" 5 ) 6 7 // Credit 8 type Credit struct { 9 IDRecord 10 UserID uuid.UUID `bun:"type:uuid,notnull"` 11 User *User `bun:"rel:has-one"` 12 Total int64 `bun:",notnull,default:128"` 13 } 14 15 // ConvertCreditToEuro converts credit value to euro 16 func ConvertCreditToEuro(total int64) float64 { 17 return float64(total) / 1022 * 1.25 18 } 19 20 // CalculateCost 21 func CalculateCost(count int64) int64 { 22 cost := int64(0) 23 n := int64(2) 24 25 if count > 8 { 26 return cost 27 } 28 29 for n < count { 30 cost *= 2 31 n++ 32 } 33 34 return cost 35 } 36 37 // CalculateRemainingCost 38 func CalculateRemainingCost(count int64) int64 { 39 cost := int64(0) 40 n := int64(2) 41 42 if count > 8 { 43 return cost 44 } 45 46 for n < count { 47 cost += CalculateCost(n) 48 n++ 49 } 50 51 return 1022 - cost 52 }