2022-12-10 05:57:57 +00:00
|
|
|
package repo
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
2023-03-21 05:48:22 +00:00
|
|
|
"errors"
|
2022-12-10 05:57:57 +00:00
|
|
|
"time"
|
|
|
|
|
|
|
|
"github.com/google/uuid"
|
|
|
|
"github.com/hay-kot/homebox/backend/internal/data/ent"
|
2023-03-21 04:32:10 +00:00
|
|
|
"github.com/hay-kot/homebox/backend/internal/data/ent/group"
|
|
|
|
"github.com/hay-kot/homebox/backend/internal/data/ent/item"
|
2022-12-10 05:57:57 +00:00
|
|
|
"github.com/hay-kot/homebox/backend/internal/data/ent/maintenanceentry"
|
2023-02-27 03:42:23 +00:00
|
|
|
"github.com/hay-kot/homebox/backend/internal/data/types"
|
2022-12-10 05:57:57 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
// MaintenanceEntryRepository is a repository for maintenance entries that are
|
|
|
|
// associated with an item in the database. An entry represents a maintenance event
|
|
|
|
// that has been performed on an item.
|
|
|
|
type MaintenanceEntryRepository struct {
|
|
|
|
db *ent.Client
|
|
|
|
}
|
2023-03-21 05:48:22 +00:00
|
|
|
|
|
|
|
type MaintenanceEntryCreate struct {
|
|
|
|
CompletedDate types.Date `json:"completedDate"`
|
|
|
|
ScheduledDate types.Date `json:"scheduledDate"`
|
2024-01-04 17:55:26 +00:00
|
|
|
Name string `json:"name" validate:"required"`
|
2023-03-21 05:48:22 +00:00
|
|
|
Description string `json:"description"`
|
|
|
|
Cost float64 `json:"cost,string"`
|
|
|
|
}
|
|
|
|
|
|
|
|
func (mc MaintenanceEntryCreate) Validate() error {
|
|
|
|
if mc.CompletedDate.Time().IsZero() && mc.ScheduledDate.Time().IsZero() {
|
|
|
|
return errors.New("either completedDate or scheduledDate must be set")
|
2022-12-10 05:57:57 +00:00
|
|
|
}
|
2023-03-21 05:48:22 +00:00
|
|
|
return nil
|
|
|
|
}
|
2022-12-10 05:57:57 +00:00
|
|
|
|
2023-03-21 05:48:22 +00:00
|
|
|
type MaintenanceEntryUpdate struct {
|
|
|
|
CompletedDate types.Date `json:"completedDate"`
|
|
|
|
ScheduledDate types.Date `json:"scheduledDate"`
|
|
|
|
Name string `json:"name"`
|
|
|
|
Description string `json:"description"`
|
|
|
|
Cost float64 `json:"cost,string"`
|
|
|
|
}
|
|
|
|
|
|
|
|
func (mu MaintenanceEntryUpdate) Validate() error {
|
|
|
|
if mu.CompletedDate.Time().IsZero() && mu.ScheduledDate.Time().IsZero() {
|
|
|
|
return errors.New("either completedDate or scheduledDate must be set")
|
2022-12-10 05:57:57 +00:00
|
|
|
}
|
2023-03-21 05:48:22 +00:00
|
|
|
return nil
|
|
|
|
}
|
2022-12-10 05:57:57 +00:00
|
|
|
|
2023-03-21 05:48:22 +00:00
|
|
|
type (
|
|
|
|
MaintenanceEntry struct {
|
|
|
|
ID uuid.UUID `json:"id"`
|
2023-02-27 03:42:23 +00:00
|
|
|
CompletedDate types.Date `json:"completedDate"`
|
|
|
|
ScheduledDate types.Date `json:"scheduledDate"`
|
|
|
|
Name string `json:"name"`
|
|
|
|
Description string `json:"description"`
|
|
|
|
Cost float64 `json:"cost,string"`
|
2022-12-10 05:57:57 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
MaintenanceLog struct {
|
|
|
|
ItemID uuid.UUID `json:"itemId"`
|
|
|
|
CostAverage float64 `json:"costAverage"`
|
|
|
|
CostTotal float64 `json:"costTotal"`
|
|
|
|
Entries []MaintenanceEntry `json:"entries"`
|
|
|
|
}
|
|
|
|
)
|
|
|
|
|
|
|
|
var (
|
|
|
|
mapMaintenanceEntryErr = mapTErrFunc(mapMaintenanceEntry)
|
|
|
|
mapEachMaintenanceEntry = mapTEachFunc(mapMaintenanceEntry)
|
|
|
|
)
|
|
|
|
|
|
|
|
func mapMaintenanceEntry(entry *ent.MaintenanceEntry) MaintenanceEntry {
|
|
|
|
return MaintenanceEntry{
|
2023-02-27 03:42:23 +00:00
|
|
|
ID: entry.ID,
|
|
|
|
CompletedDate: types.Date(entry.Date),
|
|
|
|
ScheduledDate: types.Date(entry.ScheduledDate),
|
|
|
|
Name: entry.Name,
|
|
|
|
Description: entry.Description,
|
|
|
|
Cost: entry.Cost,
|
2022-12-10 05:57:57 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-03-21 19:32:48 +00:00
|
|
|
func (r *MaintenanceEntryRepository) GetScheduled(ctx context.Context, GID uuid.UUID, dt types.Date) ([]MaintenanceEntry, error) {
|
|
|
|
entries, err := r.db.MaintenanceEntry.Query().
|
|
|
|
Where(
|
|
|
|
maintenanceentry.HasItemWith(
|
|
|
|
item.HasGroupWith(group.ID(GID)),
|
|
|
|
),
|
|
|
|
maintenanceentry.ScheduledDate(dt.Time()),
|
|
|
|
maintenanceentry.Or(
|
|
|
|
maintenanceentry.DateIsNil(),
|
|
|
|
maintenanceentry.DateEQ(time.Time{}),
|
|
|
|
),
|
|
|
|
).
|
|
|
|
All(ctx)
|
|
|
|
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
return mapEachMaintenanceEntry(entries), nil
|
|
|
|
}
|
|
|
|
|
2022-12-10 05:57:57 +00:00
|
|
|
func (r *MaintenanceEntryRepository) Create(ctx context.Context, itemID uuid.UUID, input MaintenanceEntryCreate) (MaintenanceEntry, error) {
|
|
|
|
item, err := r.db.MaintenanceEntry.Create().
|
|
|
|
SetItemID(itemID).
|
2023-02-27 03:42:23 +00:00
|
|
|
SetDate(input.CompletedDate.Time()).
|
|
|
|
SetScheduledDate(input.ScheduledDate.Time()).
|
2022-12-10 05:57:57 +00:00
|
|
|
SetName(input.Name).
|
|
|
|
SetDescription(input.Description).
|
|
|
|
SetCost(input.Cost).
|
|
|
|
Save(ctx)
|
|
|
|
|
|
|
|
return mapMaintenanceEntryErr(item, err)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (r *MaintenanceEntryRepository) Update(ctx context.Context, ID uuid.UUID, input MaintenanceEntryUpdate) (MaintenanceEntry, error) {
|
|
|
|
item, err := r.db.MaintenanceEntry.UpdateOneID(ID).
|
2023-02-27 03:42:23 +00:00
|
|
|
SetDate(input.CompletedDate.Time()).
|
|
|
|
SetScheduledDate(input.ScheduledDate.Time()).
|
2022-12-10 05:57:57 +00:00
|
|
|
SetName(input.Name).
|
|
|
|
SetDescription(input.Description).
|
|
|
|
SetCost(input.Cost).
|
|
|
|
Save(ctx)
|
|
|
|
|
|
|
|
return mapMaintenanceEntryErr(item, err)
|
|
|
|
}
|
|
|
|
|
2023-02-27 03:42:23 +00:00
|
|
|
type MaintenanceLogQuery struct {
|
2023-03-21 04:32:10 +00:00
|
|
|
Completed bool `json:"completed" schema:"completed"`
|
|
|
|
Scheduled bool `json:"scheduled" schema:"scheduled"`
|
2023-02-27 03:42:23 +00:00
|
|
|
}
|
|
|
|
|
2023-03-21 04:32:10 +00:00
|
|
|
func (r *MaintenanceEntryRepository) GetLog(ctx context.Context, groupID, itemID uuid.UUID, query MaintenanceLogQuery) (MaintenanceLog, error) {
|
2022-12-10 05:57:57 +00:00
|
|
|
log := MaintenanceLog{
|
|
|
|
ItemID: itemID,
|
|
|
|
}
|
|
|
|
|
2023-03-21 04:32:10 +00:00
|
|
|
q := r.db.MaintenanceEntry.Query().Where(
|
|
|
|
maintenanceentry.ItemID(itemID),
|
|
|
|
maintenanceentry.HasItemWith(
|
|
|
|
item.HasGroupWith(group.IDEQ(groupID)),
|
|
|
|
),
|
|
|
|
)
|
2023-02-27 03:42:23 +00:00
|
|
|
|
|
|
|
if query.Completed {
|
|
|
|
q = q.Where(maintenanceentry.And(
|
|
|
|
maintenanceentry.DateNotNil(),
|
|
|
|
maintenanceentry.DateNEQ(time.Time{}),
|
|
|
|
))
|
|
|
|
} else if query.Scheduled {
|
|
|
|
q = q.Where(maintenanceentry.And(
|
|
|
|
maintenanceentry.Or(
|
|
|
|
maintenanceentry.DateIsNil(),
|
|
|
|
maintenanceentry.DateEQ(time.Time{}),
|
|
|
|
),
|
|
|
|
maintenanceentry.ScheduledDateNotNil(),
|
|
|
|
maintenanceentry.ScheduledDateNEQ(time.Time{}),
|
|
|
|
))
|
|
|
|
}
|
|
|
|
|
|
|
|
entries, err := q.Order(ent.Desc(maintenanceentry.FieldDate)).
|
2022-12-10 05:57:57 +00:00
|
|
|
All(ctx)
|
|
|
|
if err != nil {
|
|
|
|
return MaintenanceLog{}, err
|
|
|
|
}
|
|
|
|
|
|
|
|
log.Entries = mapEachMaintenanceEntry(entries)
|
|
|
|
|
|
|
|
var maybeTotal *float64
|
|
|
|
var maybeAverage *float64
|
|
|
|
|
2023-02-27 03:42:23 +00:00
|
|
|
statement := `
|
2022-12-10 05:57:57 +00:00
|
|
|
SELECT
|
|
|
|
SUM(cost_total) AS total_of_totals,
|
|
|
|
AVG(cost_total) AS avg_of_averages
|
|
|
|
FROM
|
|
|
|
(
|
|
|
|
SELECT
|
|
|
|
strftime('%m-%Y', date) AS my,
|
|
|
|
SUM(cost) AS cost_total
|
|
|
|
FROM
|
|
|
|
maintenance_entries
|
|
|
|
WHERE
|
|
|
|
item_id = ?
|
|
|
|
GROUP BY
|
|
|
|
my
|
|
|
|
)`
|
|
|
|
|
2023-02-27 03:42:23 +00:00
|
|
|
row := r.db.Sql().QueryRowContext(ctx, statement, itemID)
|
2022-12-10 05:57:57 +00:00
|
|
|
err = row.Scan(&maybeTotal, &maybeAverage)
|
|
|
|
if err != nil {
|
|
|
|
return MaintenanceLog{}, err
|
|
|
|
}
|
|
|
|
|
|
|
|
log.CostAverage = orDefault(maybeAverage, 0)
|
|
|
|
log.CostTotal = orDefault(maybeTotal, 0)
|
|
|
|
return log, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (r *MaintenanceEntryRepository) Delete(ctx context.Context, ID uuid.UUID) error {
|
|
|
|
return r.db.MaintenanceEntry.DeleteOneID(ID).Exec(ctx)
|
|
|
|
}
|