Changes
4 changed files (+184/-2)
-
-
@@ -5,6 +5,7 @@ package projectionimport ( "database/sql" "slices" "google.golang.org/protobuf/proto"
-
@@ -31,7 +32,8 @@ Worked: proto.String("出勤"),SkipWork: proto.String("欠勤"), PaidLeave: proto.String("年休"), }, NumberOfAdmins: proto.Uint32(0), NumberOfAdmins: proto.Uint32(0), CustomAttributes: []*workspace.Workspace_CustomAttribute{}, }, } }
-
@@ -96,6 +98,30 @@ }if v.AbbreviationsConfigured.GetPaidLeave() != "" { p.Projection.Abbreviations.PaidLeave = v.AbbreviationsConfigured.PaidLeave } case *workspaceEventsv1.Event_CustomAttributeDefined: id := v.CustomAttributeDefined.GetId() found := false for _, attr := range p.Projection.CustomAttributes { if attr.GetId() == id { attr.DisplayName = v.CustomAttributeDefined.DisplayName found = true break } } if !found { p.Projection.CustomAttributes = append(p.Projection.CustomAttributes, &workspace.Workspace_CustomAttribute{ Id: v.CustomAttributeDefined.Id, DisplayName: v.CustomAttributeDefined.DisplayName, }) } case *workspaceEventsv1.Event_CustomAttributeUndefined: for i := len(p.Projection.CustomAttributes) - 1; i >= 0; i-- { if p.Projection.CustomAttributes[i].GetId() == v.CustomAttributeUndefined.GetId() { p.Projection.CustomAttributes = slices.Delete(p.Projection.CustomAttributes, i, i+1) } } }
-
-
-
@@ -113,3 +113,141 @@ if abbr.GetPaidLeave() == "" {t.Error("paid_leave has no default abbreviation") } } func TestCustomAttributes(t *testing.T) { logger := slog.New(slog.NewTextHandler(io.Discard, nil)) db, err := sql.Open("sqlite", ":memory:") if err != nil { t.Fatal(err) } core, err := core.New(db, logger) if err != nil { t.Fatal(err) } tx, err := core.DB.Begin() if err != nil { t.Fatal(err) } err = event.AppendEvents(tx, []*eventV1.Event{ // Should be ignored { Event: &eventV1.Event_WorkspaceEvent{ WorkspaceEvent: &workspaceV1.Event{ Event: &workspaceV1.Event_CustomAttributeUndefined{ CustomAttributeUndefined: &workspaceV1.CustomAttributeUndefined{ Id: proto.String("foo"), }, }, }, }, }, { Event: &eventV1.Event_WorkspaceEvent{ WorkspaceEvent: &workspaceV1.Event{ Event: &workspaceV1.Event_CustomAttributeDefined{ CustomAttributeDefined: &workspaceV1.CustomAttributeDefined{ Id: proto.String("foo"), DisplayName: proto.String("Foo"), }, }, }, }, }, { Event: &eventV1.Event_WorkspaceEvent{ WorkspaceEvent: &workspaceV1.Event{ Event: &workspaceV1.Event_CustomAttributeDefined{ CustomAttributeDefined: &workspaceV1.CustomAttributeDefined{ Id: proto.String("bar"), DisplayName: proto.String("Bar"), }, }, }, }, }, { Event: &eventV1.Event_WorkspaceEvent{ WorkspaceEvent: &workspaceV1.Event{ Event: &workspaceV1.Event_CustomAttributeDefined{ CustomAttributeDefined: &workspaceV1.CustomAttributeDefined{ Id: proto.String("baz"), DisplayName: proto.String("Baz"), }, }, }, }, }, { Event: &eventV1.Event_WorkspaceEvent{ WorkspaceEvent: &workspaceV1.Event{ Event: &workspaceV1.Event_CustomAttributeDefined{ CustomAttributeDefined: &workspaceV1.CustomAttributeDefined{ Id: proto.String("baz"), DisplayName: proto.String("Baz Baz"), }, }, }, }, }, { Event: &eventV1.Event_WorkspaceEvent{ WorkspaceEvent: &workspaceV1.Event{ Event: &workspaceV1.Event_CustomAttributeUndefined{ CustomAttributeUndefined: &workspaceV1.CustomAttributeUndefined{ Id: proto.String("bar"), }, }, }, }, }, }) if err != nil { t.Fatal(err) } p, err := projection.GetWorkspace(tx) if err != nil { t.Fatal(err) } if err := event.UpdateProjections(tx, p); err != nil { t.Fatal(err) } foo := p.Projection.CustomAttributes[0] if foo == nil { t.Error("Expected foo to be set, got nil") } else { if foo.GetId() != "foo" { t.Errorf("Expected foo to have ID \"foo\", got \"%s\"", foo.GetId()) } if foo.GetDisplayName() != "Foo" { t.Errorf("Expected foo to be named \"Foo\", got \"%s\"", foo.GetDisplayName()) } } if len(p.Projection.CustomAttributes) != 2 { t.Errorf( "Expected custom attribute definitions to have 2 items, got %d", len(p.Projection.CustomAttributes), ) } baz := p.Projection.CustomAttributes[1] if baz == nil { t.Error("Expected bar to be set, got nil") } else { switch baz.GetDisplayName() { case "Baz": t.Error("Name of baz was not updated") case "Baz Baz": break default: t.Errorf("Expected baz to be named \"Baz Baz\", got \"%s\"", baz.GetDisplayName()) } } }
-
-
-
@@ -54,6 +54,17 @@ for i, user := range u.Users {users[i] = projectionUserToMessage(user) } customAttributeDefs := make([]*workspaceV2.CustomAttributeDefinition, len(p.CustomAttributes)) for i, def := range p.CustomAttributes { customAttributeDefs[i] = &workspaceV2.CustomAttributeDefinition{ Id: &workspaceV2.CustomAttributeDefinitionID{ Value: def.Id, }, DisplayName: def.DisplayName, } } return &workspaceV2.Workspace{ DisplayName: p.DisplayName, HasAdmin: proto.Bool(p.GetNumberOfAdmins() > 0),
-
@@ -63,6 +74,7 @@ Worked: p.Abbreviations.Worked,SkipWork: p.Abbreviations.SkipWork, PaidLeave: p.Abbreviations.PaidLeave, }, Users: users, Users: users, CustomAttributeDefinition: customAttributeDefs, } }
-
-
-
@@ -11,11 +11,17 @@ message Workspace {uint32 number_of_admins = 1; string display_name = 2; Abbreviations abbreviations = 3; repeated CustomAttribute custom_attributes = 4; message Abbreviations { string day_off = 1; string worked = 2; string skip_work = 3; string paid_leave = 4; } message CustomAttribute { string id = 1; string display_name = 2; } }
-