Ejemplo n.º 1
0
// RetrieveFactcollection calls the RetrieveCollection method from Contacts via RPC (Fact client). Returns results via jsonapi.
func RetrieveFactCollection(w http.ResponseWriter, r *http.Request) {
	var (
		contactClient = rpcClient(r, "ContactClient")
		args          = models.FactArgs{Fact: &models.Fact{GroupID: router.Context(r).Env["GroupID"].(uint)}}
		reply         = models.FactReply{}
	)
	err := contactClient.Call("Fact.RetrieveCollection", args, &reply)
	if err != nil {
		Error(w, r, err.Error(), http.StatusInternalServerError)
		return
	}

	if reply.Facts == nil {
		reply.Facts = make([]models.Fact, 0)
	}

	Success(w, r, views.Facts{Facts: reply.Facts}, http.StatusOK)
}
Ejemplo n.º 2
0
// CreateFacts receive a polygon and retrieves every contact in this polygon. It then calls the CreateFact method for each contact.
func CreateFactsByShape(w http.ResponseWriter, r *http.Request) {
	var sourceArgs = &models.FactsJson{GroupID: router.Context(r).Env["GroupID"].(uint)}

	if err := Request(sourceArgs, r); err != nil {
		logs.Debug(err)
		Fail(w, r, map[string]interface{}{"sourceArgs": err.Error()}, http.StatusBadRequest)
		return
	}

	var (
		contactClient = rpcClient(r, "ContactClient")
		args          = models.SearchArgs{}
		reply         = models.SearchReply{}
	)
	args.Search = new(models.Search)
	args.Search.Polygon = sourceArgs.Points
	args.Search.Filter = sourceArgs.Filter

	err := contactClient.Call("Search.SearchIDViaGeoPolygon", args, &reply)
	if err != nil {
		logs.Error(err)
		Error(w, r, err.Error(), http.StatusInternalServerError)
		return
	}

	var (
		replyFacts = models.FactReply{}
		factArgs   = models.FactArgs{Fact: &models.Fact{GroupID: sourceArgs.GroupID}}
	)

	DefaultFact := &models.Fact{}
	DefaultFact = &models.Fact{}
	DefaultFact.GroupID = sourceArgs.GroupID
	DefaultFact.Action.Name = sourceArgs.Name
	DefaultFact.Action.Pitch = sourceArgs.Pitch
	DefaultFact.Action.TypeData = sourceArgs.TypeData
	DefaultFact.Status = sourceArgs.Status

	if reply.IDs != nil {
		for _, ID := range reply.IDs {
			factArgs.Fact = DefaultFact
			factArgs.Fact.ContactID = ID
			err := contactClient.Call("Fact.Create", factArgs, &replyFacts)
			if err != nil {
				Error(w, r, err.Error(), http.StatusInternalServerError)
				return
			}

			if replyFacts.Fact.Contact.ID == 0 {
				logs.Debug("Contact %d didn't exist. Fact %d may therefore be incorrect", ID, replyFacts.Fact.ID)
			}

			factArgs.Fact = replyFacts.Fact

			err = contactClient.Call("Search.IndexFact", factArgs, &replyFacts)
			if err != nil {
				Error(w, r, err.Error(), http.StatusInternalServerError)
				return
			}

			logs.Debug("Fact %d created and indexed", replyFacts.Fact.ID)

			replyFacts.Facts = append(replyFacts.Facts, *replyFacts.Fact)
		}
	}

	Success(w, r, views.Facts{Facts: replyFacts.Facts}, http.StatusOK)
}