aboutsummaryrefslogtreecommitdiff
path: root/api/admin.go
blob: e4bfda53f0cdabeb3aa530be56db424923738667 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
package api

import (
	"fmt"

	"git.immae.eu/Cryptoportfolio/Front.git/db"
	"github.com/shopspring/decimal"
)

type GetAllPortfoliosQuery struct {
	In struct {
		Market string
	}
	Out struct {
		Portfolios        map[string]Portfolio `json:"portfolios"`
		TotalBTCValue     decimal.Decimal      `json:"totalBtcValue"`
		TotalBTCVariation decimal.Decimal      `json:"totalBtcVariation"`
	}
}

func (q GetAllPortfoliosQuery) ValidateParams() *Error {
	if q.In.Market != "poloniex" {
		return &Error{BadRequest, "invalid market name", fmt.Errorf("'%v' is not a valid market name", q.In.Market)}
	}

	return nil
}

func (q GetAllPortfoliosQuery) Run() (interface{}, *Error) {
	u, err := db.GetActiveUsers()
	if err != nil {
		return nil, NewInternalError(err)
	}

	q.Out.Portfolios = make(map[string]Portfolio)

	for _, marketConfig := range u {
		report, err := GetWeekPortfolio(marketConfig)
		if ErrorIs(err, NotFound) {
			continue
		}

		if err != nil {
			return nil, NewInternalError(err)
		}

		q.Out.Portfolios[marketConfig.User.Email] = report.Round()
		q.Out.TotalBTCValue = q.Out.TotalBTCValue.Add(report.Performance.Value)
		q.Out.TotalBTCVariation = q.Out.TotalBTCVariation.Add(report.Performance.Variation)
	}

	q.Out.TotalBTCValue = q.Out.TotalBTCValue.Round(3)
	q.Out.TotalBTCVariation = q.Out.TotalBTCVariation.Round(3)

	return q.Out, nil
}