-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathclient.go
156 lines (137 loc) · 4.66 KB
/
client.go
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
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
package storage
import (
"fmt"
"github.com/dadosjusbr/storage/models"
"github.com/dadosjusbr/storage/repo/database"
"github.com/dadosjusbr/storage/repo/file_storage"
)
// Client is composed by mongoDbClient and Cloud5 client (used for backup).
type Client struct {
Db database.Interface
Cloud file_storage.Interface
}
// NewClient NewClient
func NewClient(db database.Interface, cloud file_storage.Interface) (*Client, error) {
c := Client{Db: db, Cloud: cloud}
if err := c.Db.Connect(); err != nil {
return nil, err
}
return &c, nil
}
// Close Connection with DB
func (c *Client) Close() error {
return c.Db.Disconnect()
}
// GetStateAgencies Connect to db to collect state agencies by UF
func (c *Client) GetStateAgencies(uf string) ([]models.Agency, error) {
ags, err := c.Db.GetStateAgencies(uf)
if err != nil {
return nil, fmt.Errorf("GetStateAgencies() error: %q", err)
}
return ags, err
}
// GetOPJ Connect to db to collect data to build 'Órgao por jurisdição' screen
func (c *Client) GetOPJ(group string) ([]models.Agency, error) {
ags, err := c.Db.GetOPJ(group)
if err != nil {
return nil, fmt.Errorf("GetOPJ() error: %q", err)
}
return ags, err
}
// GetOMA Connect to db to collect data for a month including all employees
func (c *Client) GetOMA(month int, year int, agency string) (*models.AgencyMonthlyInfo, *models.Agency, error) {
agsMR, agencyObj, err := c.Db.GetOMA(month, year, agency)
if err != nil {
return nil, nil, fmt.Errorf("GetOMA() error: %q", err)
}
return agsMR, agencyObj, nil
}
// Store stores the Agency Monthly Info stats.
func (c *Client) Store(agmi models.AgencyMonthlyInfo) error {
if err := c.Db.Store(agmi); err != nil {
return fmt.Errorf("Store() error: %q", err)
}
return nil
}
func (c *Client) StorePaychecks(p []models.Paycheck, r []models.PaycheckItem) error {
if err := c.Db.StorePaychecks(p, r); err != nil {
return fmt.Errorf("StorePaychecks() error: %q", err)
}
return nil
}
func (c *Client) StoreRemunerations(remu models.Remunerations) error {
if err := c.Db.StoreRemunerations(remu); err != nil {
return fmt.Errorf("StoreRemunerations() error: %q", err)
}
return nil
}
// GetAgenciesCount Return the Agencies amount
func (c *Client) GetAgenciesCount() (int, error) {
count, err := c.Db.GetAgenciesCount()
if err != nil {
return count, fmt.Errorf("GetAgenciesCount() error: %q", err)
}
return count, nil
}
// GetNumberOfMonthsCollected Return the Agencies amount
func (c *Client) GetNumberOfMonthsCollected() (int, error) {
count, err := c.Db.GetNumberOfMonthsCollected()
if err != nil {
return count, fmt.Errorf("GetNumberOfMonthsCollected() error: %q", err)
}
return count, nil
}
// GetLastDateWithMonthlyInfo return the latest year and month with collected data
func (c *Client) GetLastDateWithMonthlyInfo() (int, int, error) {
month, year, err := c.Db.GetLastDateWithMonthlyInfo()
if err != nil {
return 0, 0, fmt.Errorf("GetLastDateWithMonthlyInfo() error: %q", err)
}
return month, year, nil
}
// GetFirstDateWithMonthlyInfo return the initial year and month with collected data
func (c *Client) GetFirstDateWithMonthlyInfo() (int, int, error) {
month, year, err := c.Db.GetFirstDateWithMonthlyInfo()
if err != nil {
return 0, 0, fmt.Errorf("GetFirstDateWithMonthlyInfo() error: %q", err)
}
return month, year, nil
}
func (c *Client) GetAnnualSummary(agency string) ([]models.AnnualSummary, error) {
summary, err := c.Db.GetAnnualSummary(agency)
if err != nil {
return nil, fmt.Errorf("Error getting annual data from database: %q", err)
}
for i := range summary {
dstKey := fmt.Sprintf("%s/datapackage/%s-%d.zip", agency, agency, summary[i].Year)
pkg, err := c.Cloud.GetFile(dstKey)
if err != nil {
return nil, fmt.Errorf("Error getting annual data from file storage: %q", err)
}
summary[i].Package = pkg
}
return summary, nil
}
// Get index information by agency's ID or group (name)
func (c *Client) GetIndexInformation(name string, month, year int) (map[string][]models.IndexInformation, error) {
agg, err := c.Db.GetIndexInformation(name, month, year)
if err != nil {
return nil, fmt.Errorf("GetIndexInformation() error: %w", err)
}
return agg, nil
}
// Get all agency collection
func (c *Client) GetAllAgencyCollection(agency string) ([]models.AgencyMonthlyInfo, error) {
collections, err := c.Db.GetAllAgencyCollection(agency)
if err != nil {
return nil, fmt.Errorf("GetAllAgencyCollection() error: %w", err)
}
return collections, nil
}
func (c *Client) GetAveragePerCapita(agency string, year int) (*models.PerCapitaData, error) {
avg, err := c.Db.GetAveragePerCapita(agency, year)
if err != nil {
return nil, fmt.Errorf("GetAveragePerCapita() error: %w", err)
}
return avg, nil
}