Beispiel #1
0
func handleUserCreated(msg []byte) {
	usr := authModels.User{}
	err := json.Unmarshal(msg, &usr)

	if err != nil {
		fmt.Println("[ERROR] ", err.Error())
		return
	}

	if usr.Scope == "patient" {
		subs := Subscription{Owner: strconv.Itoa(usr.ID), Email: usr.Email}

		db := postgres.GetDatabase(DBConfig())
		if err := subs.Create(db); err != nil {
			fmt.Println("[ERROR] ", err.Error())
			return
		}

		subscriber, err := NewSubscriber(usr.ID, usr.Email, time.Now(), time.Now().AddDate(0, 1, 0), PaymentStatusOpen)
		if err != nil {
			fmt.Println("[ERROR]", err.Error())
			return
		}
		if err := subscriber.Create(db); err != nil {
			fmt.Println("[ERROR] ", err.Error())
			return
		}
	}
}
Beispiel #2
0
func PayHandler(w http.ResponseWriter, req *http.Request) {
	r := render.New()

	w.Header().Set("Access-Control-Allow-Origin", "*")
	w.Header().Add("Access-Control-Allow-Methods", "GET, POST, PUT, OPTIONS, HEAD")
	w.Header().Add("Access-Control-Allow-Headers", "X-PINGOTHER, Origin, X-Requested-With, Content-Type, Accept")

	db := postgres.GetDatabase(DBConfig())

	if req.ParseForm() != nil || req.FormValue("email") == "" {
		http.Error(w, "Invalid Input", 400)
		return
	}

	email := req.FormValue("email")
	var sub Subscription
	var s Subscriber
	var p PaymentManager

	if GetSubscription(email, &sub, db) != nil || GetSubscriber(email, &s, db) != nil || p.Pay(&s, sub, db) != nil {
		http.Error(w, "Not Found", 404)
		return
	}

	sendSubscriptionPaid(&sub)

	r.JSON(w, http.StatusOK, "{}")
}
Beispiel #3
0
/*
*	Handlers
 */
