Exemple #1
0
func (i *VehicleInquiry) SendEmail(dtx *apicontext.DataContext) error {

	cts, err := contact.GetAllContactTypes(dtx)
	if err != nil {
		return err
	}

	var ct contact.ContactType
	for _, t := range cts {
		if t.Name == "Vehicle Inquiry" {
			ct = t
			break
		}
	}

	// Get Category
	// var cat Category
	// cat.CategoryID = i.Category
	// cat.GetCategory(dtx.APIKey, 1, 1, true, nil, nil, dtx)

	// Start to build email body
	body := fmt.Sprintf("Name: %s\n", i.Name)
	body = fmt.Sprintf("%sEmail: %s\n", body, i.Email)
	body = fmt.Sprintf("%sPhone: %s\n", body, i.Phone)
	// body = fmt.Sprintf("%sCategory: %s\n", body, cat.Title)

	// Decode vehicle
	var v Vehicle
	if err := json.Unmarshal([]byte(i.Vehicle), &v); err == nil {
		str := v.stringify()
		if str != "" {
			body = fmt.Sprintf("%sVehicle: %s\n", body, str)
		}
	}

	if i.Message != "" {
		body = fmt.Sprintf("%s\nMessage: %s\n", body, i.Message)
	}

	// Send Email
	return contact.SendEmail(ct, "Email from VehicleInquiry Request Form", body) //contact type id, subject, techSupport

}
Exemple #2
0
func CreateTechSupport(rw http.ResponseWriter, req *http.Request, enc encoding.Encoder, params martini.Params, dtx *apicontext.DataContext) string {
	contType := req.Header.Get("Content-Type")

	var t techSupport.TechSupport
	var err error
	contactTypeID, err := strconv.Atoi(params["contactReceiverTypeID"]) //to whom the emails go
	sendEmail, err := strconv.ParseBool(params["sendEmail"])

	// if contType == "application/json" {
	if strings.Contains(contType, "application/json") {
		//json
		requestBody, err := ioutil.ReadAll(req.Body)
		if err != nil {
			apierror.GenerateError("Error parsing JSON.", err, rw, req)
		}

		err = json.Unmarshal(requestBody, &t)
		if err != nil {
			apierror.GenerateError("Error unmarshalling request body.", err, rw, req)
		}
	} else {
		//else, form
		t.VehicleMake = req.FormValue("vehicle_make")
		t.VehicleModel = req.FormValue("vehicle_model")
		t.VehicleYear, err = strconv.Atoi(req.FormValue("vehicle_year"))
		d, err := time.Parse(timeFormat, req.FormValue("purchase_date"))
		t.PurchaseDate = d
		t.PurchasedFrom = req.FormValue("purchased_from")
		t.DealerName = req.FormValue("dealer_name")
		t.ProductCode = req.FormValue("product_code")
		t.DateCode = req.FormValue("date_code")
		t.Issue = req.FormValue("issue")

		t.Contact.FirstName = req.FormValue("first_name")
		t.Contact.LastName = req.FormValue("last_name")
		t.Contact.Email = req.FormValue("email")
		t.Contact.Phone = req.FormValue("phone")
		t.Contact.Subject = req.FormValue("subject")
		t.Contact.Message = req.FormValue("message")
		t.Contact.Type = req.FormValue("type")
		t.Contact.Address1 = req.FormValue("address1")
		t.Contact.Address2 = req.FormValue("address2")
		t.Contact.City = req.FormValue("city")
		t.Contact.State = req.FormValue("state")
		t.Contact.PostalCode = req.FormValue("postal_code")
		t.Contact.Country = req.FormValue("country")
		if err != nil {
			apierror.GenerateError("Error parsing purchase date.", err, rw, req)
		}
	}
	t.BrandID = dtx.BrandID
	err = t.Create()
	if err != nil {
		apierror.GenerateError("Error creating Tech Support.", err, rw, req)
	}

	if sendEmail == true {
		//Send Email
		body :=
			"Name: " + t.Contact.FirstName + " " + t.Contact.LastName + "\n" +
				"Email: " + t.Contact.Email + "\n" +
				"Phone: " + t.Contact.Phone + "\n" +
				"Make: " + t.VehicleMake + "\n" +
				"Model: " + t.VehicleModel + "\n" +
				"Year: " + strconv.Itoa(t.VehicleYear) + "\n" +
				"Purchase Date: " + t.PurchaseDate.String() + "\n" +
				"Purchased From: " + t.PurchasedFrom + "\n" +
				"Dealer Name: " + t.DealerName + "\n" +
				"Product Code: " + t.ProductCode + "\n" +
				"Date Code: " + t.DateCode + "\n\n" +
				"Issue: " + t.Issue + "\n"

		var ct contact.ContactType
		ct.ID = contactTypeID
		subject := "Email from Tech Support Request Form"
		err = contact.SendEmail(ct, subject, body) //contact type id, subject, techSupport
		if err != nil {
			apierror.GenerateError("Error sending email to Tech Support.", err, rw, req)
		}
	}
	//Return JSON
	return encoding.Must(enc.Encode(t))
}
Exemple #3
0
func CreateWarranty(rw http.ResponseWriter, req *http.Request, enc encoding.Encoder, params martini.Params) string {
	contType := req.Header.Get("Content-Type")
	var w warranty.Warranty
	var err error

	contactTypeID, err := strconv.Atoi(params["contactReceiverTypeID"]) //to whom the emails go
	if err != nil {
		apierror.GenerateError("Trouble parsing contact type ID.", err, rw, req)
		return ""
	}
	sendEmail, err := strconv.ParseBool(params["sendEmail"])
	if err != nil {
		apierror.GenerateError("Trouble parsing send email.", err, rw, req)
		return ""
	}
	if strings.Contains(contType, "application/json") {
		//json
		requestBody, err := ioutil.ReadAll(req.Body)
		if err != nil {
			apierror.GenerateError("Trouble reading request body for creating warranty", err, rw, req)
			return ""
		}

		err = json.Unmarshal(requestBody, &w)
		if err != nil {
			apierror.GenerateError("Trouble unmarshalling request body for creating warranty", err, rw, req)
			return ""
		}

	} else {
		//else, form
		w.PartNumber, err = strconv.Atoi(req.FormValue("part_number"))
		w.OldPartNumber = req.FormValue("old_part_number")
		date, err := time.Parse(timeFormat, req.FormValue("date"))
		if err != nil {
			apierror.GenerateError("Trouble creating warranty", err, rw, req)
			return ""
		}
		w.Date = &date
		w.SerialNumber = req.FormValue("serial_number")

		w.Contact.FirstName = req.FormValue("first_name")
		w.Contact.LastName = req.FormValue("last_name")
		w.Contact.Email = req.FormValue("email")
		w.Contact.Phone = req.FormValue("phone")
		w.Contact.Type = req.FormValue("type")
		w.Contact.Address1 = req.FormValue("address1")
		w.Contact.Address2 = req.FormValue("address2")
		w.Contact.City = req.FormValue("city")
		w.Contact.State = req.FormValue("state")
		w.Contact.PostalCode = req.FormValue("postal_code")
		w.Contact.Country = req.FormValue("country")
	}
	err = w.Create()
	if err != nil {
		apierror.GenerateError("Trouble creating warranty", err, rw, req)
		return ""
	}
	if sendEmail == true {
		//Send Email
		body :=
			"Name: " + w.Contact.FirstName + " " + w.Contact.LastName + "\n" +
				"Email: " + w.Contact.Email + "\n" +
				"Phone: " + w.Contact.Phone + "\n" +
				"Serial Number: " + w.SerialNumber + "\n" +
				"Date: " + w.Date.String() + "\n" +
				"Part Number: " + strconv.Itoa(w.PartNumber) + "\n"

		var ct contact.ContactType
		ct.ID = contactTypeID
		subject := "Email from Warranty Applications Form"
		err = contact.SendEmail(ct, subject, body) //contact type id, subject, techSupport
		if err != nil {
			apierror.GenerateError("Trouble sending email to receivers while creating warranty", err, rw, req)
			return ""
		}
	}
	//Return JSON
	return encoding.Must(enc.Encode(w))
}
Exemple #4
0
func AddDealerContact(rw http.ResponseWriter, req *http.Request, enc encoding.Encoder, params martini.Params, dtx *apicontext.DataContext) string {
	flag.Parse()

	var d contact.DealerContact
	var ct contact.ContactType
	var subject string
	var err error
	var brandName string

	brandIDs, err := dtx.GetBrandsFromKey()
	if err != nil {
		brandName = "Unknown Brand"
	}
	for _, bID := range brandIDs {
		var brand brand.Brand
		brand.ID = bID
		brand.Get()
		brandName = brand.Name
	}

	ct.ID, err = strconv.Atoi(params["contactTypeID"]) //determines to whom emails go
	if err != nil {
		apierror.GenerateError("Trouble getting contact type ID", err, rw, req)
		return ""
	}
	d.Type = ct.Name

	contType := req.Header.Get("Content-Type")

	if strings.Contains(contType, "application/json") {
		//this is our json payload
		requestBody, err := ioutil.ReadAll(req.Body)
		if err != nil {
			apierror.GenerateError("Trouble reading JSON request body for adding contact", err, rw, req)
			return ""
		}

		if err = json.Unmarshal(requestBody, &d); err != nil {
			apierror.GenerateError("Trouble unmarshalling JSON request body into contact", err, rw, req)
			return ""
		}
	}

	ct.ID, err = strconv.Atoi(d.Type)
	if err != nil {
		apierror.GenerateError("Trouble parsing contact type Id.", err, rw, req)
		return ""
	}

	d.Type, err = contact.GetContactTypeNameFromId(ct.ID)
	if err != nil {
		apierror.GenerateError("Trouble getting contact type from Id.", err, rw, req)
		return ""
	}

	if d.Type == "" {
		d.Type = "New Customer"
		d.Subject = "Becoming a Dealer"
		subject = d.Subject
	} else {
		d.Subject = "Contact Submission regarding " + d.Type + ", from " + d.FirstName + " " + d.LastName + "."
	}

	//state/country
	st := d.State
	id, err := strconv.Atoi(st)
	if id > 0 && err == nil {
		countries, err := geography.GetAllCountriesAndStates()
		if err == nil {
			for _, ctry := range countries {
				for _, state := range *ctry.States {
					if state.Id == id {
						d.State = state.State
						d.Country = ctry.Country
						break
					}
				}
			}
		}
	}

	if err := d.Add(dtx); err != nil {
		apierror.GenerateError("Trouble adding contact", err, rw, req)
		return ""
	}

	emailBody := fmt.Sprintf(
		`This %s contact is inquiring about: %s.
				Name: %s
				Email: %s
				Phone: %s
				Address 1: %s
				Address 2: %s
				City, State, Zip: %s, %s %s
				Country: %s
				Subject: %s 
				Message: %s`,
		brandName,
		d.Type,
		d.FirstName+" "+d.LastName,
		d.Email,
		d.Phone,
		d.Address1,
		d.Address2,
		d.City, d.State, d.PostalCode,
		d.Country,
		d.Subject,
		d.Message,
	)

	emailAppend1 := fmt.Sprintf(`
			Business: %s
			Business Type: %s`,
		d.BusinessName,
		d.BusinessType.Type,
	)
	if d.BusinessName != "" || d.BusinessType.Type != "" {
		emailBody += emailAppend1
	}

	if emailBody != "" && *noEmail == false {
		if err := contact.SendEmail(ct, subject, emailBody); err != nil {
			apierror.GenerateError("Trouble sending email to receivers", err, rw, req)
			return ""
		}
	}
	return encoding.Must(enc.Encode(d))
}