// 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 } } }
/* * 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 } } } } }
func subscriptionPaidHandler(msg []byte) { subs := subscriptionModels.Subscription{} if err := json.Unmarshal(msg, &subs); err != nil { fmt.Println("[ERROR] ", err.Error()) return } // 1) GET no core para pegar os tratamentos do paciente com status = INATIVO baseUrl := "http://" + os.Getenv("DEPLOY_CORE_1_PORT_8080_TCP_ADDR") + ":" + os.Getenv("DEPLOY_CORE_1_PORT_8080_TCP_PORT") resp, err := http.Get(baseUrl + "/api/treatments?eq=patient_id|" + subs.Owner + "&eq=status|" + strconv.Itoa(coreModels.TREATMENT_STATUS_INACTIVE)) if err != nil { fmt.Println("[ERROR] ", err.Error()) return } defer resp.Body.Close() body, err := ioutil.ReadAll(resp.Body) if err != nil { fmt.Println("[ERROR] ", err.Error()) return } ts := []coreModels.Treatment{} if err := json.Unmarshal(body, &ts); err != nil { fmt.Println("[ERROR] ", err.Error()) } // Até aqui foi só para montar a struct do tratamento for _, currTreatment := range ts { box := models.Box{} box.TreatmentId = currTreatment.ID box.Status = models.BOX_PENDING boxes, err := box.RetrieveOrdered(db) if err != nil { fmt.Println("[ERROR] ", err.Error()) return } if len(boxes) > 0 { boxes[0].Status = models.BOX_SCHEDULED boxes[0].Update(db) } else { fmt.Println("[INFO] No boxes to schedule") } } b, err := json.Marshal(ts) if err != nil { fmt.Println("[ERROR] ", err.Error()) return } producer.Publish("activate_treatments", b) }
func retrieveBoxes(w http.ResponseWriter, r *http.Request) errors.Http { b := models.Box{} if err := BuildStructFromQueryString(&b, r.URL.Query()); err != nil { return errors.BadRequest(err.Error()) } b.Base.Query = r.URL.Query() boxes, err := b.Retrieve(db) if err != nil { return errors.BadRequest(err.Error()) } if len(boxes) == 0 { return errors.NotFound("record not found") } rend.JSON(w, http.StatusOK, boxes) return nil }
func createBox(currBoxPacks []models.Pack, currBoxFinalDate int64, t coreModels.Treatment) { currBoxValue := 0.0 for _, currBoxPacksPack := range currBoxPacks { currBoxValue += currBoxPacksPack.Value } fmt.Println("[INFO] Will save box with ", len(currBoxPacks), " Packs!") box := models.Box{ Status: models.BOX_PENDING, StartDate: currBoxFinalDate - ONE_MONTH, EndDate: currBoxFinalDate, TreatmentId: t.ID, PatientId: t.PatientId, Packs: currBoxPacks, Value: currBoxValue, } err := box.Save(db) if err != nil { fmt.Println("[ERROR] Could not save box on database: ", err.Error()) return } sendBoxCreated(box) }