added: delete zero prices in period

This commit is contained in:
nquidox 2025-12-06 17:33:18 +03:00
parent a338fd03b2
commit d997d8bfa4
3 changed files with 94 additions and 28 deletions

View file

@ -54,6 +54,9 @@ func (h *Handler) RegisterRoutes(r *gin.RouterGroup, authMW gin.HandlerFunc, ref
zeroPricesGroup := merchGroup.Group("/zeroprices", authMW) zeroPricesGroup := merchGroup.Group("/zeroprices", authMW)
zeroPricesGroup.GET("", h.controller.getZeroPrices) zeroPricesGroup.GET("", h.controller.getZeroPrices)
zeroPricesGroup.DELETE("", h.controller.deleteZeroPrices) zeroPricesGroup.DELETE("", h.controller.deleteZeroPrices)
zeroPricesGroup.DELETE("/period", h.controller.deleteZeroPricesPeriod)
} }
// @Summary Добавить новый мерч // @Summary Добавить новый мерч
@ -156,7 +159,7 @@ func (co *controller) getAllMerch(c *gin.Context) {
// @Description Обновить информацию про мерч по его uuid в json-е // @Description Обновить информацию про мерч по его uuid в json-е
// @Tags Merch // @Tags Merch
// @Security BearerAuth // @Security BearerAuth
// @Accept json // @Accept json
// @Param body body UpdateMerchDTO true "merch_uuid" // @Param body body UpdateMerchDTO true "merch_uuid"
// @Success 200 // @Success 200
// @Failure 400 {object} responses.ErrorResponse400 // @Failure 400 {object} responses.ErrorResponse400
@ -194,7 +197,7 @@ func (co *controller) updateMerch(c *gin.Context) {
// @Failure 400 {object} responses.ErrorResponse400 // @Failure 400 {object} responses.ErrorResponse400
// @Failure 500 {object} responses.ErrorResponse500 // @Failure 500 {object} responses.ErrorResponse500
// //
// @Router /merch/{uuid} [delete] // @Router /merch/{uuid} [delete]
func (co *controller) deleteMerch(c *gin.Context) { func (co *controller) deleteMerch(c *gin.Context) {
merchUuid := c.Param("uuid") merchUuid := c.Param("uuid")
if merchUuid == "" { if merchUuid == "" {
@ -229,9 +232,9 @@ func (co *controller) deleteMerch(c *gin.Context) {
// @Failure 500 {object} responses.ErrorResponse500 // @Failure 500 {object} responses.ErrorResponse500
// @Router /prices [get] // @Router /prices [get]
// //
// @Failure 400 {object} responses.ErrorResponse400 // @Failure 400 {object} responses.ErrorResponse400
// @Failure 500 {object} responses.ErrorResponse500 // @Failure 500 {object} responses.ErrorResponse500
// @Router /prices [get] // @Router /prices [get]
func (co *controller) getChartsPrices(c *gin.Context) { func (co *controller) getChartsPrices(c *gin.Context) {
daysQuery := strings.ToLower(c.DefaultQuery("days", "")) daysQuery := strings.ToLower(c.DefaultQuery("days", ""))
@ -442,7 +445,7 @@ func (co *controller) deleteMerchImage(c *gin.Context) {
// @Description Создать новую метку для товара // @Description Создать новую метку для товара
// @Tags Merch labels // @Tags Merch labels
// @Security BearerAuth // @Security BearerAuth
// @Accept json // @Accept json
// @Param payload body LabelDTO true "payload" // @Param payload body LabelDTO true "payload"
// @Success 200 // @Success 200
// @Failure 400 {object} responses.ErrorResponse400 // @Failure 400 {object} responses.ErrorResponse400
@ -507,7 +510,7 @@ func (co *controller) getLabels(c *gin.Context) {
// @Description Изменить метку // @Description Изменить метку
// @Tags Merch labels // @Tags Merch labels
// @Security BearerAuth // @Security BearerAuth
// @Accept json // @Accept json
// @Param uuid path string true "label uuid" // @Param uuid path string true "label uuid"
// @Param payload body LabelDTO true "payload" // @Param payload body LabelDTO true "payload"
// @Success 200 // @Success 200
@ -580,16 +583,16 @@ func (co *controller) deleteLabel(c *gin.Context) {
c.Status(http.StatusOK) c.Status(http.StatusOK)
} }
// @Summary Прикрепить метку к товару // @Summary Прикрепить метку к товару
// @Description Прикрепить метку к товару // @Description Прикрепить метку к товару
// @Tags Merch labels // @Tags Merch labels
// @Security BearerAuth // @Security BearerAuth
// @Accept json // @Accept json
// @Param payload body LabelLink true "payload" // @Param payload body LabelLink true "payload"
// @Success 200 // @Success 200
// @Failure 400 {object} responses.ErrorResponse400 // @Failure 400 {object} responses.ErrorResponse400
// @Failure 500 {object} responses.ErrorResponse500 // @Failure 500 {object} responses.ErrorResponse500
// @Router /merch/labels/attach [post] // @Router /merch/labels/attach [post]
func (co *controller) attachLabel(c *gin.Context) { func (co *controller) attachLabel(c *gin.Context) {
const logMsg = "Merch | Attach label" const logMsg = "Merch | Attach label"
@ -615,16 +618,16 @@ func (co *controller) attachLabel(c *gin.Context) {
c.Status(http.StatusOK) c.Status(http.StatusOK)
} }
// @Summary Удалить привязку метки к товару // @Summary Удалить привязку метки к товару
// @Description Удалить привязку метки к товару // @Description Удалить привязку метки к товару
// @Tags Merch labels // @Tags Merch labels
// @Security BearerAuth // @Security BearerAuth
// @Accept json // @Accept json
// @Param payload body LabelLink true "payload" // @Param payload body LabelLink true "payload"
// @Success 200 // @Success 200
// @Failure 400 {object} responses.ErrorResponse400 // @Failure 400 {object} responses.ErrorResponse400
// @Failure 500 {object} responses.ErrorResponse500 // @Failure 500 {object} responses.ErrorResponse500
// @Router /merch/labels/detach [post] // @Router /merch/labels/detach [post]
func (co *controller) detachLabel(c *gin.Context) { func (co *controller) detachLabel(c *gin.Context) {
const logMsg = "Merch | Detach label" const logMsg = "Merch | Detach label"
@ -719,7 +722,7 @@ func (co *controller) getZeroPrices(c *gin.Context) {
// @Description Пометить нулевые цены как удаленные // @Description Пометить нулевые цены как удаленные
// @Tags Merch zero prices // @Tags Merch zero prices
// @Security BearerAuth // @Security BearerAuth
// @Accept json // @Accept json
// @Param payload body DeleteZeroPrices true "payload" // @Param payload body DeleteZeroPrices true "payload"
// @Success 200 // @Success 200
// @Failure 400 {object} responses.ErrorResponse400 // @Failure 400 {object} responses.ErrorResponse400
@ -749,3 +752,45 @@ func (co *controller) deleteZeroPrices(c *gin.Context) {
} }
c.Status(http.StatusOK) c.Status(http.StatusOK)
} }
// @Summary Пометить нулевые цены как удаленные за указанный период
// @Description Пометить нулевые цены как удаленные за указанный период
// @Tags Merch zero prices
// @Security BearerAuth
// @Param start query string true "start"
// @Param end query string true "end"
// @Success 200
// @Failure 400 {object} responses.ErrorResponse400
// @Failure 500 {object} responses.ErrorResponse500
// @Router /merch/zeroprices/period [delete]
func (co *controller) deleteZeroPricesPeriod(c *gin.Context) {
const logMsg = "Merch | Delete zero prices period"
userUuid, err := co.utils.GetUserUuidFromContext(c)
if err != nil {
c.JSON(http.StatusInternalServerError, responses.ErrorResponse500{Error: err.Error()})
log.WithError(err).Error(logMsg)
return
}
start, err := co.utils.ParseTime(c.Query("start"))
if err != nil {
c.JSON(http.StatusBadRequest, responses.ErrorResponse400{Error: err.Error()})
log.WithError(err).Error(logMsg)
return
}
end, err := co.utils.ParseTime(c.Query("end"))
if err != nil {
c.JSON(http.StatusBadRequest, responses.ErrorResponse400{Error: err.Error()})
log.WithError(err).Error(logMsg)
return
}
if err = co.service.deleteZeroPricesPeriod(userUuid, start, end); err != nil {
c.JSON(http.StatusInternalServerError, responses.ErrorResponse500{Error: err.Error()})
log.WithError(err).Error(logMsg)
return
}
c.Status(http.StatusOK)
}

View file

@ -42,6 +42,7 @@ type prices interface {
getZeroPrices(userUuid string) ([]ZeroPrice, error) getZeroPrices(userUuid string) ([]ZeroPrice, error)
deleteZeroPrices(list []DeleteZeroPrices) error deleteZeroPrices(list []DeleteZeroPrices) error
deleteZeroPricesPeriod(userUuid string, start, end time.Time) error
} }
type labels interface { type labels interface {
@ -371,3 +372,19 @@ func (r *Repo) deleteZeroPrices(list []DeleteZeroPrices) error {
} }
return nil return nil
} }
func (r *Repo) deleteZeroPricesPeriod(userUuid string, start, end time.Time) error {
if err := r.db.Exec(`
UPDATE prices
SET deleted_at = ?
FROM merch
WHERE prices.merch_uuid = merch.merch_uuid
AND merch.user_uuid = ?
AND prices.price = 0
AND prices.deleted_at IS NULL
AND prices.created_at BETWEEN ? AND ?;
`, time.Now().UTC(), userUuid, start, end).Error; err != nil {
return err
}
return nil
}

View file

@ -662,3 +662,7 @@ func (s *service) deleteZeroPrices(userUuid string, list []DeleteZeroPrices) err
return s.repo.deleteZeroPrices(toDelete) return s.repo.deleteZeroPrices(toDelete)
} }
func (s *service) deleteZeroPricesPeriod(userUuid string, start, end time.Time) error {
return s.repo.deleteZeroPricesPeriod(userUuid, start, end)
}