Beispiel #1
0
func UpdateCustomerUser(rw http.ResponseWriter, r *http.Request, enc encoding.Encoder, params martini.Params) string {
	var err error
	var cu customer.CustomerUser

	qs := r.URL.Query()
	key := qs.Get("key")

	if params["id"] != "" {
		cu.Id = params["id"]
	} else if r.FormValue("id") != "" {
		cu.Id = r.FormValue("id")
	} else {
		err = errors.New("Trouble getting customer user ID")
		apierror.GenerateError("Trouble getting customer user ID", err, rw, r)
		return ""
	}

	if err = cu.Get(key); err != nil {
		apierror.GenerateError("Trouble getting customer user", err, rw, r)
		return ""
	}

	if strings.ToLower(r.Header.Get("Content-Type")) == "application/json" {
		var data []byte
		if data, err = ioutil.ReadAll(r.Body); err != nil {
			apierror.GenerateError("Trouble reading request body while updating customer user", err, rw, r)
			return ""
		}
		if err = json.Unmarshal(data, &cu); err != nil {
			apierror.GenerateError("Trouble unmarshalling json request body while updating customer user", err, rw, r)
			return ""
		}
	} else {
		name := r.FormValue("name")
		email := r.FormValue("email")
		isActive := r.FormValue("isActive")
		locationID := r.FormValue("locationID")
		isSudo := r.FormValue("isSudo")
		notCustomer := r.FormValue("notCustomer")
		if name != "" {
			cu.Name = name
		}
		if email != "" {
			cu.Email = email
		}
		if isActive != "" {
			if cu.Active, err = strconv.ParseBool(isActive); err != nil {
				cu.Active = false
			}
		}
		if locationID != "" {
			if cu.Location.Id, err = strconv.Atoi(locationID); err != nil {
				apierror.GenerateError("Trouble getting location ID", err, rw, r)
				return ""
			}
		}
		if isSudo != "" {
			if cu.Sudo, err = strconv.ParseBool(isSudo); err != nil {
				cu.Sudo = false
			}
		}
		if notCustomer != "" {
			if cu.NotCustomer, err = strconv.ParseBool(notCustomer); err != nil {
				cu.NotCustomer = false
			}
		}
	}

	if err = cu.UpdateCustomerUser(); err != nil {
		apierror.GenerateError("Trouble updating customer user", err, rw, r)
		return ""
	}

	return encoding.Must(enc.Encode(cu))
}
Beispiel #2
0
//registers an inactive user; emails user and webdev that a new inactive user exists - used by dealers site
func RegisterUser(rw http.ResponseWriter, r *http.Request, enc encoding.Encoder) string {
	var err error

	name := r.FormValue("name")
	email := r.FormValue("email")
	pass := r.FormValue("pass")
	customerID, err := strconv.Atoi(r.FormValue("customerID"))
	// isActive, err := strconv.ParseBool(r.FormValue("isActive"))
	locationID, err := strconv.Atoi(r.FormValue("locationID"))
	// isSudo, err := strconv.ParseBool(r.FormValue("isSudo"))
	cust_ID, err := strconv.Atoi(r.FormValue("cust_ID"))
	notCustomer, err := strconv.ParseBool(r.FormValue("notCustomer"))

	if email == "" || pass == "" {
		err = errors.New("Email and password are required.")
		apierror.GenerateError("Email and password are required", err, rw, r)
		return ""
	}

	var user customer.CustomerUser
	user.Email = email
	user.Password = pass
	if name != "" {
		user.Name = name
	}
	if customerID != 0 {
		user.OldCustomerID = customerID
	}
	if locationID != 0 {
		user.Location.Id = locationID
	}
	if cust_ID != 0 {
		user.CustomerID = cust_ID
	}
	user.Active = false
	user.Sudo = false
	user.Current = notCustomer

	//check for existence of user
	err = user.FindByEmail()
	if err == nil {
		apierror.GenerateError("A user with that email address already exists.", err, rw, r)
		return ""
	}
	err = nil

	user.Brands, err = brand.GetUserBrands(cust_ID)
	if err != nil {
		apierror.GenerateError("Trouble getting user brands.", err, rw, r)
		return ""
	}
	var brandIds []int
	for _, brand := range user.Brands {
		brandIds = append(brandIds, brand.ID)
	}

	if err = user.Create(brandIds); err != nil {
		apierror.GenerateError("Trouble registering new customer user", err, rw, r)
		return ""
	}

	//email
	if err = user.SendRegistrationEmail(); err != nil {
		apierror.GenerateError("Trouble emailing new customer user", err, rw, r)
		return ""
	}

	if err = user.SendRegistrationRequestEmail(); err != nil {
		apierror.GenerateError("Trouble emailing webdevelopment regarding new customer user", err, rw, r)
		return ""
	}

	return encoding.Must(enc.Encode(user))
}