Gastansicht, Verbesserungen bei Planung
This commit is contained in:
1 parent
b3afa3ef32
commit
c08d43e7dd
38 files changed
+3845
-219
No files matched your search
+60
-15
@@ -49,6 +49,25 @@ func encodeInts(values []int) string {
|
||||
return string(data)
|
||||
}
|
||||
|
||||
func encodeWeekdayPeriods(values []domain.WeekdayPeriodPreference) string {
|
||||
if values == nil {
|
||||
values = []domain.WeekdayPeriodPreference{}
|
||||
}
|
||||
data, err := json.Marshal(values)
|
||||
if err != nil {
|
||||
return "[]"
|
||||
}
|
||||
return string(data)
|
||||
}
|
||||
|
||||
func decodeWeekdayPeriods(value string) []domain.WeekdayPeriodPreference {
|
||||
var out []domain.WeekdayPeriodPreference
|
||||
if err := json.Unmarshal([]byte(value), &out); err != nil {
|
||||
return nil
|
||||
}
|
||||
return out
|
||||
}
|
||||
|
||||
// Churches lists a tenant's churches.
|
||||
func (s *Store) Churches(ctx context.Context, tenantID string) ([]domain.Church, error) {
|
||||
rows, err := s.db.QueryContext(ctx, `
|
||||
@@ -152,8 +171,9 @@ func (s *Store) DeleteChurch(ctx context.Context, tenantID, id string) error {
|
||||
// People lists a tenant's volunteers.
|
||||
func (s *Store) People(ctx context.Context, tenantID string) ([]domain.Person, error) {
|
||||
rows, err := s.db.QueryContext(ctx, `
|
||||
SELECT id, first_name, last_name, roles, email, preferred_church_ids,
|
||||
preferred_weekdays, preferred_service_titles, max_per_month, active, notes
|
||||
SELECT id, short_code, first_name, last_name, roles, email, preferred_church_ids,
|
||||
preferred_weekdays, preferred_weekday_periods, preferred_service_titles,
|
||||
max_per_month, allow_beyond_preferences, active, notes
|
||||
FROM people WHERE tenant_id=? ORDER BY last_name COLLATE NOCASE, first_name COLLATE NOCASE`, tenantID)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
@@ -183,8 +203,9 @@ func (s *Store) People(ctx context.Context, tenantID string) ([]domain.Person, e
|
||||
// Person loads one volunteer.
|
||||
func (s *Store) Person(ctx context.Context, tenantID, id string) (*domain.Person, error) {
|
||||
row := s.db.QueryRowContext(ctx, `
|
||||
SELECT id, first_name, last_name, roles, email, preferred_church_ids,
|
||||
preferred_weekdays, preferred_service_titles, max_per_month, active, notes
|
||||
SELECT id, short_code, first_name, last_name, roles, email, preferred_church_ids,
|
||||
preferred_weekdays, preferred_weekday_periods, preferred_service_titles,
|
||||
max_per_month, allow_beyond_preferences, active, notes
|
||||
FROM people WHERE tenant_id=? AND id=?`, tenantID, id)
|
||||
person, err := scanPerson(row)
|
||||
if errors.Is(err, sql.ErrNoRows) {
|
||||
@@ -218,11 +239,11 @@ func (s *Store) hydratePersonContacts(ctx context.Context, tenantID string, pers
|
||||
|
||||
func scanPerson(row rowScanner) (*domain.Person, error) {
|
||||
var person domain.Person
|
||||
var roles, churches, weekdays, titles string
|
||||
var active int
|
||||
if err := row.Scan(&person.ID, &person.FirstName, &person.LastName, &roles,
|
||||
&person.Email, &churches, &weekdays, &titles,
|
||||
&person.MaxPerMonth, &active, &person.Notes); err != nil {
|
||||
var roles, churches, weekdays, periods, titles string
|
||||
var allowBeyond, active int
|
||||
if err := row.Scan(&person.ID, &person.ShortCode, &person.FirstName, &person.LastName, &roles,
|
||||
&person.Email, &churches, &weekdays, &periods, &titles,
|
||||
&person.MaxPerMonth, &allowBeyond, &active, &person.Notes); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
for _, raw := range decodeStrings(roles) {
|
||||
@@ -233,7 +254,9 @@ func scanPerson(row rowScanner) (*domain.Person, error) {
|
||||
}
|
||||
person.PreferredChurchIDs = decodeStrings(churches)
|
||||
person.PreferredWeekdays = decodeInts(weekdays)
|
||||
person.PreferredWeekdayPeriods = decodeWeekdayPeriods(periods)
|
||||
person.PreferredServiceTitles = decodeStrings(titles)
|
||||
person.AllowBeyondPreferences = allowBeyond != 0
|
||||
person.Active = active != 0
|
||||
return &person, nil
|
||||
}
|
||||
@@ -285,21 +308,28 @@ func (s *Store) UpsertPerson(ctx context.Context, tenantID string, person domain
|
||||
email = ""
|
||||
}
|
||||
_, err := tx.ExecContext(ctx, `
|
||||
INSERT INTO people(tenant_id, id, first_name, last_name, roles, email,
|
||||
preferred_church_ids, preferred_weekdays, preferred_service_titles,
|
||||
max_per_month, active, notes)
|
||||
VALUES(?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)
|
||||
INSERT INTO people(tenant_id, id, short_code, first_name, last_name, roles, email,
|
||||
preferred_church_ids, preferred_weekdays, preferred_weekday_periods,
|
||||
preferred_service_titles, max_per_month, allow_beyond_preferences, active, notes)
|
||||
VALUES(?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)
|
||||
ON CONFLICT(tenant_id, id) DO UPDATE SET
|
||||
short_code=excluded.short_code,
|
||||
first_name=excluded.first_name, last_name=excluded.last_name, roles=excluded.roles,
|
||||
email=CASE WHEN ? THEN excluded.email ELSE people.email END,
|
||||
preferred_church_ids=excluded.preferred_church_ids,
|
||||
preferred_weekdays=excluded.preferred_weekdays,
|
||||
preferred_weekday_periods=excluded.preferred_weekday_periods,
|
||||
preferred_service_titles=excluded.preferred_service_titles,
|
||||
max_per_month=excluded.max_per_month, active=excluded.active, notes=excluded.notes`,
|
||||
tenantID, person.ID, person.FirstName, person.LastName, encodeStrings(roleStrings),
|
||||
max_per_month=excluded.max_per_month,
|
||||
allow_beyond_preferences=excluded.allow_beyond_preferences,
|
||||
active=excluded.active, notes=excluded.notes`,
|
||||
tenantID, person.ID, strings.TrimSpace(person.ShortCode),
|
||||
person.FirstName, person.LastName, encodeStrings(roleStrings),
|
||||
email, encodeStrings(person.PreferredChurchIDs),
|
||||
encodeInts(person.PreferredWeekdays),
|
||||
encodeWeekdayPeriods(person.PreferredWeekdayPeriods),
|
||||
encodeStrings(person.PreferredServiceTitles), person.MaxPerMonth,
|
||||
boolInt(person.AllowBeyondPreferences),
|
||||
boolInt(person.Active), person.Notes, boolInt(updateLegacyContact))
|
||||
if err != nil {
|
||||
return err
|
||||
@@ -360,6 +390,21 @@ func (s *Store) DeletePerson(ctx context.Context, tenantID, id string) error {
|
||||
ScopePerson, tenantID, id); err != nil {
|
||||
return err
|
||||
}
|
||||
if _, err := tx.ExecContext(ctx,
|
||||
"DELETE FROM contact_shares WHERE tenant_id=? AND (owner_person_id=? OR viewer_user_id IN ("+
|
||||
"SELECT id FROM users WHERE linked_tenant_id=? AND linked_person_id=?))",
|
||||
tenantID, id, tenantID, id); err != nil {
|
||||
return err
|
||||
}
|
||||
if _, err := tx.ExecContext(ctx,
|
||||
"DELETE FROM contact_reviews WHERE tenant_id=? AND owner_person_id=?", tenantID, id); err != nil {
|
||||
return err
|
||||
}
|
||||
if _, err := tx.ExecContext(ctx,
|
||||
"DELETE FROM swap_requests WHERE tenant_id=? AND (requester_person_id=? OR target_person_id=?)",
|
||||
tenantID, id, id); err != nil {
|
||||
return err
|
||||
}
|
||||
_, err := tx.ExecContext(ctx, "DELETE FROM people WHERE tenant_id=? AND id=?", tenantID, id)
|
||||
return err
|
||||
})
|
||||
|
||||
Reference in new issue
Block a user