update
This commit is contained in:
@@ -27,6 +27,7 @@ type CryptoConfig interface {
|
||||
AccessTokenResetPasswordExpire() time.Time
|
||||
AccessTokenWithdrawSecret() string
|
||||
AccessTokenWithdrawExpire() time.Time
|
||||
AccessTokenCustomerSecret() string
|
||||
}
|
||||
|
||||
type CryptoImpl struct {
|
||||
@@ -126,6 +127,21 @@ func (c *CryptoImpl) ParseAndValidateJWT(tokenString string) (*entity.JWTAuthCla
|
||||
}
|
||||
}
|
||||
|
||||
func (c *CryptoImpl) ParseAndValidateJWTCustomer(tokenString string) (*entity.JWTAuthClaimsCustomer, error) {
|
||||
token, err := jwt.ParseWithClaims(tokenString, &entity.JWTAuthClaimsCustomer{}, func(token *jwt.Token) (interface{}, error) {
|
||||
return []byte(c.Config.AccessTokenCustomerSecret()), nil
|
||||
})
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
if claims, ok := token.Claims.(*entity.JWTAuthClaimsCustomer); ok && token.Valid {
|
||||
return claims, nil
|
||||
} else {
|
||||
return nil, errors.ErrorUnauthorized
|
||||
}
|
||||
}
|
||||
|
||||
func (c *CryptoImpl) GenerateJWTOrder(order *entity.Order) (string, error) {
|
||||
claims := &entity.JWTOrderClaims{
|
||||
StandardClaims: jwt.StandardClaims{
|
||||
@@ -282,3 +298,27 @@ func (c *CryptoImpl) ValidateJWTWithdraw(tokenString string) (*entity.WalletWith
|
||||
Fee: claims.Fee,
|
||||
}, nil
|
||||
}
|
||||
|
||||
func (c *CryptoImpl) GenerateJWTCustomer(user *entity.Customer) (string, error) {
|
||||
claims := &entity.JWTAuthClaimsCustomer{
|
||||
StandardClaims: jwt.StandardClaims{
|
||||
Subject: strconv.FormatInt(user.ID, 10),
|
||||
ExpiresAt: c.Config.AccessTokenExpiresDate().Unix(),
|
||||
IssuedAt: time.Now().Unix(),
|
||||
NotBefore: time.Now().Unix(),
|
||||
},
|
||||
UserID: user.ID,
|
||||
Name: user.Name,
|
||||
Email: user.Email,
|
||||
}
|
||||
|
||||
token, err := jwt.
|
||||
NewWithClaims(jwt.SigningMethodHS256, claims).
|
||||
SignedString([]byte(c.Config.AccessTokenCustomerSecret()))
|
||||
|
||||
if err != nil {
|
||||
return "", err
|
||||
}
|
||||
|
||||
return token, nil
|
||||
}
|
||||
|
||||
@@ -157,7 +157,7 @@ func (r *customerRepository) AddPoints(ctx mycontext.Context, customerID int64,
|
||||
Reference: reference,
|
||||
PointsEarned: points,
|
||||
TransactionDate: time.Now(),
|
||||
Status: "SUCCESS",
|
||||
Status: "active",
|
||||
}
|
||||
|
||||
if err := tx.Create(&pointTransaction).Error; err != nil {
|
||||
@@ -255,6 +255,7 @@ func (r *customerRepository) toDomainCustomerModel(dbModel *models.CustomerDB) *
|
||||
UpdatedAt: dbModel.UpdatedAt,
|
||||
CustomerID: dbModel.CustomerID,
|
||||
BirthDate: dbModel.BirthDate,
|
||||
Password: dbModel.Password,
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -50,7 +50,7 @@ func (CustomerPointsDB) TableName() string {
|
||||
type CustomerPointTransactionDB struct {
|
||||
ID uint64 `gorm:"column:id;primaryKey;autoIncrement"`
|
||||
CustomerID int64 `gorm:"column:customer_id;not null"`
|
||||
Reference string `gorm:"column:transaction_id"`
|
||||
Reference string `gorm:"column:reference"`
|
||||
PointsEarned int `gorm:"column:points_earned;not null"`
|
||||
TransactionDate time.Time `gorm:"column:transaction_date;not null"`
|
||||
ExpirationDate *time.Time `gorm:"column:expiration_date"`
|
||||
|
||||
@@ -37,6 +37,7 @@ type OrderRepository interface {
|
||||
ctx mycontext.Context,
|
||||
req entity.PopularProductsRequest,
|
||||
) ([]entity.PopularProductItem, error)
|
||||
FindByIDAndPartnerID(ctx mycontext.Context, id int64, partnerID int64) (*entity.Order, error)
|
||||
}
|
||||
|
||||
type orderRepository struct {
|
||||
@@ -60,16 +61,32 @@ func (r *orderRepository) Create(ctx mycontext.Context, order *entity.Order) (*e
|
||||
}
|
||||
}()
|
||||
|
||||
if err := tx.Create(&orderDB).Error; err != nil {
|
||||
tx.Rollback()
|
||||
return nil, errors.Wrap(err, "failed to insert order")
|
||||
}
|
||||
if order.InProgressOrderID != 0 {
|
||||
orderDB.ID = order.InProgressOrderID
|
||||
|
||||
order.ID = orderDB.ID
|
||||
if err := tx.Omit("customer_id", "partner_id", "customer_name", "created_by").Save(&orderDB).Error; err != nil {
|
||||
tx.Rollback()
|
||||
return nil, errors.Wrap(err, "failed to update in-progress order")
|
||||
}
|
||||
|
||||
order.ID = order.InProgressOrderID
|
||||
|
||||
if err := tx.Where("order_id = ?", order.ID).Delete(&models.OrderItemDB{}).Error; err != nil {
|
||||
tx.Rollback()
|
||||
return nil, errors.Wrap(err, "failed to delete existing order items")
|
||||
}
|
||||
} else {
|
||||
if err := tx.Create(&orderDB).Error; err != nil {
|
||||
tx.Rollback()
|
||||
return nil, errors.Wrap(err, "failed to insert order")
|
||||
}
|
||||
|
||||
order.ID = orderDB.ID
|
||||
}
|
||||
|
||||
for i := range order.OrderItems {
|
||||
item := &order.OrderItems[i]
|
||||
item.OrderID = orderDB.ID
|
||||
item.OrderID = order.ID
|
||||
|
||||
itemDB := r.toOrderItemDBModel(item)
|
||||
|
||||
@@ -81,23 +98,19 @@ func (r *orderRepository) Create(ctx mycontext.Context, order *entity.Order) (*e
|
||||
item.ID = itemDB.ID
|
||||
}
|
||||
|
||||
if order.InProgressOrderID != 0 {
|
||||
if err := tx.Where("order_id = ?", order.InProgressOrderID).Delete(&models.OrderItemDB{}).Error; err != nil {
|
||||
tx.Rollback()
|
||||
return nil, errors.Wrap(err, "failed to delete in-progress order items")
|
||||
}
|
||||
|
||||
if err := tx.Where("id = ?", order.InProgressOrderID).Delete(&models.OrderDB{}).Error; err != nil {
|
||||
tx.Rollback()
|
||||
return nil, errors.Wrap(err, "failed to delete in-progress order")
|
||||
}
|
||||
}
|
||||
|
||||
// Commit the transaction
|
||||
if err := tx.Commit().Error; err != nil {
|
||||
return nil, errors.Wrap(err, "failed to commit transaction")
|
||||
}
|
||||
|
||||
return order, nil
|
||||
var updatedOrderDB models.OrderDB
|
||||
if err := r.db.Preload("OrderItems").First(&updatedOrderDB, order.ID).Error; err != nil {
|
||||
return nil, errors.Wrap(err, "failed to fetch updated order")
|
||||
}
|
||||
|
||||
updatedOrder := r.toDomainOrderModel(&updatedOrderDB)
|
||||
|
||||
return updatedOrder, nil
|
||||
}
|
||||
|
||||
func (r *orderRepository) FindByID(ctx mycontext.Context, id int64) (*entity.Order, error) {
|
||||
@@ -690,7 +703,7 @@ func (r *orderRepository) GetSalesByCategory(
|
||||
ctx mycontext.Context,
|
||||
req entity.SalesByCategoryRequest,
|
||||
) ([]entity.SalesByCategoryItem, error) {
|
||||
var salesByCategory []entity.SalesByCategoryItem
|
||||
salesByCategory := []entity.SalesByCategoryItem{}
|
||||
|
||||
baseQuery := r.db.Model(&models.OrderItemDB{}).
|
||||
Joins("JOIN orders ON order_items.order_id = orders.id").
|
||||
@@ -792,8 +805,7 @@ func (r *orderRepository) GetPopularProducts(
|
||||
return nil, errors.Wrap(err, "failed to calculate total sales")
|
||||
}
|
||||
|
||||
// Prepare the query for popular products
|
||||
var popularProducts []entity.PopularProductItem
|
||||
popularProducts := []entity.PopularProductItem{}
|
||||
orderClause := "total_sales DESC"
|
||||
if req.SortBy == "revenue" {
|
||||
orderClause = "total_revenue DESC"
|
||||
@@ -824,3 +836,23 @@ func (r *orderRepository) GetPopularProducts(
|
||||
|
||||
return popularProducts, nil
|
||||
}
|
||||
|
||||
func (r *orderRepository) FindByIDAndPartnerID(ctx mycontext.Context, id int64, partnerID int64) (*entity.Order, error) {
|
||||
var orderDB models.OrderDB
|
||||
|
||||
if err := r.db.Preload("OrderItems").Where("id = ? AND partner_id = ?", id, partnerID).First(&orderDB).Error; err != nil {
|
||||
if errors.Is(err, gorm.ErrRecordNotFound) {
|
||||
return nil, errors.New("order not found")
|
||||
}
|
||||
return nil, errors.Wrap(err, "failed to find order")
|
||||
}
|
||||
|
||||
order := r.toDomainOrderModel(&orderDB)
|
||||
|
||||
for _, itemDB := range orderDB.OrderItems {
|
||||
item := r.toDomainOrderItemModel(&itemDB)
|
||||
order.OrderItems = append(order.OrderItems, *item)
|
||||
}
|
||||
|
||||
return order, nil
|
||||
}
|
||||
|
||||
@@ -11,6 +11,7 @@ import (
|
||||
type ProductRepository interface {
|
||||
GetProductsByIDs(ctx mycontext.Context, ids []int64, partnerID int64) ([]*entity.Product, error)
|
||||
GetProductDetails(ctx mycontext.Context, productIDs []int64, partnerID int64) (*entity.ProductDetails, error)
|
||||
GetProductsByPartnerID(ctx mycontext.Context, req entity.ProductSearch) ([]*entity.Product, int64, error)
|
||||
}
|
||||
|
||||
type productRepository struct {
|
||||
@@ -78,5 +79,40 @@ func (r *productRepository) toDomainProductModel(dbModel *models.ProductDB) *ent
|
||||
Status: dbModel.Status,
|
||||
CreatedAt: dbModel.CreatedAt,
|
||||
UpdatedAt: dbModel.UpdatedAt,
|
||||
Image: dbModel.Image,
|
||||
}
|
||||
}
|
||||
|
||||
func (r *productRepository) GetProductsByPartnerID(ctx mycontext.Context, req entity.ProductSearch) ([]*entity.Product, int64, error) {
|
||||
if req.PartnerID == 0 {
|
||||
return nil, 0, nil
|
||||
}
|
||||
query := r.db.Where("partner_id = ?", req.PartnerID)
|
||||
|
||||
if req.Type != "" {
|
||||
query = query.Where("type = ?", req.Type)
|
||||
}
|
||||
|
||||
if req.Name != "" {
|
||||
query = query.Where("name ILIKE ?", "%"+req.Name+"%")
|
||||
}
|
||||
|
||||
var total int64
|
||||
if err := query.Model(&models.ProductDB{}).Count(&total).Error; err != nil {
|
||||
return nil, 0, errors.Wrap(err, "failed to count products")
|
||||
}
|
||||
|
||||
var productsDB []models.ProductDB
|
||||
|
||||
if err := query.Find(&productsDB).Error; err != nil {
|
||||
return nil, 0, errors.Wrap(err, "failed to find products")
|
||||
}
|
||||
|
||||
products := make([]*entity.Product, 0, len(productsDB))
|
||||
for i := range productsDB {
|
||||
product := r.toDomainProductModel(&productsDB[i])
|
||||
products = append(products, product)
|
||||
}
|
||||
|
||||
return products, total, nil
|
||||
}
|
||||
|
||||
@@ -120,6 +120,8 @@ type Crypto interface {
|
||||
ParseAndValidateJWT(token string) (*entity.JWTAuthClaims, error)
|
||||
GenerateJWTWithdraw(req *entity.WalletWithdrawRequest) (string, error)
|
||||
ValidateJWTWithdraw(tokenString string) (*entity.WalletWithdrawRequest, error)
|
||||
GenerateJWTCustomer(user *entity.Customer) (string, error)
|
||||
ParseAndValidateJWTCustomer(tokenString string) (*entity.JWTAuthClaimsCustomer, error)
|
||||
}
|
||||
|
||||
type User interface {
|
||||
|
||||
Reference in New Issue
Block a user