Refactor marketplace product fetching and caching logic; update environment configuration for Redis and PostgreSQL

This commit is contained in:
2025-07-07 19:20:05 +03:00
parent c7be7e2cea
commit 3976c7d0cf
22 changed files with 319 additions and 349 deletions

View File

@@ -1,43 +1,17 @@
package wb
import (
"encoding/json"
"fmt"
"github.com/golang-jwt/jwt/v5"
"net/http"
"sipro-mps/internal/marketplace"
wbclient "sipro-mps/pkg/api/wb/client"
"sipro-mps/pkg/utils"
"time"
)
type WbAuthData struct {
Token string `json:"token"`
}
func NewWbAuthData(token string) WbAuthData {
return WbAuthData{
Token: token,
}
}
func DecodeWildberriesJwt(token []byte) (WbAuthData, jwt.MapClaims, error) {
var authData WbAuthData
err := json.Unmarshal(token, &authData)
if err != nil {
return authData, nil, fmt.Errorf("failed to unmarshal JWT: %w", err)
}
claims := jwt.MapClaims{}
_, _, err = jwt.NewParser().ParseUnverified(authData.Token, claims)
if err != nil {
return authData, nil, fmt.Errorf("invalid JWT: %w", err)
}
return authData, claims, nil
}
func GetClientFromMarketplace(mp *marketplace.Marketplace) (*wbclient.Client, error) {
authData, claims, err := DecodeWildberriesJwt(mp.AuthDataJson)
authData, claims, err := utils.DecodeWildberriesJwt(mp.AuthDataJson)
if err != nil {
return nil, fmt.Errorf("failed to decode Wildberries JWT")
}

View File

@@ -15,6 +15,7 @@ import (
"sipro-mps/internal/wb"
"sipro-mps/internal/wb/products/mapping/generated"
wbapi "sipro-mps/pkg/api/wb/client"
"sipro-mps/pkg/utils"
)
const (
@@ -31,7 +32,7 @@ func (a apiRepository) ParseMarketplace(ctx context.Context, marketplaceId int)
if err != nil {
return nil, "", err
}
_, claims, err := wb.DecodeWildberriesJwt(marketplaceByID.AuthDataJson)
_, claims, err := utils.DecodeWildberriesJwt(marketplaceByID.AuthDataJson)
if err != nil {
return nil, "", err
}
@@ -113,7 +114,7 @@ func (a apiRepository) StreamAllProductsCache(ctx context.Context, marketplaceId
errChan <- fmt.Errorf("unmarshalling products from cache: %w", err)
return
}
task, err := types.NewFetchProductsTask(marketplaceId)
task, err := types.NewFetchProductsTask(types.TypeWbFetchProducts, marketplaceId)
if err != nil {
errChan <- fmt.Errorf("creating fetch products task: %w", err)
return

View File

@@ -7,6 +7,7 @@ import (
"github.com/redis/rueidis"
"net/http"
"sipro-mps/internal/redis"
"sipro-mps/pkg/utils"
"time"
)
@@ -68,12 +69,12 @@ func (t *RateLimitTransport) RoundTrip(req *http.Request) (*http.Response, error
ctx := req.Context()
tokenString := req.Header.Get("Authorization")
authData := NewWbAuthData(tokenString)
authData := utils.NewWbAuthData(tokenString)
authDataBytes, err := json.Marshal(authData)
if err != nil {
return nil, fmt.Errorf("failed to marshal Wildberries auth data: %w", err)
}
_, claims, err := DecodeWildberriesJwt(authDataBytes)
_, claims, err := utils.DecodeWildberriesJwt(authDataBytes)
if err != nil {
return nil, fmt.Errorf("failed to decode Wildberries JWT: %w", err)
}