2022-09-03 09:17:48 +00:00
|
|
|
package repo
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
2023-02-05 21:12:54 +00:00
|
|
|
"fmt"
|
2022-09-27 23:52:13 +00:00
|
|
|
"time"
|
2022-09-03 09:17:48 +00:00
|
|
|
|
|
|
|
"github.com/google/uuid"
|
2023-08-02 21:00:57 +00:00
|
|
|
"github.com/hay-kot/homebox/backend/internal/core/services/reporting/eventbus"
|
2022-10-30 04:05:38 +00:00
|
|
|
"github.com/hay-kot/homebox/backend/internal/data/ent"
|
2023-10-07 02:51:08 +00:00
|
|
|
"github.com/hay-kot/homebox/backend/internal/data/ent/attachment"
|
2022-10-30 04:05:38 +00:00
|
|
|
"github.com/hay-kot/homebox/backend/internal/data/ent/group"
|
|
|
|
"github.com/hay-kot/homebox/backend/internal/data/ent/item"
|
|
|
|
"github.com/hay-kot/homebox/backend/internal/data/ent/itemfield"
|
|
|
|
"github.com/hay-kot/homebox/backend/internal/data/ent/label"
|
|
|
|
"github.com/hay-kot/homebox/backend/internal/data/ent/location"
|
|
|
|
"github.com/hay-kot/homebox/backend/internal/data/ent/predicate"
|
2023-02-15 17:40:35 +00:00
|
|
|
"github.com/hay-kot/homebox/backend/internal/data/types"
|
2022-09-03 09:17:48 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
type ItemsRepository struct {
|
2023-08-02 21:00:57 +00:00
|
|
|
db *ent.Client
|
|
|
|
bus *eventbus.EventBus
|
2022-09-03 09:17:48 +00:00
|
|
|
}
|
|
|
|
|
2022-09-27 23:52:13 +00:00
|
|
|
type (
|
2023-02-05 21:12:54 +00:00
|
|
|
FieldQuery struct {
|
|
|
|
Name string
|
|
|
|
Value string
|
|
|
|
}
|
|
|
|
|
2022-10-13 05:13:07 +00:00
|
|
|
ItemQuery struct {
|
2022-11-01 07:30:42 +00:00
|
|
|
Page int
|
|
|
|
PageSize int
|
2023-04-02 06:01:21 +00:00
|
|
|
Search string `json:"search"`
|
|
|
|
AssetID AssetID `json:"assetId"`
|
|
|
|
LocationIDs []uuid.UUID `json:"locationIds"`
|
|
|
|
LabelIDs []uuid.UUID `json:"labelIds"`
|
2023-11-16 03:41:24 +00:00
|
|
|
ParentItemIDs []uuid.UUID `json:"parentIds"`
|
2023-04-02 06:01:21 +00:00
|
|
|
SortBy string `json:"sortBy"`
|
|
|
|
IncludeArchived bool `json:"includeArchived"`
|
|
|
|
Fields []FieldQuery `json:"fields"`
|
|
|
|
OrderBy string `json:"orderBy"`
|
2022-10-13 05:13:07 +00:00
|
|
|
}
|
|
|
|
|
2022-10-16 05:41:27 +00:00
|
|
|
ItemField struct {
|
|
|
|
ID uuid.UUID `json:"id,omitempty"`
|
|
|
|
Type string `json:"type"`
|
|
|
|
Name string `json:"name"`
|
|
|
|
TextValue string `json:"textValue"`
|
|
|
|
NumberValue int `json:"numberValue"`
|
|
|
|
BooleanValue bool `json:"booleanValue"`
|
2023-02-18 06:41:01 +00:00
|
|
|
// TimeValue time.Time `json:"timeValue,omitempty"`
|
2022-10-16 05:41:27 +00:00
|
|
|
}
|
|
|
|
|
2022-09-27 23:52:13 +00:00
|
|
|
ItemCreate struct {
|
2022-10-24 04:54:39 +00:00
|
|
|
ImportRef string `json:"-"`
|
2024-01-04 17:55:26 +00:00
|
|
|
ParentID uuid.UUID `json:"parentId" extensions:"x-nullable"`
|
|
|
|
Name string `json:"name" validate:"required,min=1,max=255"`
|
2023-03-21 05:48:22 +00:00
|
|
|
Description string `json:"description" validate:"max=1000"`
|
2022-11-13 23:17:55 +00:00
|
|
|
AssetID AssetID `json:"-"`
|
2022-09-27 23:52:13 +00:00
|
|
|
|
|
|
|
// Edges
|
|
|
|
LocationID uuid.UUID `json:"locationId"`
|
|
|
|
LabelIDs []uuid.UUID `json:"labelIds"`
|
|
|
|
}
|
2023-02-26 02:54:40 +00:00
|
|
|
|
2022-09-27 23:52:13 +00:00
|
|
|
ItemUpdate struct {
|
2024-01-04 17:55:26 +00:00
|
|
|
ParentID uuid.UUID `json:"parentId" extensions:"x-nullable,x-omitempty"`
|
2022-09-27 23:52:13 +00:00
|
|
|
ID uuid.UUID `json:"id"`
|
2024-01-18 19:45:42 +00:00
|
|
|
AssetID AssetID `json:"assetId" swaggertype:"string"`
|
2022-09-27 23:52:13 +00:00
|
|
|
Name string `json:"name"`
|
|
|
|
Description string `json:"description"`
|
|
|
|
Quantity int `json:"quantity"`
|
|
|
|
Insured bool `json:"insured"`
|
2022-11-01 07:30:42 +00:00
|
|
|
Archived bool `json:"archived"`
|
2022-09-27 23:52:13 +00:00
|
|
|
|
|
|
|
// Edges
|
|
|
|
LocationID uuid.UUID `json:"locationId"`
|
|
|
|
LabelIDs []uuid.UUID `json:"labelIds"`
|
|
|
|
|
|
|
|
// Identifications
|
|
|
|
SerialNumber string `json:"serialNumber"`
|
|
|
|
ModelNumber string `json:"modelNumber"`
|
|
|
|
Manufacturer string `json:"manufacturer"`
|
|
|
|
|
|
|
|
// Warranty
|
2023-02-15 17:40:35 +00:00
|
|
|
LifetimeWarranty bool `json:"lifetimeWarranty"`
|
|
|
|
WarrantyExpires types.Date `json:"warrantyExpires"`
|
|
|
|
WarrantyDetails string `json:"warrantyDetails"`
|
2022-09-27 23:52:13 +00:00
|
|
|
|
|
|
|
// Purchase
|
2023-02-15 17:40:35 +00:00
|
|
|
PurchaseTime types.Date `json:"purchaseTime"`
|
|
|
|
PurchaseFrom string `json:"purchaseFrom"`
|
|
|
|
PurchasePrice float64 `json:"purchasePrice,string"`
|
2022-09-27 23:52:13 +00:00
|
|
|
|
|
|
|
// Sold
|
2023-02-15 17:40:35 +00:00
|
|
|
SoldTime types.Date `json:"soldTime"`
|
|
|
|
SoldTo string `json:"soldTo"`
|
|
|
|
SoldPrice float64 `json:"soldPrice,string"`
|
|
|
|
SoldNotes string `json:"soldNotes"`
|
2022-09-27 23:52:13 +00:00
|
|
|
|
|
|
|
// Extras
|
2022-10-16 05:41:27 +00:00
|
|
|
Notes string `json:"notes"`
|
|
|
|
Fields []ItemField `json:"fields"`
|
2022-09-27 23:52:13 +00:00
|
|
|
}
|
|
|
|
|
2023-02-26 02:54:40 +00:00
|
|
|
ItemPatch struct {
|
|
|
|
ID uuid.UUID `json:"id"`
|
|
|
|
Quantity *int `json:"quantity,omitempty" extensions:"x-nullable,x-omitempty"`
|
2024-01-04 17:55:26 +00:00
|
|
|
ImportRef *string `json:"-,omitempty" extensions:"x-nullable,x-omitempty"`
|
2023-02-26 02:54:40 +00:00
|
|
|
}
|
|
|
|
|
2022-09-27 23:52:13 +00:00
|
|
|
ItemSummary struct {
|
|
|
|
ImportRef string `json:"-"`
|
|
|
|
ID uuid.UUID `json:"id"`
|
|
|
|
Name string `json:"name"`
|
|
|
|
Description string `json:"description"`
|
|
|
|
Quantity int `json:"quantity"`
|
|
|
|
Insured bool `json:"insured"`
|
2022-11-01 07:30:42 +00:00
|
|
|
Archived bool `json:"archived"`
|
2022-09-27 23:52:13 +00:00
|
|
|
CreatedAt time.Time `json:"createdAt"`
|
|
|
|
UpdatedAt time.Time `json:"updatedAt"`
|
|
|
|
|
2022-12-30 01:19:15 +00:00
|
|
|
PurchasePrice float64 `json:"purchasePrice,string"`
|
|
|
|
|
2022-09-27 23:52:13 +00:00
|
|
|
// Edges
|
2022-10-24 04:54:39 +00:00
|
|
|
Location *LocationSummary `json:"location,omitempty" extensions:"x-nullable,x-omitempty"`
|
|
|
|
Labels []LabelSummary `json:"labels"`
|
2023-10-07 02:51:08 +00:00
|
|
|
|
|
|
|
ImageID *uuid.UUID `json:"imageId,omitempty"`
|
2022-09-27 23:52:13 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
ItemOut struct {
|
2022-10-24 04:54:39 +00:00
|
|
|
Parent *ItemSummary `json:"parent,omitempty" extensions:"x-nullable,x-omitempty"`
|
2022-09-27 23:52:13 +00:00
|
|
|
ItemSummary
|
2022-11-13 23:17:55 +00:00
|
|
|
AssetID AssetID `json:"assetId,string"`
|
2022-09-27 23:52:13 +00:00
|
|
|
|
|
|
|
SerialNumber string `json:"serialNumber"`
|
|
|
|
ModelNumber string `json:"modelNumber"`
|
|
|
|
Manufacturer string `json:"manufacturer"`
|
|
|
|
|
|
|
|
// Warranty
|
2023-02-15 17:40:35 +00:00
|
|
|
LifetimeWarranty bool `json:"lifetimeWarranty"`
|
|
|
|
WarrantyExpires types.Date `json:"warrantyExpires"`
|
|
|
|
WarrantyDetails string `json:"warrantyDetails"`
|
2022-09-27 23:52:13 +00:00
|
|
|
|
|
|
|
// Purchase
|
2023-02-15 17:40:35 +00:00
|
|
|
PurchaseTime types.Date `json:"purchaseTime"`
|
|
|
|
PurchaseFrom string `json:"purchaseFrom"`
|
2022-09-27 23:52:13 +00:00
|
|
|
|
|
|
|
// Sold
|
2023-02-15 17:40:35 +00:00
|
|
|
SoldTime types.Date `json:"soldTime"`
|
|
|
|
SoldTo string `json:"soldTo"`
|
|
|
|
SoldPrice float64 `json:"soldPrice,string"`
|
|
|
|
SoldNotes string `json:"soldNotes"`
|
2022-09-27 23:52:13 +00:00
|
|
|
|
|
|
|
// Extras
|
|
|
|
Notes string `json:"notes"`
|
|
|
|
|
|
|
|
Attachments []ItemAttachment `json:"attachments"`
|
2022-10-24 04:54:39 +00:00
|
|
|
Fields []ItemField `json:"fields"`
|
2022-09-27 23:52:13 +00:00
|
|
|
}
|
|
|
|
)
|
|
|
|
|
2023-02-18 06:41:01 +00:00
|
|
|
var mapItemsSummaryErr = mapTEachErrFunc(mapItemSummary)
|
2022-09-27 23:52:13 +00:00
|
|
|
|
|
|
|
func mapItemSummary(item *ent.Item) ItemSummary {
|
2022-10-24 04:54:39 +00:00
|
|
|
var location *LocationSummary
|
2022-09-27 23:52:13 +00:00
|
|
|
if item.Edges.Location != nil {
|
2022-10-24 04:54:39 +00:00
|
|
|
loc := mapLocationSummary(item.Edges.Location)
|
|
|
|
location = &loc
|
2022-09-27 23:52:13 +00:00
|
|
|
}
|
|
|
|
|
2022-10-24 04:54:39 +00:00
|
|
|
labels := make([]LabelSummary, len(item.Edges.Label))
|
2022-09-27 23:52:13 +00:00
|
|
|
if item.Edges.Label != nil {
|
|
|
|
labels = mapEach(item.Edges.Label, mapLabelSummary)
|
|
|
|
}
|
|
|
|
|
2023-10-07 02:51:08 +00:00
|
|
|
var imageID *uuid.UUID
|
|
|
|
if item.Edges.Attachments != nil {
|
|
|
|
for _, a := range item.Edges.Attachments {
|
|
|
|
if a.Primary && a.Edges.Document != nil {
|
|
|
|
imageID = &a.ID
|
|
|
|
break
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-09-27 23:52:13 +00:00
|
|
|
return ItemSummary{
|
2022-12-30 01:19:15 +00:00
|
|
|
ID: item.ID,
|
|
|
|
Name: item.Name,
|
|
|
|
Description: item.Description,
|
2023-02-26 02:54:40 +00:00
|
|
|
ImportRef: item.ImportRef,
|
2022-12-30 01:19:15 +00:00
|
|
|
Quantity: item.Quantity,
|
|
|
|
CreatedAt: item.CreatedAt,
|
|
|
|
UpdatedAt: item.UpdatedAt,
|
|
|
|
Archived: item.Archived,
|
|
|
|
PurchasePrice: item.PurchasePrice,
|
2022-09-27 23:52:13 +00:00
|
|
|
|
|
|
|
// Edges
|
|
|
|
Location: location,
|
|
|
|
Labels: labels,
|
|
|
|
|
|
|
|
// Warranty
|
|
|
|
Insured: item.Insured,
|
2023-10-07 02:51:08 +00:00
|
|
|
ImageID: imageID,
|
2022-09-27 23:52:13 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
var (
|
2023-02-13 19:00:29 +00:00
|
|
|
mapItemOutErr = mapTErrFunc(mapItemOut)
|
|
|
|
mapItemsOutErr = mapTEachErrFunc(mapItemOut)
|
2022-09-27 23:52:13 +00:00
|
|
|
)
|
|
|
|
|
2022-10-16 05:41:27 +00:00
|
|
|
func mapFields(fields []*ent.ItemField) []ItemField {
|
|
|
|
result := make([]ItemField, len(fields))
|
|
|
|
for i, f := range fields {
|
|
|
|
result[i] = ItemField{
|
|
|
|
ID: f.ID,
|
|
|
|
Type: f.Type.String(),
|
|
|
|
Name: f.Name,
|
|
|
|
TextValue: f.TextValue,
|
|
|
|
NumberValue: f.NumberValue,
|
|
|
|
BooleanValue: f.BooleanValue,
|
2023-02-18 06:41:01 +00:00
|
|
|
// TimeValue: f.TimeValue,
|
2022-10-16 05:41:27 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
return result
|
|
|
|
}
|
|
|
|
|
2022-09-27 23:52:13 +00:00
|
|
|
func mapItemOut(item *ent.Item) ItemOut {
|
|
|
|
var attachments []ItemAttachment
|
|
|
|
if item.Edges.Attachments != nil {
|
|
|
|
attachments = mapEach(item.Edges.Attachments, ToItemAttachment)
|
|
|
|
}
|
|
|
|
|
2022-10-16 05:41:27 +00:00
|
|
|
var fields []ItemField
|
|
|
|
if item.Edges.Fields != nil {
|
|
|
|
fields = mapFields(item.Edges.Fields)
|
|
|
|
}
|
|
|
|
|
2022-10-24 04:54:39 +00:00
|
|
|
var parent *ItemSummary
|
|
|
|
if item.Edges.Parent != nil {
|
|
|
|
v := mapItemSummary(item.Edges.Parent)
|
|
|
|
parent = &v
|
|
|
|
}
|
|
|
|
|
2022-09-27 23:52:13 +00:00
|
|
|
return ItemOut{
|
2022-10-24 04:54:39 +00:00
|
|
|
Parent: parent,
|
2022-11-13 23:17:55 +00:00
|
|
|
AssetID: AssetID(item.AssetID),
|
2022-09-27 23:52:13 +00:00
|
|
|
ItemSummary: mapItemSummary(item),
|
|
|
|
LifetimeWarranty: item.LifetimeWarranty,
|
2023-02-15 17:40:35 +00:00
|
|
|
WarrantyExpires: types.DateFromTime(item.WarrantyExpires),
|
2022-09-27 23:52:13 +00:00
|
|
|
WarrantyDetails: item.WarrantyDetails,
|
|
|
|
|
|
|
|
// Identification
|
|
|
|
SerialNumber: item.SerialNumber,
|
|
|
|
ModelNumber: item.ModelNumber,
|
|
|
|
Manufacturer: item.Manufacturer,
|
|
|
|
|
|
|
|
// Purchase
|
2023-02-15 17:40:35 +00:00
|
|
|
PurchaseTime: types.DateFromTime(item.PurchaseTime),
|
2022-12-30 01:19:15 +00:00
|
|
|
PurchaseFrom: item.PurchaseFrom,
|
2022-09-27 23:52:13 +00:00
|
|
|
|
|
|
|
// Sold
|
2023-02-15 17:40:35 +00:00
|
|
|
SoldTime: types.DateFromTime(item.SoldTime),
|
2022-09-27 23:52:13 +00:00
|
|
|
SoldTo: item.SoldTo,
|
|
|
|
SoldPrice: item.SoldPrice,
|
|
|
|
SoldNotes: item.SoldNotes,
|
|
|
|
|
|
|
|
// Extras
|
|
|
|
Notes: item.Notes,
|
|
|
|
Attachments: attachments,
|
2022-10-16 05:41:27 +00:00
|
|
|
Fields: fields,
|
2022-09-27 23:52:13 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-04-28 17:21:43 +00:00
|
|
|
func (e *ItemsRepository) publishMutationEvent(groupID uuid.UUID) {
|
2024-01-04 17:55:26 +00:00
|
|
|
if e.bus != nil {
|
2024-04-28 17:21:43 +00:00
|
|
|
e.bus.Publish(eventbus.EventItemMutation, eventbus.GroupMutationEvent{GID: groupID})
|
2023-08-02 21:00:57 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-09-27 23:52:13 +00:00
|
|
|
func (e *ItemsRepository) getOne(ctx context.Context, where ...predicate.Item) (ItemOut, error) {
|
|
|
|
q := e.db.Item.Query().Where(where...)
|
|
|
|
|
|
|
|
return mapItemOutErr(q.
|
2022-09-03 09:17:48 +00:00
|
|
|
WithFields().
|
|
|
|
WithLabel().
|
|
|
|
WithLocation().
|
|
|
|
WithGroup().
|
2022-10-24 04:54:39 +00:00
|
|
|
WithParent().
|
2022-09-12 22:47:27 +00:00
|
|
|
WithAttachments(func(aq *ent.AttachmentQuery) {
|
|
|
|
aq.WithDocument()
|
|
|
|
}).
|
2022-09-27 23:52:13 +00:00
|
|
|
Only(ctx),
|
|
|
|
)
|
|
|
|
}
|
|
|
|
|
|
|
|
// GetOne returns a single item by ID. If the item does not exist, an error is returned.
|
|
|
|
// See also: GetOneByGroup to ensure that the item belongs to a specific group.
|
|
|
|
func (e *ItemsRepository) GetOne(ctx context.Context, id uuid.UUID) (ItemOut, error) {
|
|
|
|
return e.getOne(ctx, item.ID(id))
|
|
|
|
}
|
|
|
|
|
2024-04-28 17:21:43 +00:00
|
|
|
func (e *ItemsRepository) CheckRef(ctx context.Context, groupID uuid.UUID, ref string) (bool, error) {
|
|
|
|
q := e.db.Item.Query().Where(item.HasGroupWith(group.ID(groupID)))
|
2022-10-16 01:46:57 +00:00
|
|
|
return q.Where(item.ImportRef(ref)).Exist(ctx)
|
|
|
|
}
|
|
|
|
|
2024-04-28 17:21:43 +00:00
|
|
|
func (e *ItemsRepository) GetByRef(ctx context.Context, groupID uuid.UUID, ref string) (ItemOut, error) {
|
|
|
|
return e.getOne(ctx, item.ImportRef(ref), item.HasGroupWith(group.ID(groupID)))
|
2023-02-26 02:54:40 +00:00
|
|
|
}
|
|
|
|
|
2022-09-27 23:52:13 +00:00
|
|
|
// GetOneByGroup returns a single item by ID. If the item does not exist, an error is returned.
|
|
|
|
// GetOneByGroup ensures that the item belongs to a specific group.
|
|
|
|
func (e *ItemsRepository) GetOneByGroup(ctx context.Context, gid, id uuid.UUID) (ItemOut, error) {
|
|
|
|
return e.getOne(ctx, item.ID(id), item.HasGroupWith(group.ID(gid)))
|
2022-09-03 09:17:48 +00:00
|
|
|
}
|
|
|
|
|
2022-10-13 05:13:07 +00:00
|
|
|
// QueryByGroup returns a list of items that belong to a specific group based on the provided query.
|
|
|
|
func (e *ItemsRepository) QueryByGroup(ctx context.Context, gid uuid.UUID, q ItemQuery) (PaginationResult[ItemSummary], error) {
|
2022-11-01 07:30:42 +00:00
|
|
|
qb := e.db.Item.Query().Where(
|
|
|
|
item.HasGroupWith(group.ID(gid)),
|
|
|
|
)
|
|
|
|
|
|
|
|
if q.IncludeArchived {
|
|
|
|
qb = qb.Where(
|
|
|
|
item.Or(
|
|
|
|
item.Archived(true),
|
|
|
|
item.Archived(false),
|
|
|
|
),
|
|
|
|
)
|
|
|
|
} else {
|
|
|
|
qb = qb.Where(item.Archived(false))
|
|
|
|
}
|
2022-10-13 05:13:07 +00:00
|
|
|
|
|
|
|
if q.Search != "" {
|
|
|
|
qb.Where(
|
|
|
|
item.Or(
|
|
|
|
item.NameContainsFold(q.Search),
|
|
|
|
item.DescriptionContainsFold(q.Search),
|
2024-01-18 19:46:40 +00:00
|
|
|
item.SerialNumberContainsFold(q.Search),
|
|
|
|
item.ModelNumberContainsFold(q.Search),
|
2023-04-02 06:01:21 +00:00
|
|
|
item.ManufacturerContainsFold(q.Search),
|
2024-01-18 19:46:40 +00:00
|
|
|
item.NotesContainsFold(q.Search),
|
2022-10-13 05:13:07 +00:00
|
|
|
),
|
|
|
|
)
|
|
|
|
}
|
|
|
|
|
2023-02-05 21:12:54 +00:00
|
|
|
if !q.AssetID.Nil() {
|
|
|
|
qb = qb.Where(item.AssetID(q.AssetID.Int()))
|
|
|
|
}
|
|
|
|
|
2023-02-10 02:47:41 +00:00
|
|
|
// Filters within this block define a AND relationship where each subset
|
|
|
|
// of filters is OR'd together.
|
|
|
|
//
|
|
|
|
// The goal is to allow matches like where the item has
|
|
|
|
// - one of the selected labels AND
|
|
|
|
// - one of the selected locations AND
|
|
|
|
// - one of the selected fields key/value matches
|
|
|
|
var andPredicates []predicate.Item
|
|
|
|
{
|
|
|
|
if len(q.LabelIDs) > 0 {
|
|
|
|
labelPredicates := make([]predicate.Item, 0, len(q.LabelIDs))
|
|
|
|
for _, l := range q.LabelIDs {
|
|
|
|
labelPredicates = append(labelPredicates, item.HasLabelWith(label.ID(l)))
|
|
|
|
}
|
|
|
|
|
|
|
|
andPredicates = append(andPredicates, item.Or(labelPredicates...))
|
2023-02-05 21:12:54 +00:00
|
|
|
}
|
|
|
|
|
2023-02-10 02:47:41 +00:00
|
|
|
if len(q.LocationIDs) > 0 {
|
|
|
|
locationPredicates := make([]predicate.Item, 0, len(q.LocationIDs))
|
|
|
|
for _, l := range q.LocationIDs {
|
|
|
|
locationPredicates = append(locationPredicates, item.HasLocationWith(location.ID(l)))
|
|
|
|
}
|
|
|
|
|
|
|
|
andPredicates = append(andPredicates, item.Or(locationPredicates...))
|
|
|
|
}
|
|
|
|
|
|
|
|
if len(q.Fields) > 0 {
|
|
|
|
fieldPredicates := make([]predicate.Item, 0, len(q.Fields))
|
|
|
|
for _, f := range q.Fields {
|
|
|
|
fieldPredicates = append(fieldPredicates, item.HasFieldsWith(
|
|
|
|
itemfield.And(
|
|
|
|
itemfield.Name(f.Name),
|
|
|
|
itemfield.TextValue(f.Value),
|
|
|
|
),
|
|
|
|
))
|
|
|
|
}
|
|
|
|
|
|
|
|
andPredicates = append(andPredicates, item.Or(fieldPredicates...))
|
|
|
|
}
|
2023-11-16 03:41:24 +00:00
|
|
|
|
|
|
|
if len(q.ParentItemIDs) > 0 {
|
|
|
|
andPredicates = append(andPredicates, item.HasParentWith(item.IDIn(q.ParentItemIDs...)))
|
|
|
|
}
|
2023-02-10 02:47:41 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if len(andPredicates) > 0 {
|
|
|
|
qb = qb.Where(item.And(andPredicates...))
|
2023-02-05 21:12:54 +00:00
|
|
|
}
|
|
|
|
|
2023-01-01 21:50:48 +00:00
|
|
|
count, err := qb.Count(ctx)
|
|
|
|
if err != nil {
|
|
|
|
return PaginationResult[ItemSummary]{}, err
|
|
|
|
}
|
|
|
|
|
2023-04-02 06:01:21 +00:00
|
|
|
// Order
|
|
|
|
switch q.OrderBy {
|
|
|
|
case "createdAt":
|
|
|
|
qb = qb.Order(ent.Desc(item.FieldCreatedAt))
|
|
|
|
case "updatedAt":
|
|
|
|
qb = qb.Order(ent.Desc(item.FieldUpdatedAt))
|
|
|
|
default: // "name"
|
|
|
|
qb = qb.Order(ent.Asc(item.FieldName))
|
|
|
|
}
|
|
|
|
|
|
|
|
qb = qb.
|
2023-01-01 21:50:48 +00:00
|
|
|
WithLabel().
|
2023-10-07 02:51:08 +00:00
|
|
|
WithLocation().
|
|
|
|
WithAttachments(func(aq *ent.AttachmentQuery) {
|
|
|
|
aq.Where(
|
|
|
|
attachment.Primary(true),
|
|
|
|
).
|
|
|
|
WithDocument()
|
|
|
|
})
|
2023-01-01 21:50:48 +00:00
|
|
|
|
2022-10-13 05:13:07 +00:00
|
|
|
if q.Page != -1 || q.PageSize != -1 {
|
|
|
|
qb = qb.
|
|
|
|
Offset(calculateOffset(q.Page, q.PageSize)).
|
|
|
|
Limit(q.PageSize)
|
|
|
|
}
|
|
|
|
|
2023-01-01 21:50:48 +00:00
|
|
|
items, err := mapItemsSummaryErr(qb.All(ctx))
|
2022-10-13 05:13:07 +00:00
|
|
|
if err != nil {
|
|
|
|
return PaginationResult[ItemSummary]{}, err
|
|
|
|
}
|
|
|
|
|
|
|
|
return PaginationResult[ItemSummary]{
|
|
|
|
Page: q.Page,
|
|
|
|
PageSize: q.PageSize,
|
|
|
|
Total: count,
|
|
|
|
Items: items,
|
|
|
|
}, nil
|
|
|
|
}
|
|
|
|
|
2023-01-14 18:24:11 +00:00
|
|
|
// QueryByAssetID returns items by asset ID. If the item does not exist, an error is returned.
|
|
|
|
func (e *ItemsRepository) QueryByAssetID(ctx context.Context, gid uuid.UUID, assetID AssetID, page int, pageSize int) (PaginationResult[ItemSummary], error) {
|
|
|
|
qb := e.db.Item.Query().Where(
|
|
|
|
item.HasGroupWith(group.ID(gid)),
|
|
|
|
item.AssetID(int(assetID)),
|
|
|
|
)
|
|
|
|
|
|
|
|
if page != -1 || pageSize != -1 {
|
|
|
|
qb.Offset(calculateOffset(page, pageSize)).
|
|
|
|
Limit(pageSize)
|
|
|
|
} else {
|
|
|
|
page = -1
|
|
|
|
pageSize = -1
|
|
|
|
}
|
|
|
|
|
2023-01-28 20:53:00 +00:00
|
|
|
items, err := mapItemsSummaryErr(
|
2023-01-14 18:24:11 +00:00
|
|
|
qb.Order(ent.Asc(item.FieldName)).
|
|
|
|
WithLabel().
|
|
|
|
WithLocation().
|
|
|
|
All(ctx),
|
|
|
|
)
|
|
|
|
if err != nil {
|
|
|
|
return PaginationResult[ItemSummary]{}, err
|
|
|
|
}
|
|
|
|
|
|
|
|
return PaginationResult[ItemSummary]{
|
|
|
|
Page: page,
|
|
|
|
PageSize: pageSize,
|
|
|
|
Total: len(items),
|
|
|
|
Items: items,
|
|
|
|
}, nil
|
|
|
|
}
|
|
|
|
|
2022-09-12 22:47:27 +00:00
|
|
|
// GetAll returns all the items in the database with the Labels and Locations eager loaded.
|
2023-02-13 19:00:29 +00:00
|
|
|
func (e *ItemsRepository) GetAll(ctx context.Context, gid uuid.UUID) ([]ItemOut, error) {
|
|
|
|
return mapItemsOutErr(e.db.Item.Query().
|
2022-09-03 09:17:48 +00:00
|
|
|
Where(item.HasGroupWith(group.ID(gid))).
|
|
|
|
WithLabel().
|
|
|
|
WithLocation().
|
2023-02-18 06:41:01 +00:00
|
|
|
WithFields().
|
2022-09-27 23:52:13 +00:00
|
|
|
All(ctx))
|
2022-09-03 09:17:48 +00:00
|
|
|
}
|
|
|
|
|
2024-04-28 17:21:43 +00:00
|
|
|
func (e *ItemsRepository) GetAllZeroAssetID(ctx context.Context, groupID uuid.UUID) ([]ItemSummary, error) {
|
2022-11-13 23:17:55 +00:00
|
|
|
q := e.db.Item.Query().Where(
|
2024-04-28 17:21:43 +00:00
|
|
|
item.HasGroupWith(group.ID(groupID)),
|
2022-11-13 23:17:55 +00:00
|
|
|
item.AssetID(0),
|
|
|
|
).Order(
|
|
|
|
ent.Asc(item.FieldCreatedAt),
|
|
|
|
)
|
|
|
|
|
|
|
|
return mapItemsSummaryErr(q.All(ctx))
|
|
|
|
}
|
|
|
|
|
2024-04-28 17:21:43 +00:00
|
|
|
func (e *ItemsRepository) GetHighestAssetID(ctx context.Context, groupID uuid.UUID) (AssetID, error) {
|
2022-11-13 23:17:55 +00:00
|
|
|
q := e.db.Item.Query().Where(
|
2024-04-28 17:21:43 +00:00
|
|
|
item.HasGroupWith(group.ID(groupID)),
|
2022-11-13 23:17:55 +00:00
|
|
|
).Order(
|
|
|
|
ent.Desc(item.FieldAssetID),
|
|
|
|
).Limit(1)
|
|
|
|
|
|
|
|
result, err := q.First(ctx)
|
|
|
|
if err != nil {
|
|
|
|
if ent.IsNotFound(err) {
|
|
|
|
return 0, nil
|
|
|
|
}
|
|
|
|
return 0, err
|
|
|
|
}
|
|
|
|
|
|
|
|
return AssetID(result.AssetID), nil
|
|
|
|
}
|
|
|
|
|
2024-04-28 17:21:43 +00:00
|
|
|
func (e *ItemsRepository) SetAssetID(ctx context.Context, groupID uuid.UUID, itemID uuid.UUID, assetID AssetID) error {
|
2022-11-13 23:17:55 +00:00
|
|
|
q := e.db.Item.Update().Where(
|
2024-04-28 17:21:43 +00:00
|
|
|
item.HasGroupWith(group.ID(groupID)),
|
|
|
|
item.ID(itemID),
|
2022-11-13 23:17:55 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
_, err := q.SetAssetID(int(assetID)).Save(ctx)
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2022-09-27 23:52:13 +00:00
|
|
|
func (e *ItemsRepository) Create(ctx context.Context, gid uuid.UUID, data ItemCreate) (ItemOut, error) {
|
2022-09-05 08:26:21 +00:00
|
|
|
q := e.db.Item.Create().
|
2022-10-16 01:46:57 +00:00
|
|
|
SetImportRef(data.ImportRef).
|
2022-09-03 09:17:48 +00:00
|
|
|
SetName(data.Name).
|
|
|
|
SetDescription(data.Description).
|
|
|
|
SetGroupID(gid).
|
2022-11-13 23:17:55 +00:00
|
|
|
SetLocationID(data.LocationID).
|
|
|
|
SetAssetID(int(data.AssetID))
|
2022-09-05 08:26:21 +00:00
|
|
|
|
|
|
|
if data.LabelIDs != nil && len(data.LabelIDs) > 0 {
|
|
|
|
q.AddLabelIDs(data.LabelIDs...)
|
|
|
|
}
|
|
|
|
|
|
|
|
result, err := q.Save(ctx)
|
|
|
|
if err != nil {
|
2022-09-27 23:52:13 +00:00
|
|
|
return ItemOut{}, err
|
2022-09-05 08:26:21 +00:00
|
|
|
}
|
|
|
|
|
2023-08-02 21:00:57 +00:00
|
|
|
e.publishMutationEvent(gid)
|
2022-09-05 08:26:21 +00:00
|
|
|
return e.GetOne(ctx, result.ID)
|
2022-09-03 09:17:48 +00:00
|
|
|
}
|
|
|
|
|
2022-09-05 08:26:21 +00:00
|
|
|
func (e *ItemsRepository) Delete(ctx context.Context, id uuid.UUID) error {
|
2023-10-07 02:51:08 +00:00
|
|
|
err := e.db.Item.DeleteOneID(id).Exec(ctx)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2023-08-02 21:00:57 +00:00
|
|
|
|
2023-10-07 02:51:08 +00:00
|
|
|
e.publishMutationEvent(id)
|
|
|
|
return nil
|
2022-09-03 09:17:48 +00:00
|
|
|
}
|
|
|
|
|
2022-09-27 23:52:13 +00:00
|
|
|
func (e *ItemsRepository) DeleteByGroup(ctx context.Context, gid, id uuid.UUID) error {
|
|
|
|
_, err := e.db.Item.
|
|
|
|
Delete().
|
|
|
|
Where(
|
|
|
|
item.ID(id),
|
|
|
|
item.HasGroupWith(group.ID(gid)),
|
|
|
|
).Exec(ctx)
|
2023-10-07 02:51:08 +00:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2023-08-02 21:00:57 +00:00
|
|
|
|
2023-10-07 02:51:08 +00:00
|
|
|
e.publishMutationEvent(gid)
|
2022-09-27 23:52:13 +00:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2024-04-28 17:21:43 +00:00
|
|
|
func (e *ItemsRepository) UpdateByGroup(ctx context.Context, groupID uuid.UUID, data ItemUpdate) (ItemOut, error) {
|
|
|
|
q := e.db.Item.Update().Where(item.ID(data.ID), item.HasGroupWith(group.ID(groupID))).
|
2022-09-05 08:26:21 +00:00
|
|
|
SetName(data.Name).
|
|
|
|
SetDescription(data.Description).
|
|
|
|
SetLocationID(data.LocationID).
|
|
|
|
SetSerialNumber(data.SerialNumber).
|
|
|
|
SetModelNumber(data.ModelNumber).
|
|
|
|
SetManufacturer(data.Manufacturer).
|
2022-11-01 07:30:42 +00:00
|
|
|
SetArchived(data.Archived).
|
2023-02-15 17:40:35 +00:00
|
|
|
SetPurchaseTime(data.PurchaseTime.Time()).
|
2022-09-05 08:26:21 +00:00
|
|
|
SetPurchaseFrom(data.PurchaseFrom).
|
|
|
|
SetPurchasePrice(data.PurchasePrice).
|
2023-02-15 17:40:35 +00:00
|
|
|
SetSoldTime(data.SoldTime.Time()).
|
2022-09-05 08:26:21 +00:00
|
|
|
SetSoldTo(data.SoldTo).
|
|
|
|
SetSoldPrice(data.SoldPrice).
|
|
|
|
SetSoldNotes(data.SoldNotes).
|
2022-09-09 18:20:38 +00:00
|
|
|
SetNotes(data.Notes).
|
|
|
|
SetLifetimeWarranty(data.LifetimeWarranty).
|
2022-09-12 22:47:27 +00:00
|
|
|
SetInsured(data.Insured).
|
2023-02-15 17:40:35 +00:00
|
|
|
SetWarrantyExpires(data.WarrantyExpires.Time()).
|
2022-09-12 22:47:27 +00:00
|
|
|
SetWarrantyDetails(data.WarrantyDetails).
|
2022-11-13 23:17:55 +00:00
|
|
|
SetQuantity(data.Quantity).
|
|
|
|
SetAssetID(int(data.AssetID))
|
2022-09-05 08:26:21 +00:00
|
|
|
|
2022-09-12 22:47:27 +00:00
|
|
|
currentLabels, err := e.db.Item.Query().Where(item.ID(data.ID)).QueryLabel().All(ctx)
|
|
|
|
if err != nil {
|
2022-09-27 23:52:13 +00:00
|
|
|
return ItemOut{}, err
|
2022-09-12 22:47:27 +00:00
|
|
|
}
|
|
|
|
|
2022-09-27 23:52:13 +00:00
|
|
|
set := newIDSet(currentLabels)
|
2022-09-12 22:47:27 +00:00
|
|
|
|
|
|
|
for _, l := range data.LabelIDs {
|
2022-09-27 23:52:13 +00:00
|
|
|
if set.Contains(l) {
|
2022-09-12 22:47:27 +00:00
|
|
|
set.Remove(l)
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
q.AddLabelIDs(l)
|
|
|
|
}
|
|
|
|
|
|
|
|
if set.Len() > 0 {
|
|
|
|
q.RemoveLabelIDs(set.Slice()...)
|
|
|
|
}
|
2022-09-05 08:26:21 +00:00
|
|
|
|
2022-10-24 04:54:39 +00:00
|
|
|
if data.ParentID != uuid.Nil {
|
|
|
|
q.SetParentID(data.ParentID)
|
|
|
|
} else {
|
|
|
|
q.ClearParent()
|
|
|
|
}
|
|
|
|
|
2022-09-12 22:47:27 +00:00
|
|
|
err = q.Exec(ctx)
|
2022-09-05 08:26:21 +00:00
|
|
|
if err != nil {
|
2022-09-27 23:52:13 +00:00
|
|
|
return ItemOut{}, err
|
2022-09-05 08:26:21 +00:00
|
|
|
}
|
|
|
|
|
2022-10-16 05:41:27 +00:00
|
|
|
fields, err := e.db.ItemField.Query().Where(itemfield.HasItemWith(item.ID(data.ID))).All(ctx)
|
|
|
|
if err != nil {
|
|
|
|
return ItemOut{}, err
|
|
|
|
}
|
|
|
|
|
|
|
|
fieldIds := newIDSet(fields)
|
|
|
|
|
|
|
|
// Update Existing Fields
|
|
|
|
for _, f := range data.Fields {
|
|
|
|
if f.ID == uuid.Nil {
|
|
|
|
// Create New Field
|
|
|
|
_, err = e.db.ItemField.Create().
|
|
|
|
SetItemID(data.ID).
|
|
|
|
SetType(itemfield.Type(f.Type)).
|
|
|
|
SetName(f.Name).
|
|
|
|
SetTextValue(f.TextValue).
|
|
|
|
SetNumberValue(f.NumberValue).
|
|
|
|
SetBooleanValue(f.BooleanValue).
|
2023-02-18 06:41:01 +00:00
|
|
|
// SetTimeValue(f.TimeValue).
|
2022-10-16 05:41:27 +00:00
|
|
|
Save(ctx)
|
|
|
|
if err != nil {
|
|
|
|
return ItemOut{}, err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
opt := e.db.ItemField.Update().
|
|
|
|
Where(
|
|
|
|
itemfield.ID(f.ID),
|
|
|
|
itemfield.HasItemWith(item.ID(data.ID)),
|
|
|
|
).
|
|
|
|
SetType(itemfield.Type(f.Type)).
|
|
|
|
SetName(f.Name).
|
|
|
|
SetTextValue(f.TextValue).
|
|
|
|
SetNumberValue(f.NumberValue).
|
2023-02-18 06:41:01 +00:00
|
|
|
SetBooleanValue(f.BooleanValue)
|
|
|
|
// SetTimeValue(f.TimeValue)
|
2022-10-16 05:41:27 +00:00
|
|
|
|
|
|
|
_, err = opt.Save(ctx)
|
|
|
|
if err != nil {
|
|
|
|
return ItemOut{}, err
|
|
|
|
}
|
|
|
|
|
|
|
|
fieldIds.Remove(f.ID)
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
|
|
|
// Delete Fields that are no longer present
|
|
|
|
if fieldIds.Len() > 0 {
|
|
|
|
_, err = e.db.ItemField.Delete().
|
|
|
|
Where(
|
|
|
|
itemfield.IDIn(fieldIds.Slice()...),
|
|
|
|
itemfield.HasItemWith(item.ID(data.ID)),
|
|
|
|
).Exec(ctx)
|
|
|
|
if err != nil {
|
|
|
|
return ItemOut{}, err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-04-28 17:21:43 +00:00
|
|
|
e.publishMutationEvent(groupID)
|
2022-09-05 08:26:21 +00:00
|
|
|
return e.GetOne(ctx, data.ID)
|
2022-09-03 09:17:48 +00:00
|
|
|
}
|
2023-02-05 21:12:54 +00:00
|
|
|
|
2024-04-28 17:21:43 +00:00
|
|
|
func (e *ItemsRepository) GetAllZeroImportRef(ctx context.Context, groupID uuid.UUID) ([]uuid.UUID, error) {
|
2023-02-26 02:54:40 +00:00
|
|
|
var ids []uuid.UUID
|
|
|
|
|
|
|
|
err := e.db.Item.Query().
|
|
|
|
Where(
|
2024-04-28 17:21:43 +00:00
|
|
|
item.HasGroupWith(group.ID(groupID)),
|
2023-02-26 02:54:40 +00:00
|
|
|
item.Or(
|
|
|
|
item.ImportRefEQ(""),
|
|
|
|
item.ImportRefIsNil(),
|
|
|
|
),
|
|
|
|
).
|
|
|
|
Select(item.FieldID).
|
|
|
|
Scan(ctx, &ids)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
return ids, nil
|
|
|
|
}
|
|
|
|
|
2024-04-28 17:21:43 +00:00
|
|
|
func (e *ItemsRepository) Patch(ctx context.Context, groupID, itemID uuid.UUID, data ItemPatch) error {
|
2023-02-26 02:54:40 +00:00
|
|
|
q := e.db.Item.Update().
|
|
|
|
Where(
|
2024-04-28 17:21:43 +00:00
|
|
|
item.ID(itemID),
|
|
|
|
item.HasGroupWith(group.ID(groupID)),
|
2023-02-26 02:54:40 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
if data.ImportRef != nil {
|
|
|
|
q.SetImportRef(*data.ImportRef)
|
|
|
|
}
|
|
|
|
|
|
|
|
if data.Quantity != nil {
|
|
|
|
q.SetQuantity(*data.Quantity)
|
|
|
|
}
|
|
|
|
|
2024-04-28 17:21:43 +00:00
|
|
|
e.publishMutationEvent(groupID)
|
2023-02-26 02:54:40 +00:00
|
|
|
return q.Exec(ctx)
|
|
|
|
}
|
|
|
|
|
2024-04-28 17:21:43 +00:00
|
|
|
func (e *ItemsRepository) GetAllCustomFieldValues(ctx context.Context, groupID uuid.UUID, name string) ([]string, error) {
|
2023-02-05 21:12:54 +00:00
|
|
|
type st struct {
|
|
|
|
Value string `json:"text_value"`
|
|
|
|
}
|
|
|
|
|
|
|
|
var values []st
|
|
|
|
|
|
|
|
err := e.db.Item.Query().
|
|
|
|
Where(
|
2024-04-28 17:21:43 +00:00
|
|
|
item.HasGroupWith(group.ID(groupID)),
|
2023-02-05 21:12:54 +00:00
|
|
|
).
|
|
|
|
QueryFields().
|
|
|
|
Where(
|
|
|
|
itemfield.Name(name),
|
|
|
|
).
|
|
|
|
Unique(true).
|
|
|
|
Select(itemfield.FieldTextValue).
|
|
|
|
Scan(ctx, &values)
|
|
|
|
if err != nil {
|
|
|
|
return nil, fmt.Errorf("failed to get field values: %w", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
valueStrings := make([]string, len(values))
|
|
|
|
for i, f := range values {
|
|
|
|
valueStrings[i] = f.Value
|
|
|
|
}
|
|
|
|
|
|
|
|
return valueStrings, nil
|
|
|
|
}
|
|
|
|
|
2024-04-28 17:21:43 +00:00
|
|
|
func (e *ItemsRepository) GetAllCustomFieldNames(ctx context.Context, groupID uuid.UUID) ([]string, error) {
|
2023-02-05 21:12:54 +00:00
|
|
|
type st struct {
|
|
|
|
Name string `json:"name"`
|
|
|
|
}
|
|
|
|
|
|
|
|
var fields []st
|
|
|
|
|
2023-02-05 21:25:30 +00:00
|
|
|
err := e.db.Item.Query().
|
2023-02-05 21:12:54 +00:00
|
|
|
Where(
|
2024-04-28 17:21:43 +00:00
|
|
|
item.HasGroupWith(group.ID(groupID)),
|
2023-02-05 21:12:54 +00:00
|
|
|
).
|
|
|
|
QueryFields().
|
|
|
|
Unique(true).
|
|
|
|
Select(itemfield.FieldName).
|
|
|
|
Scan(ctx, &fields)
|
|
|
|
if err != nil {
|
|
|
|
return nil, fmt.Errorf("failed to get custom fields: %w", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
fieldNames := make([]string, len(fields))
|
|
|
|
for i, f := range fields {
|
|
|
|
fieldNames[i] = f.Name
|
|
|
|
}
|
|
|
|
|
|
|
|
return fieldNames, nil
|
|
|
|
}
|
2023-02-09 02:59:04 +00:00
|
|
|
|
|
|
|
// ZeroOutTimeFields is a helper function that can be invoked via the UI by a group member which will
|
|
|
|
// set all date fields to the beginning of the day.
|
|
|
|
//
|
|
|
|
// This is designed to resolve a long-time bug that has since been fixed with the time selector on the
|
|
|
|
// frontend. This function is intended to be used as a one-time fix for existing databases and may be
|
|
|
|
// removed in the future.
|
2024-04-28 17:21:43 +00:00
|
|
|
func (e *ItemsRepository) ZeroOutTimeFields(ctx context.Context, groupID uuid.UUID) (int, error) {
|
2023-02-09 02:59:04 +00:00
|
|
|
q := e.db.Item.Query().Where(
|
2024-04-28 17:21:43 +00:00
|
|
|
item.HasGroupWith(group.ID(groupID)),
|
2023-02-09 02:59:04 +00:00
|
|
|
item.Or(
|
|
|
|
item.PurchaseTimeNotNil(),
|
2024-02-29 18:58:26 +00:00
|
|
|
item.PurchaseFromLT("0002-01-01"),
|
2023-02-09 02:59:04 +00:00
|
|
|
item.SoldTimeNotNil(),
|
2024-02-29 18:58:26 +00:00
|
|
|
item.SoldToLT("0002-01-01"),
|
2023-02-09 02:59:04 +00:00
|
|
|
item.WarrantyExpiresNotNil(),
|
2024-02-29 18:58:26 +00:00
|
|
|
item.WarrantyDetailsLT("0002-01-01"),
|
2023-02-09 02:59:04 +00:00
|
|
|
),
|
|
|
|
)
|
|
|
|
|
|
|
|
items, err := q.All(ctx)
|
|
|
|
if err != nil {
|
|
|
|
return -1, fmt.Errorf("ZeroOutTimeFields() -> failed to get items: %w", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
toDateOnly := func(t time.Time) time.Time {
|
|
|
|
return time.Date(t.Year(), t.Month(), t.Day(), 0, 0, 0, 0, t.Location())
|
|
|
|
}
|
|
|
|
|
|
|
|
updated := 0
|
|
|
|
|
|
|
|
for _, i := range items {
|
|
|
|
updateQ := e.db.Item.Update().Where(item.ID(i.ID))
|
|
|
|
|
|
|
|
if !i.PurchaseTime.IsZero() {
|
2024-02-29 18:58:26 +00:00
|
|
|
switch {
|
|
|
|
case i.PurchaseTime.Year() < 100:
|
|
|
|
updateQ.ClearPurchaseTime()
|
|
|
|
default:
|
|
|
|
updateQ.SetPurchaseTime(toDateOnly(i.PurchaseTime))
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
updateQ.ClearPurchaseTime()
|
2023-02-09 02:59:04 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if !i.SoldTime.IsZero() {
|
2024-02-29 18:58:26 +00:00
|
|
|
switch {
|
|
|
|
case i.SoldTime.Year() < 100:
|
|
|
|
updateQ.ClearSoldTime()
|
|
|
|
default:
|
|
|
|
updateQ.SetSoldTime(toDateOnly(i.SoldTime))
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
updateQ.ClearSoldTime()
|
2023-02-09 02:59:04 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if !i.WarrantyExpires.IsZero() {
|
2024-02-29 18:58:26 +00:00
|
|
|
switch {
|
|
|
|
case i.WarrantyExpires.Year() < 100:
|
|
|
|
updateQ.ClearWarrantyExpires()
|
|
|
|
default:
|
|
|
|
updateQ.SetWarrantyExpires(toDateOnly(i.WarrantyExpires))
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
updateQ.ClearWarrantyExpires()
|
2023-02-09 02:59:04 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
_, err = updateQ.Save(ctx)
|
|
|
|
if err != nil {
|
|
|
|
return updated, fmt.Errorf("ZeroOutTimeFields() -> failed to update item: %w", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
updated++
|
|
|
|
}
|
|
|
|
|
|
|
|
return updated, nil
|
|
|
|
}
|
2023-10-07 02:51:08 +00:00
|
|
|
|
2024-04-28 17:21:43 +00:00
|
|
|
func (e *ItemsRepository) SetPrimaryPhotos(ctx context.Context, groupID uuid.UUID) (int, error) {
|
2023-10-07 02:51:08 +00:00
|
|
|
// All items where there is no primary photo
|
|
|
|
itemIDs, err := e.db.Item.Query().
|
|
|
|
Where(
|
2024-04-28 17:21:43 +00:00
|
|
|
item.HasGroupWith(group.ID(groupID)),
|
2023-10-07 02:51:08 +00:00
|
|
|
item.HasAttachmentsWith(
|
2023-12-01 17:57:29 +00:00
|
|
|
attachment.TypeEQ(attachment.TypePhoto),
|
2023-10-07 02:51:08 +00:00
|
|
|
attachment.Not(
|
|
|
|
attachment.And(
|
|
|
|
attachment.Primary(true),
|
|
|
|
attachment.TypeEQ(attachment.TypePhoto),
|
|
|
|
),
|
|
|
|
),
|
|
|
|
),
|
|
|
|
).
|
|
|
|
IDs(ctx)
|
|
|
|
if err != nil {
|
|
|
|
return -1, err
|
|
|
|
}
|
|
|
|
|
|
|
|
updated := 0
|
|
|
|
for _, id := range itemIDs {
|
|
|
|
// Find the first photo attachment
|
|
|
|
a, err := e.db.Attachment.Query().
|
|
|
|
Where(
|
|
|
|
attachment.HasItemWith(item.ID(id)),
|
|
|
|
attachment.TypeEQ(attachment.TypePhoto),
|
|
|
|
attachment.Primary(false),
|
|
|
|
).
|
|
|
|
First(ctx)
|
|
|
|
if err != nil {
|
|
|
|
return updated, err
|
|
|
|
}
|
|
|
|
|
|
|
|
// Set it as primary
|
|
|
|
_, err = e.db.Attachment.UpdateOne(a).
|
|
|
|
SetPrimary(true).
|
|
|
|
Save(ctx)
|
|
|
|
if err != nil {
|
|
|
|
return updated, err
|
|
|
|
}
|
|
|
|
|
|
|
|
updated++
|
|
|
|
}
|
|
|
|
|
|
|
|
return updated, nil
|
|
|
|
}
|