add delete discounts if user have cash and user!="

This commit is contained in:
Pavel 2024-03-11 21:44:01 +03:00
parent 5264dcc64e
commit f73680cbde
2 changed files with 35 additions and 0 deletions

@ -60,3 +60,26 @@ func (receiver *DiscountClient) Apply(ctx context.Context, request *discount.App
return response, nil
}
func (receiver *DiscountClient) DeleteDiscount(ctx context.Context, request *discount.GetDiscountByIDRequest) (*discount.Discount, errors.Error) {
connection, err := grpc.Dial(receiver.discountServiceHost, grpc.WithTransportCredentials(insecure.NewCredentials()))
if err != nil {
receiver.logger.Error("failed to connect on <DeleteDiscount> of <DiscountClient>", zap.Error(err), zap.String("discount host", receiver.discountServiceHost))
return nil, errors.New(fmt.Errorf("failed connect to discount service: %w", err), errors.ErrInternalError)
}
defer func() {
if closeErr := connection.Close(); closeErr != nil {
receiver.logger.Error("failed to close connection on <DeleteDiscount> of <DiscountClient>", zap.Error(closeErr))
}
}()
client := discount.NewDiscountServiceClient(connection)
response, err := client.DeleteDiscount(ctx, request)
if err != nil {
receiver.logger.Error("failed to apply discounts on <DeleteDiscount> of <DiscountClient>", zap.Error(err), zap.Any("request", request))
return nil, errors.New(fmt.Errorf("failed to delete discount by id: %w", err), errors.ErrInternalError)
}
return response, nil
}

@ -365,6 +365,18 @@ func (api *API2) PayCart(ctx echo.Context) error {
return api.error(ctx, http.StatusPaymentRequired, "insufficient funds: %d", int64(discountResponse.Price)-account.Wallet.Money)
}
for _, applied := range discountResponse.AppliedDiscounts {
if applied.Condition.User != nil && *applied.Condition.User != "" {
_, err := api.clients.discount.DeleteDiscount(ctx.Request().Context(), &discount.GetDiscountByIDRequest{
ID: applied.ID,
})
if err != nil {
return api.error(ctx, http.StatusInternalServerError, "failed delete discount by id:%s", applied.ID)
}
}
}
// WithdrawAccountWalletMoney
request := models.WithdrawAccountWallet{