Example #1
0
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)
}