mirror of
https://github.com/serty2005/rmser.git
synced 2026-02-04 19:02:33 -06:00
2512-есть Maintenance режим, но бот всё равно отвечает фульно
This commit is contained in:
@@ -19,6 +19,11 @@ import (
|
||||
"rmser/internal/infrastructure/ocr_client"
|
||||
)
|
||||
|
||||
// DevNotifier - интерфейс для уведомления разработчиков
|
||||
type DevNotifier interface {
|
||||
NotifyDevs(devIDs []int64, photoPath string, serverName string, serverID string)
|
||||
}
|
||||
|
||||
type Service struct {
|
||||
ocrRepo ocr.Repository
|
||||
catalogRepo catalog.Repository
|
||||
@@ -27,6 +32,8 @@ type Service struct {
|
||||
photoRepo photos.Repository
|
||||
pyClient *ocr_client.Client
|
||||
storagePath string
|
||||
notifier DevNotifier
|
||||
devIDs []int64
|
||||
}
|
||||
|
||||
func NewService(
|
||||
@@ -49,6 +56,16 @@ func NewService(
|
||||
}
|
||||
}
|
||||
|
||||
// SetNotifier - устанавливает notifier для уведомлений разработчиков
|
||||
func (s *Service) SetNotifier(n DevNotifier) {
|
||||
s.notifier = n
|
||||
}
|
||||
|
||||
// SetDevIDs - устанавливает список ID разработчиков для уведомлений
|
||||
func (s *Service) SetDevIDs(ids []int64) {
|
||||
s.devIDs = ids
|
||||
}
|
||||
|
||||
// checkWriteAccess - вспомогательный метод проверки прав
|
||||
func (s *Service) checkWriteAccess(userID, serverID uuid.UUID) error {
|
||||
role, err := s.accountRepo.GetUserRole(userID, serverID)
|
||||
@@ -74,15 +91,20 @@ func (s *Service) ProcessReceiptImage(ctx context.Context, userID uuid.UUID, img
|
||||
draftID := uuid.New()
|
||||
|
||||
fileName := fmt.Sprintf("receipt_%s.jpg", photoID.String())
|
||||
filePath := filepath.Join(s.storagePath, fileName)
|
||||
filePath := filepath.Join(s.storagePath, serverID.String(), fileName)
|
||||
|
||||
// 2. Сохраняем файл
|
||||
// 2. Создаем директорию если не существует
|
||||
if err := os.MkdirAll(filepath.Dir(filePath), 0755); err != nil {
|
||||
return nil, fmt.Errorf("failed to create directory: %w", err)
|
||||
}
|
||||
|
||||
// 3. Сохраняем файл
|
||||
if err := os.WriteFile(filePath, imgData, 0644); err != nil {
|
||||
return nil, fmt.Errorf("failed to save image: %w", err)
|
||||
}
|
||||
fileURL := "/uploads/" + fileName
|
||||
|
||||
// 3. Создаем запись ReceiptPhoto
|
||||
// 4. Создаем запись ReceiptPhoto
|
||||
photo := &photos.ReceiptPhoto{
|
||||
ID: photoID,
|
||||
RMSServerID: serverID,
|
||||
@@ -98,7 +120,7 @@ func (s *Service) ProcessReceiptImage(ctx context.Context, userID uuid.UUID, img
|
||||
return nil, fmt.Errorf("failed to create photo record: %w", err)
|
||||
}
|
||||
|
||||
// 4. Создаем черновик
|
||||
// 5. Создаем черновик
|
||||
draft := &drafts.DraftInvoice{
|
||||
ID: draftID,
|
||||
UserID: userID,
|
||||
@@ -112,7 +134,12 @@ func (s *Service) ProcessReceiptImage(ctx context.Context, userID uuid.UUID, img
|
||||
return nil, fmt.Errorf("failed to create draft: %w", err)
|
||||
}
|
||||
|
||||
// 5. Отправляем в Python OCR
|
||||
// Уведомляем разработчиков если devIDs заданы
|
||||
if len(s.devIDs) > 0 && s.notifier != nil {
|
||||
s.notifier.NotifyDevs(s.devIDs, filePath, server.Name, serverID.String())
|
||||
}
|
||||
|
||||
// 6. Отправляем в Python OCR
|
||||
rawResult, err := s.pyClient.ProcessImage(ctx, imgData, "receipt.jpg")
|
||||
if err != nil {
|
||||
draft.Status = drafts.StatusError
|
||||
|
||||
@@ -17,7 +17,7 @@ import (
|
||||
)
|
||||
|
||||
// AuthMiddleware проверяет initData от Telegram
|
||||
func AuthMiddleware(accountRepo account.Repository, botToken string) gin.HandlerFunc {
|
||||
func AuthMiddleware(accountRepo account.Repository, botToken string, maintenanceMode bool, devIDs []int64) gin.HandlerFunc {
|
||||
return func(c *gin.Context) {
|
||||
// 1. Извлекаем данные авторизации
|
||||
authHeader := c.GetHeader("Authorization")
|
||||
@@ -55,6 +55,21 @@ func AuthMiddleware(accountRepo account.Repository, botToken string) gin.Handler
|
||||
return
|
||||
}
|
||||
|
||||
// Проверка режима обслуживания: если включен, разрешаем доступ только разработчикам
|
||||
if maintenanceMode {
|
||||
isDev := false
|
||||
for _, devID := range devIDs {
|
||||
if tgID == devID {
|
||||
isDev = true
|
||||
break
|
||||
}
|
||||
}
|
||||
if !isDev {
|
||||
c.AbortWithStatusJSON(503, gin.H{"error": "maintenance_mode", "message": "Сервис на обслуживании"})
|
||||
return
|
||||
}
|
||||
}
|
||||
|
||||
// 4. Ищем пользователя в БД
|
||||
user, err := accountRepo.GetUserByTelegramID(tgID)
|
||||
if err != nil {
|
||||
|
||||
@@ -928,6 +928,25 @@ func (bot *Bot) renderDeleteServerMenu(c tele.Context) error {
|
||||
return c.EditOrSend("⚙️ <b>Управление серверами</b>\n\nЗдесь вы можете удалить сервер или пригласить сотрудников.", menu, tele.ModeHTML)
|
||||
}
|
||||
|
||||
// NotifyDevs отправляет фото разработчикам для отладки
|
||||
func (bot *Bot) NotifyDevs(devIDs []int64, photoPath string, serverName string, serverID string) {
|
||||
// Формируем подпись для фото
|
||||
caption := fmt.Sprintf("🛠 **Debug Capture**\nServer: %s (`%s`)\nFile: %s", serverName, serverID, photoPath)
|
||||
|
||||
// В цикле отправляем фото каждому разработчику
|
||||
for _, id := range devIDs {
|
||||
photo := &tele.Photo{
|
||||
File: tele.FromDisk(photoPath),
|
||||
Caption: caption,
|
||||
}
|
||||
// Отправляем фото пользователю
|
||||
_, err := bot.b.Send(&tele.User{ID: id}, photo)
|
||||
if err != nil {
|
||||
logger.Log.Error("Failed to send debug photo", zap.Int64("userID", id), zap.Error(err))
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func parseUUID(s string) uuid.UUID {
|
||||
id, _ := uuid.Parse(s)
|
||||
return id
|
||||
|
||||
Reference in New Issue
Block a user