| package entity |
|
|
| import ( |
| "errors" |
| "fmt" |
| "time" |
|
|
| "github.com/openmeterio/openmeter/pkg/models" |
| ) |
|
|
| |
| type ProgressID struct { |
| models.NamespacedModel `json:"namespace,inline"` |
| ID string |
| } |
|
|
| func (a *ProgressID) Validate() error { |
| var errs []error |
|
|
| if err := a.NamespacedModel.Validate(); err != nil { |
| errs = append(errs, fmt.Errorf("namespaced model: %w", err)) |
| } |
|
|
| if a.ID == "" { |
| errs = append(errs, errors.New("id is required")) |
| } |
|
|
| return errors.Join(errs...) |
| } |
|
|
| |
| type Progress struct { |
| ProgressID `json:"id"` |
|
|
| |
| Success uint64 `json:"success"` |
| |
| Failed uint64 `json:"failed"` |
| |
| Total uint64 `json:"total"` |
| |
| UpdatedAt time.Time `json:"updatedAt"` |
| } |
|
|
| func (a *Progress) Validate() error { |
| var errs []error |
|
|
| if err := a.ProgressID.Validate(); err != nil { |
| errs = append(errs, fmt.Errorf("progress id: %w", err)) |
| } |
|
|
| if a.Success+a.Failed > a.Total { |
| errs = append(errs, errors.New("success and failed must be less than or equal to total")) |
| } |
|
|
| if a.Total == 0 && (a.Success > 0 || a.Failed > 0) { |
| errs = append(errs, errors.New("success and failed must be zero when total is zero")) |
| } |
|
|
| if a.UpdatedAt.IsZero() { |
| errs = append(errs, errors.New("updated at is required")) |
| } |
|
|
| return errors.Join(errs...) |
| } |
|
|
| |
| type GetProgressInput struct { |
| ProgressID |
| } |
|
|
| func (a *GetProgressInput) Validate() error { |
| var errs []error |
|
|
| if err := a.ProgressID.Validate(); err != nil { |
| errs = append(errs, fmt.Errorf("progress id: %w", err)) |
| } |
|
|
| return errors.Join(errs...) |
| } |
|
|
| |
| type UpsertProgressInput struct { |
| Progress |
| } |
|
|
| func (a *UpsertProgressInput) Validate() error { |
| var errs []error |
|
|
| if err := a.Progress.Validate(); err != nil { |
| errs = append(errs, fmt.Errorf("progress: %w", err)) |
| } |
|
|
| return errors.Join(errs...) |
| } |
|
|