zitadel/internal/eventstore/write_model.go
Elio Bischof 8fc11a7366
feat: user api requests to resource API (#9794)
# Which Problems Are Solved

This pull request addresses a significant gap in the user service v2
API, which currently lacks methods for managing machine users.

# How the Problems Are Solved

This PR adds new API endpoints to the user service v2 to manage machine
users including their secret, keys and personal access tokens.
Additionally, there's now a CreateUser and UpdateUser endpoints which
allow to create either a human or machine user and update them. The
existing `CreateHumanUser` endpoint has been deprecated along the
corresponding management service endpoints. For details check the
additional context section.

# Additional Context

- Closes https://github.com/zitadel/zitadel/issues/9349

## More details
- API changes: https://github.com/zitadel/zitadel/pull/9680
- Implementation: https://github.com/zitadel/zitadel/pull/9763
- Tests: https://github.com/zitadel/zitadel/pull/9771

## Follow-ups

- Metadata: support managing user metadata using resource API
https://github.com/zitadel/zitadel/pull/10005
- Machine token type: support managing the machine token type (migrate
to new enum with zero value unspecified?)

---------

Co-authored-by: Copilot <175728472+Copilot@users.noreply.github.com>
Co-authored-by: Livio Spring <livio.a@gmail.com>
2025-06-04 07:17:23 +00:00

54 lines
1.4 KiB
Go

package eventstore
import (
"time"
)
// WriteModel is the minimum representation of a command side write model.
// It implements a basic reducer
// it's purpose is to reduce events to create new ones
type WriteModel struct {
AggregateID string `json:"-"`
ProcessedSequence uint64 `json:"-"`
Events []Event `json:"-"`
ResourceOwner string `json:"-"`
InstanceID string `json:"-"`
ChangeDate time.Time `json:"-"`
}
// AppendEvents adds all the events to the read model.
// The function doesn't compute the new state of the read model
func (rm *WriteModel) AppendEvents(events ...Event) {
rm.Events = append(rm.Events, events...)
}
// Reduce is the basic implementation of reducer
// If this function is extended the extending function should be the last step
func (wm *WriteModel) Reduce() error {
if len(wm.Events) == 0 {
return nil
}
latestEvent := wm.Events[len(wm.Events)-1]
if wm.AggregateID == "" {
wm.AggregateID = latestEvent.Aggregate().ID
}
if wm.ResourceOwner == "" {
wm.ResourceOwner = latestEvent.Aggregate().ResourceOwner
}
if wm.InstanceID == "" {
wm.InstanceID = latestEvent.Aggregate().InstanceID
}
wm.ProcessedSequence = latestEvent.Sequence()
wm.ChangeDate = latestEvent.CreatedAt()
// all events processed and not needed anymore
wm.Events = nil
wm.Events = []Event{}
return nil
}