2023-02-27 21:36:43 +00:00
|
|
|
package query
|
|
|
|
|
|
|
|
import (
|
|
|
|
"time"
|
|
|
|
|
|
|
|
"github.com/zitadel/zitadel/internal/eventstore"
|
|
|
|
"github.com/zitadel/zitadel/internal/repository/quota"
|
|
|
|
)
|
|
|
|
|
|
|
|
type quotaNotificationsReadModel struct {
|
|
|
|
eventstore.ReadModel
|
2023-03-28 22:09:06 +00:00
|
|
|
periodStart time.Time
|
|
|
|
latestDueThresholds map[string]uint16
|
2023-02-27 21:36:43 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func newQuotaNotificationsReadModel(aggregateId, instanceId, resourceOwner string, periodStart time.Time) *quotaNotificationsReadModel {
|
|
|
|
return "aNotificationsReadModel{
|
|
|
|
ReadModel: eventstore.ReadModel{
|
|
|
|
AggregateID: aggregateId,
|
|
|
|
InstanceID: instanceId,
|
|
|
|
ResourceOwner: resourceOwner,
|
|
|
|
},
|
2023-03-28 22:09:06 +00:00
|
|
|
periodStart: periodStart,
|
|
|
|
latestDueThresholds: make(map[string]uint16),
|
2023-02-27 21:36:43 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func (rm *quotaNotificationsReadModel) Query() *eventstore.SearchQueryBuilder {
|
|
|
|
return eventstore.NewSearchQueryBuilder(eventstore.ColumnsEvent).
|
|
|
|
ResourceOwner(rm.ResourceOwner).
|
|
|
|
AllowTimeTravel().
|
|
|
|
AddQuery().
|
|
|
|
InstanceID(rm.InstanceID).
|
|
|
|
AggregateTypes(quota.AggregateType).
|
|
|
|
AggregateIDs(rm.AggregateID).
|
|
|
|
CreationDateAfter(rm.periodStart).
|
2023-03-28 22:09:06 +00:00
|
|
|
EventTypes(quota.NotificationDueEventType).Builder()
|
2023-02-27 21:36:43 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func (rm *quotaNotificationsReadModel) Reduce() error {
|
|
|
|
for _, event := range rm.Events {
|
2023-03-28 22:09:06 +00:00
|
|
|
e := event.(*quota.NotificationDueEvent)
|
|
|
|
rm.latestDueThresholds[e.ID] = e.Threshold
|
2023-02-27 21:36:43 +00:00
|
|
|
}
|
|
|
|
return rm.ReadModel.Reduce()
|
|
|
|
}
|