Example #1
0
// POST admin add driver to company
func AdminCompanyDriverAdd(w http.ResponseWriter, r *http.Request, c *web.Context) {
	if !c.CheckAuth(w, r, "/login", "admin", "employee", "developer") {
		return
	}
	if !service.CanUpdate("", r.FormValue("email")) {
		c.SetFlash("alertError", "Email already registered")
		http.Redirect(w, r, "/admin/company/"+c.GetPathVar("companyId")+"/driver/new", 303)
		return
	}
	r.ParseForm()
	driver, user := service.NewDriver(r.Form)
	service.SaveDriver(driver)
	service.SaveUser(user)
	c.SetFlash("alertSuccess", "Successfully added driver")
	http.Redirect(w, r, "/admin/company/"+c.GetPathVar("companyId")+"/driver", 303)
	return
}
func postCompanyRegister(w http.ResponseWriter, r *http.Request, c *web.Context) {
	company, ok := service.FindOneCompanyBySlug(c.GetPathVar("slug"))
	if !ok || !company.Feature {
		fmt.Fprintf(w, "404 Not Page Found")
		return
	}
	if !service.CanUpdate("", r.FormValue("email")) {
		c.SetFlash("alertError", "Email already registered")
		http.Redirect(w, r, "/"+c.GetPathVar("slug")+"/register", 303)
		return
	}
	driver, user := service.NewDriver(r.Form)
	fmt.Println(driver.DOB)
	dobTS, err := time.Parse("2006-01-02", driver.DOB)
	if err != nil {
		c.SetFlash("alertError", "Error invalid date of birth")
		http.Redirect(w, r, "/"+c.GetPathVar("slug")+"/register", 303)
		return
	}
	if age := service.GetDriverAge(dobTS.UnixNano()); age < company.AgeRequirement {
		c.SetFlash("alertError", "Not old enough sucker")
		user.Active = false
		driver.Status = 0
		driver.CompanyId = ""
		service.SaveUser(user)
		service.SaveDriver(driver)
		http.Redirect(w, r, "/"+c.GetPathVar("slug")+"/register", 303)
		return
	}
	document := service.Document{
		Id:         util.UUID4(),
		DocumentId: "100",
		CompanyId:  company.Id,
		DriverId:   driver.Id,
		Name:       "dqf-100",
		Complete:   false,
	}
	service.SaveUser(user)
	service.SaveDriver(driver)
	service.SaveDocument(document)
	c.SetFlash("alertSuccess", "Successfully Registered for "+company.Name+". Please Login")
	http.Redirect(w, r, "/"+c.GetPathVar("slug")+"/login", 303)
	return
}