-
Notifications
You must be signed in to change notification settings - Fork 0
/
cron.go
216 lines (182 loc) · 4.87 KB
/
cron.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
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
package main
import (
"encoding/json"
"errors"
"fmt"
"io/ioutil"
"net/http"
"os"
"strconv"
"time"
"github.com/asvins/notification/mailer"
"github.com/asvins/operations/models"
)
/*
* Daily CRON for shipping boxes.
*/
func startShipCron() {
fmt.Println("[INFO] Starting ship CRON")
go func() {
allIt()
for {
<-time.After(time.Minute * 5)
allIt()
}
}()
}
// VERIFICAÇÂO DE ESTOQUE(consumir)
// /api/inventory/product/:id/consume/:quantity
func consumeFromWarehouse(box *models.Box) error {
for _, pack := range box.Packs {
for _, medication := range pack.PackMedications {
medId := strconv.Itoa(medication.MedicationId)
baseUrl := "http://" + os.Getenv("DEPLOY_WAREHOUSE_1_PORT_8080_TCP_ADDR") + ":" + os.Getenv("DEPLOY_WAREHOUSE_1_PORT_8080_TCP_PORT")
url := baseUrl + "/api/inventory/product/" + medId + "/consume/1"
response, err := http.Get(url)
if err != nil {
return err
}
defer response.Body.Close()
if response.StatusCode != http.StatusOK {
b, _ := ioutil.ReadAll(response.Body)
return errors.New(string(b))
}
}
}
return nil
}
/*
* 1) SHIPAR BOX QUE SE INICIAM EM 7 DIAS COM UMA MARGEM DE +-1 dia - ok
*/
func shipIt() {
b := models.Box{}
now := time.Now().Unix()
nowPlus6Dyas := now + 6*24*60*60
nowPlus8Days := now + 8*24*60*60
gteSlice := []string{"start_date|" + strconv.FormatInt(nowPlus6Dyas, 10)}
lteSlice := []string{"start_date|" + strconv.FormatInt(nowPlus8Days, 10)}
eqSlice := []string{"status|" + strconv.Itoa(models.BOX_PENDING)}
b.Base.Query = make(map[string][]string)
b.Base.Query["gte"] = gteSlice
b.Base.Query["lte"] = lteSlice
b.Base.Query["eq"] = eqSlice
toShip, err := b.Retrieve(db)
if err != nil {
fmt.Println("[ERROR] Unable to retrieve boxes to ship: ", err.Error())
return
}
for _, curr := range toShip {
err := consumeFromWarehouse(&curr)
if err != nil {
fmt.Println("[ERROR] ", err.Error())
} else {
if ok := curr.VerifyOwnerPaymentStatus(); ok == true {
curr.Status = models.BOX_SHIPED
if err := curr.Update(db); err != nil {
fmt.Println("[ERROR] ", err.Error())
return
}
}
}
}
}
// 3) As box que começam hoje trocar o status de shiped para DELIVERED
func onIt() {
b := models.Box{}
now := time.Now().Unix()
nowPlus1Day := now + 1*24*60*60
nowMinus1Day := now - 1*24*60*60
gteSlice := []string{"start_date|" + strconv.FormatInt(nowPlus1Day, 10)}
lteSlice := []string{"start_date|" + strconv.FormatInt(nowMinus1Day, 10)}
eqSlice := []string{"status|" + strconv.Itoa(models.BOX_SHIPED)}
b.Base.Query = make(map[string][]string)
b.Base.Query["gte"] = gteSlice
b.Base.Query["lte"] = lteSlice
b.Base.Query["eq"] = eqSlice
toOn, err := b.Retrieve(db)
if err != nil {
fmt.Println("[ERROR] ", err.Error())
return
}
for _, curr := range toOn {
curr.Status = models.BOX_DELIVERED
if err := curr.Update(db); err != nil {
fmt.Println("[ERROR] ", err.Error())
return
}
}
}
// 4) As box que terminar ontem trocar o status para OFF
func offIt() {
b := models.Box{}
now := time.Now().Unix()
nowMinus2Days := now - 2*24*60*60
gteSlice := []string{"end_date|" + strconv.FormatInt(nowMinus2Days, 10)}
lteSlice := []string{"end_date|" + strconv.FormatInt(now, 10)}
eqSlice := []string{"status|" + strconv.Itoa(models.BOX_DELIVERED)}
b.Base.Query = make(map[string][]string)
b.Base.Query["gte"] = gteSlice
b.Base.Query["lte"] = lteSlice
b.Base.Query["eq"] = eqSlice
toOff, err := b.Retrieve(db)
if err != nil {
fmt.Println("[ERROR] ", err.Error())
return
}
for _, curr := range toOff {
curr.Status = models.BOX_FINISHED
if err := curr.Update(db); err != nil {
fmt.Println("[ERROR] ", err.Error())
return
}
}
}
// Execute all actions for shipping and updating everything
func allIt() {
shipIt()
onIt()
offIt()
}
/*
* Hour cron to notify time to Pack
*/
func startPackNotificationCron() {
fmt.Println("[INFO] Starting ship CRON")
//now := time.Now().Unix()
//wait := ((60 * 60) - (now % (60 * 60)))
//<-time.After(time.Second * time.Duration(wait))
go func() {
notify()
for {
<-time.After(time.Minute * 5)
notify()
}
}()
}
func notify() {
pack := models.Pack{}
now := time.Now().Unix()
nowPlus30Min := now + 30*60
nowMinus30Min := now - 30*30
gteSlice := []string{"date|" + strconv.FormatInt(nowMinus30Min, 10)}
lteSlice := []string{"date|" + strconv.FormatInt(nowPlus30Min, 10)}
pack.Base.Query = make(map[string][]string)
pack.Base.Query["gte"] = gteSlice
pack.Base.Query["lte"] = lteSlice
packs, err := pack.Retrieve(db)
if err != nil {
fmt.Println("[ERROR] ", err.Error())
return
}
mailTo := []string{}
for _, p := range packs {
mailTo = append(mailTo, p.Email)
}
mail := mailer.Mail{To: mailTo, Subject: "Hora do remédio", Body: mailer.TemplatePackTime}
b, err := json.Marshal(&mail)
if err != nil {
fmt.Println("[ERROR] ", err.Error())
return
}
producer.Publish("send_mail", b)
}