fix : update fixing schedule publish

This commit is contained in:
hanif salafi 2025-10-16 11:04:01 +07:00
parent 79735695df
commit 5699c1b7fa
8 changed files with 93 additions and 20 deletions

View File

@ -2,13 +2,14 @@ package repository
import (
"fmt"
"github.com/google/uuid"
"strings"
"web-medols-be/app/database"
"web-medols-be/app/database/entity"
"web-medols-be/app/module/article_files/request"
"web-medols-be/utils/paginator"
utilSvc "web-medols-be/utils/service"
"github.com/google/uuid"
)
type articleFilesRepository struct {
@ -136,7 +137,32 @@ func (_i *articleFilesRepository) Update(clientId *uuid.UUID, id uint, articleFi
}
}
articleFilesMap, err := utilSvc.StructToMap(articleFiles)
// Create a copy without the relation field to avoid updating non-existent columns
updateData := &entity.ArticleFiles{
ID: articleFiles.ID,
ArticleId: articleFiles.ArticleId,
UploadID: articleFiles.UploadID,
FilePath: articleFiles.FilePath,
FileUrl: articleFiles.FileUrl,
FileName: articleFiles.FileName,
FileThumbnail: articleFiles.FileThumbnail,
FileAlt: articleFiles.FileAlt,
WidthPixel: articleFiles.WidthPixel,
HeightPixel: articleFiles.HeightPixel,
Size: articleFiles.Size,
DownloadCount: articleFiles.DownloadCount,
CreatedById: articleFiles.CreatedById,
StatusId: articleFiles.StatusId,
IsPublish: articleFiles.IsPublish,
PublishedAt: articleFiles.PublishedAt,
ClientId: articleFiles.ClientId,
IsActive: articleFiles.IsActive,
CreatedAt: articleFiles.CreatedAt,
UpdatedAt: articleFiles.UpdatedAt,
// Exclude Article relation field
}
articleFilesMap, err := utilSvc.StructToMap(updateData)
if err != nil {
return err
}

View File