func handleTreatmentCreated(msg []byte) {
	t := tm.Treatment{}
	err := json.Unmarshal(msg, &t)

	if err != nil {
		fmt.Println("[ERROR] ", err.Error())
		return
	}
	var s Subscriber
	db := postgres.GetDatabase(DBConfig())

	err = GetSubscriberByPatient(t.PatientId, &s, db)
	if err != nil {
		fmt.Println("[ERROR] ", err.Error())
		return
	}
	if s.PaymentStatus == PaymentStatusOpen {
		m := mailer.Mail{
			To:      []string{s.Email},
			Subject: "Atualize seus dados!",
			Body:    mailer.TemplateFinishProfile,
		}
		mailMsg, _ := json.Marshal(m)
		producer.Publish("send_mail", mailMsg)
	}

}
Beispiel #4
0
func retrievePaymentStatus(w http.ResponseWriter, req *http.Request) {
	r := render.New()
	db := postgres.GetDatabase(DBConfig())
	subs := &Subscriber{}

	patient_id, err := strconv.Atoi(req.URL.Query().Get("id"))
	if err != nil {
		http.Error(w, err.Error(), 400)
		return
	}

	subs.PatientId = patient_id
	subs, err = subs.RetrieveSubscriber(db)
	if err != nil {
		http.Error(w, err.Error(), 500)
		return
	}

	// Gambeta sinixtra
	if subs.PaymentStatus == PaymentStatusOK {
		r.JSON(w, http.StatusOK, "{}")
	} else {
		r.JSON(w, http.StatusNotFound, "{}")
	}
}
Beispiel #5
0
func SubscriptionNewHandler(w http.ResponseWriter, req *http.Request) {
	r := render.New()

	w.Header().Set("Access-Control-Allow-Origin", "*")
	w.Header().Add("Access-Control-Allow-Methods", "GET, POST, PUT, OPTIONS, HEAD")
	w.Header().Add("Access-Control-Allow-Headers", "X-PINGOTHER, Origin, X-Requested-With, Content-Type, Accept")

	db := postgres.GetDatabase(DBConfig())
	var s Subscription

	decoder := json.NewDecoder(req.Body)
	if err := decoder.Decode(&s); err != nil {
		http.Error(w, "Invalid parameters", 400)
		return
	}

	sub, err := NewSubscriber(0, s.Email, time.Now(), time.Now().AddDate(0, 1, 0), PaymentStatusOK)
	if s.Create(db) != nil || sub.Create(db) != nil || err != nil {
		http.Error(w, "Service Unavailable", 503)
		return
	}

	b, err := json.Marshal(&s)
	if err != nil {
		fmt.Println(err.Error())
	} else {
		producer.Publish("subscription_updated", b)
	}

	r.JSON(w, http.StatusCreated, "{}")
}
Beispiel #6
0
func TestGetPacks(t *testing.T) {
	p1 := &Pack{Owner: "*****@*****.**", Supervisor: "*****@*****.**", From: time.Now().AddDate(0, 1, 0), To: time.Now().AddDate(0, 0, -15), TrackingCode: "A29CDE", Status: PackStatusDelivered, PackType: "medication", PackHash: "ccf3b89a"}
	p2 := &Pack{Owner: "*****@*****.**", Supervisor: "*****@*****.**", From: time.Now(), To: time.Now().AddDate(0, 0, 14), TrackingCode: "A29CDE", Status: PackStatusShipped, PackType: "medication", PackHash: "ccf3b89a"}
	db := postgres.GetDatabase(DatabaseConfig)
	p1.Create(db)
	p2.Create(db)
	Convey("When getting packs", t, func() {
		Convey("We can get them by owner", func() {
			var p []Pack
			GetPacksByOwner("*****@*****.**", &p, db)
			So(len(p), ShouldEqual, 2)
		})
		Convey("We can get them by status", func() {
			var p []Pack
			GetPacksByStatus(PackStatusDelivered, &p, db)
			So(len(p), ShouldEqual, 1)
			GetPacksByStatusString("shipped", &p, db)
			So(len(p), ShouldEqual, 1)
		})
		Convey("We can get active packs", func() {
			var p []Pack
			GetActivePacks("*****@*****.**", &p, db)
			So(len(p), ShouldEqual, 1)
		})
	})
	db.Exec("TRUNCATE TABLE packs")
}
Beispiel #7
0
func TestSubscriber(t *testing.T) {
	Convey("When creating a subscriber", t, func() {
		email := "*****@*****.**"
		lastPayed := time.Date(2009, time.November, 10, 23, 0, 0, 0, time.UTC)
		nextPayment := time.Date(2009, time.December, 10, 23, 0, 0, 0, time.UTC)
		s, _ := NewSubscriber(email, lastPayed, nextPayment, PaymentStatusDelayed)
		dbCfg := DBConfig()
		db := postgres.GetDatabase(dbCfg)
		err := s.Create(db)
		Convey("We can create a subscriber successfully", func() {
			So(err, ShouldEqual, nil)
		})
		Convey("We can retrieve a saved subscriber", func() {
			var newSub Subscriber
			GetSubscriber(email, &newSub, db)
			So(newSub.LastPayed.Unix(), ShouldEqual, s.LastPayed.Unix())
		})
		Convey("We can update an already saved subscriber", func() {
			var newSub Subscriber
			t := time.Date(2015, time.November, 10, 23, 0, 0, 0, time.UTC)
			s.LastPayed = t
			s.NextPayment = t.AddDate(0, 1, 0)
			s.Save(db)
			GetSubscriber(email, &newSub, db)
			So(newSub.LastPayed.Unix(), ShouldEqual, t.Unix())
		})
		db.Exec("TRUNCATE TABLE subscribers")
	})
}
Beispiel #8
0
func initDatabase() {
	err := config.Load("warehouse_config.gcfg", ServerConfig)
	if err != nil {
		log.Fatal(err)
	}

	DatabaseConfig := postgres.NewConfig(ServerConfig.Database.User, ServerConfig.Database.DbName, ServerConfig.Database.SSLMode)
	testdb = postgres.GetDatabase(DatabaseConfig)
}
Beispiel #9
0
// function that will run before main
func init() {
	fmt.Println("[INFO] Initializing server")
	err := config.Load("warehouse_config.gcfg", ServerConfig)
	if err != nil {
		log.Fatal(err)
	}

	DatabaseConfig := postgres.NewConfig(ServerConfig.Database.User, ServerConfig.Database.DbName, ServerConfig.Database.SSLMode)
	db = postgres.GetDatabase(DatabaseConfig)
	fmt.Println("[INFO] Initialization Done!")

	/*
	*	Common io
	 */
	setupCommonIo()
}
Beispiel #10
0
func systemPaymentVerification() {
	subs := Subscriber{}
	db := postgres.GetDatabase(DBConfig())

	subscribers, err := subs.Retrieve(db)
	if err != nil {
		fmt.Println("[ERROR] ", err.Error())
		return
	}
	for _, currSubscriber := range subscribers {
		if time.Now().After(currSubscriber.NextPayment) {
			currSubscriber.PaymentStatus = PaymentStatusDelayed
			if err := currSubscriber.Update(db); err != nil {
				fmt.Println("[ERROR] ", err.Error())
				continue
			}
		}
	}
}
Beispiel #11
0
func TestPayment(t *testing.T) {
	Convey("When paying a montly subscription", t, func() {
		p := PaymentManager{}
		email := "*****@*****.**"
		lastPayed := time.Date(2009, time.November, 10, 23, 0, 0, 0, time.UTC)
		nextPayment := time.Date(2009, time.December, 10, 23, 0, 0, 0, time.UTC)
		s, _ := NewSubscriber(email, lastPayed, nextPayment, PaymentStatusDelayed)
		dbCfg := DBConfig()
		db := postgres.GetDatabase(dbCfg)
		s.Create(db)
		sub, _ := NewSubscription("07051368923", "Rua Luciano Gualberto, 300", "Rua Almeida Prado, 21", "555123459994032", email, "+5511987726423")
		sub.Create(db)
		Convey("We can pay a subscription", func() {
			err := p.Pay(s, *sub, db)
			So(err, ShouldEqual, nil)
		})
		db.Exec("TRUNCATE TABLE subscribers;")
		db.Exec("TRUNCATE TABLE subscriptions;")
	})
}
Beispiel #12
0
func SubscriptionListHandler(w http.ResponseWriter, req *http.Request) {
	r := render.New()
	db := postgres.GetDatabase(DBConfig())

	w.Header().Set("Access-Control-Allow-Origin", "*")
	w.Header().Add("Access-Control-Allow-Methods", "GET, POST, PUT, OPTIONS, HEAD")
	w.Header().Add("Access-Control-Allow-Headers", "X-PINGOTHER, Origin, X-Requested-With, Content-Type, Accept")

	page, err := strconv.Atoi(req.URL.Query().Get("page"))
	if err != nil {
		page = 0
	}

	var subs []Subscription
	err = GetSubscriptions(page, &subs, db)
	if err != nil {
		http.Error(w, "Not found", 404)
		return
	}

	r.JSON(w, http.StatusOK, subs)
}
Beispiel #13
0
func updateSubscription(w http.ResponseWriter, req *http.Request) errors.Http {
	subs := Subscription{}

	if err := BuildStructFromReqBody(&subs, req.Body); err != nil {
		return errors.BadRequest("[ERROR] Malformed request body")
	}

	email := req.URL.Query().Get("email")
	if email == "" {
		return errors.BadRequest("[ERROR] email is mandatory")
	}

	db := postgres.GetDatabase(DBConfig())
	if err := subs.UpdateByEmail(email, db); err != nil {
		return errors.InternalServerError(err.Error())
	}

	sendSubscriptionUpdated(&subs)

	rend := render.New()
	rend.JSON(w, http.StatusOK, subs)
	return nil
}
Beispiel #14
0
func SubscriptionShowHandler(w http.ResponseWriter, req *http.Request) {
	r := render.New()

	w.Header().Set("Access-Control-Allow-Origin", "*")
	w.Header().Add("Access-Control-Allow-Methods", "GET, POST, PUT, OPTIONS, HEAD")
	w.Header().Add("Access-Control-Allow-Headers", "X-PINGOTHER, Origin, X-Requested-With, Content-Type, Accept")

	email := req.URL.Query().Get("email")
	if email == "" {
		http.Error(w, "Invalid Input", 400)
		return
	}

	db := postgres.GetDatabase(DBConfig())
	var s Subscription
	err := GetSubscription(email, &s, db)
	if err != nil {
		http.Error(w, "Not Found", 404)
		return
	}

	r.JSON(w, http.StatusOK, s)
}