This repository has been archived by the owner on Jan 24, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
organization_processor.go
105 lines (96 loc) · 3.09 KB
/
organization_processor.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
package gismo
import (
"context"
"errors"
"fmt"
"time"
"github.com/ugent-library/old-people-service/models"
)
type OrganizationProcessor struct {
repository models.Repository
}
func NewOrganizationProcessor(repo models.Repository) *OrganizationProcessor {
return &OrganizationProcessor{
repository: repo,
}
}
func (op *OrganizationProcessor) Process(buf []byte) (*models.Message, error) {
msg, err := ParseOrganizationMessage(buf)
if err != nil {
return nil, err
}
ctx := context.TODO()
org, err := op.repository.GetOrganizationByIdentifier(ctx, models.NewURN("gismo_id", msg.ID))
if errors.Is(err, models.ErrNotFound) {
org = models.NewOrganization()
} else if err != nil {
return nil, fmt.Errorf("%w: unable to fetch organization record: %s", models.ErrFatal, err)
}
if msg.Source == "gismo.organization.update" {
now := time.Now()
org.NameDut = ""
org.NameEng = ""
org.Type = "organization"
org.Parent = nil
org.ClearIdentifier()
org.AddIdentifier(models.NewURN("gismo_id", msg.ID))
// only recent values needed: name_dut, name_eng, type
// all values needed: ugent_memorialis_id, code, biblio_code
for _, attr := range msg.Attributes {
withinDateRange := attr.ValidAt(now)
switch attr.Name {
case "parent_id":
orgParentByGismo, err := op.repository.GetOrganizationByIdentifier(ctx, models.NewURN("gismo_id", attr.Value))
if errors.Is(err, models.ErrNotFound) {
orgParentByGismo := models.NewOrganization()
orgParentByGismo.AddIdentifier(models.NewURN("gismo_id", attr.Value))
orgParentByGismo, err = op.repository.CreateOrganization(ctx, orgParentByGismo)
if err != nil {
return nil, fmt.Errorf("%w: unable to create parent organization: %s", models.ErrFatal, err)
}
org.AddParent(models.OrganizationParent{
ID: orgParentByGismo.ID,
From: attr.StartDate,
Until: attr.EndDate,
})
} else if err != nil {
return nil, fmt.Errorf("%w: unable to query database: %s", models.ErrFatal, err)
} else {
org.AddParent(models.OrganizationParent{
ID: orgParentByGismo.ID,
From: attr.StartDate,
Until: attr.EndDate,
})
}
case "acronym":
org.Acronym = attr.Value
case "name_dut":
if withinDateRange {
org.NameDut = attr.Value
}
case "name_eng":
if withinDateRange {
org.NameEng = attr.Value
}
case "type":
org.Type = attr.Value
case "ugent_memorialis_id":
org.AddIdentifier(models.NewURN("ugent_memorialis_id", attr.Value))
case "code":
org.AddIdentifier(models.NewURN("ugent_id", attr.Value))
case "biblio_code":
org.AddIdentifier(models.NewURN("biblio_id", attr.Value))
}
}
if _, err := op.repository.SaveOrganization(ctx, org); err != nil {
return nil, fmt.Errorf("%w: unable to save organization record: %s", models.ErrFatal, err)
}
} else if msg.Source == "gismo.organization.delete" {
if org.IsStored() {
if err := op.repository.DeleteOrganization(ctx, org.ID); err != nil {
return nil, fmt.Errorf("%w: unable to delete organization record: %s", models.ErrFatal, err)
}
}
}
return msg, nil
}