Web Variante in Go V0.1

This commit is contained in:
concierge committed 2026-11-12 20:00:33 +01:00
1 parent f9e3e94629
commit 692b9b6ef8
60 files changed
+13140

No files matched your search

+239
View File
@@ -0,0 +1,239 @@
package store
import (
"context"
"encoding/json"
"errors"
"fmt"
"time"
"lektorenplan/domain"
)
// ExchangeScope selects what an exchange file contains.
type ExchangeScope string
const (
ScopeResources ExchangeScope = "resources"
ScopePlan ExchangeScope = "plan"
)
func (s ExchangeScope) Title() string {
if s == ScopeResources {
return "Ressourcen"
}
return "Planungsabschnitt"
}
// ExchangeRange is a serialisable inclusive day range.
type ExchangeRange struct {
Start string `json:"start"`
End string `json:"end"`
}
// ResourceBundle carries configuration and volunteer data.
type ResourceBundle struct {
Churches []domain.Church `json:"churches"`
People []domain.Person `json:"people"`
Absences []domain.Absence `json:"absences"`
Templates []domain.ServiceTemplate `json:"templates"`
Exceptions []domain.ServiceException `json:"exceptions"`
}
// PlanExchange is the JSON document exchanged with other installations.
type PlanExchange struct {
FormatVersion int `json:"formatVersion"`
ExportedAt time.Time `json:"exportedAt"`
AppName string `json:"appName"`
Scope ExchangeScope `json:"scope"`
Range ExchangeRange `json:"range"`
Resources ResourceBundle `json:"resources"`
Services []domain.ServiceOccurrence `json:"services"`
Assignments []domain.Assignment `json:"assignments"`
LiturgicalDays []domain.LiturgicalDay `json:"liturgicalDays"`
Note string `json:"note"`
}
// ExchangeSummary reports the imported record counts.
type ExchangeSummary struct {
Scope ExchangeScope `json:"scope"`
Range ExchangeRange `json:"range"`
Churches int `json:"churches"`
People int `json:"people"`
Templates int `json:"templates"`
Services int `json:"services"`
Assignments int `json:"assignments"`
LiturgicalDays int `json:"liturgicalDays"`
}
// Summary returns counts for display after import or before export.
func (e PlanExchange) Summary() ExchangeSummary {
return ExchangeSummary{
Scope: e.Scope, Range: e.Range,
Churches: len(e.Resources.Churches), People: len(e.Resources.People),
Templates: len(e.Resources.Templates), Services: len(e.Services),
Assignments: len(e.Assignments), LiturgicalDays: len(e.LiturgicalDays),
}
}
// MarshalJSON indents the exchange document for readability.
func (e PlanExchange) MarshalJSON() ([]byte, error) {
type alias PlanExchange
return json.MarshalIndent(alias(e), "", " ")
}
// ExportResources builds a resources-only exchange document.
func (s *Store) ExportResources(ctx context.Context, tenantID string) (PlanExchange, error) {
churches, err := s.Churches(ctx, tenantID)
if err != nil {
return PlanExchange{}, err
}
people, err := s.People(ctx, tenantID)
if err != nil {
return PlanExchange{}, err
}
absences, err := s.Absences(ctx, tenantID)
if err != nil {
return PlanExchange{}, err
}
templates, err := s.Templates(ctx, tenantID)
if err != nil {
return PlanExchange{}, err
}
exceptions, err := s.Exceptions(ctx, tenantID)
if err != nil {
return PlanExchange{}, err
}
return PlanExchange{
FormatVersion: 1, ExportedAt: time.Now().UTC(), AppName: "LektorenPlan Web",
Scope: ScopeResources,
Resources: ResourceBundle{
Churches: churches, People: people, Absences: absences,
Templates: templates, Exceptions: exceptions,
},
Note: "Export aus LektorenPlan Web",
}, nil
}
// ExportPlan builds a plan exchange document for a range.
func (s *Store) ExportPlan(ctx context.Context, tenantID string, r domain.DayRange) (PlanExchange, error) {
resources, err := s.ExportResources(ctx, tenantID)
if err != nil {
return PlanExchange{}, err
}
services, err := s.Services(ctx, tenantID, r)
if err != nil {
return PlanExchange{}, err
}
serviceIDs := make([]string, len(services))
for i, service := range services {
serviceIDs[i] = service.ID
}
assignments, err := s.AssignmentsForServices(ctx, tenantID, serviceIDs)
if err != nil {
return PlanExchange{}, err
}
liturgicalDays, err := s.LiturgicalDays(ctx, r)
if err != nil {
return PlanExchange{}, err
}
personIDs := map[string]bool{}
for _, assignment := range assignments {
personIDs[assignment.PersonID] = true
}
var relevantPeople []domain.Person
for _, person := range resources.Resources.People {
if personIDs[person.ID] {
relevantPeople = append(relevantPeople, person)
}
}
var relevantAbsences []domain.Absence
for _, absence := range resources.Resources.Absences {
if personIDs[absence.PersonID] {
relevantAbsences = append(relevantAbsences, absence)
}
}
resources.Resources.People = relevantPeople
resources.Resources.Absences = relevantAbsences
return PlanExchange{
FormatVersion: 1, ExportedAt: time.Now().UTC(), AppName: "LektorenPlan Web",
Scope: ScopePlan,
Range: ExchangeRange{Start: domain.Key(r.Start), End: domain.Key(r.End)},
Resources: resources.Resources, Services: services, Assignments: assignments,
LiturgicalDays: liturgicalDays,
Note: fmt.Sprintf("Export aus LektorenPlan Web · %s bis %s",
domain.Key(r.Start), domain.Key(r.End)),
}, nil
}
// ParseExchange decodes an uploaded JSON document.
func ParseExchange(data []byte) (PlanExchange, error) {
var exchange PlanExchange
if err := json.Unmarshal(data, &exchange); err != nil {
return exchange, fmt.Errorf("Die Datei konnte nicht gelesen werden: %w", err)
}
if exchange.FormatVersion < 1 {
exchange.FormatVersion = 1
}
if exchange.Scope == "" {
exchange.Scope = ScopePlan
}
if exchange.Scope != ScopeResources && exchange.Scope != ScopePlan {
return exchange, errors.New("Unbekannter Dateityp")
}
return exchange, nil
}
// ApplyExchange imports resources, services and assignments into a tenant.
func (s *Store) ApplyExchange(ctx context.Context, tenantID string, exchange PlanExchange, replaceExisting bool) (ExchangeSummary, error) {
if replaceExisting {
if err := s.DeleteAllTenantData(ctx, tenantID); err != nil {
return ExchangeSummary{}, err
}
}
// Use the store methods so validation and secondary cleanup stay in one place.
for _, church := range exchange.Resources.Churches {
if err := s.UpsertChurch(ctx, tenantID, church); err != nil {
return ExchangeSummary{}, err
}
}
for _, person := range exchange.Resources.People {
if err := s.UpsertPerson(ctx, tenantID, person); err != nil {
return ExchangeSummary{}, err
}
}
for _, absence := range exchange.Resources.Absences {
if err := s.UpsertAbsence(ctx, tenantID, absence); err != nil {
return ExchangeSummary{}, err
}
}
for _, template := range exchange.Resources.Templates {
if err := s.UpsertTemplate(ctx, tenantID, template); err != nil {
return ExchangeSummary{}, err
}
}
for _, exception := range exchange.Resources.Exceptions {
if err := s.UpsertException(ctx, tenantID, exception); err != nil {
return ExchangeSummary{}, err
}
}
if len(exchange.LiturgicalDays) > 0 {
if err := s.UpsertLiturgicalDays(ctx, exchange.LiturgicalDays); err != nil {
return ExchangeSummary{}, err
}
}
for _, service := range exchange.Services {
if _, err := s.UpsertService(ctx, tenantID, service); err != nil {
return ExchangeSummary{}, err
}
}
for _, assignment := range exchange.Assignments {
if err := s.UpsertAssignment(ctx, tenantID, assignment); err != nil {
if errors.Is(err, ErrConflict) {
continue
}
return ExchangeSummary{}, err
}
}
return exchange.Summary(), nil
}