2023-02-27 22:36:43 +01: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-29 00:09:06 +02:00
|
|
|
periodStart time.Time
|
|
|
|
latestDueThresholds map[string]uint16
|
2023-02-27 22:36:43 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
func newQuotaNotificationsReadModel(aggregateId, instanceId, resourceOwner string, periodStart time.Time) *quotaNotificationsReadModel {
|
|
|
|
return "aNotificationsReadModel{
|
|
|
|
ReadModel: eventstore.ReadModel{
|
|
|
|
AggregateID: aggregateId,
|
|
|
|
InstanceID: instanceId,
|
|
|
|
ResourceOwner: resourceOwner,
|
|
|
|
},
|
2023-03-29 00:09:06 +02:00
|
|
|
periodStart: periodStart,
|
|
|
|
latestDueThresholds: make(map[string]uint16),
|
2023-02-27 22:36:43 +01: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-29 00:09:06 +02:00
|
|
|
EventTypes(quota.NotificationDueEventType).Builder()
|
2023-02-27 22:36:43 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
func (rm *quotaNotificationsReadModel) Reduce() error {
|
|
|
|
for _, event := range rm.Events {
|
2023-03-29 00:09:06 +02:00
|
|
|
e := event.(*quota.NotificationDueEvent)
|
|
|
|
rm.latestDueThresholds[e.ID] = e.Threshold
|
2023-02-27 22:36:43 +01:00
|
|
|
}
|
|
|
|
return rm.ReadModel.Reduce()
|
|
|
|
}
|