@ -3,10 +3,6 @@ package service
import (
"context"
"fmt"
"github.com/gofiber/fiber/v2"
"github.com/google/uuid"
"github.com/minio/minio-go/v7"
"github.com/rs/zerolog"
"io"
"log"
"math/rand"
@ -25,6 +21,11 @@ import (
config "web-medols-be/config/config"
minioStorage "web-medols-be/config/config"
"web-medols-be/utils/paginator"
"github.com/gofiber/fiber/v2"
"github.com/google/uuid"
"github.com/minio/minio-go/v7"
"github.com/rs/zerolog"
)
// ArticleFilesService

View File

@ -473,14 +473,14 @@ func (_i *articlesController) ArticleMonthlyStats(c *fiber.Ctx) error {
// PublishScheduling Articles
// @Summary PublishScheduling Articles
// @Description API for Publish Schedule of Article
// @Description API for Publish Schedule of Article. Supports both date-only format (2006-01-02) and datetime format (2006-01-02 15:04:05)
// @Tags Articles
// @Security Bearer
// @Param X-Client-Key header string true "Insert the X-Client-Key"
// @Param X-Csrf-Token header string false "Insert the X-Csrf-Token"
// @Param Authorization header string false "Insert your access token" default(Bearer <Add access token here>)
// @Param id query int false "article id"
// @Param date query string false "publish date"
// @Param date query string false "publish date/time (format: 2006-01-02 or 2006-01-02 15:04:05)"
// @Success 200 {object} response.Response
// @Failure 400 {object} response.BadRequestError
// @Failure 401 {object} response.UnauthorizedError

View File

@ -663,6 +663,21 @@ func (_i *articlesService) PublishScheduling(clientId *uuid.UUID, id uint, publi
if err != nil {
return err
}
// Validate publish schedule format
dateLayout := "2006-01-02"
datetimeLayout := "2006-01-02 15:04:05"
// Try parsing as datetime first (with time)
_, parseErr := time.Parse(datetimeLayout, publishSchedule)
if parseErr != nil {
// If datetime parsing fails, try parsing as date only
_, parseErr = time.Parse(dateLayout, publishSchedule)
if parseErr != nil {
return fmt.Errorf("invalid publish schedule format. Supported formats: '2006-01-02' or '2006-01-02 15:04:05'")
}
}
result.PublishSchedule = &publishSchedule
return _i.Repo.Update(clientId, id, result)
}
@ -683,21 +698,47 @@ func (_i *articlesService) ExecuteScheduling() error {
return err
}
layout := "2006-01-02"
// Support both date-only and datetime formats
dateLayout := "2006-01-02"
datetimeLayout := "2006-01-02 15:04:05"
now := time.Now()
today := now.Truncate(24 * time.Hour)
for _, article := range articles { // Looping setiap artikel
if article.PublishSchedule == nil {
continue
}
parsedDate, err := time.Parse(layout, *article.PublishSchedule)
if err != nil {
var scheduledTime time.Time
var parseErr error
// Try parsing as datetime first (with time)
scheduledTime, parseErr = time.Parse(datetimeLayout, *article.PublishSchedule)
if parseErr != nil {
// If datetime parsing fails, try parsing as date only
scheduledTime, parseErr = time.Parse(dateLayout, *article.PublishSchedule)
if parseErr != nil {
_i.Log.Warn().Str("timestamp", time.Now().
Format(time.RFC3339)).Str("Service:articlesService", "Methods:ExecuteScheduling").
Str("Invalid schedule format", *article.PublishSchedule).
Interface("Article ID", article.ID).Msg("")
continue
}
// If parsed as date only, set time to start of day (00:00:00)
scheduledTime = scheduledTime.Truncate(24 * time.Hour)
}
if parsedDate.Equal(today) {
// Check if the scheduled time has passed (for datetime) or if it's today (for date only)
shouldPublish := false
if len(*article.PublishSchedule) > 10 { // Contains time (datetime format)
// For datetime format, check if scheduled time has passed
shouldPublish = now.After(scheduledTime) || now.Equal(scheduledTime)
} else {
// For date-only format, check if it's today
today := now.Truncate(24 * time.Hour)
shouldPublish = scheduledTime.Equal(today)
}
if shouldPublish {
isPublish := true
statusIdTwo := 2
@ -711,6 +752,10 @@ func (_i *articlesService) ExecuteScheduling() error {
_i.Log.Info().Str("timestamp", time.Now().
Format(time.RFC3339)).Str("Service:articlesService", "Methods:ExecuteScheduling").
Interface("Failed to publish Article ID : ", article.ID).Msg("")
} else {
_i.Log.Info().Str("timestamp", time.Now().
Format(time.RFC3339)).Str("Service:articlesService", "Methods:ExecuteScheduling").
Interface("Successfully published Article ID : ", article.ID).Msg("")
}
}
}

View File

@ -7240,7 +7240,7 @@ const docTemplate = `{
"Bearer": []
}
],
"description": "API for Publish Schedule of Article",
"description": "API for Publish Schedule of Article. Supports both date-only format (2006-01-02) and datetime format (2006-01-02 15:04:05)",
"tags": [
"Articles"
],
@ -7274,7 +7274,7 @@ const docTemplate = `{
},
{
"type": "string",
"description": "publish date",
"description": "publish date/time (format: 2006-01-02 or 2006-01-02 15:04:05)",
"name": "date",
"in": "query"
}

View File

@ -7229,7 +7229,7 @@
"Bearer": []
}
],
"description": "API for Publish Schedule of Article",
"description": "API for Publish Schedule of Article. Supports both date-only format (2006-01-02) and datetime format (2006-01-02 15:04:05)",
"tags": [
"Articles"
],
@ -7263,7 +7263,7 @@
},
{
"type": "string",
"description": "publish date",
"description": "publish date/time (format: 2006-01-02 or 2006-01-02 15:04:05)",
"name": "date",
"in": "query"
}

View File

@ -6285,7 +6285,8 @@ paths:
- Articles
/articles/publish-scheduling:
post:
description: API for Publish Schedule of Article
description: API for Publish Schedule of Article. Supports both date-only format
(2006-01-02) and datetime format (2006-01-02 15:04:05)
parameters:
- description: Insert the X-Client-Key
in: header
@ -6305,7 +6306,7 @@ paths:
in: query
name: id
type: integer
- description: publish date
- description: 'publish date/time (format: 2006-01-02 or 2006-01-02 15:04:05)'
in: query
name: date
type: string

Binary file not shown.