feat: update protobuf message structure for product tariffs; change Offers to Offer and adjust related conversions
This commit is contained in:
		@@ -6,7 +6,7 @@ import (
 | 
			
		||||
 | 
			
		||||
type PbCalculateProductTariffsRequest = pb.CalculateProductTariffsRequest
 | 
			
		||||
type PbCalculateProductTariffsResponse = pb.CalculateProductTariffsResponse
 | 
			
		||||
type PbOffer = pb.CalculateProductTariffsRequest_Offers
 | 
			
		||||
type PbOffer = pb.CalculateProductTariffsRequest_Offer
 | 
			
		||||
type PbResponseOffer = pb.CalculateProductTariffsResponse_Offers
 | 
			
		||||
type PbParameters = pb.CalculateProductTariffsRequest_Parameters
 | 
			
		||||
type PbGetProductsOffer = pb.GetProductsResponse_Offer
 | 
			
		||||
 
 | 
			
		||||
@@ -1,8 +1,9 @@
 | 
			
		||||
package mapping
 | 
			
		||||
 | 
			
		||||
import (
 | 
			
		||||
	"git.denco.store/fakz9/yandex-go-client"
 | 
			
		||||
	proto "sipro-mps/api/generated/v1/yandexmarket/products"
 | 
			
		||||
 | 
			
		||||
	"git.denco.store/fakz9/yandex-go-client"
 | 
			
		||||
)
 | 
			
		||||
 | 
			
		||||
//go:generate go run github.com/jmattheis/goverter/cmd/goverter gen .
 | 
			
		||||
@@ -16,7 +17,7 @@ import (
 | 
			
		||||
// goverter:useZeroValueOnPointerInconsistency yes
 | 
			
		||||
// goverter:extend Int64ToFloat32 Int64ToInt32 Float32ToInt64 Int32ToInt64 PointerInt32ToInt64
 | 
			
		||||
type Converter interface {
 | 
			
		||||
	ProtoOfferToYmOffer(details *proto.CalculateProductTariffsRequest_Offers) *ymclient.CalculateTariffsOfferDTO
 | 
			
		||||
	ProtoOfferToYmOffer(details *proto.CalculateProductTariffsRequest_Offer) *ymclient.CalculateTariffsOfferDTO
 | 
			
		||||
	ProtoParametersToYmParameters(details *proto.CalculateProductTariffsRequest_Parameters) *ymclient.CalculateTariffsParametersDTO
 | 
			
		||||
 | 
			
		||||
	// Response converters
 | 
			
		||||
 
 | 
			
		||||
@@ -11,16 +11,16 @@ import (
 | 
			
		||||
 | 
			
		||||
type ConverterImpl struct{}
 | 
			
		||||
 | 
			
		||||
func (c *ConverterImpl) ProtoOfferToYmOffer(source *products.CalculateProductTariffsRequest_Offers) *yandexgoclient.CalculateTariffsOfferDTO {
 | 
			
		||||
func (c *ConverterImpl) ProtoOfferToYmOffer(source *products.CalculateProductTariffsRequest_Offer) *yandexgoclient.CalculateTariffsOfferDTO {
 | 
			
		||||
	var pYmclientCalculateTariffsOfferDTO *yandexgoclient.CalculateTariffsOfferDTO
 | 
			
		||||
	if source != nil {
 | 
			
		||||
		var ymclientCalculateTariffsOfferDTO yandexgoclient.CalculateTariffsOfferDTO
 | 
			
		||||
		ymclientCalculateTariffsOfferDTO.CategoryId = (*source).CategoryId
 | 
			
		||||
		ymclientCalculateTariffsOfferDTO.Price = mapping.Int64ToFloat32((*source).Price)
 | 
			
		||||
		ymclientCalculateTariffsOfferDTO.Length = mapping.Int64ToFloat32((*source).Length)
 | 
			
		||||
		ymclientCalculateTariffsOfferDTO.Width = mapping.Int64ToFloat32((*source).Width)
 | 
			
		||||
		ymclientCalculateTariffsOfferDTO.Height = mapping.Int64ToFloat32((*source).Height)
 | 
			
		||||
		ymclientCalculateTariffsOfferDTO.Weight = mapping.Int64ToFloat32((*source).Weight)
 | 
			
		||||
		ymclientCalculateTariffsOfferDTO.Price = (*source).Price
 | 
			
		||||
		ymclientCalculateTariffsOfferDTO.Length = (*source).Length
 | 
			
		||||
		ymclientCalculateTariffsOfferDTO.Width = (*source).Width
 | 
			
		||||
		ymclientCalculateTariffsOfferDTO.Height = (*source).Height
 | 
			
		||||
		ymclientCalculateTariffsOfferDTO.Weight = (*source).Weight
 | 
			
		||||
		pInt32 := mapping.Int64ToInt32((*source).Quantity)
 | 
			
		||||
		ymclientCalculateTariffsOfferDTO.Quantity = &pInt32
 | 
			
		||||
		pYmclientCalculateTariffsOfferDTO = &ymclientCalculateTariffsOfferDTO
 | 
			
		||||
@@ -117,11 +117,11 @@ func (c *ConverterImpl) pYmclientOfferWeightDimensionsDTOToPProductsGetProductsR
 | 
			
		||||
func (c *ConverterImpl) ymclientCalculateTariffsOfferDTOToPProductsCalculateProductTariffsResponse_Offer(source yandexgoclient.CalculateTariffsOfferDTO) *products.CalculateProductTariffsResponse_Offer {
 | 
			
		||||
	var productsCalculateProductTariffsResponse_Offer products.CalculateProductTariffsResponse_Offer
 | 
			
		||||
	productsCalculateProductTariffsResponse_Offer.CategoryId = source.CategoryId
 | 
			
		||||
	productsCalculateProductTariffsResponse_Offer.Price = mapping.Float32ToInt64(source.Price)
 | 
			
		||||
	productsCalculateProductTariffsResponse_Offer.Length = mapping.Float32ToInt64(source.Length)
 | 
			
		||||
	productsCalculateProductTariffsResponse_Offer.Width = mapping.Float32ToInt64(source.Width)
 | 
			
		||||
	productsCalculateProductTariffsResponse_Offer.Height = mapping.Float32ToInt64(source.Height)
 | 
			
		||||
	productsCalculateProductTariffsResponse_Offer.Weight = mapping.Float32ToInt64(source.Weight)
 | 
			
		||||
	productsCalculateProductTariffsResponse_Offer.Price = source.Price
 | 
			
		||||
	productsCalculateProductTariffsResponse_Offer.Length = source.Length
 | 
			
		||||
	productsCalculateProductTariffsResponse_Offer.Width = source.Width
 | 
			
		||||
	productsCalculateProductTariffsResponse_Offer.Height = source.Height
 | 
			
		||||
	productsCalculateProductTariffsResponse_Offer.Weight = source.Weight
 | 
			
		||||
	if source.Quantity != nil {
 | 
			
		||||
		productsCalculateProductTariffsResponse_Offer.Quantity = mapping.Int32ToInt64(*source.Quantity)
 | 
			
		||||
	}
 | 
			
		||||
 
 | 
			
		||||
@@ -184,6 +184,7 @@ func (r *apiRepository) processOfferMappings(offerMappings []ymclient.GetOfferMa
 | 
			
		||||
			resultOffers = append(resultOffers, &pb.GetProductsResponse_Offer{})
 | 
			
		||||
			continue
 | 
			
		||||
		}
 | 
			
		||||
		protoOffer.MarketCategoryId = *offerMapping.Mapping.MarketCategoryId
 | 
			
		||||
 | 
			
		||||
		resultOffers = append(resultOffers, protoOffer)
 | 
			
		||||
	}
 | 
			
		||||
@@ -233,15 +234,22 @@ func (r *apiRepository) setTariffsRateLimit() {
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
// processTariffChunk processes a single chunk of offers for tariff calculation
 | 
			
		||||
func (r *apiRepository) processTariffChunk(ctx context.Context, client *ymclient.APIClient, ymParameters *ymclient.CalculateTariffsParametersDTO, offerChunk []*pb.CalculateProductTariffsRequest_Offers, chunkIndex int) (*pb.CalculateProductTariffsResponse, error) {
 | 
			
		||||
func (r *apiRepository) processTariffChunk(ctx context.Context, client *ymclient.APIClient, ymParameters *ymclient.CalculateTariffsParametersDTO, offerChunk []*pb.CalculateProductTariffsRequest_Offer, chunkIndex int) (*pb.CalculateProductTariffsResponse, error) {
 | 
			
		||||
	ymOffers := r.convertOffersToYM(offerChunk)
 | 
			
		||||
	if len(ymOffers) == 0 {
 | 
			
		||||
		fmt.Printf("Skipping chunk %d: no valid offers\n", chunkIndex+1)
 | 
			
		||||
		return nil, nil
 | 
			
		||||
	}
 | 
			
		||||
 | 
			
		||||
	ymRequest := ymclient.NewCalculateTariffsRequest(*ymParameters, ymOffers)
 | 
			
		||||
	if ymParameters.CampaignId != nil && *ymParameters.CampaignId > 0 {
 | 
			
		||||
		ymParameters.SellingProgram = nil
 | 
			
		||||
		ymParameters.Frequency = nil
 | 
			
		||||
		ymParameters.Currency = nil
 | 
			
		||||
 | 
			
		||||
	} else {
 | 
			
		||||
		ymParameters.CampaignId = nil
 | 
			
		||||
	}
 | 
			
		||||
	ymRequest := ymclient.NewCalculateTariffsRequest(*ymParameters, ymOffers)
 | 
			
		||||
	response, httpResp, err := client.TariffsAPI.CalculateTariffs(ctx).
 | 
			
		||||
		CalculateTariffsRequest(*ymRequest).
 | 
			
		||||
		Execute()
 | 
			
		||||
@@ -263,7 +271,7 @@ func (r *apiRepository) processTariffChunk(ctx context.Context, client *ymclient
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
// convertOffersToYM converts protobuf offers to Yandex Market format
 | 
			
		||||
func (r *apiRepository) convertOffersToYM(offers []*pb.CalculateProductTariffsRequest_Offers) []ymclient.CalculateTariffsOfferDTO {
 | 
			
		||||
func (r *apiRepository) convertOffersToYM(offers []*pb.CalculateProductTariffsRequest_Offer) []ymclient.CalculateTariffsOfferDTO {
 | 
			
		||||
	var ymOffers []ymclient.CalculateTariffsOfferDTO
 | 
			
		||||
	for _, offer := range offers {
 | 
			
		||||
		ymOffer := r.converter.ProtoOfferToYmOffer(offer)
 | 
			
		||||
 
 | 
			
		||||
		Reference in New Issue
	
	Block a user