From 5f55f3f65dd6c4d95ec7cc0f1e2041e6edf14106 Mon Sep 17 00:00:00 2001 From: Aleksandr Trushkin Date: Sun, 11 Feb 2024 15:56:39 +0300 Subject: [PATCH] add fix sizes command --- cmd/cli/main.go | 40 ++++++++++++++++++++++++++++++++++++++++ 1 file changed, 40 insertions(+) diff --git a/cmd/cli/main.go b/cmd/cli/main.go index f0b8957..4d043f2 100644 --- a/cmd/cli/main.go +++ b/cmd/cli/main.go @@ -375,10 +375,19 @@ func newViewItemsCmd() *cli.Command { newViewItemsCountCmd(), newViewItemsUniqueParams(), newViewItemsParamsKnownValues(), + newViewItemsFixSizesCmd(), }, } } +func newViewItemsFixSizesCmd() *cli.Command { + return &cli.Command{ + Name: "fix-sizes", + Usage: "Iterates over params and sets sizes from parameters", + Action: decorateAction(viewItemsFixSizesAction), + } +} + func newViewItemsUniqueParams() *cli.Command { return &cli.Command{ Name: "unique-params", @@ -1109,6 +1118,37 @@ func parseEwayListAction(ctx context.Context, cmd *cli.Command) error { return nil } +func viewItemsFixSizesAction(ctx context.Context, cmd *cli.Command) error { + repository, err := components.GetRepository() + if err != nil { + return fmt.Errorf("getting repository: %w", err) + } + + dimensionDispatcher := makeDefaultDimensionDispatcher() + + toUpdate := make([]entity.GoodsItem, 0, 20_000) + bus := getItemsIter(ctx, repository.GoodsItem()) + for bus.Next() { + item := bus.Get() + + for key, value := range item.Parameters { + dimensionDispatcher.dispatch(ctx, value, key, &item.Sizes) + } + + toUpdate = append(toUpdate, item) + } + if bus.Err() != nil { + return fmt.Errorf("iterating: %w", bus.Err()) + } + + _, err = repository.GoodsItem().UpsertMany(ctx, toUpdate...) + if err != nil { + return fmt.Errorf("updating items: %w", err) + } + + return nil +} + func parseEwayDumpAction(ctx context.Context, cmd *cli.Command) error { client, err := components.GetEwayClient() if err != nil {