Ejemplo n.º 1
0
//NewInternship turns a convention into an internship.
//Returns the internship, the student token and an error
func (s *Store) NewInternship(c schema.Convention) (schema.Internship, []byte, error) {
	i := schema.Internship{
		Convention: c,
	}
	if c.Begin.After(c.End) {
		return i, []byte{}, schema.ErrInvalidPeriod
	}

	tx := newTxErr(s.db)
	tx.Exec(insertConvention, c.Student.User.Person.Email, c.Begin.Truncate(time.Minute).UTC(), c.End.Truncate(time.Minute).UTC(), c.Tutor.Person.Email, c.Company.Name, c.Company.WWW, c.Supervisor.Firstname, c.Supervisor.Lastname, c.Supervisor.Email, c.Supervisor.Tel, c.Company.Title, c.Creation.Truncate(time.Minute).UTC(), c.ForeignCountry, c.Lab, c.Gratification)
	token := randomBytes(32)
	//We delete in case we start a reset over an unvalidated account
	tx.Exec(deletePasswordRenewalRequest, c.Student.User.Person.Email)
	tx.Exec(startPasswordRenewal, c.Student.User.Person.Email, token)

	i.Reports = make([]schema.ReportHeader, len(s.config.Reports))
	//Instantiate the reports and the surveys
	for idx, report := range s.config.Reports {
		tx.Exec(insertReport, c.Student.User.Person.Email, report.Kind, report.Delivery.Value(c.Begin).Truncate(time.Minute).UTC(), false, report.Grade)
		i.Reports[idx] = schema.ReportHeader{
			Kind:     report.Kind,
			ToGrade:  report.Grade,
			Grade:    -1,
			Deadline: report.Delivery.Value(c.Begin).Truncate(time.Minute).UTC(),
		}
	}

	i.Surveys = make([]schema.SurveyHeader, len(s.config.Surveys))
	for idx, survey := range s.config.Surveys {
		token = randomBytes(16)
		inv := survey.Invitation.Value(c.Begin).Truncate(time.Minute).UTC()
		dead := inv.Add(survey.Deadline.Duration).Truncate(time.Minute).UTC()
		tx.Exec(insertSurvey, c.Student.User.Person.Email, survey.Kind, token, inv, inv, dead)
		i.Surveys[idx] = schema.SurveyHeader{
			Kind:           survey.Kind,
			Token:          string(token),
			Deadline:       dead,
			Invitation:     inv,
			LastInvitation: inv,
		}
	}

	//refresh student & tutor informations for a clean version
	//as here, we only consider in c the person emails
	stu, err := s.Student(c.Student.User.Person.Email)
	if err != nil {
		tx.err = err
		return i, []byte{}, tx.Done()
	}
	tut, err := s.User(c.Tutor.Person.Email)
	if err != nil {
		tx.err = err
		return i, []byte{}, tx.Done()
	}
	i.Convention.Tutor = tut
	i.Convention.Student = stu
	return i, token, tx.Done()
}
Ejemplo n.º 2
0
//Internships returns all the internships. Not necessarily validated
func (s *Store) Internships() (schema.Internships, error) {
	conventions, err := s.conventions()
	if err != nil {
		return schema.Internships{}, err
	}
	defs, err := s.defenses()
	if err != nil {
		return schema.Internships{}, err
	}

	surveys, err := s.allSurveys()
	if err != nil {
		return schema.Internships{}, err
	}

	reports, err := s.allReports()
	if err != nil {
		return schema.Internships{}, err
	}

	ints := make([]schema.Internship, 0, 0)
	for _, c := range conventions {
		stu := c.Student.User.Person.Email
		i := schema.Internship{Convention: c}
		d, ok := defs[stu]
		if ok {
			i.Defense = d
		}
		s, ok := surveys[stu]
		if ok {
			i.Surveys = s
		}
		r, ok := reports[stu]
		if ok {
			i.Reports = r
		}
		ints = append(ints, i)
	}
	return ints, err
}