Compare commits
24 Commits
725d4c4474
...
feature/co
| Author | SHA1 | Date | |
|---|---|---|---|
| 3bb9074a18 | |||
| 5c0052398e | |||
| a707c138fe | |||
| 71b45b0b60 | |||
| e4e56c577f | |||
| 3783283f92 | |||
| e251b73f41 | |||
| b19f6df20c | |||
| 508b57bf2d | |||
| c9d1a75f56 | |||
| e65863a782 | |||
| e490e49c93 | |||
| 79c3eed25b | |||
| 6f93a1f9bc | |||
| 94bf3a6b86 | |||
| f99db54c03 | |||
| a846a2dce4 | |||
| 91d651184a | |||
| 582353ecf1 | |||
| 87595300b7 | |||
| 0f93308c65 | |||
| cce7622ae1 | |||
| ae84ce74a7 | |||
| 9777114e16 |
@@ -15,23 +15,23 @@
|
|||||||
## 🚀 Этап 1: Фундамент (Недели 1-2)
|
## 🚀 Этап 1: Фундамент (Недели 1-2)
|
||||||
|
|
||||||
### Шаг 1.1: Очистка и настройка проекта
|
### Шаг 1.1: Очистка и настройка проекта
|
||||||
- [ ] Удалить зависимости: `grpc`, `redis`, `prometheus`
|
- [x] Удалить зависимости: `grpc`, `redis`, `prometheus`
|
||||||
- [ ] Обновить `go.mod` - оставить только необходимые пакеты
|
- [x] Обновить `go.mod` - оставить только необходимые пакеты
|
||||||
- [ ] Настроить структуру проекта согласно Go standards
|
- [x] Настроить структуру проекта согласно Go standards
|
||||||
- [ ] Добавить `.env` для конфигурации
|
- [x] Добавить `.env` для конфигурации
|
||||||
|
|
||||||
**Файлы для изменения:**
|
**Файлы для изменения:**
|
||||||
```
|
```
|
||||||
go.mod - удалить grpc, redis, prometheus
|
go.mod - удалить grpc, redis, prometheus ✅
|
||||||
cmd/main.go - убрать redis, grpc клиенты
|
cmd/main.go - убрать redis, grpc клиенты ✅
|
||||||
internal/config/config.go - упростить конфигурацию
|
internal/config/config.go - упростить конфигурацию ✅
|
||||||
```
|
```
|
||||||
|
|
||||||
### Шаг 1.2: Базовая конфигурация
|
### Шаг 1.2: Базовая конфигурация
|
||||||
- [ ] Создать `internal/config/config.go` с упрощённой структурой
|
- [x] Создать `internal/config/config.go` с упрощённой структурой
|
||||||
- [ ] Добавить поддержку `.env` файлов
|
- [x] Добавить поддержку `.env` файлов
|
||||||
- [ ] Настроить логирование через logrus
|
- [x] Настроить логирование через logrus
|
||||||
- [ ] Добавить health check endpoint
|
- [x] Добавить health check endpoint
|
||||||
|
|
||||||
**Структура конфигурации:**
|
**Структура конфигурации:**
|
||||||
```go
|
```go
|
||||||
@@ -62,16 +62,16 @@ type JWTConfig struct {
|
|||||||
```
|
```
|
||||||
|
|
||||||
### Шаг 1.3: Подключение к базе данных
|
### Шаг 1.3: Подключение к базе данных
|
||||||
- [ ] Создать `internal/database/connection.go`
|
- [x] Создать `internal/database/connection.go`
|
||||||
- [ ] Настроить подключение к PostgreSQL
|
- [x] Настроить подключение к PostgreSQL
|
||||||
- [ ] Добавить миграции через `golang-migrate`
|
- [x] Добавить миграции через `golang-migrate`
|
||||||
- [ ] Создать базовые таблицы
|
- [x] Создать базовые таблицы
|
||||||
|
|
||||||
**Структура БД (упрощённая):**
|
**Структура БД (упрощённая):**
|
||||||
```sql
|
```sql
|
||||||
-- organizations
|
-- organizations
|
||||||
CREATE TABLE organizations (
|
CREATE TABLE organizations (
|
||||||
id UUID PRIMARY KEY DEFAULT gen_random_uuid(),
|
id UUID PRIMARY KEY DEFAULT uuid_generate_v4(),
|
||||||
name VARCHAR(255) NOT NULL,
|
name VARCHAR(255) NOT NULL,
|
||||||
type VARCHAR(100),
|
type VARCHAR(100),
|
||||||
settings JSONB,
|
settings JSONB,
|
||||||
@@ -80,8 +80,8 @@ CREATE TABLE organizations (
|
|||||||
|
|
||||||
-- users
|
-- users
|
||||||
CREATE TABLE users (
|
CREATE TABLE users (
|
||||||
id UUID PRIMARY KEY DEFAULT gen_random_uuid(),
|
id UUID PRIMARY KEY DEFAULT uuid_generate_v4(),
|
||||||
organization_id UUID REFERENCES organizations(id),
|
organization_id UUID REFERENCES organizations(id) ON DELETE CASCADE,
|
||||||
email VARCHAR(255) UNIQUE NOT NULL,
|
email VARCHAR(255) UNIQUE NOT NULL,
|
||||||
password_hash VARCHAR(255) NOT NULL,
|
password_hash VARCHAR(255) NOT NULL,
|
||||||
role VARCHAR(50) DEFAULT 'user',
|
role VARCHAR(50) DEFAULT 'user',
|
||||||
@@ -90,9 +90,9 @@ CREATE TABLE users (
|
|||||||
|
|
||||||
-- storage_locations
|
-- storage_locations
|
||||||
CREATE TABLE storage_locations (
|
CREATE TABLE storage_locations (
|
||||||
id UUID PRIMARY KEY DEFAULT gen_random_uuid(),
|
id UUID PRIMARY KEY DEFAULT uuid_generate_v4(),
|
||||||
organization_id UUID REFERENCES organizations(id),
|
organization_id UUID REFERENCES organizations(id) ON DELETE CASCADE,
|
||||||
parent_id UUID REFERENCES storage_locations(id),
|
parent_id UUID REFERENCES storage_locations(id) ON DELETE CASCADE,
|
||||||
name VARCHAR(255) NOT NULL,
|
name VARCHAR(255) NOT NULL,
|
||||||
address VARCHAR(100) NOT NULL,
|
address VARCHAR(100) NOT NULL,
|
||||||
type VARCHAR(50) NOT NULL,
|
type VARCHAR(50) NOT NULL,
|
||||||
@@ -102,8 +102,8 @@ CREATE TABLE storage_locations (
|
|||||||
|
|
||||||
-- items
|
-- items
|
||||||
CREATE TABLE items (
|
CREATE TABLE items (
|
||||||
id UUID PRIMARY KEY DEFAULT gen_random_uuid(),
|
id UUID PRIMARY KEY DEFAULT uuid_generate_v4(),
|
||||||
organization_id UUID REFERENCES organizations(id),
|
organization_id UUID REFERENCES organizations(id) ON DELETE CASCADE,
|
||||||
name VARCHAR(255) NOT NULL,
|
name VARCHAR(255) NOT NULL,
|
||||||
description TEXT,
|
description TEXT,
|
||||||
category VARCHAR(100),
|
category VARCHAR(100),
|
||||||
@@ -112,19 +112,19 @@ CREATE TABLE items (
|
|||||||
|
|
||||||
-- item_placements
|
-- item_placements
|
||||||
CREATE TABLE item_placements (
|
CREATE TABLE item_placements (
|
||||||
id UUID PRIMARY KEY DEFAULT gen_random_uuid(),
|
id UUID PRIMARY KEY DEFAULT uuid_generate_v4(),
|
||||||
organization_id UUID REFERENCES organizations(id),
|
organization_id UUID REFERENCES organizations(id) ON DELETE CASCADE,
|
||||||
item_id UUID REFERENCES items(id),
|
item_id UUID REFERENCES items(id) ON DELETE CASCADE,
|
||||||
location_id UUID REFERENCES storage_locations(id),
|
location_id UUID REFERENCES storage_locations(id) ON DELETE CASCADE,
|
||||||
quantity INTEGER DEFAULT 1,
|
quantity INTEGER DEFAULT 1,
|
||||||
created_at TIMESTAMP DEFAULT NOW()
|
created_at TIMESTAMP DEFAULT NOW()
|
||||||
);
|
);
|
||||||
```
|
```
|
||||||
|
|
||||||
### Шаг 1.4: Базовые модели
|
### Шаг 1.4: Базовые модели
|
||||||
- [ ] Создать `internal/models/` с основными структурами
|
- [x] Создать `internal/models/` с основными структурами
|
||||||
- [ ] Добавить валидацию через `validator`
|
- [x] Добавить валидацию через `validator`
|
||||||
- [ ] Реализовать JSON теги для API
|
- [x] Реализовать JSON теги для API
|
||||||
|
|
||||||
**Основные модели:**
|
**Основные модели:**
|
||||||
```go
|
```go
|
||||||
@@ -142,20 +142,21 @@ type User struct {
|
|||||||
ID uuid.UUID `json:"id" db:"id"`
|
ID uuid.UUID `json:"id" db:"id"`
|
||||||
OrganizationID uuid.UUID `json:"organization_id" db:"organization_id"`
|
OrganizationID uuid.UUID `json:"organization_id" db:"organization_id"`
|
||||||
Email string `json:"email" validate:"required,email"`
|
Email string `json:"email" validate:"required,email"`
|
||||||
|
PasswordHash string `json:"-" db:"password_hash"`
|
||||||
Role string `json:"role"`
|
Role string `json:"role"`
|
||||||
CreatedAt time.Time `json:"created_at" db:"created_at"`
|
CreatedAt time.Time `json:"created_at" db:"created_at"`
|
||||||
}
|
}
|
||||||
|
|
||||||
// internal/models/storage_location.go
|
// internal/models/storage_location.go
|
||||||
type StorageLocation struct {
|
type StorageLocation struct {
|
||||||
ID uuid.UUID `json:"id" db:"id"`
|
ID uuid.UUID `json:"id" db:"id"`
|
||||||
OrganizationID uuid.UUID `json:"organization_id" db:"organization_id"`
|
OrganizationID uuid.UUID `json:"organization_id" db:"organization_id"`
|
||||||
ParentID *uuid.UUID `json:"parent_id,omitempty" db:"parent_id"`
|
ParentID *uuid.UUID `json:"parent_id,omitempty" db:"parent_id"`
|
||||||
Name string `json:"name" validate:"required"`
|
Name string `json:"name" validate:"required"`
|
||||||
Address string `json:"address" validate:"required"`
|
Address string `json:"address" validate:"required"`
|
||||||
Type string `json:"type" validate:"required"`
|
Type string `json:"type" validate:"required"`
|
||||||
Coordinates JSON `json:"coordinates"`
|
Coordinates JSON `json:"coordinates"`
|
||||||
CreatedAt time.Time `json:"created_at" db:"created_at"`
|
CreatedAt time.Time `json:"created_at" db:"created_at"`
|
||||||
}
|
}
|
||||||
|
|
||||||
// internal/models/item.go
|
// internal/models/item.go
|
||||||
@@ -181,13 +182,13 @@ type ItemPlacement struct {
|
|||||||
|
|
||||||
---
|
---
|
||||||
|
|
||||||
## 🔐 Этап 2: Аутентификация (Неделя 2)
|
## 🔐 Этап 2: Аутентификация (Неделя 2) ✅ ЗАВЕРШЁН
|
||||||
|
|
||||||
### Шаг 2.1: JWT аутентификация
|
### Шаг 2.1: JWT аутентификация ✅
|
||||||
- [ ] Создать `internal/auth/jwt.go`
|
- [x] Создать `internal/auth/jwt.go`
|
||||||
- [ ] Реализовать генерацию и валидацию JWT токенов
|
- [x] Реализовать генерацию и валидацию JWT токенов
|
||||||
- [ ] Добавить organization-scope в токены
|
- [x] Добавить organization-scope в токены
|
||||||
- [ ] Создать middleware для проверки аутентификации
|
- [x] Создать middleware для проверки аутентификации
|
||||||
|
|
||||||
**JWT структура:**
|
**JWT структура:**
|
||||||
```go
|
```go
|
||||||
@@ -200,15 +201,15 @@ type Claims struct {
|
|||||||
}
|
}
|
||||||
```
|
```
|
||||||
|
|
||||||
### Шаг 2.2: Хеширование паролей
|
### Шаг 2.2: Хеширование паролей ✅
|
||||||
- [ ] Создать `internal/auth/password.go`
|
- [x] Создать `internal/auth/password.go`
|
||||||
- [ ] Использовать bcrypt для хеширования
|
- [x] Использовать bcrypt для хеширования
|
||||||
- [ ] Добавить функции проверки паролей
|
- [x] Добавить функции проверки паролей
|
||||||
|
|
||||||
### Шаг 2.3: API endpoints для аутентификации
|
### Шаг 2.3: API endpoints для аутентификации ✅
|
||||||
- [ ] `POST /api/auth/register` - регистрация организации и пользователя
|
- [x] `POST /api/auth/register` - регистрация организации и пользователя
|
||||||
- [ ] `POST /api/auth/login` - вход в систему
|
- [x] `POST /api/auth/login` - вход в систему
|
||||||
- [ ] `POST /api/auth/refresh` - обновление токена (опционально)
|
- [x] `POST /api/auth/refresh` - обновление токена (опционально)
|
||||||
|
|
||||||
**Структура запросов:**
|
**Структура запросов:**
|
||||||
```go
|
```go
|
||||||
@@ -225,14 +226,21 @@ type LoginRequest struct {
|
|||||||
}
|
}
|
||||||
```
|
```
|
||||||
|
|
||||||
|
**Результаты тестирования:**
|
||||||
|
- ✅ Регистрация: `POST /api/auth/register` - 201 Created
|
||||||
|
- ✅ Вход: `POST /api/auth/login` - 200 OK
|
||||||
|
- ✅ Middleware: JWT токены проходят валидацию
|
||||||
|
- ✅ JSON поля: исправлена конвертация в PostgreSQL
|
||||||
|
```
|
||||||
|
|
||||||
---
|
---
|
||||||
|
|
||||||
## 🏗️ Этап 3: API структура (Неделя 3)
|
## 🏗️ Этап 3: API структура (Неделя 3) ✅ ЗАВЕРШЁН
|
||||||
|
|
||||||
### Шаг 3.1: Базовые handlers
|
### Шаг 3.1: Repository pattern ✅
|
||||||
- [ ] Создать `internal/api/handlers/` с базовыми структурами
|
- [x] Создать `internal/repository/` для работы с БД
|
||||||
- [ ] Реализовать middleware для CORS, логирования, аутентификации
|
- [x] Реализовать CRUD операции для всех сущностей
|
||||||
- [ ] Добавить обработку ошибок
|
- [x] Добавить organization-scope фильтрацию
|
||||||
|
|
||||||
**Структура handlers:**
|
**Структура handlers:**
|
||||||
```
|
```
|
||||||
@@ -251,51 +259,58 @@ internal/api/
|
|||||||
└── server.go
|
└── server.go
|
||||||
```
|
```
|
||||||
|
|
||||||
### Шаг 3.2: Repository pattern
|
### Шаг 3.2: Service layer ✅
|
||||||
- [ ] Создать `internal/repository/` для работы с БД
|
- [x] Создать `internal/service/` с бизнес-логикой
|
||||||
- [ ] Реализовать CRUD операции для всех сущностей
|
- [x] Реализовать сервисы для всех сущностей
|
||||||
- [ ] Добавить organization-scope фильтрацию
|
- [x] Добавить валидацию и логирование
|
||||||
|
|
||||||
**Основные репозитории:**
|
**Основные сервисы:**
|
||||||
```go
|
```go
|
||||||
// internal/repository/organizations.go
|
// internal/service/location_service.go
|
||||||
type OrganizationRepository interface {
|
type LocationService interface {
|
||||||
Create(ctx context.Context, org *models.Organization) error
|
CreateLocation(ctx context.Context, orgID uuid.UUID, req *models.CreateLocationRequest) (*models.StorageLocation, error)
|
||||||
GetByID(ctx context.Context, id uuid.UUID) (*models.Organization, error)
|
GetLocation(ctx context.Context, id uuid.UUID, orgID uuid.UUID) (*models.StorageLocation, error)
|
||||||
Update(ctx context.Context, org *models.Organization) error
|
GetLocations(ctx context.Context, orgID uuid.UUID) ([]*models.StorageLocation, error)
|
||||||
|
UpdateLocation(ctx context.Context, id uuid.UUID, orgID uuid.UUID, req *models.CreateLocationRequest) (*models.StorageLocation, error)
|
||||||
|
DeleteLocation(ctx context.Context, id uuid.UUID, orgID uuid.UUID) error
|
||||||
}
|
}
|
||||||
|
|
||||||
// internal/repository/users.go
|
// internal/service/item_service.go
|
||||||
type UserRepository interface {
|
type ItemService interface {
|
||||||
Create(ctx context.Context, user *models.User, password string) error
|
CreateItem(ctx context.Context, orgID uuid.UUID, req *models.CreateItemRequest) (*models.Item, error)
|
||||||
GetByEmail(ctx context.Context, email string) (*models.User, error)
|
GetItem(ctx context.Context, id uuid.UUID, orgID uuid.UUID) (*models.Item, error)
|
||||||
GetByID(ctx context.Context, id uuid.UUID) (*models.User, error)
|
GetItems(ctx context.Context, orgID uuid.UUID) ([]*models.Item, error)
|
||||||
|
UpdateItem(ctx context.Context, id uuid.UUID, orgID uuid.UUID, req *models.CreateItemRequest) (*models.Item, error)
|
||||||
|
DeleteItem(ctx context.Context, id uuid.UUID, orgID uuid.UUID) error
|
||||||
|
SearchItems(ctx context.Context, orgID uuid.UUID, query string, category string) ([]*models.Item, error)
|
||||||
}
|
}
|
||||||
|
|
||||||
// internal/repository/locations.go
|
// internal/service/operations_service.go
|
||||||
type LocationRepository interface {
|
type OperationsService interface {
|
||||||
Create(ctx context.Context, location *models.StorageLocation) error
|
PlaceItem(ctx context.Context, orgID uuid.UUID, req *models.PlaceItemRequest) (*models.ItemPlacement, error)
|
||||||
GetByID(ctx context.Context, id uuid.UUID, orgID uuid.UUID) (*models.StorageLocation, error)
|
MoveItem(ctx context.Context, placementID uuid.UUID, newLocationID uuid.UUID, orgID uuid.UUID) error
|
||||||
GetByOrganization(ctx context.Context, orgID uuid.UUID) ([]*models.StorageLocation, error)
|
GetItemPlacements(ctx context.Context, itemID uuid.UUID, orgID uuid.UUID) ([]*models.ItemPlacement, error)
|
||||||
Update(ctx context.Context, location *models.StorageLocation) error
|
GetLocationPlacements(ctx context.Context, locationID uuid.UUID, orgID uuid.UUID) ([]*models.ItemPlacement, error)
|
||||||
Delete(ctx context.Context, id uuid.UUID, orgID uuid.UUID) error
|
Search(ctx context.Context, orgID uuid.UUID, req *models.SearchRequest) (*models.SearchResponse, error)
|
||||||
}
|
|
||||||
|
|
||||||
// internal/repository/items.go
|
|
||||||
type ItemRepository interface {
|
|
||||||
Create(ctx context.Context, item *models.Item) error
|
|
||||||
GetByID(ctx context.Context, id uuid.UUID, orgID uuid.UUID) (*models.Item, error)
|
|
||||||
GetByOrganization(ctx context.Context, orgID uuid.UUID) ([]*models.Item, error)
|
|
||||||
Search(ctx context.Context, orgID uuid.UUID, query string) ([]*models.Item, error)
|
|
||||||
Update(ctx context.Context, item *models.Item) error
|
|
||||||
Delete(ctx context.Context, id uuid.UUID, orgID uuid.UUID) error
|
|
||||||
}
|
}
|
||||||
```
|
```
|
||||||
|
|
||||||
### Шаг 3.3: Service layer
|
### Шаг 3.3: HTTP Handlers ✅
|
||||||
- [ ] Создать `internal/service/` для бизнес-логики
|
- [x] Создать `internal/api/handlers/` с базовыми структурами
|
||||||
- [ ] Реализовать валидацию и обработку данных
|
- [x] Реализовать handlers для всех API endpoints
|
||||||
- [ ] Добавить транзакции для сложных операций
|
- [x] Добавить валидацию запросов
|
||||||
|
|
||||||
|
**Результаты этапа 3:**
|
||||||
|
- ✅ Созданы репозитории для locations, items, operations
|
||||||
|
- ✅ Реализованы сервисы с бизнес-логикой
|
||||||
|
- ✅ Созданы HTTP handlers для всех API endpoints
|
||||||
|
- ✅ Добавлена функция GetClaims в middleware
|
||||||
|
- ✅ Organization-scope фильтрация во всех операциях
|
||||||
|
- ✅ Поддержка JSON полей в PostgreSQL
|
||||||
|
- ✅ Валидация всех входящих запросов
|
||||||
|
```
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
---
|
---
|
||||||
|
|
||||||
|
|||||||
4
core-service/README.md
Normal file
4
core-service/README.md
Normal file
@@ -0,0 +1,4 @@
|
|||||||
|
# Test updated hook
|
||||||
|
# Test fixed hook
|
||||||
|
# Test benchmark fix
|
||||||
|
# Test subtests fix
|
||||||
@@ -2,7 +2,6 @@ package main
|
|||||||
|
|
||||||
import (
|
import (
|
||||||
"context"
|
"context"
|
||||||
"log"
|
|
||||||
"net/http"
|
"net/http"
|
||||||
"os"
|
"os"
|
||||||
"os/signal"
|
"os/signal"
|
||||||
@@ -12,9 +11,7 @@ import (
|
|||||||
"erp-mvp/core-service/internal/api"
|
"erp-mvp/core-service/internal/api"
|
||||||
"erp-mvp/core-service/internal/config"
|
"erp-mvp/core-service/internal/config"
|
||||||
"erp-mvp/core-service/internal/database"
|
"erp-mvp/core-service/internal/database"
|
||||||
"erp-mvp/core-service/internal/grpc"
|
|
||||||
"erp-mvp/core-service/internal/logger"
|
"erp-mvp/core-service/internal/logger"
|
||||||
"erp-mvp/core-service/internal/redis"
|
|
||||||
)
|
)
|
||||||
|
|
||||||
func main() {
|
func main() {
|
||||||
@@ -34,22 +31,8 @@ func main() {
|
|||||||
}
|
}
|
||||||
defer db.Close()
|
defer db.Close()
|
||||||
|
|
||||||
// Подключение к Redis
|
|
||||||
redisClient, err := redis.Connect(cfg.Redis)
|
|
||||||
if err != nil {
|
|
||||||
logger.Fatal("Failed to connect to Redis", err)
|
|
||||||
}
|
|
||||||
defer redisClient.Close()
|
|
||||||
|
|
||||||
// Инициализация gRPC клиента для Document Service
|
|
||||||
grpcClient, err := grpc.NewDocumentServiceClient(cfg.DocumentService.URL)
|
|
||||||
if err != nil {
|
|
||||||
logger.Fatal("Failed to connect to Document Service", err)
|
|
||||||
}
|
|
||||||
defer grpcClient.Close()
|
|
||||||
|
|
||||||
// Создание API сервера
|
// Создание API сервера
|
||||||
server := api.NewServer(cfg, db, redisClient, grpcClient, logger)
|
server := api.NewServer(cfg, db, logger)
|
||||||
|
|
||||||
// Запуск HTTP сервера
|
// Запуск HTTP сервера
|
||||||
go func() {
|
go func() {
|
||||||
|
|||||||
348
core-service/examples/api_test.go
Normal file
348
core-service/examples/api_test.go
Normal file
@@ -0,0 +1,348 @@
|
|||||||
|
package examples
|
||||||
|
|
||||||
|
import (
|
||||||
|
"bytes"
|
||||||
|
"context"
|
||||||
|
"encoding/json"
|
||||||
|
"net/http"
|
||||||
|
"net/http/httptest"
|
||||||
|
"testing"
|
||||||
|
"time"
|
||||||
|
|
||||||
|
"github.com/gin-gonic/gin"
|
||||||
|
"github.com/google/uuid"
|
||||||
|
"github.com/stretchr/testify/assert"
|
||||||
|
"github.com/stretchr/testify/mock"
|
||||||
|
|
||||||
|
"erp-mvp/core-service/internal/api/handlers"
|
||||||
|
"erp-mvp/core-service/internal/api/middleware"
|
||||||
|
"erp-mvp/core-service/internal/auth"
|
||||||
|
"erp-mvp/core-service/internal/models"
|
||||||
|
)
|
||||||
|
|
||||||
|
// MockAuthService мок для AuthService
|
||||||
|
type MockAuthService struct {
|
||||||
|
mock.Mock
|
||||||
|
}
|
||||||
|
|
||||||
|
func (m *MockAuthService) Register(ctx context.Context, req *models.RegisterRequest) (*models.LoginResponse, error) {
|
||||||
|
args := m.Called(ctx, req)
|
||||||
|
return args.Get(0).(*models.LoginResponse), args.Error(1)
|
||||||
|
}
|
||||||
|
|
||||||
|
func (m *MockAuthService) Login(ctx context.Context, req *models.LoginRequest) (*models.LoginResponse, error) {
|
||||||
|
args := m.Called(ctx, req)
|
||||||
|
return args.Get(0).(*models.LoginResponse), args.Error(1)
|
||||||
|
}
|
||||||
|
|
||||||
|
// TestAuthHandler_Register тестирует endpoint регистрации
|
||||||
|
func TestAuthHandler_Register(t *testing.T) {
|
||||||
|
// Arrange
|
||||||
|
gin.SetMode(gin.TestMode)
|
||||||
|
|
||||||
|
mockAuthService := &MockAuthService{}
|
||||||
|
handler := handlers.NewAuthHandler(mockAuthService)
|
||||||
|
|
||||||
|
router := gin.New()
|
||||||
|
router.POST("/register", handler.Register)
|
||||||
|
|
||||||
|
registerReq := &models.RegisterRequest{
|
||||||
|
OrganizationName: "Test Workshop",
|
||||||
|
UserEmail: "admin@test.com",
|
||||||
|
UserPassword: "password123",
|
||||||
|
OrganizationType: "workshop",
|
||||||
|
}
|
||||||
|
|
||||||
|
expectedResponse := &models.LoginResponse{
|
||||||
|
Token: "test_token",
|
||||||
|
User: models.UserResponse{
|
||||||
|
ID: uuid.New(),
|
||||||
|
Email: "admin@test.com",
|
||||||
|
Role: "admin",
|
||||||
|
},
|
||||||
|
Organization: models.OrganizationResponse{
|
||||||
|
ID: uuid.New(),
|
||||||
|
Name: "Test Workshop",
|
||||||
|
Type: "workshop",
|
||||||
|
},
|
||||||
|
}
|
||||||
|
|
||||||
|
mockAuthService.On("Register", mock.Anything, registerReq).Return(expectedResponse, nil)
|
||||||
|
|
||||||
|
reqBody, _ := json.Marshal(registerReq)
|
||||||
|
|
||||||
|
// Act
|
||||||
|
w := httptest.NewRecorder()
|
||||||
|
req, _ := http.NewRequest("POST", "/register", bytes.NewBuffer(reqBody))
|
||||||
|
req.Header.Set("Content-Type", "application/json")
|
||||||
|
router.ServeHTTP(w, req)
|
||||||
|
|
||||||
|
// Assert
|
||||||
|
assert.Equal(t, http.StatusCreated, w.Code)
|
||||||
|
|
||||||
|
var response models.LoginResponse
|
||||||
|
err := json.Unmarshal(w.Body.Bytes(), &response)
|
||||||
|
assert.NoError(t, err)
|
||||||
|
assert.Equal(t, expectedResponse.Token, response.Token)
|
||||||
|
assert.Equal(t, expectedResponse.User.Email, response.User.Email)
|
||||||
|
assert.Equal(t, expectedResponse.Organization.Name, response.Organization.Name)
|
||||||
|
|
||||||
|
mockAuthService.AssertExpectations(t)
|
||||||
|
}
|
||||||
|
|
||||||
|
// TestAuthHandler_Login тестирует endpoint входа
|
||||||
|
func TestAuthHandler_Login(t *testing.T) {
|
||||||
|
// Arrange
|
||||||
|
gin.SetMode(gin.TestMode)
|
||||||
|
|
||||||
|
mockAuthService := &MockAuthService{}
|
||||||
|
handler := handlers.NewAuthHandler(mockAuthService)
|
||||||
|
|
||||||
|
router := gin.New()
|
||||||
|
router.POST("/login", handler.Login)
|
||||||
|
|
||||||
|
loginReq := &models.LoginRequest{
|
||||||
|
Email: "admin@test.com",
|
||||||
|
Password: "password123",
|
||||||
|
}
|
||||||
|
|
||||||
|
expectedResponse := &models.LoginResponse{
|
||||||
|
Token: "test_token",
|
||||||
|
User: models.UserResponse{
|
||||||
|
ID: uuid.New(),
|
||||||
|
Email: "admin@test.com",
|
||||||
|
Role: "admin",
|
||||||
|
},
|
||||||
|
Organization: models.OrganizationResponse{
|
||||||
|
ID: uuid.New(),
|
||||||
|
Name: "Test Workshop",
|
||||||
|
Type: "workshop",
|
||||||
|
},
|
||||||
|
}
|
||||||
|
|
||||||
|
mockAuthService.On("Login", mock.Anything, loginReq).Return(expectedResponse, nil)
|
||||||
|
|
||||||
|
reqBody, _ := json.Marshal(loginReq)
|
||||||
|
|
||||||
|
// Act
|
||||||
|
w := httptest.NewRecorder()
|
||||||
|
req, _ := http.NewRequest("POST", "/login", bytes.NewBuffer(reqBody))
|
||||||
|
req.Header.Set("Content-Type", "application/json")
|
||||||
|
router.ServeHTTP(w, req)
|
||||||
|
|
||||||
|
// Assert
|
||||||
|
assert.Equal(t, http.StatusOK, w.Code)
|
||||||
|
|
||||||
|
var response models.LoginResponse
|
||||||
|
err := json.Unmarshal(w.Body.Bytes(), &response)
|
||||||
|
assert.NoError(t, err)
|
||||||
|
assert.Equal(t, expectedResponse.Token, response.Token)
|
||||||
|
|
||||||
|
mockAuthService.AssertExpectations(t)
|
||||||
|
}
|
||||||
|
|
||||||
|
// TestAuthHandler_Register_ValidationError тестирует валидацию при регистрации
|
||||||
|
func TestAuthHandler_Register_ValidationError(t *testing.T) {
|
||||||
|
// Arrange
|
||||||
|
gin.SetMode(gin.TestMode)
|
||||||
|
|
||||||
|
mockAuthService := &MockAuthService{}
|
||||||
|
handler := handlers.NewAuthHandler(mockAuthService)
|
||||||
|
|
||||||
|
router := gin.New()
|
||||||
|
router.POST("/register", handler.Register)
|
||||||
|
|
||||||
|
// Неверный запрос - отсутствует email
|
||||||
|
registerReq := map[string]interface{}{
|
||||||
|
"organization_name": "Test Workshop",
|
||||||
|
"user_password": "password123",
|
||||||
|
"organization_type": "workshop",
|
||||||
|
}
|
||||||
|
|
||||||
|
reqBody, _ := json.Marshal(registerReq)
|
||||||
|
|
||||||
|
// Act
|
||||||
|
w := httptest.NewRecorder()
|
||||||
|
req, _ := http.NewRequest("POST", "/register", bytes.NewBuffer(reqBody))
|
||||||
|
req.Header.Set("Content-Type", "application/json")
|
||||||
|
router.ServeHTTP(w, req)
|
||||||
|
|
||||||
|
// Assert
|
||||||
|
assert.Equal(t, http.StatusBadRequest, w.Code)
|
||||||
|
|
||||||
|
var errorResponse map[string]interface{}
|
||||||
|
err := json.Unmarshal(w.Body.Bytes(), &errorResponse)
|
||||||
|
assert.NoError(t, err)
|
||||||
|
assert.Contains(t, errorResponse["error"], "Validation failed")
|
||||||
|
}
|
||||||
|
|
||||||
|
// TestAuthMiddleware тестирует middleware аутентификации
|
||||||
|
func TestAuthMiddleware(t *testing.T) {
|
||||||
|
// Arrange
|
||||||
|
gin.SetMode(gin.TestMode)
|
||||||
|
|
||||||
|
secret := "test_secret_key"
|
||||||
|
ttl := 24 * time.Hour
|
||||||
|
jwtService := auth.NewJWTService(secret, ttl)
|
||||||
|
authMiddleware := middleware.NewAuthMiddleware(jwtService)
|
||||||
|
|
||||||
|
router := gin.New()
|
||||||
|
router.Use(authMiddleware.AuthRequired())
|
||||||
|
|
||||||
|
router.GET("/protected", func(c *gin.Context) {
|
||||||
|
claims := middleware.GetClaims(c)
|
||||||
|
c.JSON(http.StatusOK, gin.H{
|
||||||
|
"user_id": claims.UserID.String(),
|
||||||
|
"organization_id": claims.OrganizationID.String(),
|
||||||
|
"email": claims.Email,
|
||||||
|
"role": claims.Role,
|
||||||
|
})
|
||||||
|
})
|
||||||
|
|
||||||
|
// Создаем валидный JWT токен
|
||||||
|
userID := uuid.New()
|
||||||
|
orgID := uuid.New()
|
||||||
|
validToken, err := jwtService.GenerateToken(userID, orgID, "test@example.com", "admin")
|
||||||
|
assert.NoError(t, err)
|
||||||
|
|
||||||
|
// Act - тест с валидным токеном
|
||||||
|
w := httptest.NewRecorder()
|
||||||
|
req, _ := http.NewRequest("GET", "/protected", nil)
|
||||||
|
req.Header.Set("Authorization", "Bearer "+validToken)
|
||||||
|
router.ServeHTTP(w, req)
|
||||||
|
|
||||||
|
// Assert
|
||||||
|
assert.Equal(t, http.StatusOK, w.Code)
|
||||||
|
|
||||||
|
var response map[string]interface{}
|
||||||
|
err = json.Unmarshal(w.Body.Bytes(), &response)
|
||||||
|
assert.NoError(t, err)
|
||||||
|
assert.Equal(t, userID.String(), response["user_id"])
|
||||||
|
assert.Equal(t, "test@example.com", response["email"])
|
||||||
|
|
||||||
|
// Act - тест без токена
|
||||||
|
w = httptest.NewRecorder()
|
||||||
|
req, _ = http.NewRequest("GET", "/protected", nil)
|
||||||
|
router.ServeHTTP(w, req)
|
||||||
|
|
||||||
|
// Assert
|
||||||
|
assert.Equal(t, http.StatusUnauthorized, w.Code)
|
||||||
|
|
||||||
|
// Act - тест с неверным токеном
|
||||||
|
w = httptest.NewRecorder()
|
||||||
|
req, _ = http.NewRequest("GET", "/protected", nil)
|
||||||
|
req.Header.Set("Authorization", "Bearer invalid_token")
|
||||||
|
router.ServeHTTP(w, req)
|
||||||
|
|
||||||
|
// Assert
|
||||||
|
assert.Equal(t, http.StatusUnauthorized, w.Code)
|
||||||
|
}
|
||||||
|
|
||||||
|
// TestLocationHandler_CreateLocation тестирует создание места хранения
|
||||||
|
func TestLocationHandler_CreateLocation(t *testing.T) {
|
||||||
|
// Arrange
|
||||||
|
gin.SetMode(gin.TestMode)
|
||||||
|
|
||||||
|
mockLocationService := &MockLocationService{}
|
||||||
|
handler := handlers.NewLocationHandler(mockLocationService)
|
||||||
|
|
||||||
|
router := gin.New()
|
||||||
|
router.Use(mockAuthMiddleware())
|
||||||
|
router.POST("/locations", handler.CreateLocation)
|
||||||
|
|
||||||
|
locationReq := &models.CreateLocationRequest{
|
||||||
|
Name: "Test Location",
|
||||||
|
Address: "Test Address",
|
||||||
|
Type: "warehouse",
|
||||||
|
Coordinates: models.JSON{
|
||||||
|
"lat": 55.7558,
|
||||||
|
"lng": 37.6176,
|
||||||
|
},
|
||||||
|
}
|
||||||
|
|
||||||
|
expectedLocation := &models.StorageLocation{
|
||||||
|
ID: uuid.New(),
|
||||||
|
Name: "Test Location",
|
||||||
|
Address: "Test Address",
|
||||||
|
Type: "warehouse",
|
||||||
|
Coordinates: models.JSON{
|
||||||
|
"lat": 55.7558,
|
||||||
|
"lng": 37.6176,
|
||||||
|
},
|
||||||
|
}
|
||||||
|
|
||||||
|
mockLocationService.On("CreateLocation", mock.Anything, mock.Anything, locationReq).Return(expectedLocation, nil)
|
||||||
|
|
||||||
|
reqBody, _ := json.Marshal(locationReq)
|
||||||
|
|
||||||
|
// Act
|
||||||
|
w := httptest.NewRecorder()
|
||||||
|
req, _ := http.NewRequest("POST", "/locations", bytes.NewBuffer(reqBody))
|
||||||
|
req.Header.Set("Content-Type", "application/json")
|
||||||
|
req.Header.Set("Authorization", "Bearer test_token")
|
||||||
|
router.ServeHTTP(w, req)
|
||||||
|
|
||||||
|
// Assert
|
||||||
|
assert.Equal(t, http.StatusCreated, w.Code)
|
||||||
|
|
||||||
|
var response models.StorageLocation
|
||||||
|
err := json.Unmarshal(w.Body.Bytes(), &response)
|
||||||
|
assert.NoError(t, err)
|
||||||
|
assert.Equal(t, expectedLocation.Name, response.Name)
|
||||||
|
assert.Equal(t, expectedLocation.Type, response.Type)
|
||||||
|
|
||||||
|
mockLocationService.AssertExpectations(t)
|
||||||
|
}
|
||||||
|
|
||||||
|
// MockLocationService мок для LocationService
|
||||||
|
type MockLocationService struct {
|
||||||
|
mock.Mock
|
||||||
|
}
|
||||||
|
|
||||||
|
func (m *MockLocationService) CreateLocation(ctx context.Context, orgID uuid.UUID, req *models.CreateLocationRequest) (*models.StorageLocation, error) {
|
||||||
|
args := m.Called(ctx, orgID, req)
|
||||||
|
return args.Get(0).(*models.StorageLocation), args.Error(1)
|
||||||
|
}
|
||||||
|
|
||||||
|
func (m *MockLocationService) GetLocations(ctx context.Context, orgID uuid.UUID) ([]*models.StorageLocation, error) {
|
||||||
|
args := m.Called(ctx, orgID)
|
||||||
|
return args.Get(0).([]*models.StorageLocation), args.Error(1)
|
||||||
|
}
|
||||||
|
|
||||||
|
func (m *MockLocationService) GetLocation(ctx context.Context, id, orgID uuid.UUID) (*models.StorageLocation, error) {
|
||||||
|
args := m.Called(ctx, id, orgID)
|
||||||
|
return args.Get(0).(*models.StorageLocation), args.Error(1)
|
||||||
|
}
|
||||||
|
|
||||||
|
func (m *MockLocationService) UpdateLocation(ctx context.Context, id, orgID uuid.UUID, req *models.CreateLocationRequest) (*models.StorageLocation, error) {
|
||||||
|
args := m.Called(ctx, id, orgID, req)
|
||||||
|
return args.Get(0).(*models.StorageLocation), args.Error(1)
|
||||||
|
}
|
||||||
|
|
||||||
|
func (m *MockLocationService) DeleteLocation(ctx context.Context, id, orgID uuid.UUID) error {
|
||||||
|
args := m.Called(ctx, id, orgID)
|
||||||
|
return args.Error(0)
|
||||||
|
}
|
||||||
|
|
||||||
|
func (m *MockLocationService) GetChildren(ctx context.Context, parentID, orgID uuid.UUID) ([]*models.StorageLocation, error) {
|
||||||
|
args := m.Called(ctx, parentID, orgID)
|
||||||
|
return args.Get(0).([]*models.StorageLocation), args.Error(1)
|
||||||
|
}
|
||||||
|
|
||||||
|
// mockAuthMiddleware создает middleware для тестов
|
||||||
|
func mockAuthMiddleware() gin.HandlerFunc {
|
||||||
|
return func(c *gin.Context) {
|
||||||
|
// Устанавливаем тестовые claims
|
||||||
|
claims := &auth.Claims{
|
||||||
|
UserID: uuid.New(),
|
||||||
|
OrganizationID: uuid.New(),
|
||||||
|
Email: "test@example.com",
|
||||||
|
Role: "admin",
|
||||||
|
}
|
||||||
|
c.Set("user_id", claims.UserID)
|
||||||
|
c.Set("organization_id", claims.OrganizationID)
|
||||||
|
c.Set("email", claims.Email)
|
||||||
|
c.Set("role", claims.Role)
|
||||||
|
c.Next()
|
||||||
|
}
|
||||||
|
}
|
||||||
183
core-service/examples/auth_test.go
Normal file
183
core-service/examples/auth_test.go
Normal file
@@ -0,0 +1,183 @@
|
|||||||
|
package examples
|
||||||
|
|
||||||
|
import (
|
||||||
|
"testing"
|
||||||
|
"time"
|
||||||
|
|
||||||
|
"github.com/golang-jwt/jwt/v5"
|
||||||
|
"github.com/google/uuid"
|
||||||
|
"github.com/stretchr/testify/assert"
|
||||||
|
"github.com/stretchr/testify/require"
|
||||||
|
|
||||||
|
"erp-mvp/core-service/internal/auth"
|
||||||
|
)
|
||||||
|
|
||||||
|
// TestJWTService_GenerateToken тестирует генерацию JWT токенов
|
||||||
|
func TestJWTService_GenerateToken(t *testing.T) {
|
||||||
|
// Arrange
|
||||||
|
secret := "test_secret_key"
|
||||||
|
ttl := 24 * time.Hour
|
||||||
|
jwtService := auth.NewJWTService(secret, ttl)
|
||||||
|
userID := uuid.New()
|
||||||
|
orgID := uuid.New()
|
||||||
|
email := "test@example.com"
|
||||||
|
role := "admin"
|
||||||
|
|
||||||
|
// Act
|
||||||
|
token, err := jwtService.GenerateToken(userID, orgID, email, role)
|
||||||
|
|
||||||
|
// Assert
|
||||||
|
require.NoError(t, err)
|
||||||
|
assert.NotEmpty(t, token)
|
||||||
|
|
||||||
|
// Проверяем, что токен можно декодировать
|
||||||
|
parsedToken, err := jwt.Parse(token, func(token *jwt.Token) (interface{}, error) {
|
||||||
|
return []byte(secret), nil
|
||||||
|
})
|
||||||
|
require.NoError(t, err)
|
||||||
|
assert.True(t, parsedToken.Valid)
|
||||||
|
|
||||||
|
// Проверяем claims
|
||||||
|
claims, ok := parsedToken.Claims.(jwt.MapClaims)
|
||||||
|
require.True(t, ok)
|
||||||
|
assert.Equal(t, userID.String(), claims["user_id"])
|
||||||
|
assert.Equal(t, orgID.String(), claims["organization_id"])
|
||||||
|
assert.Equal(t, email, claims["email"])
|
||||||
|
assert.Equal(t, role, claims["role"])
|
||||||
|
}
|
||||||
|
|
||||||
|
// TestJWTService_ValidateToken тестирует валидацию JWT токенов
|
||||||
|
func TestJWTService_ValidateToken(t *testing.T) {
|
||||||
|
// Arrange
|
||||||
|
secret := "test_secret_key"
|
||||||
|
ttl := 24 * time.Hour
|
||||||
|
jwtService := auth.NewJWTService(secret, ttl)
|
||||||
|
|
||||||
|
// Создаем валидный токен для тестирования
|
||||||
|
userID := uuid.New()
|
||||||
|
orgID := uuid.New()
|
||||||
|
validToken, err := jwtService.GenerateToken(userID, orgID, "test@example.com", "admin")
|
||||||
|
require.NoError(t, err)
|
||||||
|
|
||||||
|
tests := []struct {
|
||||||
|
name string
|
||||||
|
secret string
|
||||||
|
token string
|
||||||
|
wantErr bool
|
||||||
|
}{
|
||||||
|
{
|
||||||
|
name: "valid token",
|
||||||
|
secret: secret,
|
||||||
|
token: validToken,
|
||||||
|
wantErr: false,
|
||||||
|
},
|
||||||
|
{
|
||||||
|
name: "invalid signature",
|
||||||
|
secret: "wrong_secret",
|
||||||
|
token: validToken,
|
||||||
|
wantErr: true,
|
||||||
|
},
|
||||||
|
{
|
||||||
|
name: "invalid token format",
|
||||||
|
secret: secret,
|
||||||
|
token: "invalid_token_format",
|
||||||
|
wantErr: true,
|
||||||
|
},
|
||||||
|
}
|
||||||
|
|
||||||
|
for _, tt := range tests {
|
||||||
|
t.Run(tt.name, func(t *testing.T) {
|
||||||
|
// Arrange
|
||||||
|
testJWTService := auth.NewJWTService(tt.secret, ttl)
|
||||||
|
|
||||||
|
// Act
|
||||||
|
claims, err := testJWTService.ValidateToken(tt.token)
|
||||||
|
|
||||||
|
// Assert
|
||||||
|
if tt.wantErr {
|
||||||
|
assert.Error(t, err)
|
||||||
|
assert.Nil(t, claims)
|
||||||
|
} else {
|
||||||
|
assert.NoError(t, err)
|
||||||
|
assert.NotNil(t, claims)
|
||||||
|
assert.Equal(t, userID, claims.UserID)
|
||||||
|
assert.Equal(t, orgID, claims.OrganizationID)
|
||||||
|
}
|
||||||
|
})
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// TestPasswordHashing тестирует хеширование и проверку паролей
|
||||||
|
func TestPasswordHashing(t *testing.T) {
|
||||||
|
// Arrange
|
||||||
|
password := "mySecurePassword123"
|
||||||
|
|
||||||
|
// Act - хешируем пароль
|
||||||
|
hashedPassword, err := auth.HashPassword(password)
|
||||||
|
|
||||||
|
// Assert
|
||||||
|
require.NoError(t, err)
|
||||||
|
assert.NotEmpty(t, hashedPassword)
|
||||||
|
assert.NotEqual(t, password, hashedPassword)
|
||||||
|
|
||||||
|
// Act - проверяем правильный пароль
|
||||||
|
isValid := auth.CheckPassword(password, hashedPassword)
|
||||||
|
|
||||||
|
// Assert
|
||||||
|
assert.True(t, isValid)
|
||||||
|
|
||||||
|
// Act - проверяем неправильный пароль
|
||||||
|
isValid = auth.CheckPassword("wrongPassword", hashedPassword)
|
||||||
|
|
||||||
|
// Assert
|
||||||
|
assert.False(t, isValid)
|
||||||
|
}
|
||||||
|
|
||||||
|
// TestPasswordHashing_EmptyPassword тестирует обработку пустого пароля
|
||||||
|
func TestPasswordHashing_EmptyPassword(t *testing.T) {
|
||||||
|
// Arrange
|
||||||
|
password := ""
|
||||||
|
|
||||||
|
// Act
|
||||||
|
hashedPassword, err := auth.HashPassword(password)
|
||||||
|
|
||||||
|
// Assert
|
||||||
|
assert.NoError(t, err)
|
||||||
|
assert.NotEmpty(t, hashedPassword)
|
||||||
|
|
||||||
|
// Проверяем, что пустой пароль работает
|
||||||
|
isValid := auth.CheckPassword(password, hashedPassword)
|
||||||
|
assert.True(t, isValid)
|
||||||
|
}
|
||||||
|
|
||||||
|
// BenchmarkPasswordHashing тестирует производительность хеширования
|
||||||
|
func BenchmarkPasswordHashing(b *testing.B) {
|
||||||
|
password := "benchmarkPassword123"
|
||||||
|
|
||||||
|
b.ResetTimer()
|
||||||
|
for i := 0; i < b.N; i++ {
|
||||||
|
_, err := auth.HashPassword(password)
|
||||||
|
if err != nil {
|
||||||
|
b.Fatal(err)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// BenchmarkJWTGeneration тестирует производительность генерации JWT
|
||||||
|
func BenchmarkJWTGeneration(b *testing.B) {
|
||||||
|
secret := "benchmark_secret_key"
|
||||||
|
ttl := 24 * time.Hour
|
||||||
|
jwtService := auth.NewJWTService(secret, ttl)
|
||||||
|
userID := uuid.New()
|
||||||
|
orgID := uuid.New()
|
||||||
|
email := "benchmark@example.com"
|
||||||
|
role := "admin"
|
||||||
|
|
||||||
|
b.ResetTimer()
|
||||||
|
for i := 0; i < b.N; i++ {
|
||||||
|
_, err := jwtService.GenerateToken(userID, orgID, email, role)
|
||||||
|
if err != nil {
|
||||||
|
b.Fatal(err)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
373
core-service/examples/repository_test.go
Normal file
373
core-service/examples/repository_test.go
Normal file
@@ -0,0 +1,373 @@
|
|||||||
|
package examples
|
||||||
|
|
||||||
|
import (
|
||||||
|
"context"
|
||||||
|
"database/sql"
|
||||||
|
"testing"
|
||||||
|
"time"
|
||||||
|
|
||||||
|
"github.com/DATA-DOG/go-sqlmock"
|
||||||
|
"github.com/google/uuid"
|
||||||
|
"github.com/stretchr/testify/assert"
|
||||||
|
"github.com/stretchr/testify/require"
|
||||||
|
|
||||||
|
"erp-mvp/core-service/internal/models"
|
||||||
|
"erp-mvp/core-service/internal/repository"
|
||||||
|
)
|
||||||
|
|
||||||
|
// TestOrganizationRepository_Create тестирует создание организации
|
||||||
|
func TestOrganizationRepository_Create(t *testing.T) {
|
||||||
|
// Arrange
|
||||||
|
db, mock, err := sqlmock.New()
|
||||||
|
require.NoError(t, err)
|
||||||
|
defer db.Close()
|
||||||
|
|
||||||
|
repo := repository.NewOrganizationRepository(db)
|
||||||
|
|
||||||
|
orgID := uuid.New()
|
||||||
|
org := &models.Organization{
|
||||||
|
ID: orgID,
|
||||||
|
Name: "Test Organization",
|
||||||
|
Type: "workshop",
|
||||||
|
Settings: models.JSON{
|
||||||
|
"created_at": 1234567890,
|
||||||
|
},
|
||||||
|
CreatedAt: time.Now(),
|
||||||
|
}
|
||||||
|
|
||||||
|
// Ожидаем SQL запрос
|
||||||
|
mock.ExpectExec("INSERT INTO organizations").
|
||||||
|
WithArgs(orgID, org.Name, org.Type, sqlmock.AnyArg(), sqlmock.AnyArg()).
|
||||||
|
WillReturnResult(sqlmock.NewResult(1, 1))
|
||||||
|
|
||||||
|
// Act
|
||||||
|
err = repo.Create(context.Background(), org)
|
||||||
|
|
||||||
|
// Assert
|
||||||
|
assert.NoError(t, err)
|
||||||
|
assert.NoError(t, mock.ExpectationsWereMet())
|
||||||
|
}
|
||||||
|
|
||||||
|
// TestOrganizationRepository_GetByID тестирует получение организации по ID
|
||||||
|
func TestOrganizationRepository_GetByID(t *testing.T) {
|
||||||
|
// Arrange
|
||||||
|
db, mock, err := sqlmock.New()
|
||||||
|
require.NoError(t, err)
|
||||||
|
defer db.Close()
|
||||||
|
|
||||||
|
repo := repository.NewOrganizationRepository(db)
|
||||||
|
|
||||||
|
orgID := uuid.New()
|
||||||
|
expectedOrg := &models.Organization{
|
||||||
|
ID: orgID,
|
||||||
|
Name: "Test Organization",
|
||||||
|
Type: "workshop",
|
||||||
|
Settings: models.JSON{
|
||||||
|
"created_at": 1234567890,
|
||||||
|
},
|
||||||
|
}
|
||||||
|
|
||||||
|
// Ожидаем SQL запрос
|
||||||
|
rows := sqlmock.NewRows([]string{"id", "name", "type", "settings", "created_at"}).
|
||||||
|
AddRow(orgID, expectedOrg.Name, expectedOrg.Type, `{"created_at":1234567890}`, time.Now())
|
||||||
|
|
||||||
|
mock.ExpectQuery("SELECT (.+) FROM organizations").
|
||||||
|
WithArgs(orgID).
|
||||||
|
WillReturnRows(rows)
|
||||||
|
|
||||||
|
// Act
|
||||||
|
org, err := repo.GetByID(context.Background(), orgID)
|
||||||
|
|
||||||
|
// Assert
|
||||||
|
assert.NoError(t, err)
|
||||||
|
assert.NotNil(t, org)
|
||||||
|
assert.Equal(t, expectedOrg.ID, org.ID)
|
||||||
|
assert.Equal(t, expectedOrg.Name, org.Name)
|
||||||
|
assert.Equal(t, expectedOrg.Type, org.Type)
|
||||||
|
assert.NoError(t, mock.ExpectationsWereMet())
|
||||||
|
}
|
||||||
|
|
||||||
|
// TestOrganizationRepository_GetByID_NotFound тестирует случай, когда организация не найдена
|
||||||
|
func TestOrganizationRepository_GetByID_NotFound(t *testing.T) {
|
||||||
|
// Arrange
|
||||||
|
db, mock, err := sqlmock.New()
|
||||||
|
require.NoError(t, err)
|
||||||
|
defer db.Close()
|
||||||
|
|
||||||
|
repo := repository.NewOrganizationRepository(db)
|
||||||
|
orgID := uuid.New()
|
||||||
|
|
||||||
|
// Ожидаем SQL запрос с пустым результатом
|
||||||
|
mock.ExpectQuery("SELECT (.+) FROM organizations").
|
||||||
|
WithArgs(orgID).
|
||||||
|
WillReturnError(sql.ErrNoRows)
|
||||||
|
|
||||||
|
// Act
|
||||||
|
org, err := repo.GetByID(context.Background(), orgID)
|
||||||
|
|
||||||
|
// Assert
|
||||||
|
assert.Error(t, err)
|
||||||
|
assert.Nil(t, org)
|
||||||
|
assert.Equal(t, "organization not found", err.Error())
|
||||||
|
assert.NoError(t, mock.ExpectationsWereMet())
|
||||||
|
}
|
||||||
|
|
||||||
|
// TestUserRepository_Create тестирует создание пользователя
|
||||||
|
func TestUserRepository_Create(t *testing.T) {
|
||||||
|
// Arrange
|
||||||
|
db, mock, err := sqlmock.New()
|
||||||
|
require.NoError(t, err)
|
||||||
|
defer db.Close()
|
||||||
|
|
||||||
|
repo := repository.NewUserRepository(db)
|
||||||
|
|
||||||
|
userID := uuid.New()
|
||||||
|
orgID := uuid.New()
|
||||||
|
user := &models.User{
|
||||||
|
ID: userID,
|
||||||
|
OrganizationID: orgID,
|
||||||
|
Email: "test@example.com",
|
||||||
|
PasswordHash: "hashed_password",
|
||||||
|
Role: "admin",
|
||||||
|
CreatedAt: time.Now(),
|
||||||
|
}
|
||||||
|
password := "hashed_password"
|
||||||
|
|
||||||
|
// Ожидаем SQL запрос
|
||||||
|
mock.ExpectExec("INSERT INTO users").
|
||||||
|
WithArgs(userID, orgID, user.Email, password, user.Role, sqlmock.AnyArg()).
|
||||||
|
WillReturnResult(sqlmock.NewResult(1, 1))
|
||||||
|
|
||||||
|
// Act
|
||||||
|
err = repo.Create(context.Background(), user, password)
|
||||||
|
|
||||||
|
// Assert
|
||||||
|
assert.NoError(t, err)
|
||||||
|
assert.NoError(t, mock.ExpectationsWereMet())
|
||||||
|
}
|
||||||
|
|
||||||
|
// TestUserRepository_GetByEmail тестирует получение пользователя по email
|
||||||
|
func TestUserRepository_GetByEmail(t *testing.T) {
|
||||||
|
// Arrange
|
||||||
|
db, mock, err := sqlmock.New()
|
||||||
|
require.NoError(t, err)
|
||||||
|
defer db.Close()
|
||||||
|
|
||||||
|
repo := repository.NewUserRepository(db)
|
||||||
|
|
||||||
|
userID := uuid.New()
|
||||||
|
orgID := uuid.New()
|
||||||
|
email := "test@example.com"
|
||||||
|
expectedUser := &models.User{
|
||||||
|
ID: userID,
|
||||||
|
OrganizationID: orgID,
|
||||||
|
Email: email,
|
||||||
|
PasswordHash: "hashed_password",
|
||||||
|
Role: "admin",
|
||||||
|
}
|
||||||
|
|
||||||
|
// Ожидаем SQL запрос
|
||||||
|
rows := sqlmock.NewRows([]string{"id", "organization_id", "email", "password_hash", "role", "created_at"}).
|
||||||
|
AddRow(userID, orgID, email, "hashed_password", "admin", time.Now())
|
||||||
|
|
||||||
|
mock.ExpectQuery("SELECT (.+) FROM users").
|
||||||
|
WithArgs(email).
|
||||||
|
WillReturnRows(rows)
|
||||||
|
|
||||||
|
// Act
|
||||||
|
user, err := repo.GetByEmail(context.Background(), email)
|
||||||
|
|
||||||
|
// Assert
|
||||||
|
assert.NoError(t, err)
|
||||||
|
assert.NotNil(t, user)
|
||||||
|
assert.Equal(t, expectedUser.ID, user.ID)
|
||||||
|
assert.Equal(t, expectedUser.Email, user.Email)
|
||||||
|
assert.Equal(t, expectedUser.Role, user.Role)
|
||||||
|
assert.NoError(t, mock.ExpectationsWereMet())
|
||||||
|
}
|
||||||
|
|
||||||
|
// TestLocationRepository_Create тестирует создание места хранения
|
||||||
|
func TestLocationRepository_Create(t *testing.T) {
|
||||||
|
// Arrange
|
||||||
|
db, mock, err := sqlmock.New()
|
||||||
|
require.NoError(t, err)
|
||||||
|
defer db.Close()
|
||||||
|
|
||||||
|
repo := repository.NewLocationRepository(db)
|
||||||
|
|
||||||
|
locationID := uuid.New()
|
||||||
|
orgID := uuid.New()
|
||||||
|
location := &models.StorageLocation{
|
||||||
|
ID: locationID,
|
||||||
|
OrganizationID: orgID,
|
||||||
|
Name: "Test Location",
|
||||||
|
Address: "Test Address",
|
||||||
|
Type: "warehouse",
|
||||||
|
Coordinates: models.JSON{
|
||||||
|
"lat": 55.7558,
|
||||||
|
"lng": 37.6176,
|
||||||
|
},
|
||||||
|
CreatedAt: time.Now(),
|
||||||
|
}
|
||||||
|
|
||||||
|
// Ожидаем SQL запрос
|
||||||
|
mock.ExpectExec("INSERT INTO storage_locations").
|
||||||
|
WithArgs(locationID, orgID, sqlmock.AnyArg(), location.Name, location.Address, location.Type, sqlmock.AnyArg(), sqlmock.AnyArg()).
|
||||||
|
WillReturnResult(sqlmock.NewResult(1, 1))
|
||||||
|
|
||||||
|
// Act
|
||||||
|
err = repo.Create(context.Background(), location)
|
||||||
|
|
||||||
|
// Assert
|
||||||
|
assert.NoError(t, err)
|
||||||
|
assert.NoError(t, mock.ExpectationsWereMet())
|
||||||
|
}
|
||||||
|
|
||||||
|
// TestLocationRepository_GetByID тестирует получение места хранения по ID
|
||||||
|
func TestLocationRepository_GetByID(t *testing.T) {
|
||||||
|
// Arrange
|
||||||
|
db, mock, err := sqlmock.New()
|
||||||
|
require.NoError(t, err)
|
||||||
|
defer db.Close()
|
||||||
|
|
||||||
|
repo := repository.NewLocationRepository(db)
|
||||||
|
|
||||||
|
locationID := uuid.New()
|
||||||
|
orgID := uuid.New()
|
||||||
|
expectedLocation := &models.StorageLocation{
|
||||||
|
ID: locationID,
|
||||||
|
OrganizationID: orgID,
|
||||||
|
Name: "Test Location",
|
||||||
|
Address: "Test Address",
|
||||||
|
Type: "warehouse",
|
||||||
|
Coordinates: models.JSON{
|
||||||
|
"lat": 55.7558,
|
||||||
|
"lng": 37.6176,
|
||||||
|
},
|
||||||
|
}
|
||||||
|
|
||||||
|
// Ожидаем SQL запрос
|
||||||
|
rows := sqlmock.NewRows([]string{"id", "organization_id", "parent_id", "name", "address", "type", "coordinates", "created_at"}).
|
||||||
|
AddRow(locationID, orgID, nil, expectedLocation.Name, expectedLocation.Address, expectedLocation.Type, `{"lat":55.7558,"lng":37.6176}`, time.Now())
|
||||||
|
|
||||||
|
mock.ExpectQuery("SELECT (.+) FROM storage_locations").
|
||||||
|
WithArgs(locationID, orgID).
|
||||||
|
WillReturnRows(rows)
|
||||||
|
|
||||||
|
// Act
|
||||||
|
location, err := repo.GetByID(context.Background(), locationID, orgID)
|
||||||
|
|
||||||
|
// Assert
|
||||||
|
assert.NoError(t, err)
|
||||||
|
assert.NotNil(t, location)
|
||||||
|
assert.Equal(t, expectedLocation.ID, location.ID)
|
||||||
|
assert.Equal(t, expectedLocation.Name, location.Name)
|
||||||
|
assert.Equal(t, expectedLocation.Type, location.Type)
|
||||||
|
assert.NoError(t, mock.ExpectationsWereMet())
|
||||||
|
}
|
||||||
|
|
||||||
|
// TestItemRepository_Create тестирует создание товара
|
||||||
|
func TestItemRepository_Create(t *testing.T) {
|
||||||
|
// Arrange
|
||||||
|
db, mock, err := sqlmock.New()
|
||||||
|
require.NoError(t, err)
|
||||||
|
defer db.Close()
|
||||||
|
|
||||||
|
repo := repository.NewItemRepository(db)
|
||||||
|
|
||||||
|
itemID := uuid.New()
|
||||||
|
orgID := uuid.New()
|
||||||
|
item := &models.Item{
|
||||||
|
ID: itemID,
|
||||||
|
OrganizationID: orgID,
|
||||||
|
Name: "Test Item",
|
||||||
|
Description: "Test Description",
|
||||||
|
Category: "Test Category",
|
||||||
|
CreatedAt: time.Now(),
|
||||||
|
}
|
||||||
|
|
||||||
|
// Ожидаем SQL запрос
|
||||||
|
mock.ExpectExec("INSERT INTO items").
|
||||||
|
WithArgs(itemID, orgID, item.Name, item.Description, item.Category, sqlmock.AnyArg()).
|
||||||
|
WillReturnResult(sqlmock.NewResult(1, 1))
|
||||||
|
|
||||||
|
// Act
|
||||||
|
err = repo.Create(context.Background(), item)
|
||||||
|
|
||||||
|
// Assert
|
||||||
|
assert.NoError(t, err)
|
||||||
|
assert.NoError(t, mock.ExpectationsWereMet())
|
||||||
|
}
|
||||||
|
|
||||||
|
// TestOperationsRepository_PlaceItem тестирует размещение товара
|
||||||
|
func TestOperationsRepository_PlaceItem(t *testing.T) {
|
||||||
|
// Arrange
|
||||||
|
db, mock, err := sqlmock.New()
|
||||||
|
require.NoError(t, err)
|
||||||
|
defer db.Close()
|
||||||
|
|
||||||
|
repo := repository.NewOperationsRepository(db)
|
||||||
|
|
||||||
|
placementID := uuid.New()
|
||||||
|
orgID := uuid.New()
|
||||||
|
itemID := uuid.New()
|
||||||
|
locationID := uuid.New()
|
||||||
|
placement := &models.ItemPlacement{
|
||||||
|
ID: placementID,
|
||||||
|
OrganizationID: orgID,
|
||||||
|
ItemID: itemID,
|
||||||
|
LocationID: locationID,
|
||||||
|
Quantity: 5,
|
||||||
|
CreatedAt: time.Now(),
|
||||||
|
}
|
||||||
|
|
||||||
|
// Ожидаем SQL запрос
|
||||||
|
mock.ExpectExec("INSERT INTO item_placements").
|
||||||
|
WithArgs(placementID, orgID, itemID, locationID, placement.Quantity, sqlmock.AnyArg()).
|
||||||
|
WillReturnResult(sqlmock.NewResult(1, 1))
|
||||||
|
|
||||||
|
// Act
|
||||||
|
err = repo.PlaceItem(context.Background(), placement)
|
||||||
|
|
||||||
|
// Assert
|
||||||
|
assert.NoError(t, err)
|
||||||
|
assert.NoError(t, mock.ExpectationsWereMet())
|
||||||
|
}
|
||||||
|
|
||||||
|
// TestOperationsRepository_Search тестирует поиск товаров с местами размещения
|
||||||
|
func TestOperationsRepository_Search(t *testing.T) {
|
||||||
|
// Arrange
|
||||||
|
db, mock, err := sqlmock.New()
|
||||||
|
require.NoError(t, err)
|
||||||
|
defer db.Close()
|
||||||
|
|
||||||
|
repo := repository.NewOperationsRepository(db)
|
||||||
|
|
||||||
|
orgID := uuid.New()
|
||||||
|
itemID := uuid.New()
|
||||||
|
locationID := uuid.New()
|
||||||
|
|
||||||
|
// Ожидаем SQL запрос с JOIN
|
||||||
|
rows := sqlmock.NewRows([]string{
|
||||||
|
"i.id", "i.organization_id", "i.name", "i.description", "i.category", "i.created_at",
|
||||||
|
"sl.id", "sl.organization_id", "sl.parent_id", "sl.name", "sl.address", "sl.type", "sl.coordinates", "sl.created_at",
|
||||||
|
"ip.quantity",
|
||||||
|
}).AddRow(
|
||||||
|
itemID, orgID, "Test Item", "Test Description", "Test Category", time.Now(),
|
||||||
|
locationID, orgID, nil, "Test Location", "Test Address", "warehouse", `{"lat":55.7558,"lng":37.6176}`, time.Now(),
|
||||||
|
5,
|
||||||
|
)
|
||||||
|
|
||||||
|
mock.ExpectQuery("SELECT (.+) FROM items i").
|
||||||
|
WithArgs(orgID, "%test%").
|
||||||
|
WillReturnRows(rows)
|
||||||
|
|
||||||
|
// Act
|
||||||
|
results, err := repo.Search(context.Background(), orgID, "test", "", "")
|
||||||
|
|
||||||
|
// Assert
|
||||||
|
assert.NoError(t, err)
|
||||||
|
assert.NotNil(t, results)
|
||||||
|
assert.Len(t, results, 1)
|
||||||
|
assert.NoError(t, mock.ExpectationsWereMet())
|
||||||
|
}
|
||||||
@@ -3,45 +3,44 @@ module erp-mvp/core-service
|
|||||||
go 1.21
|
go 1.21
|
||||||
|
|
||||||
require (
|
require (
|
||||||
github.com/gin-gonic/gin v1.9.1
|
github.com/DATA-DOG/go-sqlmock v1.5.2
|
||||||
github.com/golang-jwt/jwt/v5 v5.0.0
|
github.com/gin-gonic/gin v1.10.1
|
||||||
github.com/lib/pq v1.10.9
|
github.com/go-playground/validator/v10 v10.20.0
|
||||||
github.com/go-playground/validator/v10 v10.15.5
|
github.com/golang-jwt/jwt/v5 v5.3.0
|
||||||
google.golang.org/grpc v1.58.0
|
|
||||||
github.com/swaggo/gin-swagger v1.6.0
|
|
||||||
github.com/redis/go-redis/v9 v9.2.1
|
|
||||||
github.com/google/uuid v1.3.1
|
github.com/google/uuid v1.3.1
|
||||||
github.com/joho/godotenv v1.4.0
|
github.com/joho/godotenv v1.4.0
|
||||||
|
github.com/lib/pq v1.10.9
|
||||||
github.com/sirupsen/logrus v1.9.3
|
github.com/sirupsen/logrus v1.9.3
|
||||||
github.com/prometheus/client_golang v1.17.0
|
github.com/stretchr/testify v1.11.1
|
||||||
|
golang.org/x/crypto v0.23.0
|
||||||
)
|
)
|
||||||
|
|
||||||
require (
|
require (
|
||||||
github.com/bytedance/sonic v1.9.1 // indirect
|
github.com/bytedance/sonic v1.11.6 // indirect
|
||||||
github.com/cespare/xxhash/v2 v2.2.0 // indirect
|
github.com/bytedance/sonic/loader v0.1.1 // indirect
|
||||||
github.com/chenzhuoyu/base64x v0.0.0-20221115062448-fe3a3abad311 // indirect
|
github.com/cloudwego/base64x v0.1.4 // indirect
|
||||||
github.com/dgryski/go-rendezvous v0.0.0-20200823014737-9f7001d12a5f // indirect
|
github.com/cloudwego/iasm v0.2.0 // indirect
|
||||||
github.com/gabriel-vasile/mimetype v1.4.2 // indirect
|
github.com/davecgh/go-spew v1.1.1 // indirect
|
||||||
|
github.com/gabriel-vasile/mimetype v1.4.3 // indirect
|
||||||
github.com/gin-contrib/sse v0.1.0 // indirect
|
github.com/gin-contrib/sse v0.1.0 // indirect
|
||||||
github.com/go-playground/locales v0.14.1 // indirect
|
github.com/go-playground/locales v0.14.1 // indirect
|
||||||
github.com/go-playground/universal-translator v0.18.1 // indirect
|
github.com/go-playground/universal-translator v0.18.1 // indirect
|
||||||
github.com/goccy/go-json v0.10.2 // indirect
|
github.com/goccy/go-json v0.10.2 // indirect
|
||||||
github.com/golang/protobuf v1.5.3 // indirect
|
|
||||||
github.com/json-iterator/go v1.1.12 // indirect
|
github.com/json-iterator/go v1.1.12 // indirect
|
||||||
github.com/klauspost/cpuid/v2 v2.2.4 // indirect
|
github.com/klauspost/cpuid/v2 v2.2.7 // indirect
|
||||||
github.com/leodido/go-urn v1.2.4 // indirect
|
github.com/leodido/go-urn v1.4.0 // indirect
|
||||||
github.com/mattn/go-isatty v0.0.19 // indirect
|
github.com/mattn/go-isatty v0.0.20 // indirect
|
||||||
github.com/modern-go/concurrent v0.0.0-20180306012644-bacd9c7ef1dd // indirect
|
github.com/modern-go/concurrent v0.0.0-20180306012644-bacd9c7ef1dd // indirect
|
||||||
github.com/modern-go/reflect2 v1.0.2 // indirect
|
github.com/modern-go/reflect2 v1.0.2 // indirect
|
||||||
github.com/pelletier/go-toml/v2 v2.0.8 // indirect
|
github.com/pelletier/go-toml/v2 v2.2.2 // indirect
|
||||||
|
github.com/pmezard/go-difflib v1.0.0 // indirect
|
||||||
|
github.com/stretchr/objx v0.5.2 // indirect
|
||||||
github.com/twitchyliquid64/golang-asm v0.15.1 // indirect
|
github.com/twitchyliquid64/golang-asm v0.15.1 // indirect
|
||||||
github.com/ugorji/go/codec v1.2.11 // indirect
|
github.com/ugorji/go/codec v1.2.12 // indirect
|
||||||
golang.org/x/arch v0.3.0 // indirect
|
golang.org/x/arch v0.8.0 // indirect
|
||||||
golang.org/x/crypto v0.12.0 // indirect
|
golang.org/x/net v0.25.0 // indirect
|
||||||
golang.org/x/net v0.14.0 // indirect
|
golang.org/x/sys v0.20.0 // indirect
|
||||||
golang.org/x/sys v0.11.0 // indirect
|
golang.org/x/text v0.15.0 // indirect
|
||||||
golang.org/x/text v0.12.0 // indirect
|
google.golang.org/protobuf v1.34.1 // indirect
|
||||||
google.golang.org/genproto/googleapis/rpc v0.0.0-20230711160842-782d3b101e98 // indirect
|
|
||||||
google.golang.org/protobuf v1.31.0 // indirect
|
|
||||||
gopkg.in/yaml.v3 v3.0.1 // indirect
|
gopkg.in/yaml.v3 v3.0.1 // indirect
|
||||||
)
|
)
|
||||||
|
|||||||
105
core-service/go.sum
Normal file
105
core-service/go.sum
Normal file
@@ -0,0 +1,105 @@
|
|||||||
|
github.com/DATA-DOG/go-sqlmock v1.5.2 h1:OcvFkGmslmlZibjAjaHm3L//6LiuBgolP7OputlJIzU=
|
||||||
|
github.com/DATA-DOG/go-sqlmock v1.5.2/go.mod h1:88MAG/4G7SMwSE3CeA0ZKzrT5CiOU3OJ+JlNzwDqpNU=
|
||||||
|
github.com/bytedance/sonic v1.11.6 h1:oUp34TzMlL+OY1OUWxHqsdkgC/Zfc85zGqw9siXjrc0=
|
||||||
|
github.com/bytedance/sonic v1.11.6/go.mod h1:LysEHSvpvDySVdC2f87zGWf6CIKJcAvqab1ZaiQtds4=
|
||||||
|
github.com/bytedance/sonic/loader v0.1.1 h1:c+e5Pt1k/cy5wMveRDyk2X4B9hF4g7an8N3zCYjJFNM=
|
||||||
|
github.com/bytedance/sonic/loader v0.1.1/go.mod h1:ncP89zfokxS5LZrJxl5z0UJcsk4M4yY2JpfqGeCtNLU=
|
||||||
|
github.com/cloudwego/base64x v0.1.4 h1:jwCgWpFanWmN8xoIUHa2rtzmkd5J2plF/dnLS6Xd/0Y=
|
||||||
|
github.com/cloudwego/base64x v0.1.4/go.mod h1:0zlkT4Wn5C6NdauXdJRhSKRlJvmclQ1hhJgA0rcu/8w=
|
||||||
|
github.com/cloudwego/iasm v0.2.0 h1:1KNIy1I1H9hNNFEEH3DVnI4UujN+1zjpuk6gwHLTssg=
|
||||||
|
github.com/cloudwego/iasm v0.2.0/go.mod h1:8rXZaNYT2n95jn+zTI1sDr+IgcD2GVs0nlbbQPiEFhY=
|
||||||
|
github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
|
||||||
|
github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c=
|
||||||
|
github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
|
||||||
|
github.com/gabriel-vasile/mimetype v1.4.3 h1:in2uUcidCuFcDKtdcBxlR0rJ1+fsokWf+uqxgUFjbI0=
|
||||||
|
github.com/gabriel-vasile/mimetype v1.4.3/go.mod h1:d8uq/6HKRL6CGdk+aubisF/M5GcPfT7nKyLpA0lbSSk=
|
||||||
|
github.com/gin-contrib/sse v0.1.0 h1:Y/yl/+YNO8GZSjAhjMsSuLt29uWRFHdHYUb5lYOV9qE=
|
||||||
|
github.com/gin-contrib/sse v0.1.0/go.mod h1:RHrZQHXnP2xjPF+u1gW/2HnVO7nvIa9PG3Gm+fLHvGI=
|
||||||
|
github.com/gin-gonic/gin v1.10.1 h1:T0ujvqyCSqRopADpgPgiTT63DUQVSfojyME59Ei63pQ=
|
||||||
|
github.com/gin-gonic/gin v1.10.1/go.mod h1:4PMNQiOhvDRa013RKVbsiNwoyezlm2rm0uX/T7kzp5Y=
|
||||||
|
github.com/go-playground/assert/v2 v2.2.0 h1:JvknZsQTYeFEAhQwI4qEt9cyV5ONwRHC+lYKSsYSR8s=
|
||||||
|
github.com/go-playground/assert/v2 v2.2.0/go.mod h1:VDjEfimB/XKnb+ZQfWdccd7VUvScMdVu0Titje2rxJ4=
|
||||||
|
github.com/go-playground/locales v0.14.1 h1:EWaQ/wswjilfKLTECiXz7Rh+3BjFhfDFKv/oXslEjJA=
|
||||||
|
github.com/go-playground/locales v0.14.1/go.mod h1:hxrqLVvrK65+Rwrd5Fc6F2O76J/NuW9t0sjnWqG1slY=
|
||||||
|
github.com/go-playground/universal-translator v0.18.1 h1:Bcnm0ZwsGyWbCzImXv+pAJnYK9S473LQFuzCbDbfSFY=
|
||||||
|
github.com/go-playground/universal-translator v0.18.1/go.mod h1:xekY+UJKNuX9WP91TpwSH2VMlDf28Uj24BCp08ZFTUY=
|
||||||
|
github.com/go-playground/validator/v10 v10.20.0 h1:K9ISHbSaI0lyB2eWMPJo+kOS/FBExVwjEviJTixqxL8=
|
||||||
|
github.com/go-playground/validator/v10 v10.20.0/go.mod h1:dbuPbCMFw/DrkbEynArYaCwl3amGuJotoKCe95atGMM=
|
||||||
|
github.com/goccy/go-json v0.10.2 h1:CrxCmQqYDkv1z7lO7Wbh2HN93uovUHgrECaO5ZrCXAU=
|
||||||
|
github.com/goccy/go-json v0.10.2/go.mod h1:6MelG93GURQebXPDq3khkgXZkazVtN9CRI+MGFi0w8I=
|
||||||
|
github.com/golang-jwt/jwt/v5 v5.3.0 h1:pv4AsKCKKZuqlgs5sUmn4x8UlGa0kEVt/puTpKx9vvo=
|
||||||
|
github.com/golang-jwt/jwt/v5 v5.3.0/go.mod h1:fxCRLWMO43lRc8nhHWY6LGqRcf+1gQWArsqaEUEa5bE=
|
||||||
|
github.com/google/go-cmp v0.5.5 h1:Khx7svrCpmxxtHBq5j2mp/xVjsi8hQMfNLvJFAlrGgU=
|
||||||
|
github.com/google/go-cmp v0.5.5/go.mod h1:v8dTdLbMG2kIc/vJvl+f65V22dbkXbowE6jgT/gNBxE=
|
||||||
|
github.com/google/gofuzz v1.0.0/go.mod h1:dBl0BpW6vV/+mYPU4Po3pmUjxk6FQPldtuIdl/M65Eg=
|
||||||
|
github.com/google/uuid v1.3.1 h1:KjJaJ9iWZ3jOFZIf1Lqf4laDRCasjl0BCmnEGxkdLb4=
|
||||||
|
github.com/google/uuid v1.3.1/go.mod h1:TIyPZe4MgqvfeYDBFedMoGGpEw/LqOeaOT+nhxU+yHo=
|
||||||
|
github.com/joho/godotenv v1.4.0 h1:3l4+N6zfMWnkbPEXKng2o2/MR5mSwTrBih4ZEkkz1lg=
|
||||||
|
github.com/joho/godotenv v1.4.0/go.mod h1:f4LDr5Voq0i2e/R5DDNOoa2zzDfwtkZa6DnEwAbqwq4=
|
||||||
|
github.com/json-iterator/go v1.1.12 h1:PV8peI4a0ysnczrg+LtxykD8LfKY9ML6u2jnxaEnrnM=
|
||||||
|
github.com/json-iterator/go v1.1.12/go.mod h1:e30LSqwooZae/UwlEbR2852Gd8hjQvJoHmT4TnhNGBo=
|
||||||
|
github.com/kisielk/sqlstruct v0.0.0-20201105191214-5f3e10d3ab46/go.mod h1:yyMNCyc/Ib3bDTKd379tNMpB/7/H5TjM2Y9QJ5THLbE=
|
||||||
|
github.com/klauspost/cpuid/v2 v2.0.9/go.mod h1:FInQzS24/EEf25PyTYn52gqo7WaD8xa0213Md/qVLRg=
|
||||||
|
github.com/klauspost/cpuid/v2 v2.2.7 h1:ZWSB3igEs+d0qvnxR/ZBzXVmxkgt8DdzP6m9pfuVLDM=
|
||||||
|
github.com/klauspost/cpuid/v2 v2.2.7/go.mod h1:Lcz8mBdAVJIBVzewtcLocK12l3Y+JytZYpaMropDUws=
|
||||||
|
github.com/knz/go-libedit v1.10.1/go.mod h1:MZTVkCWyz0oBc7JOWP3wNAzd002ZbM/5hgShxwh4x8M=
|
||||||
|
github.com/leodido/go-urn v1.4.0 h1:WT9HwE9SGECu3lg4d/dIA+jxlljEa1/ffXKmRjqdmIQ=
|
||||||
|
github.com/leodido/go-urn v1.4.0/go.mod h1:bvxc+MVxLKB4z00jd1z+Dvzr47oO32F/QSNjSBOlFxI=
|
||||||
|
github.com/lib/pq v1.10.9 h1:YXG7RB+JIjhP29X+OtkiDnYaXQwpS4JEWq7dtCCRUEw=
|
||||||
|
github.com/lib/pq v1.10.9/go.mod h1:AlVN5x4E4T544tWzH6hKfbfQvm3HdbOxrmggDNAPY9o=
|
||||||
|
github.com/mattn/go-isatty v0.0.20 h1:xfD0iDuEKnDkl03q4limB+vH+GxLEtL/jb4xVJSWWEY=
|
||||||
|
github.com/mattn/go-isatty v0.0.20/go.mod h1:W+V8PltTTMOvKvAeJH7IuucS94S2C6jfK/D7dTCTo3Y=
|
||||||
|
github.com/modern-go/concurrent v0.0.0-20180228061459-e0a39a4cb421/go.mod h1:6dJC0mAP4ikYIbvyc7fijjWJddQyLn8Ig3JB5CqoB9Q=
|
||||||
|
github.com/modern-go/concurrent v0.0.0-20180306012644-bacd9c7ef1dd h1:TRLaZ9cD/w8PVh93nsPXa1VrQ6jlwL5oN8l14QlcNfg=
|
||||||
|
github.com/modern-go/concurrent v0.0.0-20180306012644-bacd9c7ef1dd/go.mod h1:6dJC0mAP4ikYIbvyc7fijjWJddQyLn8Ig3JB5CqoB9Q=
|
||||||
|
github.com/modern-go/reflect2 v1.0.2 h1:xBagoLtFs94CBntxluKeaWgTMpvLxC4ur3nMaC9Gz0M=
|
||||||
|
github.com/modern-go/reflect2 v1.0.2/go.mod h1:yWuevngMOJpCy52FWWMvUC8ws7m/LJsjYzDa0/r8luk=
|
||||||
|
github.com/pelletier/go-toml/v2 v2.2.2 h1:aYUidT7k73Pcl9nb2gScu7NSrKCSHIDE89b3+6Wq+LM=
|
||||||
|
github.com/pelletier/go-toml/v2 v2.2.2/go.mod h1:1t835xjRzz80PqgE6HHgN2JOsmgYu/h4qDAS4n929Rs=
|
||||||
|
github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM=
|
||||||
|
github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4=
|
||||||
|
github.com/sirupsen/logrus v1.9.3 h1:dueUQJ1C2q9oE3F7wvmSGAaVtTmUizReu6fjN8uqzbQ=
|
||||||
|
github.com/sirupsen/logrus v1.9.3/go.mod h1:naHLuLoDiP4jHNo9R0sCBMtWGeIprob74mVsIT4qYEQ=
|
||||||
|
github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME=
|
||||||
|
github.com/stretchr/objx v0.4.0/go.mod h1:YvHI0jy2hoMjB+UWwv71VJQ9isScKT/TqJzVSSt89Yw=
|
||||||
|
github.com/stretchr/objx v0.5.0/go.mod h1:Yh+to48EsGEfYuaHDzXPcE3xhTkx73EhmCGUpEOglKo=
|
||||||
|
github.com/stretchr/objx v0.5.2 h1:xuMeJ0Sdp5ZMRXx/aWO6RZxdr3beISkG5/G/aIRr3pY=
|
||||||
|
github.com/stretchr/objx v0.5.2/go.mod h1:FRsXN1f5AsAjCGJKqEizvkpNtU+EGNCLh3NxZ/8L+MA=
|
||||||
|
github.com/stretchr/testify v1.3.0/go.mod h1:M5WIy9Dh21IEIfnGCwXGc5bZfKNJtfHm1UVUgZn+9EI=
|
||||||
|
github.com/stretchr/testify v1.7.0/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/h/Wwjteg=
|
||||||
|
github.com/stretchr/testify v1.7.1/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/h/Wwjteg=
|
||||||
|
github.com/stretchr/testify v1.8.0/go.mod h1:yNjHg4UonilssWZ8iaSj1OCr/vHnekPRkoO+kdMU+MU=
|
||||||
|
github.com/stretchr/testify v1.8.1/go.mod h1:w2LPCIKwWwSfY2zedu0+kehJoqGctiVI29o6fzry7u4=
|
||||||
|
github.com/stretchr/testify v1.8.4/go.mod h1:sz/lmYIOXD/1dqDmKjjqLyZ2RngseejIcXlSw2iwfAo=
|
||||||
|
github.com/stretchr/testify v1.9.0/go.mod h1:r2ic/lqez/lEtzL7wO/rwa5dbSLXVDPFyf8C91i36aY=
|
||||||
|
github.com/stretchr/testify v1.11.1 h1:7s2iGBzp5EwR7/aIZr8ao5+dra3wiQyKjjFuvgVKu7U=
|
||||||
|
github.com/stretchr/testify v1.11.1/go.mod h1:wZwfW3scLgRK+23gO65QZefKpKQRnfz6sD981Nm4B6U=
|
||||||
|
github.com/twitchyliquid64/golang-asm v0.15.1 h1:SU5vSMR7hnwNxj24w34ZyCi/FmDZTkS4MhqMhdFk5YI=
|
||||||
|
github.com/twitchyliquid64/golang-asm v0.15.1/go.mod h1:a1lVb/DtPvCB8fslRZhAngC2+aY1QWCk3Cedj/Gdt08=
|
||||||
|
github.com/ugorji/go/codec v1.2.12 h1:9LC83zGrHhuUA9l16C9AHXAqEV/2wBQ4nkvumAE65EE=
|
||||||
|
github.com/ugorji/go/codec v1.2.12/go.mod h1:UNopzCgEMSXjBc6AOMqYvWC1ktqTAfzJZUZgYf6w6lg=
|
||||||
|
golang.org/x/arch v0.0.0-20210923205945-b76863e36670/go.mod h1:5om86z9Hs0C8fWVUuoMHwpExlXzs5Tkyp9hOrfG7pp8=
|
||||||
|
golang.org/x/arch v0.8.0 h1:3wRIsP3pM4yUptoR96otTUOXI367OS0+c9eeRi9doIc=
|
||||||
|
golang.org/x/arch v0.8.0/go.mod h1:FEVrYAQjsQXMVJ1nsMoVVXPZg6p2JE2mx8psSWTDQys=
|
||||||
|
golang.org/x/crypto v0.23.0 h1:dIJU/v2J8Mdglj/8rJ6UUOM3Zc9zLZxVZwwxMooUSAI=
|
||||||
|
golang.org/x/crypto v0.23.0/go.mod h1:CKFgDieR+mRhux2Lsu27y0fO304Db0wZe70UKqHu0v8=
|
||||||
|
golang.org/x/net v0.25.0 h1:d/OCCoBEUq33pjydKrGQhw7IlUPI2Oylr+8qLx49kac=
|
||||||
|
golang.org/x/net v0.25.0/go.mod h1:JkAGAh7GEvH74S6FOH42FLoXpXbE/aqXSrIQjXgsiwM=
|
||||||
|
golang.org/x/sys v0.0.0-20220715151400-c0bba94af5f8/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
|
||||||
|
golang.org/x/sys v0.5.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
|
||||||
|
golang.org/x/sys v0.6.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
|
||||||
|
golang.org/x/sys v0.20.0 h1:Od9JTbYCk261bKm4M/mw7AklTlFYIa0bIp9BgSm1S8Y=
|
||||||
|
golang.org/x/sys v0.20.0/go.mod h1:/VUhepiaJMQUp4+oa/7Zr1D23ma6VTLIYjOOTFZPUcA=
|
||||||
|
golang.org/x/text v0.15.0 h1:h1V/4gjBv8v9cjcR6+AR5+/cIYK5N/WAgiv4xlsEtAk=
|
||||||
|
golang.org/x/text v0.15.0/go.mod h1:18ZOQIKpY8NJVqYksKHtTdi31H5itFRjB5/qKTNYzSU=
|
||||||
|
golang.org/x/xerrors v0.0.0-20191204190536-9bdfabe68543 h1:E7g+9GITq07hpfrRu66IVDexMakfv52eLZ2CXBWiKr4=
|
||||||
|
golang.org/x/xerrors v0.0.0-20191204190536-9bdfabe68543/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0=
|
||||||
|
google.golang.org/protobuf v1.34.1 h1:9ddQBjfCyZPOHPUiPxpYESBLc+T8P3E+Vo4IbKZgFWg=
|
||||||
|
google.golang.org/protobuf v1.34.1/go.mod h1:c6P6GXX6sHbq/GpV6MGZEdwhWPcYBgnhAHhKbcUYpos=
|
||||||
|
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405 h1:yhCVgyC4o1eVCa2tZl7eS0r+SDo693bJlVdllGtEeKM=
|
||||||
|
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
|
||||||
|
gopkg.in/yaml.v3 v3.0.0-20200313102051-9f266ea9e77c/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=
|
||||||
|
gopkg.in/yaml.v3 v3.0.1 h1:fxVm/GzAzEWqLHuvctI91KS9hhNmmWOoWu0XTYJS7CA=
|
||||||
|
gopkg.in/yaml.v3 v3.0.1/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=
|
||||||
|
nullprogram.com/x/optparse v1.0.0/go.mod h1:KdyPE+Igbe0jQUrVfMqDMeJQIJZEuyV7pjYmp6pbG50=
|
||||||
|
rsc.io/pdf v0.1.1/go.mod h1:n8OzWcQ6Sp37PL01nO98y4iUCRdTGarVfzxY20ICaU4=
|
||||||
78
core-service/internal/api/handlers/auth.go
Normal file
78
core-service/internal/api/handlers/auth.go
Normal file
@@ -0,0 +1,78 @@
|
|||||||
|
package handlers
|
||||||
|
|
||||||
|
import (
|
||||||
|
"net/http"
|
||||||
|
|
||||||
|
"erp-mvp/core-service/internal/models"
|
||||||
|
"erp-mvp/core-service/internal/service"
|
||||||
|
"github.com/gin-gonic/gin"
|
||||||
|
"github.com/go-playground/validator/v10"
|
||||||
|
)
|
||||||
|
|
||||||
|
type AuthHandler struct {
|
||||||
|
authService service.AuthService
|
||||||
|
validate *validator.Validate
|
||||||
|
}
|
||||||
|
|
||||||
|
func NewAuthHandler(authService service.AuthService) *AuthHandler {
|
||||||
|
return &AuthHandler{
|
||||||
|
authService: authService,
|
||||||
|
validate: validator.New(),
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// Register регистрация новой организации и пользователя
|
||||||
|
func (h *AuthHandler) Register(c *gin.Context) {
|
||||||
|
var req models.RegisterRequest
|
||||||
|
if err := c.ShouldBindJSON(&req); err != nil {
|
||||||
|
c.JSON(http.StatusBadRequest, gin.H{"error": "Invalid request body"})
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
// Валидируем запрос
|
||||||
|
if err := h.validate.Struct(req); err != nil {
|
||||||
|
c.JSON(http.StatusBadRequest, gin.H{"error": "Validation failed", "details": err.Error()})
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
// Выполняем регистрацию
|
||||||
|
response, err := h.authService.Register(c.Request.Context(), &req)
|
||||||
|
if err != nil {
|
||||||
|
if validationErr, ok := err.(*service.ValidationError); ok {
|
||||||
|
c.JSON(http.StatusBadRequest, gin.H{"error": validationErr.Message})
|
||||||
|
return
|
||||||
|
}
|
||||||
|
c.JSON(http.StatusInternalServerError, gin.H{"error": "Registration failed"})
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
c.JSON(http.StatusCreated, response)
|
||||||
|
}
|
||||||
|
|
||||||
|
// Login вход в систему
|
||||||
|
func (h *AuthHandler) Login(c *gin.Context) {
|
||||||
|
var req models.LoginRequest
|
||||||
|
if err := c.ShouldBindJSON(&req); err != nil {
|
||||||
|
c.JSON(http.StatusBadRequest, gin.H{"error": "Invalid request body"})
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
// Валидируем запрос
|
||||||
|
if err := h.validate.Struct(req); err != nil {
|
||||||
|
c.JSON(http.StatusBadRequest, gin.H{"error": "Validation failed", "details": err.Error()})
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
// Выполняем вход
|
||||||
|
response, err := h.authService.Login(c.Request.Context(), &req)
|
||||||
|
if err != nil {
|
||||||
|
if validationErr, ok := err.(*service.ValidationError); ok {
|
||||||
|
c.JSON(http.StatusUnauthorized, gin.H{"error": validationErr.Message})
|
||||||
|
return
|
||||||
|
}
|
||||||
|
c.JSON(http.StatusInternalServerError, gin.H{"error": "Login failed"})
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
c.JSON(http.StatusOK, response)
|
||||||
|
}
|
||||||
172
core-service/internal/api/handlers/items.go
Normal file
172
core-service/internal/api/handlers/items.go
Normal file
@@ -0,0 +1,172 @@
|
|||||||
|
package handlers
|
||||||
|
|
||||||
|
import (
|
||||||
|
"net/http"
|
||||||
|
|
||||||
|
"erp-mvp/core-service/internal/api/middleware"
|
||||||
|
"erp-mvp/core-service/internal/models"
|
||||||
|
"erp-mvp/core-service/internal/service"
|
||||||
|
|
||||||
|
"github.com/gin-gonic/gin"
|
||||||
|
"github.com/go-playground/validator/v10"
|
||||||
|
"github.com/google/uuid"
|
||||||
|
)
|
||||||
|
|
||||||
|
type ItemHandler struct {
|
||||||
|
itemService service.ItemService
|
||||||
|
validate *validator.Validate
|
||||||
|
}
|
||||||
|
|
||||||
|
func NewItemHandler(itemService service.ItemService) *ItemHandler {
|
||||||
|
return &ItemHandler{
|
||||||
|
itemService: itemService,
|
||||||
|
validate: validator.New(),
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// GetItems получает все товары организации
|
||||||
|
func (h *ItemHandler) GetItems(c *gin.Context) {
|
||||||
|
claims := middleware.GetClaims(c)
|
||||||
|
if claims == nil {
|
||||||
|
c.JSON(http.StatusUnauthorized, gin.H{"error": "Unauthorized"})
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
items, err := h.itemService.GetItems(c.Request.Context(), claims.OrganizationID)
|
||||||
|
if err != nil {
|
||||||
|
c.JSON(http.StatusInternalServerError, gin.H{"error": "Failed to get items"})
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
c.JSON(http.StatusOK, items)
|
||||||
|
}
|
||||||
|
|
||||||
|
// CreateItem создает новый товар
|
||||||
|
func (h *ItemHandler) CreateItem(c *gin.Context) {
|
||||||
|
claims := middleware.GetClaims(c)
|
||||||
|
if claims == nil {
|
||||||
|
c.JSON(http.StatusUnauthorized, gin.H{"error": "Unauthorized"})
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
var req models.CreateItemRequest
|
||||||
|
if err := c.ShouldBindJSON(&req); err != nil {
|
||||||
|
c.JSON(http.StatusBadRequest, gin.H{"error": "Invalid request body"})
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
if err := h.validate.Struct(req); err != nil {
|
||||||
|
c.JSON(http.StatusBadRequest, gin.H{"error": "Validation failed", "details": err.Error()})
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
item, err := h.itemService.CreateItem(c.Request.Context(), claims.OrganizationID, &req)
|
||||||
|
if err != nil {
|
||||||
|
c.JSON(http.StatusInternalServerError, gin.H{"error": "Failed to create item"})
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
c.JSON(http.StatusCreated, item)
|
||||||
|
}
|
||||||
|
|
||||||
|
// GetItem получает товар по ID
|
||||||
|
func (h *ItemHandler) GetItem(c *gin.Context) {
|
||||||
|
claims := middleware.GetClaims(c)
|
||||||
|
if claims == nil {
|
||||||
|
c.JSON(http.StatusUnauthorized, gin.H{"error": "Unauthorized"})
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
idStr := c.Param("id")
|
||||||
|
id, err := uuid.Parse(idStr)
|
||||||
|
if err != nil {
|
||||||
|
c.JSON(http.StatusBadRequest, gin.H{"error": "Invalid item ID"})
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
item, err := h.itemService.GetItem(c.Request.Context(), id, claims.OrganizationID)
|
||||||
|
if err != nil {
|
||||||
|
c.JSON(http.StatusNotFound, gin.H{"error": "Item not found"})
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
c.JSON(http.StatusOK, item)
|
||||||
|
}
|
||||||
|
|
||||||
|
// UpdateItem обновляет товар
|
||||||
|
func (h *ItemHandler) UpdateItem(c *gin.Context) {
|
||||||
|
claims := middleware.GetClaims(c)
|
||||||
|
if claims == nil {
|
||||||
|
c.JSON(http.StatusUnauthorized, gin.H{"error": "Unauthorized"})
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
idStr := c.Param("id")
|
||||||
|
id, err := uuid.Parse(idStr)
|
||||||
|
if err != nil {
|
||||||
|
c.JSON(http.StatusBadRequest, gin.H{"error": "Invalid item ID"})
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
var req models.CreateItemRequest
|
||||||
|
if err := c.ShouldBindJSON(&req); err != nil {
|
||||||
|
c.JSON(http.StatusBadRequest, gin.H{"error": "Invalid request body"})
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
if err := h.validate.Struct(req); err != nil {
|
||||||
|
c.JSON(http.StatusBadRequest, gin.H{"error": "Validation failed", "details": err.Error()})
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
item, err := h.itemService.UpdateItem(c.Request.Context(), id, claims.OrganizationID, &req)
|
||||||
|
if err != nil {
|
||||||
|
c.JSON(http.StatusNotFound, gin.H{"error": "Item not found"})
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
c.JSON(http.StatusOK, item)
|
||||||
|
}
|
||||||
|
|
||||||
|
// DeleteItem удаляет товар
|
||||||
|
func (h *ItemHandler) DeleteItem(c *gin.Context) {
|
||||||
|
claims := middleware.GetClaims(c)
|
||||||
|
if claims == nil {
|
||||||
|
c.JSON(http.StatusUnauthorized, gin.H{"error": "Unauthorized"})
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
idStr := c.Param("id")
|
||||||
|
id, err := uuid.Parse(idStr)
|
||||||
|
if err != nil {
|
||||||
|
c.JSON(http.StatusBadRequest, gin.H{"error": "Invalid item ID"})
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
if err := h.itemService.DeleteItem(c.Request.Context(), id, claims.OrganizationID); err != nil {
|
||||||
|
c.JSON(http.StatusNotFound, gin.H{"error": "Item not found"})
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
c.JSON(http.StatusNoContent, nil)
|
||||||
|
}
|
||||||
|
|
||||||
|
// SearchItems ищет товары
|
||||||
|
func (h *ItemHandler) SearchItems(c *gin.Context) {
|
||||||
|
claims := middleware.GetClaims(c)
|
||||||
|
if claims == nil {
|
||||||
|
c.JSON(http.StatusUnauthorized, gin.H{"error": "Unauthorized"})
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
query := c.Query("q")
|
||||||
|
category := c.Query("category")
|
||||||
|
|
||||||
|
items, err := h.itemService.SearchItems(c.Request.Context(), claims.OrganizationID, query, category)
|
||||||
|
if err != nil {
|
||||||
|
c.JSON(http.StatusInternalServerError, gin.H{"error": "Failed to search items"})
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
c.JSON(http.StatusOK, items)
|
||||||
|
}
|
||||||
176
core-service/internal/api/handlers/locations.go
Normal file
176
core-service/internal/api/handlers/locations.go
Normal file
@@ -0,0 +1,176 @@
|
|||||||
|
package handlers
|
||||||
|
|
||||||
|
import (
|
||||||
|
"net/http"
|
||||||
|
|
||||||
|
"erp-mvp/core-service/internal/api/middleware"
|
||||||
|
"erp-mvp/core-service/internal/models"
|
||||||
|
"erp-mvp/core-service/internal/service"
|
||||||
|
|
||||||
|
"github.com/gin-gonic/gin"
|
||||||
|
"github.com/go-playground/validator/v10"
|
||||||
|
"github.com/google/uuid"
|
||||||
|
)
|
||||||
|
|
||||||
|
type LocationHandler struct {
|
||||||
|
locationService service.LocationService
|
||||||
|
validate *validator.Validate
|
||||||
|
}
|
||||||
|
|
||||||
|
func NewLocationHandler(locationService service.LocationService) *LocationHandler {
|
||||||
|
return &LocationHandler{
|
||||||
|
locationService: locationService,
|
||||||
|
validate: validator.New(),
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// GetLocations получает все места хранения организации
|
||||||
|
func (h *LocationHandler) GetLocations(c *gin.Context) {
|
||||||
|
claims := middleware.GetClaims(c)
|
||||||
|
if claims == nil {
|
||||||
|
c.JSON(http.StatusUnauthorized, gin.H{"error": "Unauthorized"})
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
locations, err := h.locationService.GetLocations(c.Request.Context(), claims.OrganizationID)
|
||||||
|
if err != nil {
|
||||||
|
c.JSON(http.StatusInternalServerError, gin.H{"error": "Failed to get locations"})
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
c.JSON(http.StatusOK, locations)
|
||||||
|
}
|
||||||
|
|
||||||
|
// CreateLocation создает новое место хранения
|
||||||
|
func (h *LocationHandler) CreateLocation(c *gin.Context) {
|
||||||
|
claims := middleware.GetClaims(c)
|
||||||
|
if claims == nil {
|
||||||
|
c.JSON(http.StatusUnauthorized, gin.H{"error": "Unauthorized"})
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
var req models.CreateLocationRequest
|
||||||
|
if err := c.ShouldBindJSON(&req); err != nil {
|
||||||
|
c.JSON(http.StatusBadRequest, gin.H{"error": "Invalid request body"})
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
if err := h.validate.Struct(req); err != nil {
|
||||||
|
c.JSON(http.StatusBadRequest, gin.H{"error": "Validation failed", "details": err.Error()})
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
location, err := h.locationService.CreateLocation(c.Request.Context(), claims.OrganizationID, &req)
|
||||||
|
if err != nil {
|
||||||
|
c.JSON(http.StatusInternalServerError, gin.H{"error": "Failed to create location"})
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
c.JSON(http.StatusCreated, location)
|
||||||
|
}
|
||||||
|
|
||||||
|
// GetLocation получает место хранения по ID
|
||||||
|
func (h *LocationHandler) GetLocation(c *gin.Context) {
|
||||||
|
claims := middleware.GetClaims(c)
|
||||||
|
if claims == nil {
|
||||||
|
c.JSON(http.StatusUnauthorized, gin.H{"error": "Unauthorized"})
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
idStr := c.Param("id")
|
||||||
|
id, err := uuid.Parse(idStr)
|
||||||
|
if err != nil {
|
||||||
|
c.JSON(http.StatusBadRequest, gin.H{"error": "Invalid location ID"})
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
location, err := h.locationService.GetLocation(c.Request.Context(), id, claims.OrganizationID)
|
||||||
|
if err != nil {
|
||||||
|
c.JSON(http.StatusNotFound, gin.H{"error": "Location not found"})
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
c.JSON(http.StatusOK, location)
|
||||||
|
}
|
||||||
|
|
||||||
|
// UpdateLocation обновляет место хранения
|
||||||
|
func (h *LocationHandler) UpdateLocation(c *gin.Context) {
|
||||||
|
claims := middleware.GetClaims(c)
|
||||||
|
if claims == nil {
|
||||||
|
c.JSON(http.StatusUnauthorized, gin.H{"error": "Unauthorized"})
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
idStr := c.Param("id")
|
||||||
|
id, err := uuid.Parse(idStr)
|
||||||
|
if err != nil {
|
||||||
|
c.JSON(http.StatusBadRequest, gin.H{"error": "Invalid location ID"})
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
var req models.CreateLocationRequest
|
||||||
|
if err := c.ShouldBindJSON(&req); err != nil {
|
||||||
|
c.JSON(http.StatusBadRequest, gin.H{"error": "Invalid request body"})
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
if err := h.validate.Struct(req); err != nil {
|
||||||
|
c.JSON(http.StatusBadRequest, gin.H{"error": "Validation failed", "details": err.Error()})
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
location, err := h.locationService.UpdateLocation(c.Request.Context(), id, claims.OrganizationID, &req)
|
||||||
|
if err != nil {
|
||||||
|
c.JSON(http.StatusNotFound, gin.H{"error": "Location not found"})
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
c.JSON(http.StatusOK, location)
|
||||||
|
}
|
||||||
|
|
||||||
|
// DeleteLocation удаляет место хранения
|
||||||
|
func (h *LocationHandler) DeleteLocation(c *gin.Context) {
|
||||||
|
claims := middleware.GetClaims(c)
|
||||||
|
if claims == nil {
|
||||||
|
c.JSON(http.StatusUnauthorized, gin.H{"error": "Unauthorized"})
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
idStr := c.Param("id")
|
||||||
|
id, err := uuid.Parse(idStr)
|
||||||
|
if err != nil {
|
||||||
|
c.JSON(http.StatusBadRequest, gin.H{"error": "Invalid location ID"})
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
if err := h.locationService.DeleteLocation(c.Request.Context(), id, claims.OrganizationID); err != nil {
|
||||||
|
c.JSON(http.StatusNotFound, gin.H{"error": "Location not found"})
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
c.JSON(http.StatusNoContent, nil)
|
||||||
|
}
|
||||||
|
|
||||||
|
// GetChildren получает дочерние места хранения
|
||||||
|
func (h *LocationHandler) GetChildren(c *gin.Context) {
|
||||||
|
claims := middleware.GetClaims(c)
|
||||||
|
if claims == nil {
|
||||||
|
c.JSON(http.StatusUnauthorized, gin.H{"error": "Unauthorized"})
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
parentIDStr := c.Param("id")
|
||||||
|
parentID, err := uuid.Parse(parentIDStr)
|
||||||
|
if err != nil {
|
||||||
|
c.JSON(http.StatusBadRequest, gin.H{"error": "Invalid parent location ID"})
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
children, err := h.locationService.GetChildren(c.Request.Context(), parentID, claims.OrganizationID)
|
||||||
|
if err != nil {
|
||||||
|
c.JSON(http.StatusNotFound, gin.H{"error": "Parent location not found"})
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
c.JSON(http.StatusOK, children)
|
||||||
|
}
|
||||||
227
core-service/internal/api/handlers/operations.go
Normal file
227
core-service/internal/api/handlers/operations.go
Normal file
@@ -0,0 +1,227 @@
|
|||||||
|
package handlers
|
||||||
|
|
||||||
|
import (
|
||||||
|
"net/http"
|
||||||
|
|
||||||
|
"erp-mvp/core-service/internal/api/middleware"
|
||||||
|
"erp-mvp/core-service/internal/models"
|
||||||
|
"erp-mvp/core-service/internal/service"
|
||||||
|
|
||||||
|
"github.com/gin-gonic/gin"
|
||||||
|
"github.com/go-playground/validator/v10"
|
||||||
|
"github.com/google/uuid"
|
||||||
|
)
|
||||||
|
|
||||||
|
type OperationsHandler struct {
|
||||||
|
operationsService service.OperationsService
|
||||||
|
validate *validator.Validate
|
||||||
|
}
|
||||||
|
|
||||||
|
func NewOperationsHandler(operationsService service.OperationsService) *OperationsHandler {
|
||||||
|
return &OperationsHandler{
|
||||||
|
operationsService: operationsService,
|
||||||
|
validate: validator.New(),
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// PlaceItem размещает товар в месте хранения
|
||||||
|
func (h *OperationsHandler) PlaceItem(c *gin.Context) {
|
||||||
|
claims := middleware.GetClaims(c)
|
||||||
|
if claims == nil {
|
||||||
|
c.JSON(http.StatusUnauthorized, gin.H{"error": "Unauthorized"})
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
var req models.PlaceItemRequest
|
||||||
|
if err := c.ShouldBindJSON(&req); err != nil {
|
||||||
|
c.JSON(http.StatusBadRequest, gin.H{"error": "Invalid request body"})
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
if err := h.validate.Struct(req); err != nil {
|
||||||
|
c.JSON(http.StatusBadRequest, gin.H{"error": "Validation failed", "details": err.Error()})
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
placement, err := h.operationsService.PlaceItem(c.Request.Context(), claims.OrganizationID, &req)
|
||||||
|
if err != nil {
|
||||||
|
c.JSON(http.StatusInternalServerError, gin.H{"error": "Failed to place item"})
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
c.JSON(http.StatusCreated, placement)
|
||||||
|
}
|
||||||
|
|
||||||
|
// MoveItem перемещает товар в другое место хранения
|
||||||
|
func (h *OperationsHandler) MoveItem(c *gin.Context) {
|
||||||
|
claims := middleware.GetClaims(c)
|
||||||
|
if claims == nil {
|
||||||
|
c.JSON(http.StatusUnauthorized, gin.H{"error": "Unauthorized"})
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
placementIDStr := c.Param("id")
|
||||||
|
placementID, err := uuid.Parse(placementIDStr)
|
||||||
|
if err != nil {
|
||||||
|
c.JSON(http.StatusBadRequest, gin.H{"error": "Invalid placement ID"})
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
var req struct {
|
||||||
|
NewLocationID uuid.UUID `json:"new_location_id" validate:"required"`
|
||||||
|
}
|
||||||
|
if err := c.ShouldBindJSON(&req); err != nil {
|
||||||
|
c.JSON(http.StatusBadRequest, gin.H{"error": "Invalid request body"})
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
if err := h.validate.Struct(req); err != nil {
|
||||||
|
c.JSON(http.StatusBadRequest, gin.H{"error": "Validation failed", "details": err.Error()})
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
if err := h.operationsService.MoveItem(c.Request.Context(), placementID, req.NewLocationID, claims.OrganizationID); err != nil {
|
||||||
|
c.JSON(http.StatusNotFound, gin.H{"error": "Placement not found"})
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
c.JSON(http.StatusOK, gin.H{"message": "Item moved successfully"})
|
||||||
|
}
|
||||||
|
|
||||||
|
// GetItemPlacements получает все размещения товара
|
||||||
|
func (h *OperationsHandler) GetItemPlacements(c *gin.Context) {
|
||||||
|
claims := middleware.GetClaims(c)
|
||||||
|
if claims == nil {
|
||||||
|
c.JSON(http.StatusUnauthorized, gin.H{"error": "Unauthorized"})
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
itemIDStr := c.Param("item_id")
|
||||||
|
itemID, err := uuid.Parse(itemIDStr)
|
||||||
|
if err != nil {
|
||||||
|
c.JSON(http.StatusBadRequest, gin.H{"error": "Invalid item ID"})
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
placements, err := h.operationsService.GetItemPlacements(c.Request.Context(), itemID, claims.OrganizationID)
|
||||||
|
if err != nil {
|
||||||
|
c.JSON(http.StatusNotFound, gin.H{"error": "Item not found"})
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
c.JSON(http.StatusOK, placements)
|
||||||
|
}
|
||||||
|
|
||||||
|
// GetLocationPlacements получает все товары в месте хранения
|
||||||
|
func (h *OperationsHandler) GetLocationPlacements(c *gin.Context) {
|
||||||
|
claims := middleware.GetClaims(c)
|
||||||
|
if claims == nil {
|
||||||
|
c.JSON(http.StatusUnauthorized, gin.H{"error": "Unauthorized"})
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
locationIDStr := c.Param("location_id")
|
||||||
|
locationID, err := uuid.Parse(locationIDStr)
|
||||||
|
if err != nil {
|
||||||
|
c.JSON(http.StatusBadRequest, gin.H{"error": "Invalid location ID"})
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
placements, err := h.operationsService.GetLocationPlacements(c.Request.Context(), locationID, claims.OrganizationID)
|
||||||
|
if err != nil {
|
||||||
|
c.JSON(http.StatusNotFound, gin.H{"error": "Location not found"})
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
c.JSON(http.StatusOK, placements)
|
||||||
|
}
|
||||||
|
|
||||||
|
// UpdateQuantity обновляет количество товара в размещении
|
||||||
|
func (h *OperationsHandler) UpdateQuantity(c *gin.Context) {
|
||||||
|
claims := middleware.GetClaims(c)
|
||||||
|
if claims == nil {
|
||||||
|
c.JSON(http.StatusUnauthorized, gin.H{"error": "Unauthorized"})
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
placementIDStr := c.Param("id")
|
||||||
|
placementID, err := uuid.Parse(placementIDStr)
|
||||||
|
if err != nil {
|
||||||
|
c.JSON(http.StatusBadRequest, gin.H{"error": "Invalid placement ID"})
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
var req struct {
|
||||||
|
Quantity int `json:"quantity" validate:"required,min=1"`
|
||||||
|
}
|
||||||
|
if err := c.ShouldBindJSON(&req); err != nil {
|
||||||
|
c.JSON(http.StatusBadRequest, gin.H{"error": "Invalid request body"})
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
if err := h.validate.Struct(req); err != nil {
|
||||||
|
c.JSON(http.StatusBadRequest, gin.H{"error": "Validation failed", "details": err.Error()})
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
if err := h.operationsService.UpdateQuantity(c.Request.Context(), placementID, req.Quantity, claims.OrganizationID); err != nil {
|
||||||
|
c.JSON(http.StatusNotFound, gin.H{"error": "Placement not found"})
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
c.JSON(http.StatusOK, gin.H{"message": "Quantity updated successfully"})
|
||||||
|
}
|
||||||
|
|
||||||
|
// DeletePlacement удаляет размещение товара
|
||||||
|
func (h *OperationsHandler) DeletePlacement(c *gin.Context) {
|
||||||
|
claims := middleware.GetClaims(c)
|
||||||
|
if claims == nil {
|
||||||
|
c.JSON(http.StatusUnauthorized, gin.H{"error": "Unauthorized"})
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
placementIDStr := c.Param("id")
|
||||||
|
placementID, err := uuid.Parse(placementIDStr)
|
||||||
|
if err != nil {
|
||||||
|
c.JSON(http.StatusBadRequest, gin.H{"error": "Invalid placement ID"})
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
if err := h.operationsService.DeletePlacement(c.Request.Context(), placementID, claims.OrganizationID); err != nil {
|
||||||
|
c.JSON(http.StatusNotFound, gin.H{"error": "Placement not found"})
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
c.JSON(http.StatusNoContent, nil)
|
||||||
|
}
|
||||||
|
|
||||||
|
// Search выполняет поиск товаров с местами размещения
|
||||||
|
func (h *OperationsHandler) Search(c *gin.Context) {
|
||||||
|
claims := middleware.GetClaims(c)
|
||||||
|
if claims == nil {
|
||||||
|
c.JSON(http.StatusUnauthorized, gin.H{"error": "Unauthorized"})
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
var req models.SearchRequest
|
||||||
|
if err := c.ShouldBindQuery(&req); err != nil {
|
||||||
|
c.JSON(http.StatusBadRequest, gin.H{"error": "Invalid query parameters"})
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
// Устанавливаем значения по умолчанию
|
||||||
|
if req.Page <= 0 {
|
||||||
|
req.Page = 1
|
||||||
|
}
|
||||||
|
if req.PageSize <= 0 {
|
||||||
|
req.PageSize = 20
|
||||||
|
}
|
||||||
|
|
||||||
|
response, err := h.operationsService.Search(c.Request.Context(), claims.OrganizationID, &req)
|
||||||
|
if err != nil {
|
||||||
|
c.JSON(http.StatusInternalServerError, gin.H{"error": "Failed to search"})
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
c.JSON(http.StatusOK, response)
|
||||||
|
}
|
||||||
89
core-service/internal/api/middleware/auth.go
Normal file
89
core-service/internal/api/middleware/auth.go
Normal file
@@ -0,0 +1,89 @@
|
|||||||
|
package middleware
|
||||||
|
|
||||||
|
import (
|
||||||
|
"net/http"
|
||||||
|
"strings"
|
||||||
|
|
||||||
|
"erp-mvp/core-service/internal/auth"
|
||||||
|
|
||||||
|
"github.com/gin-gonic/gin"
|
||||||
|
"github.com/google/uuid"
|
||||||
|
)
|
||||||
|
|
||||||
|
type AuthMiddleware struct {
|
||||||
|
jwtService *auth.JWTService
|
||||||
|
}
|
||||||
|
|
||||||
|
func NewAuthMiddleware(jwtService *auth.JWTService) *AuthMiddleware {
|
||||||
|
return &AuthMiddleware{
|
||||||
|
jwtService: jwtService,
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func (m *AuthMiddleware) AuthRequired() gin.HandlerFunc {
|
||||||
|
return func(c *gin.Context) {
|
||||||
|
// Получаем токен из заголовка Authorization
|
||||||
|
authHeader := c.GetHeader("Authorization")
|
||||||
|
if authHeader == "" {
|
||||||
|
c.JSON(http.StatusUnauthorized, gin.H{"error": "Authorization header required"})
|
||||||
|
c.Abort()
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
// Проверяем формат "Bearer <token>"
|
||||||
|
tokenParts := strings.Split(authHeader, " ")
|
||||||
|
if len(tokenParts) != 2 || tokenParts[0] != "Bearer" {
|
||||||
|
c.JSON(http.StatusUnauthorized, gin.H{"error": "Invalid authorization header format"})
|
||||||
|
c.Abort()
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
tokenString := tokenParts[1]
|
||||||
|
|
||||||
|
// Валидируем токен
|
||||||
|
claims, err := m.jwtService.ValidateToken(tokenString)
|
||||||
|
if err != nil {
|
||||||
|
c.JSON(http.StatusUnauthorized, gin.H{"error": "Invalid token"})
|
||||||
|
c.Abort()
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
// Сохраняем claims в контексте
|
||||||
|
c.Set("user_id", claims.UserID)
|
||||||
|
c.Set("organization_id", claims.OrganizationID)
|
||||||
|
c.Set("email", claims.Email)
|
||||||
|
c.Set("role", claims.Role)
|
||||||
|
|
||||||
|
c.Next()
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// GetClaims получает claims из контекста Gin
|
||||||
|
func GetClaims(c *gin.Context) *auth.Claims {
|
||||||
|
userID, exists := c.Get("user_id")
|
||||||
|
if !exists {
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
|
||||||
|
orgID, exists := c.Get("organization_id")
|
||||||
|
if !exists {
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
|
||||||
|
email, exists := c.Get("email")
|
||||||
|
if !exists {
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
|
||||||
|
role, exists := c.Get("role")
|
||||||
|
if !exists {
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
|
||||||
|
return &auth.Claims{
|
||||||
|
UserID: userID.(uuid.UUID),
|
||||||
|
OrganizationID: orgID.(uuid.UUID),
|
||||||
|
Email: email.(string),
|
||||||
|
Role: role.(string),
|
||||||
|
}
|
||||||
|
}
|
||||||
172
core-service/internal/api/server.go
Normal file
172
core-service/internal/api/server.go
Normal file
@@ -0,0 +1,172 @@
|
|||||||
|
package api
|
||||||
|
|
||||||
|
import (
|
||||||
|
"context"
|
||||||
|
"database/sql"
|
||||||
|
"net/http"
|
||||||
|
|
||||||
|
"erp-mvp/core-service/internal/api/handlers"
|
||||||
|
"erp-mvp/core-service/internal/api/middleware"
|
||||||
|
"erp-mvp/core-service/internal/auth"
|
||||||
|
"erp-mvp/core-service/internal/config"
|
||||||
|
"erp-mvp/core-service/internal/logger"
|
||||||
|
"erp-mvp/core-service/internal/repository"
|
||||||
|
"erp-mvp/core-service/internal/service"
|
||||||
|
|
||||||
|
"github.com/gin-gonic/gin"
|
||||||
|
)
|
||||||
|
|
||||||
|
type Server struct {
|
||||||
|
config *config.Config
|
||||||
|
db *sql.DB
|
||||||
|
logger logger.Logger
|
||||||
|
router *gin.Engine
|
||||||
|
|
||||||
|
// Services
|
||||||
|
authService service.AuthService
|
||||||
|
locationService service.LocationService
|
||||||
|
itemService service.ItemService
|
||||||
|
operationsService service.OperationsService
|
||||||
|
|
||||||
|
// Handlers
|
||||||
|
authHandler *handlers.AuthHandler
|
||||||
|
locationHandler *handlers.LocationHandler
|
||||||
|
itemHandler *handlers.ItemHandler
|
||||||
|
operationsHandler *handlers.OperationsHandler
|
||||||
|
|
||||||
|
// Middleware
|
||||||
|
authMiddleware *middleware.AuthMiddleware
|
||||||
|
}
|
||||||
|
|
||||||
|
func NewServer(cfg *config.Config, db *sql.DB, log logger.Logger) *Server {
|
||||||
|
// Инициализируем JWT сервис
|
||||||
|
jwtService := auth.NewJWTService(cfg.JWT.Secret, cfg.JWT.TTL)
|
||||||
|
|
||||||
|
// Инициализируем репозитории
|
||||||
|
orgRepo := repository.NewOrganizationRepository(db)
|
||||||
|
userRepo := repository.NewUserRepository(db)
|
||||||
|
locationRepo := repository.NewLocationRepository(db)
|
||||||
|
itemRepo := repository.NewItemRepository(db)
|
||||||
|
operationsRepo := repository.NewOperationsRepository(db)
|
||||||
|
|
||||||
|
// Инициализируем сервисы
|
||||||
|
authService := service.NewAuthService(orgRepo, userRepo, jwtService)
|
||||||
|
locationService := service.NewLocationService(locationRepo)
|
||||||
|
itemService := service.NewItemService(itemRepo)
|
||||||
|
operationsService := service.NewOperationsService(operationsRepo, itemRepo, locationRepo)
|
||||||
|
|
||||||
|
// Инициализируем handlers
|
||||||
|
authHandler := handlers.NewAuthHandler(authService)
|
||||||
|
locationHandler := handlers.NewLocationHandler(locationService)
|
||||||
|
itemHandler := handlers.NewItemHandler(itemService)
|
||||||
|
operationsHandler := handlers.NewOperationsHandler(operationsService)
|
||||||
|
|
||||||
|
// Инициализируем middleware
|
||||||
|
authMiddleware := middleware.NewAuthMiddleware(jwtService)
|
||||||
|
|
||||||
|
server := &Server{
|
||||||
|
config: cfg,
|
||||||
|
db: db,
|
||||||
|
logger: log,
|
||||||
|
router: gin.Default(),
|
||||||
|
authService: authService,
|
||||||
|
locationService: locationService,
|
||||||
|
itemService: itemService,
|
||||||
|
operationsService: operationsService,
|
||||||
|
authHandler: authHandler,
|
||||||
|
locationHandler: locationHandler,
|
||||||
|
itemHandler: itemHandler,
|
||||||
|
operationsHandler: operationsHandler,
|
||||||
|
authMiddleware: authMiddleware,
|
||||||
|
}
|
||||||
|
|
||||||
|
server.setupRoutes()
|
||||||
|
return server
|
||||||
|
}
|
||||||
|
|
||||||
|
func (s *Server) setupRoutes() {
|
||||||
|
// Health check
|
||||||
|
s.router.GET("/health", s.healthCheck)
|
||||||
|
|
||||||
|
// API routes
|
||||||
|
api := s.router.Group("/api")
|
||||||
|
{
|
||||||
|
// Auth routes
|
||||||
|
auth := api.Group("/auth")
|
||||||
|
{
|
||||||
|
auth.POST("/register", s.authHandler.Register)
|
||||||
|
auth.POST("/login", s.authHandler.Login)
|
||||||
|
}
|
||||||
|
|
||||||
|
// Protected routes
|
||||||
|
protected := api.Group("/")
|
||||||
|
protected.Use(s.authMiddleware.AuthRequired())
|
||||||
|
{
|
||||||
|
// Organizations
|
||||||
|
protected.GET("/organizations/:id", s.getOrganization)
|
||||||
|
protected.PUT("/organizations/:id", s.updateOrganization)
|
||||||
|
|
||||||
|
// Locations
|
||||||
|
protected.GET("/locations", s.locationHandler.GetLocations)
|
||||||
|
protected.POST("/locations", s.locationHandler.CreateLocation)
|
||||||
|
protected.GET("/locations/:id", s.locationHandler.GetLocation)
|
||||||
|
protected.PUT("/locations/:id", s.locationHandler.UpdateLocation)
|
||||||
|
protected.DELETE("/locations/:id", s.locationHandler.DeleteLocation)
|
||||||
|
protected.GET("/locations/:id/children", s.locationHandler.GetChildren)
|
||||||
|
|
||||||
|
// Items
|
||||||
|
protected.GET("/items", s.itemHandler.GetItems)
|
||||||
|
protected.POST("/items", s.itemHandler.CreateItem)
|
||||||
|
protected.GET("/items/:id", s.itemHandler.GetItem)
|
||||||
|
protected.PUT("/items/:id", s.itemHandler.UpdateItem)
|
||||||
|
protected.DELETE("/items/:id", s.itemHandler.DeleteItem)
|
||||||
|
protected.GET("/items/search", s.itemHandler.SearchItems)
|
||||||
|
|
||||||
|
// Operations
|
||||||
|
protected.POST("/operations/place-item", s.operationsHandler.PlaceItem)
|
||||||
|
protected.POST("/operations/move-item/:id", s.operationsHandler.MoveItem)
|
||||||
|
protected.GET("/operations/search", s.operationsHandler.Search)
|
||||||
|
protected.GET("/operations/items/:item_id/placements", s.operationsHandler.GetItemPlacements)
|
||||||
|
protected.GET("/operations/locations/:location_id/placements", s.operationsHandler.GetLocationPlacements)
|
||||||
|
protected.PUT("/operations/placements/:id/quantity", s.operationsHandler.UpdateQuantity)
|
||||||
|
protected.DELETE("/operations/placements/:id", s.operationsHandler.DeletePlacement)
|
||||||
|
|
||||||
|
// Templates
|
||||||
|
protected.GET("/templates", s.getTemplates)
|
||||||
|
protected.POST("/templates/:id/apply", s.applyTemplate)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func (s *Server) healthCheck(c *gin.Context) {
|
||||||
|
c.JSON(http.StatusOK, gin.H{
|
||||||
|
"status": "ok",
|
||||||
|
"service": "erp-mvp-core",
|
||||||
|
})
|
||||||
|
}
|
||||||
|
|
||||||
|
// Placeholder handlers - will be implemented in next stages
|
||||||
|
func (s *Server) getOrganization(c *gin.Context) {
|
||||||
|
c.JSON(http.StatusNotImplemented, gin.H{"error": "Not implemented yet"})
|
||||||
|
}
|
||||||
|
|
||||||
|
func (s *Server) updateOrganization(c *gin.Context) {
|
||||||
|
c.JSON(http.StatusNotImplemented, gin.H{"error": "Not implemented yet"})
|
||||||
|
}
|
||||||
|
|
||||||
|
func (s *Server) getTemplates(c *gin.Context) {
|
||||||
|
c.JSON(http.StatusNotImplemented, gin.H{"error": "Not implemented yet"})
|
||||||
|
}
|
||||||
|
|
||||||
|
func (s *Server) applyTemplate(c *gin.Context) {
|
||||||
|
c.JSON(http.StatusNotImplemented, gin.H{"error": "Not implemented yet"})
|
||||||
|
}
|
||||||
|
|
||||||
|
func (s *Server) Start() error {
|
||||||
|
return s.router.Run(s.config.Server.Host + ":" + s.config.Server.Port)
|
||||||
|
}
|
||||||
|
|
||||||
|
func (s *Server) Shutdown(ctx context.Context) error {
|
||||||
|
// Graceful shutdown logic will be added later
|
||||||
|
return nil
|
||||||
|
}
|
||||||
69
core-service/internal/auth/jwt.go
Normal file
69
core-service/internal/auth/jwt.go
Normal file
@@ -0,0 +1,69 @@
|
|||||||
|
package auth
|
||||||
|
|
||||||
|
import (
|
||||||
|
"errors"
|
||||||
|
"time"
|
||||||
|
|
||||||
|
"github.com/golang-jwt/jwt/v5"
|
||||||
|
"github.com/google/uuid"
|
||||||
|
)
|
||||||
|
|
||||||
|
type Claims struct {
|
||||||
|
UserID uuid.UUID `json:"user_id"`
|
||||||
|
OrganizationID uuid.UUID `json:"organization_id"`
|
||||||
|
Email string `json:"email"`
|
||||||
|
Role string `json:"role"`
|
||||||
|
jwt.RegisteredClaims
|
||||||
|
}
|
||||||
|
|
||||||
|
type JWTService struct {
|
||||||
|
secret string
|
||||||
|
ttl time.Duration
|
||||||
|
}
|
||||||
|
|
||||||
|
func NewJWTService(secret string, ttl time.Duration) *JWTService {
|
||||||
|
return &JWTService{
|
||||||
|
secret: secret,
|
||||||
|
ttl: ttl,
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// GenerateToken создаёт JWT токен для пользователя
|
||||||
|
func (j *JWTService) GenerateToken(userID, organizationID uuid.UUID, email, role string) (string, error) {
|
||||||
|
claims := Claims{
|
||||||
|
UserID: userID,
|
||||||
|
OrganizationID: organizationID,
|
||||||
|
Email: email,
|
||||||
|
Role: role,
|
||||||
|
RegisteredClaims: jwt.RegisteredClaims{
|
||||||
|
ExpiresAt: jwt.NewNumericDate(time.Now().Add(j.ttl)),
|
||||||
|
IssuedAt: jwt.NewNumericDate(time.Now()),
|
||||||
|
NotBefore: jwt.NewNumericDate(time.Now()),
|
||||||
|
Issuer: "erp-mvp-core",
|
||||||
|
Subject: userID.String(),
|
||||||
|
},
|
||||||
|
}
|
||||||
|
|
||||||
|
token := jwt.NewWithClaims(jwt.SigningMethodHS256, claims)
|
||||||
|
return token.SignedString([]byte(j.secret))
|
||||||
|
}
|
||||||
|
|
||||||
|
// ValidateToken валидирует JWT токен и возвращает claims
|
||||||
|
func (j *JWTService) ValidateToken(tokenString string) (*Claims, error) {
|
||||||
|
token, err := jwt.ParseWithClaims(tokenString, &Claims{}, func(token *jwt.Token) (interface{}, error) {
|
||||||
|
if _, ok := token.Method.(*jwt.SigningMethodHMAC); !ok {
|
||||||
|
return nil, errors.New("unexpected signing method")
|
||||||
|
}
|
||||||
|
return []byte(j.secret), nil
|
||||||
|
})
|
||||||
|
|
||||||
|
if err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
|
||||||
|
if claims, ok := token.Claims.(*Claims); ok && token.Valid {
|
||||||
|
return claims, nil
|
||||||
|
}
|
||||||
|
|
||||||
|
return nil, errors.New("invalid token")
|
||||||
|
}
|
||||||
17
core-service/internal/auth/password.go
Normal file
17
core-service/internal/auth/password.go
Normal file
@@ -0,0 +1,17 @@
|
|||||||
|
package auth
|
||||||
|
|
||||||
|
import (
|
||||||
|
"golang.org/x/crypto/bcrypt"
|
||||||
|
)
|
||||||
|
|
||||||
|
// HashPassword хеширует пароль с использованием bcrypt
|
||||||
|
func HashPassword(password string) (string, error) {
|
||||||
|
bytes, err := bcrypt.GenerateFromPassword([]byte(password), bcrypt.DefaultCost)
|
||||||
|
return string(bytes), err
|
||||||
|
}
|
||||||
|
|
||||||
|
// CheckPassword проверяет пароль против хеша
|
||||||
|
func CheckPassword(password, hash string) bool {
|
||||||
|
err := bcrypt.CompareHashAndPassword([]byte(hash), []byte(password))
|
||||||
|
return err == nil
|
||||||
|
}
|
||||||
@@ -3,17 +3,15 @@ package config
|
|||||||
import (
|
import (
|
||||||
"os"
|
"os"
|
||||||
"strconv"
|
"strconv"
|
||||||
|
"time"
|
||||||
|
|
||||||
"github.com/joho/godotenv"
|
"github.com/joho/godotenv"
|
||||||
)
|
)
|
||||||
|
|
||||||
type Config struct {
|
type Config struct {
|
||||||
Server ServerConfig
|
Server ServerConfig
|
||||||
Database DatabaseConfig
|
Database DatabaseConfig
|
||||||
Redis RedisConfig
|
JWT JWTConfig
|
||||||
JWT JWTConfig
|
|
||||||
DocumentService DocumentServiceConfig
|
|
||||||
Log LogConfig
|
|
||||||
}
|
}
|
||||||
|
|
||||||
type ServerConfig struct {
|
type ServerConfig struct {
|
||||||
@@ -30,24 +28,9 @@ type DatabaseConfig struct {
|
|||||||
SSLMode string
|
SSLMode string
|
||||||
}
|
}
|
||||||
|
|
||||||
type RedisConfig struct {
|
|
||||||
Host string
|
|
||||||
Port string
|
|
||||||
Password string
|
|
||||||
DB int
|
|
||||||
}
|
|
||||||
|
|
||||||
type JWTConfig struct {
|
type JWTConfig struct {
|
||||||
Secret string
|
Secret string
|
||||||
Expiration int // в часах
|
TTL time.Duration
|
||||||
}
|
|
||||||
|
|
||||||
type DocumentServiceConfig struct {
|
|
||||||
URL string
|
|
||||||
}
|
|
||||||
|
|
||||||
type LogConfig struct {
|
|
||||||
Level string
|
|
||||||
}
|
}
|
||||||
|
|
||||||
func Load() (*Config, error) {
|
func Load() (*Config, error) {
|
||||||
@@ -67,21 +50,9 @@ func Load() (*Config, error) {
|
|||||||
DBName: getEnv("DB_NAME", "erp_mvp"),
|
DBName: getEnv("DB_NAME", "erp_mvp"),
|
||||||
SSLMode: getEnv("DB_SSLMODE", "disable"),
|
SSLMode: getEnv("DB_SSLMODE", "disable"),
|
||||||
},
|
},
|
||||||
Redis: RedisConfig{
|
|
||||||
Host: getEnv("REDIS_HOST", "localhost"),
|
|
||||||
Port: getEnv("REDIS_PORT", "6379"),
|
|
||||||
Password: getEnv("REDIS_PASSWORD", ""),
|
|
||||||
DB: getEnvAsInt("REDIS_DB", 0),
|
|
||||||
},
|
|
||||||
JWT: JWTConfig{
|
JWT: JWTConfig{
|
||||||
Secret: getEnv("JWT_SECRET", "your-secret-key"),
|
Secret: getEnv("JWT_SECRET", "your-secret-key"),
|
||||||
Expiration: getEnvAsInt("JWT_EXPIRATION", 24),
|
TTL: time.Duration(getEnvAsInt("JWT_TTL_HOURS", 24)) * time.Hour,
|
||||||
},
|
|
||||||
DocumentService: DocumentServiceConfig{
|
|
||||||
URL: getEnv("DOC_SERVICE_URL", "http://localhost:8000"),
|
|
||||||
},
|
|
||||||
Log: LogConfig{
|
|
||||||
Level: getEnv("LOG_LEVEL", "info"),
|
|
||||||
},
|
},
|
||||||
}, nil
|
}, nil
|
||||||
}
|
}
|
||||||
|
|||||||
25
core-service/internal/database/connection.go
Normal file
25
core-service/internal/database/connection.go
Normal file
@@ -0,0 +1,25 @@
|
|||||||
|
package database
|
||||||
|
|
||||||
|
import (
|
||||||
|
"database/sql"
|
||||||
|
"fmt"
|
||||||
|
|
||||||
|
"erp-mvp/core-service/internal/config"
|
||||||
|
_ "github.com/lib/pq"
|
||||||
|
)
|
||||||
|
|
||||||
|
func Connect(cfg config.DatabaseConfig) (*sql.DB, error) {
|
||||||
|
dsn := fmt.Sprintf("host=%s port=%s user=%s password=%s dbname=%s sslmode=%s",
|
||||||
|
cfg.Host, cfg.Port, cfg.User, cfg.Password, cfg.DBName, cfg.SSLMode)
|
||||||
|
|
||||||
|
db, err := sql.Open("postgres", dsn)
|
||||||
|
if err != nil {
|
||||||
|
return nil, fmt.Errorf("failed to open database: %w", err)
|
||||||
|
}
|
||||||
|
|
||||||
|
if err := db.Ping(); err != nil {
|
||||||
|
return nil, fmt.Errorf("failed to ping database: %w", err)
|
||||||
|
}
|
||||||
|
|
||||||
|
return db, nil
|
||||||
|
}
|
||||||
28
core-service/internal/logger/logger.go
Normal file
28
core-service/internal/logger/logger.go
Normal file
@@ -0,0 +1,28 @@
|
|||||||
|
package logger
|
||||||
|
|
||||||
|
import (
|
||||||
|
"os"
|
||||||
|
|
||||||
|
"github.com/sirupsen/logrus"
|
||||||
|
)
|
||||||
|
|
||||||
|
type Logger interface {
|
||||||
|
Info(args ...interface{})
|
||||||
|
Error(args ...interface{})
|
||||||
|
Fatal(args ...interface{})
|
||||||
|
Debug(args ...interface{})
|
||||||
|
Warn(args ...interface{})
|
||||||
|
}
|
||||||
|
|
||||||
|
type logger struct {
|
||||||
|
*logrus.Logger
|
||||||
|
}
|
||||||
|
|
||||||
|
func New() Logger {
|
||||||
|
l := logrus.New()
|
||||||
|
l.SetOutput(os.Stdout)
|
||||||
|
l.SetLevel(logrus.InfoLevel)
|
||||||
|
l.SetFormatter(&logrus.JSONFormatter{})
|
||||||
|
|
||||||
|
return &logger{l}
|
||||||
|
}
|
||||||
@@ -8,47 +8,43 @@ import (
|
|||||||
|
|
||||||
// Organization представляет организацию/компанию
|
// Organization представляет организацию/компанию
|
||||||
type Organization struct {
|
type Organization struct {
|
||||||
ID uuid.UUID `json:"id" db:"id"`
|
ID uuid.UUID `json:"id" db:"id"`
|
||||||
Name string `json:"name" db:"name"`
|
Name string `json:"name" validate:"required"`
|
||||||
Type string `json:"type" db:"type"`
|
Type string `json:"type"`
|
||||||
Settings map[string]any `json:"settings" db:"settings"`
|
Settings JSON `json:"settings"`
|
||||||
CreatedAt time.Time `json:"created_at" db:"created_at"`
|
CreatedAt time.Time `json:"created_at" db:"created_at"`
|
||||||
UpdatedAt time.Time `json:"updated_at" db:"updated_at"`
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// User представляет пользователя системы
|
// User представляет пользователя системы
|
||||||
type User struct {
|
type User struct {
|
||||||
ID uuid.UUID `json:"id" db:"id"`
|
ID uuid.UUID `json:"id" db:"id"`
|
||||||
OrganizationID uuid.UUID `json:"organization_id" db:"organization_id"`
|
OrganizationID uuid.UUID `json:"organization_id" db:"organization_id"`
|
||||||
Email string `json:"email" db:"email"`
|
Email string `json:"email" validate:"required,email"`
|
||||||
Role string `json:"role" db:"role"`
|
PasswordHash string `json:"-" db:"password_hash"`
|
||||||
|
Role string `json:"role"`
|
||||||
CreatedAt time.Time `json:"created_at" db:"created_at"`
|
CreatedAt time.Time `json:"created_at" db:"created_at"`
|
||||||
UpdatedAt time.Time `json:"updated_at" db:"updated_at"`
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// StorageLocation представляет место хранения
|
// StorageLocation представляет место хранения
|
||||||
type StorageLocation struct {
|
type StorageLocation struct {
|
||||||
ID uuid.UUID `json:"id" db:"id"`
|
ID uuid.UUID `json:"id" db:"id"`
|
||||||
OrganizationID uuid.UUID `json:"organization_id" db:"organization_id"`
|
OrganizationID uuid.UUID `json:"organization_id" db:"organization_id"`
|
||||||
ParentID *uuid.UUID `json:"parent_id" db:"parent_id"`
|
ParentID *uuid.UUID `json:"parent_id,omitempty" db:"parent_id"`
|
||||||
Name string `json:"name" db:"name"`
|
Name string `json:"name" validate:"required"`
|
||||||
Address string `json:"address" db:"address"`
|
Address string `json:"address" validate:"required"`
|
||||||
Type string `json:"type" db:"type"`
|
Type string `json:"type" validate:"required"`
|
||||||
Coordinates map[string]any `json:"coordinates" db:"coordinates"`
|
Coordinates JSON `json:"coordinates"`
|
||||||
QRCode string `json:"qr_code" db:"qr_code"`
|
CreatedAt time.Time `json:"created_at" db:"created_at"`
|
||||||
CreatedAt time.Time `json:"created_at" db:"created_at"`
|
|
||||||
UpdatedAt time.Time `json:"updated_at" db:"updated_at"`
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// Item представляет товар/материал
|
// Item представляет товар/материал
|
||||||
type Item struct {
|
type Item struct {
|
||||||
ID uuid.UUID `json:"id" db:"id"`
|
ID uuid.UUID `json:"id" db:"id"`
|
||||||
OrganizationID uuid.UUID `json:"organization_id" db:"organization_id"`
|
OrganizationID uuid.UUID `json:"organization_id" db:"organization_id"`
|
||||||
Name string `json:"name" db:"name"`
|
Name string `json:"name" validate:"required"`
|
||||||
Description string `json:"description" db:"description"`
|
Description string `json:"description"`
|
||||||
Category string `json:"category" db:"category"`
|
Category string `json:"category"`
|
||||||
CreatedAt time.Time `json:"created_at" db:"created_at"`
|
CreatedAt time.Time `json:"created_at" db:"created_at"`
|
||||||
UpdatedAt time.Time `json:"updated_at" db:"updated_at"`
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// ItemPlacement представляет размещение товара в месте хранения
|
// ItemPlacement представляет размещение товара в месте хранения
|
||||||
@@ -57,64 +53,89 @@ type ItemPlacement struct {
|
|||||||
OrganizationID uuid.UUID `json:"organization_id" db:"organization_id"`
|
OrganizationID uuid.UUID `json:"organization_id" db:"organization_id"`
|
||||||
ItemID uuid.UUID `json:"item_id" db:"item_id"`
|
ItemID uuid.UUID `json:"item_id" db:"item_id"`
|
||||||
LocationID uuid.UUID `json:"location_id" db:"location_id"`
|
LocationID uuid.UUID `json:"location_id" db:"location_id"`
|
||||||
Quantity int `json:"quantity" db:"quantity"`
|
Quantity int `json:"quantity" validate:"min=1"`
|
||||||
CreatedAt time.Time `json:"created_at" db:"created_at"`
|
CreatedAt time.Time `json:"created_at" db:"created_at"`
|
||||||
UpdatedAt time.Time `json:"updated_at" db:"updated_at"`
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// JSON тип для JSON полей
|
||||||
|
type JSON map[string]interface{}
|
||||||
|
|
||||||
// LoginRequest запрос на аутентификацию
|
// LoginRequest запрос на аутентификацию
|
||||||
type LoginRequest struct {
|
type LoginRequest struct {
|
||||||
Email string `json:"email" binding:"required,email"`
|
Email string `json:"email" validate:"required,email"`
|
||||||
Password string `json:"password" binding:"required"`
|
Password string `json:"password" validate:"required"`
|
||||||
|
}
|
||||||
|
|
||||||
|
// RegisterRequest запрос на регистрацию
|
||||||
|
type RegisterRequest struct {
|
||||||
|
OrganizationName string `json:"organization_name" validate:"required"`
|
||||||
|
UserEmail string `json:"user_email" validate:"required,email"`
|
||||||
|
UserPassword string `json:"user_password" validate:"required,min=8"`
|
||||||
|
OrganizationType string `json:"organization_type"`
|
||||||
|
}
|
||||||
|
|
||||||
|
// UserResponse ответ с информацией о пользователе
|
||||||
|
type UserResponse struct {
|
||||||
|
ID uuid.UUID `json:"id"`
|
||||||
|
Email string `json:"email"`
|
||||||
|
Role string `json:"role"`
|
||||||
|
}
|
||||||
|
|
||||||
|
// OrganizationResponse ответ с информацией об организации
|
||||||
|
type OrganizationResponse struct {
|
||||||
|
ID uuid.UUID `json:"id"`
|
||||||
|
Name string `json:"name"`
|
||||||
|
Type string `json:"type"`
|
||||||
}
|
}
|
||||||
|
|
||||||
// LoginResponse ответ на аутентификацию
|
// LoginResponse ответ на аутентификацию
|
||||||
type LoginResponse struct {
|
type LoginResponse struct {
|
||||||
Token string `json:"token"`
|
Token string `json:"token"`
|
||||||
RefreshToken string `json:"refresh_token"`
|
User UserResponse `json:"user"`
|
||||||
User User `json:"user"`
|
Organization OrganizationResponse `json:"organization"`
|
||||||
ExpiresAt time.Time `json:"expires_at"`
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// CreateLocationRequest запрос на создание места хранения
|
// CreateLocationRequest запрос на создание места хранения
|
||||||
type CreateLocationRequest struct {
|
type CreateLocationRequest struct {
|
||||||
Name string `json:"name" binding:"required"`
|
Name string `json:"name" validate:"required"`
|
||||||
Address string `json:"address" binding:"required"`
|
Address string `json:"address" validate:"required"`
|
||||||
Type string `json:"type" binding:"required"`
|
Type string `json:"type" validate:"required"`
|
||||||
ParentID *uuid.UUID `json:"parent_id"`
|
ParentID *uuid.UUID `json:"parent_id"`
|
||||||
Coordinates map[string]any `json:"coordinates"`
|
Coordinates JSON `json:"coordinates"`
|
||||||
}
|
}
|
||||||
|
|
||||||
// CreateItemRequest запрос на создание товара
|
// CreateItemRequest запрос на создание товара
|
||||||
type CreateItemRequest struct {
|
type CreateItemRequest struct {
|
||||||
Name string `json:"name" binding:"required"`
|
Name string `json:"name" validate:"required"`
|
||||||
Description string `json:"description"`
|
Description string `json:"description"`
|
||||||
Category string `json:"category" binding:"required"`
|
Category string `json:"category"`
|
||||||
}
|
}
|
||||||
|
|
||||||
// PlaceItemRequest запрос на размещение товара
|
// PlaceItemRequest запрос на размещение товара
|
||||||
type PlaceItemRequest struct {
|
type PlaceItemRequest struct {
|
||||||
ItemID uuid.UUID `json:"item_id" binding:"required"`
|
ItemID uuid.UUID `json:"item_id" validate:"required"`
|
||||||
LocationID uuid.UUID `json:"location_id" binding:"required"`
|
LocationID uuid.UUID `json:"location_id" validate:"required"`
|
||||||
Quantity int `json:"quantity" binding:"required,min=1"`
|
Quantity int `json:"quantity" validate:"required,min=1"`
|
||||||
}
|
}
|
||||||
|
|
||||||
// SearchRequest запрос на поиск
|
// SearchRequest запрос на поиск
|
||||||
type SearchRequest struct {
|
type SearchRequest struct {
|
||||||
Query string `json:"query" binding:"required"`
|
Query string `form:"q"`
|
||||||
Category string `json:"category"`
|
Category string `form:"category"`
|
||||||
LocationID *uuid.UUID `json:"location_id"`
|
Address string `form:"address"`
|
||||||
|
Page int `form:"page,default=1"`
|
||||||
|
PageSize int `form:"page_size,default=20"`
|
||||||
}
|
}
|
||||||
|
|
||||||
// SearchResponse результат поиска
|
// SearchResponse результат поиска
|
||||||
type SearchResponse struct {
|
type SearchResponse struct {
|
||||||
Items []ItemWithLocation `json:"items"`
|
Items []*ItemWithLocation `json:"items"`
|
||||||
TotalCount int `json:"total_count"`
|
TotalCount int `json:"total_count"`
|
||||||
}
|
}
|
||||||
|
|
||||||
// ItemWithLocation товар с информацией о месте размещения
|
// ItemWithLocation товар с информацией о месте размещения
|
||||||
type ItemWithLocation struct {
|
type ItemWithLocation struct {
|
||||||
Item Item `json:"item"`
|
Item Item `json:"item"`
|
||||||
Location StorageLocation `json:"location"`
|
Location StorageLocation `json:"location"`
|
||||||
Quantity int `json:"quantity"`
|
Quantity int `json:"quantity"`
|
||||||
}
|
}
|
||||||
|
|||||||
224
core-service/internal/repository/items.go
Normal file
224
core-service/internal/repository/items.go
Normal file
@@ -0,0 +1,224 @@
|
|||||||
|
package repository
|
||||||
|
|
||||||
|
import (
|
||||||
|
"context"
|
||||||
|
"database/sql"
|
||||||
|
"fmt"
|
||||||
|
|
||||||
|
"erp-mvp/core-service/internal/models"
|
||||||
|
|
||||||
|
"github.com/google/uuid"
|
||||||
|
)
|
||||||
|
|
||||||
|
type ItemRepository interface {
|
||||||
|
Create(ctx context.Context, item *models.Item) error
|
||||||
|
GetByID(ctx context.Context, id uuid.UUID, orgID uuid.UUID) (*models.Item, error)
|
||||||
|
GetByOrganization(ctx context.Context, orgID uuid.UUID) ([]*models.Item, error)
|
||||||
|
Update(ctx context.Context, item *models.Item) error
|
||||||
|
Delete(ctx context.Context, id uuid.UUID, orgID uuid.UUID) error
|
||||||
|
Search(ctx context.Context, orgID uuid.UUID, query string, category string) ([]*models.Item, error)
|
||||||
|
}
|
||||||
|
|
||||||
|
type itemRepository struct {
|
||||||
|
db *sql.DB
|
||||||
|
}
|
||||||
|
|
||||||
|
func NewItemRepository(db *sql.DB) ItemRepository {
|
||||||
|
return &itemRepository{db: db}
|
||||||
|
}
|
||||||
|
|
||||||
|
func (r *itemRepository) Create(ctx context.Context, item *models.Item) error {
|
||||||
|
query := `
|
||||||
|
INSERT INTO items (id, organization_id, name, description, category, created_at)
|
||||||
|
VALUES ($1, $2, $3, $4, $5, $6)
|
||||||
|
`
|
||||||
|
|
||||||
|
_, err := r.db.ExecContext(ctx, query,
|
||||||
|
item.ID,
|
||||||
|
item.OrganizationID,
|
||||||
|
item.Name,
|
||||||
|
item.Description,
|
||||||
|
item.Category,
|
||||||
|
item.CreatedAt,
|
||||||
|
)
|
||||||
|
if err != nil {
|
||||||
|
return fmt.Errorf("failed to create item: %w", err)
|
||||||
|
}
|
||||||
|
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
|
||||||
|
func (r *itemRepository) GetByID(ctx context.Context, id uuid.UUID, orgID uuid.UUID) (*models.Item, error) {
|
||||||
|
query := `
|
||||||
|
SELECT id, organization_id, name, description, category, created_at
|
||||||
|
FROM items
|
||||||
|
WHERE id = $1 AND organization_id = $2
|
||||||
|
`
|
||||||
|
|
||||||
|
item := &models.Item{}
|
||||||
|
err := r.db.QueryRowContext(ctx, query, id, orgID).Scan(
|
||||||
|
&item.ID,
|
||||||
|
&item.OrganizationID,
|
||||||
|
&item.Name,
|
||||||
|
&item.Description,
|
||||||
|
&item.Category,
|
||||||
|
&item.CreatedAt,
|
||||||
|
)
|
||||||
|
|
||||||
|
if err != nil {
|
||||||
|
if err == sql.ErrNoRows {
|
||||||
|
return nil, fmt.Errorf("item not found")
|
||||||
|
}
|
||||||
|
return nil, fmt.Errorf("failed to get item: %w", err)
|
||||||
|
}
|
||||||
|
|
||||||
|
return item, nil
|
||||||
|
}
|
||||||
|
|
||||||
|
func (r *itemRepository) GetByOrganization(ctx context.Context, orgID uuid.UUID) ([]*models.Item, error) {
|
||||||
|
query := `
|
||||||
|
SELECT id, organization_id, name, description, category, created_at
|
||||||
|
FROM items
|
||||||
|
WHERE organization_id = $1
|
||||||
|
ORDER BY name
|
||||||
|
`
|
||||||
|
|
||||||
|
rows, err := r.db.QueryContext(ctx, query, orgID)
|
||||||
|
if err != nil {
|
||||||
|
return nil, fmt.Errorf("failed to query items: %w", err)
|
||||||
|
}
|
||||||
|
defer rows.Close()
|
||||||
|
|
||||||
|
var items []*models.Item
|
||||||
|
for rows.Next() {
|
||||||
|
item := &models.Item{}
|
||||||
|
err := rows.Scan(
|
||||||
|
&item.ID,
|
||||||
|
&item.OrganizationID,
|
||||||
|
&item.Name,
|
||||||
|
&item.Description,
|
||||||
|
&item.Category,
|
||||||
|
&item.CreatedAt,
|
||||||
|
)
|
||||||
|
if err != nil {
|
||||||
|
return nil, fmt.Errorf("failed to scan item: %w", err)
|
||||||
|
}
|
||||||
|
|
||||||
|
items = append(items, item)
|
||||||
|
}
|
||||||
|
|
||||||
|
if err = rows.Err(); err != nil {
|
||||||
|
return nil, fmt.Errorf("error iterating items: %w", err)
|
||||||
|
}
|
||||||
|
|
||||||
|
return items, nil
|
||||||
|
}
|
||||||
|
|
||||||
|
func (r *itemRepository) Update(ctx context.Context, item *models.Item) error {
|
||||||
|
query := `
|
||||||
|
UPDATE items
|
||||||
|
SET name = $3, description = $4, category = $5
|
||||||
|
WHERE id = $1 AND organization_id = $2
|
||||||
|
`
|
||||||
|
|
||||||
|
result, err := r.db.ExecContext(ctx, query,
|
||||||
|
item.ID,
|
||||||
|
item.OrganizationID,
|
||||||
|
item.Name,
|
||||||
|
item.Description,
|
||||||
|
item.Category,
|
||||||
|
)
|
||||||
|
if err != nil {
|
||||||
|
return fmt.Errorf("failed to update item: %w", err)
|
||||||
|
}
|
||||||
|
|
||||||
|
rowsAffected, err := result.RowsAffected()
|
||||||
|
if err != nil {
|
||||||
|
return fmt.Errorf("failed to get rows affected: %w", err)
|
||||||
|
}
|
||||||
|
|
||||||
|
if rowsAffected == 0 {
|
||||||
|
return fmt.Errorf("item not found")
|
||||||
|
}
|
||||||
|
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
|
||||||
|
func (r *itemRepository) Delete(ctx context.Context, id uuid.UUID, orgID uuid.UUID) error {
|
||||||
|
query := `
|
||||||
|
DELETE FROM items
|
||||||
|
WHERE id = $1 AND organization_id = $2
|
||||||
|
`
|
||||||
|
|
||||||
|
result, err := r.db.ExecContext(ctx, query, id, orgID)
|
||||||
|
if err != nil {
|
||||||
|
return fmt.Errorf("failed to delete item: %w", err)
|
||||||
|
}
|
||||||
|
|
||||||
|
rowsAffected, err := result.RowsAffected()
|
||||||
|
if err != nil {
|
||||||
|
return fmt.Errorf("failed to get rows affected: %w", err)
|
||||||
|
}
|
||||||
|
|
||||||
|
if rowsAffected == 0 {
|
||||||
|
return fmt.Errorf("item not found")
|
||||||
|
}
|
||||||
|
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
|
||||||
|
func (r *itemRepository) Search(ctx context.Context, orgID uuid.UUID, query string, category string) ([]*models.Item, error) {
|
||||||
|
baseQuery := `
|
||||||
|
SELECT id, organization_id, name, description, category, created_at
|
||||||
|
FROM items
|
||||||
|
WHERE organization_id = $1
|
||||||
|
`
|
||||||
|
|
||||||
|
var args []interface{}
|
||||||
|
args = append(args, orgID)
|
||||||
|
argIndex := 2
|
||||||
|
|
||||||
|
if query != "" {
|
||||||
|
baseQuery += fmt.Sprintf(" AND (name ILIKE $%d OR description ILIKE $%d)", argIndex, argIndex)
|
||||||
|
args = append(args, "%"+query+"%")
|
||||||
|
argIndex++
|
||||||
|
}
|
||||||
|
|
||||||
|
if category != "" {
|
||||||
|
baseQuery += fmt.Sprintf(" AND category = $%d", argIndex)
|
||||||
|
args = append(args, category)
|
||||||
|
argIndex++
|
||||||
|
}
|
||||||
|
|
||||||
|
baseQuery += " ORDER BY name"
|
||||||
|
|
||||||
|
rows, err := r.db.QueryContext(ctx, baseQuery, args...)
|
||||||
|
if err != nil {
|
||||||
|
return nil, fmt.Errorf("failed to search items: %w", err)
|
||||||
|
}
|
||||||
|
defer rows.Close()
|
||||||
|
|
||||||
|
var items []*models.Item
|
||||||
|
for rows.Next() {
|
||||||
|
item := &models.Item{}
|
||||||
|
err := rows.Scan(
|
||||||
|
&item.ID,
|
||||||
|
&item.OrganizationID,
|
||||||
|
&item.Name,
|
||||||
|
&item.Description,
|
||||||
|
&item.Category,
|
||||||
|
&item.CreatedAt,
|
||||||
|
)
|
||||||
|
if err != nil {
|
||||||
|
return nil, fmt.Errorf("failed to scan item: %w", err)
|
||||||
|
}
|
||||||
|
|
||||||
|
items = append(items, item)
|
||||||
|
}
|
||||||
|
|
||||||
|
if err = rows.Err(); err != nil {
|
||||||
|
return nil, fmt.Errorf("error iterating search results: %w", err)
|
||||||
|
}
|
||||||
|
|
||||||
|
return items, nil
|
||||||
|
}
|
||||||
271
core-service/internal/repository/locations.go
Normal file
271
core-service/internal/repository/locations.go
Normal file
@@ -0,0 +1,271 @@
|
|||||||
|
package repository
|
||||||
|
|
||||||
|
import (
|
||||||
|
"context"
|
||||||
|
"database/sql"
|
||||||
|
"encoding/json"
|
||||||
|
"fmt"
|
||||||
|
|
||||||
|
"erp-mvp/core-service/internal/models"
|
||||||
|
|
||||||
|
"github.com/google/uuid"
|
||||||
|
)
|
||||||
|
|
||||||
|
type LocationRepository interface {
|
||||||
|
Create(ctx context.Context, location *models.StorageLocation) error
|
||||||
|
GetByID(ctx context.Context, id uuid.UUID, orgID uuid.UUID) (*models.StorageLocation, error)
|
||||||
|
GetByOrganization(ctx context.Context, orgID uuid.UUID) ([]*models.StorageLocation, error)
|
||||||
|
Update(ctx context.Context, location *models.StorageLocation) error
|
||||||
|
Delete(ctx context.Context, id uuid.UUID, orgID uuid.UUID) error
|
||||||
|
GetChildren(ctx context.Context, parentID uuid.UUID, orgID uuid.UUID) ([]*models.StorageLocation, error)
|
||||||
|
}
|
||||||
|
|
||||||
|
type locationRepository struct {
|
||||||
|
db *sql.DB
|
||||||
|
}
|
||||||
|
|
||||||
|
func NewLocationRepository(db *sql.DB) LocationRepository {
|
||||||
|
return &locationRepository{db: db}
|
||||||
|
}
|
||||||
|
|
||||||
|
func (r *locationRepository) Create(ctx context.Context, location *models.StorageLocation) error {
|
||||||
|
query := `
|
||||||
|
INSERT INTO storage_locations (id, organization_id, parent_id, name, address, type, coordinates, created_at)
|
||||||
|
VALUES ($1, $2, $3, $4, $5, $6, $7, $8)
|
||||||
|
`
|
||||||
|
|
||||||
|
// Конвертируем JSON в строку
|
||||||
|
var coordinatesJSON string
|
||||||
|
if location.Coordinates != nil {
|
||||||
|
coords, err := json.Marshal(location.Coordinates)
|
||||||
|
if err != nil {
|
||||||
|
return fmt.Errorf("failed to marshal coordinates: %w", err)
|
||||||
|
}
|
||||||
|
coordinatesJSON = string(coords)
|
||||||
|
}
|
||||||
|
|
||||||
|
_, err := r.db.ExecContext(ctx, query,
|
||||||
|
location.ID,
|
||||||
|
location.OrganizationID,
|
||||||
|
location.ParentID,
|
||||||
|
location.Name,
|
||||||
|
location.Address,
|
||||||
|
location.Type,
|
||||||
|
coordinatesJSON,
|
||||||
|
location.CreatedAt,
|
||||||
|
)
|
||||||
|
if err != nil {
|
||||||
|
return fmt.Errorf("failed to create storage location: %w", err)
|
||||||
|
}
|
||||||
|
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
|
||||||
|
func (r *locationRepository) GetByID(ctx context.Context, id uuid.UUID, orgID uuid.UUID) (*models.StorageLocation, error) {
|
||||||
|
query := `
|
||||||
|
SELECT id, organization_id, parent_id, name, address, type, coordinates, created_at
|
||||||
|
FROM storage_locations
|
||||||
|
WHERE id = $1 AND organization_id = $2
|
||||||
|
`
|
||||||
|
|
||||||
|
var coordinatesJSON []byte
|
||||||
|
location := &models.StorageLocation{}
|
||||||
|
err := r.db.QueryRowContext(ctx, query, id, orgID).Scan(
|
||||||
|
&location.ID,
|
||||||
|
&location.OrganizationID,
|
||||||
|
&location.ParentID,
|
||||||
|
&location.Name,
|
||||||
|
&location.Address,
|
||||||
|
&location.Type,
|
||||||
|
&coordinatesJSON,
|
||||||
|
&location.CreatedAt,
|
||||||
|
)
|
||||||
|
|
||||||
|
if err != nil {
|
||||||
|
if err == sql.ErrNoRows {
|
||||||
|
return nil, fmt.Errorf("storage location not found")
|
||||||
|
}
|
||||||
|
return nil, fmt.Errorf("failed to get storage location: %w", err)
|
||||||
|
}
|
||||||
|
|
||||||
|
// Конвертируем JSON строку в map
|
||||||
|
if len(coordinatesJSON) > 0 {
|
||||||
|
err = json.Unmarshal(coordinatesJSON, &location.Coordinates)
|
||||||
|
if err != nil {
|
||||||
|
return nil, fmt.Errorf("failed to unmarshal coordinates: %w", err)
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
location.Coordinates = make(models.JSON)
|
||||||
|
}
|
||||||
|
|
||||||
|
return location, nil
|
||||||
|
}
|
||||||
|
|
||||||
|
func (r *locationRepository) GetByOrganization(ctx context.Context, orgID uuid.UUID) ([]*models.StorageLocation, error) {
|
||||||
|
query := `
|
||||||
|
SELECT id, organization_id, parent_id, name, address, type, coordinates, created_at
|
||||||
|
FROM storage_locations
|
||||||
|
WHERE organization_id = $1
|
||||||
|
ORDER BY name
|
||||||
|
`
|
||||||
|
|
||||||
|
rows, err := r.db.QueryContext(ctx, query, orgID)
|
||||||
|
if err != nil {
|
||||||
|
return nil, fmt.Errorf("failed to query storage locations: %w", err)
|
||||||
|
}
|
||||||
|
defer rows.Close()
|
||||||
|
|
||||||
|
var locations []*models.StorageLocation
|
||||||
|
for rows.Next() {
|
||||||
|
var coordinatesJSON []byte
|
||||||
|
location := &models.StorageLocation{}
|
||||||
|
err := rows.Scan(
|
||||||
|
&location.ID,
|
||||||
|
&location.OrganizationID,
|
||||||
|
&location.ParentID,
|
||||||
|
&location.Name,
|
||||||
|
&location.Address,
|
||||||
|
&location.Type,
|
||||||
|
&coordinatesJSON,
|
||||||
|
&location.CreatedAt,
|
||||||
|
)
|
||||||
|
if err != nil {
|
||||||
|
return nil, fmt.Errorf("failed to scan storage location: %w", err)
|
||||||
|
}
|
||||||
|
|
||||||
|
// Конвертируем JSON строку в map
|
||||||
|
if len(coordinatesJSON) > 0 {
|
||||||
|
err = json.Unmarshal(coordinatesJSON, &location.Coordinates)
|
||||||
|
if err != nil {
|
||||||
|
return nil, fmt.Errorf("failed to unmarshal coordinates: %w", err)
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
location.Coordinates = make(models.JSON)
|
||||||
|
}
|
||||||
|
|
||||||
|
locations = append(locations, location)
|
||||||
|
}
|
||||||
|
|
||||||
|
if err = rows.Err(); err != nil {
|
||||||
|
return nil, fmt.Errorf("error iterating storage locations: %w", err)
|
||||||
|
}
|
||||||
|
|
||||||
|
return locations, nil
|
||||||
|
}
|
||||||
|
|
||||||
|
func (r *locationRepository) Update(ctx context.Context, location *models.StorageLocation) error {
|
||||||
|
query := `
|
||||||
|
UPDATE storage_locations
|
||||||
|
SET parent_id = $3, name = $4, address = $5, type = $6, coordinates = $7
|
||||||
|
WHERE id = $1 AND organization_id = $2
|
||||||
|
`
|
||||||
|
|
||||||
|
// Конвертируем JSON в строку
|
||||||
|
var coordinatesJSON string
|
||||||
|
if location.Coordinates != nil {
|
||||||
|
coords, err := json.Marshal(location.Coordinates)
|
||||||
|
if err != nil {
|
||||||
|
return fmt.Errorf("failed to marshal coordinates: %w", err)
|
||||||
|
}
|
||||||
|
coordinatesJSON = string(coords)
|
||||||
|
}
|
||||||
|
|
||||||
|
result, err := r.db.ExecContext(ctx, query,
|
||||||
|
location.ID,
|
||||||
|
location.OrganizationID,
|
||||||
|
location.ParentID,
|
||||||
|
location.Name,
|
||||||
|
location.Address,
|
||||||
|
location.Type,
|
||||||
|
coordinatesJSON,
|
||||||
|
)
|
||||||
|
if err != nil {
|
||||||
|
return fmt.Errorf("failed to update storage location: %w", err)
|
||||||
|
}
|
||||||
|
|
||||||
|
rowsAffected, err := result.RowsAffected()
|
||||||
|
if err != nil {
|
||||||
|
return fmt.Errorf("failed to get rows affected: %w", err)
|
||||||
|
}
|
||||||
|
|
||||||
|
if rowsAffected == 0 {
|
||||||
|
return fmt.Errorf("storage location not found")
|
||||||
|
}
|
||||||
|
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
|
||||||
|
func (r *locationRepository) Delete(ctx context.Context, id uuid.UUID, orgID uuid.UUID) error {
|
||||||
|
query := `
|
||||||
|
DELETE FROM storage_locations
|
||||||
|
WHERE id = $1 AND organization_id = $2
|
||||||
|
`
|
||||||
|
|
||||||
|
result, err := r.db.ExecContext(ctx, query, id, orgID)
|
||||||
|
if err != nil {
|
||||||
|
return fmt.Errorf("failed to delete storage location: %w", err)
|
||||||
|
}
|
||||||
|
|
||||||
|
rowsAffected, err := result.RowsAffected()
|
||||||
|
if err != nil {
|
||||||
|
return fmt.Errorf("failed to get rows affected: %w", err)
|
||||||
|
}
|
||||||
|
|
||||||
|
if rowsAffected == 0 {
|
||||||
|
return fmt.Errorf("storage location not found")
|
||||||
|
}
|
||||||
|
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
|
||||||
|
func (r *locationRepository) GetChildren(ctx context.Context, parentID uuid.UUID, orgID uuid.UUID) ([]*models.StorageLocation, error) {
|
||||||
|
query := `
|
||||||
|
SELECT id, organization_id, parent_id, name, address, type, coordinates, created_at
|
||||||
|
FROM storage_locations
|
||||||
|
WHERE parent_id = $1 AND organization_id = $2
|
||||||
|
ORDER BY name
|
||||||
|
`
|
||||||
|
|
||||||
|
rows, err := r.db.QueryContext(ctx, query, parentID, orgID)
|
||||||
|
if err != nil {
|
||||||
|
return nil, fmt.Errorf("failed to query child locations: %w", err)
|
||||||
|
}
|
||||||
|
defer rows.Close()
|
||||||
|
|
||||||
|
var locations []*models.StorageLocation
|
||||||
|
for rows.Next() {
|
||||||
|
var coordinatesJSON []byte
|
||||||
|
location := &models.StorageLocation{}
|
||||||
|
err := rows.Scan(
|
||||||
|
&location.ID,
|
||||||
|
&location.OrganizationID,
|
||||||
|
&location.ParentID,
|
||||||
|
&location.Name,
|
||||||
|
&location.Address,
|
||||||
|
&location.Type,
|
||||||
|
&coordinatesJSON,
|
||||||
|
&location.CreatedAt,
|
||||||
|
)
|
||||||
|
if err != nil {
|
||||||
|
return nil, fmt.Errorf("failed to scan child location: %w", err)
|
||||||
|
}
|
||||||
|
|
||||||
|
// Конвертируем JSON строку в map
|
||||||
|
if len(coordinatesJSON) > 0 {
|
||||||
|
err = json.Unmarshal(coordinatesJSON, &location.Coordinates)
|
||||||
|
if err != nil {
|
||||||
|
return nil, fmt.Errorf("failed to unmarshal coordinates: %w", err)
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
location.Coordinates = make(models.JSON)
|
||||||
|
}
|
||||||
|
|
||||||
|
locations = append(locations, location)
|
||||||
|
}
|
||||||
|
|
||||||
|
if err = rows.Err(); err != nil {
|
||||||
|
return nil, fmt.Errorf("error iterating child locations: %w", err)
|
||||||
|
}
|
||||||
|
|
||||||
|
return locations, nil
|
||||||
|
}
|
||||||
316
core-service/internal/repository/operations.go
Normal file
316
core-service/internal/repository/operations.go
Normal file
@@ -0,0 +1,316 @@
|
|||||||
|
package repository
|
||||||
|
|
||||||
|
import (
|
||||||
|
"context"
|
||||||
|
"database/sql"
|
||||||
|
"encoding/json"
|
||||||
|
"fmt"
|
||||||
|
|
||||||
|
"erp-mvp/core-service/internal/models"
|
||||||
|
|
||||||
|
"github.com/google/uuid"
|
||||||
|
)
|
||||||
|
|
||||||
|
type OperationsRepository interface {
|
||||||
|
PlaceItem(ctx context.Context, placement *models.ItemPlacement) error
|
||||||
|
MoveItem(ctx context.Context, placementID uuid.UUID, newLocationID uuid.UUID, orgID uuid.UUID) error
|
||||||
|
GetByItem(ctx context.Context, itemID uuid.UUID, orgID uuid.UUID) ([]*models.ItemPlacement, error)
|
||||||
|
GetByLocation(ctx context.Context, locationID uuid.UUID, orgID uuid.UUID) ([]*models.ItemPlacement, error)
|
||||||
|
GetByID(ctx context.Context, id uuid.UUID, orgID uuid.UUID) (*models.ItemPlacement, error)
|
||||||
|
UpdateQuantity(ctx context.Context, id uuid.UUID, quantity int, orgID uuid.UUID) error
|
||||||
|
Delete(ctx context.Context, id uuid.UUID, orgID uuid.UUID) error
|
||||||
|
Search(ctx context.Context, orgID uuid.UUID, query string, category string, address string) ([]*models.ItemWithLocation, error)
|
||||||
|
}
|
||||||
|
|
||||||
|
type operationsRepository struct {
|
||||||
|
db *sql.DB
|
||||||
|
}
|
||||||
|
|
||||||
|
func NewOperationsRepository(db *sql.DB) OperationsRepository {
|
||||||
|
return &operationsRepository{db: db}
|
||||||
|
}
|
||||||
|
|
||||||
|
func (r *operationsRepository) PlaceItem(ctx context.Context, placement *models.ItemPlacement) error {
|
||||||
|
query := `
|
||||||
|
INSERT INTO item_placements (id, organization_id, item_id, location_id, quantity, created_at)
|
||||||
|
VALUES ($1, $2, $3, $4, $5, $6)
|
||||||
|
`
|
||||||
|
|
||||||
|
_, err := r.db.ExecContext(ctx, query,
|
||||||
|
placement.ID,
|
||||||
|
placement.OrganizationID,
|
||||||
|
placement.ItemID,
|
||||||
|
placement.LocationID,
|
||||||
|
placement.Quantity,
|
||||||
|
placement.CreatedAt,
|
||||||
|
)
|
||||||
|
if err != nil {
|
||||||
|
return fmt.Errorf("failed to place item: %w", err)
|
||||||
|
}
|
||||||
|
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
|
||||||
|
func (r *operationsRepository) MoveItem(ctx context.Context, placementID uuid.UUID, newLocationID uuid.UUID, orgID uuid.UUID) error {
|
||||||
|
query := `
|
||||||
|
UPDATE item_placements
|
||||||
|
SET location_id = $2
|
||||||
|
WHERE id = $1 AND organization_id = $3
|
||||||
|
`
|
||||||
|
|
||||||
|
result, err := r.db.ExecContext(ctx, query, placementID, newLocationID, orgID)
|
||||||
|
if err != nil {
|
||||||
|
return fmt.Errorf("failed to move item: %w", err)
|
||||||
|
}
|
||||||
|
|
||||||
|
rowsAffected, err := result.RowsAffected()
|
||||||
|
if err != nil {
|
||||||
|
return fmt.Errorf("failed to get rows affected: %w", err)
|
||||||
|
}
|
||||||
|
|
||||||
|
if rowsAffected == 0 {
|
||||||
|
return fmt.Errorf("item placement not found")
|
||||||
|
}
|
||||||
|
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
|
||||||
|
func (r *operationsRepository) GetByItem(ctx context.Context, itemID uuid.UUID, orgID uuid.UUID) ([]*models.ItemPlacement, error) {
|
||||||
|
query := `
|
||||||
|
SELECT id, organization_id, item_id, location_id, quantity, created_at
|
||||||
|
FROM item_placements
|
||||||
|
WHERE item_id = $1 AND organization_id = $2
|
||||||
|
ORDER BY created_at DESC
|
||||||
|
`
|
||||||
|
|
||||||
|
rows, err := r.db.QueryContext(ctx, query, itemID, orgID)
|
||||||
|
if err != nil {
|
||||||
|
return nil, fmt.Errorf("failed to query item placements: %w", err)
|
||||||
|
}
|
||||||
|
defer rows.Close()
|
||||||
|
|
||||||
|
var placements []*models.ItemPlacement
|
||||||
|
for rows.Next() {
|
||||||
|
placement := &models.ItemPlacement{}
|
||||||
|
err := rows.Scan(
|
||||||
|
&placement.ID,
|
||||||
|
&placement.OrganizationID,
|
||||||
|
&placement.ItemID,
|
||||||
|
&placement.LocationID,
|
||||||
|
&placement.Quantity,
|
||||||
|
&placement.CreatedAt,
|
||||||
|
)
|
||||||
|
if err != nil {
|
||||||
|
return nil, fmt.Errorf("failed to scan item placement: %w", err)
|
||||||
|
}
|
||||||
|
|
||||||
|
placements = append(placements, placement)
|
||||||
|
}
|
||||||
|
|
||||||
|
if err = rows.Err(); err != nil {
|
||||||
|
return nil, fmt.Errorf("error iterating item placements: %w", err)
|
||||||
|
}
|
||||||
|
|
||||||
|
return placements, nil
|
||||||
|
}
|
||||||
|
|
||||||
|
func (r *operationsRepository) GetByLocation(ctx context.Context, locationID uuid.UUID, orgID uuid.UUID) ([]*models.ItemPlacement, error) {
|
||||||
|
query := `
|
||||||
|
SELECT id, organization_id, item_id, location_id, quantity, created_at
|
||||||
|
FROM item_placements
|
||||||
|
WHERE location_id = $1 AND organization_id = $2
|
||||||
|
ORDER BY created_at DESC
|
||||||
|
`
|
||||||
|
|
||||||
|
rows, err := r.db.QueryContext(ctx, query, locationID, orgID)
|
||||||
|
if err != nil {
|
||||||
|
return nil, fmt.Errorf("failed to query location placements: %w", err)
|
||||||
|
}
|
||||||
|
defer rows.Close()
|
||||||
|
|
||||||
|
var placements []*models.ItemPlacement
|
||||||
|
for rows.Next() {
|
||||||
|
placement := &models.ItemPlacement{}
|
||||||
|
err := rows.Scan(
|
||||||
|
&placement.ID,
|
||||||
|
&placement.OrganizationID,
|
||||||
|
&placement.ItemID,
|
||||||
|
&placement.LocationID,
|
||||||
|
&placement.Quantity,
|
||||||
|
&placement.CreatedAt,
|
||||||
|
)
|
||||||
|
if err != nil {
|
||||||
|
return nil, fmt.Errorf("failed to scan item placement: %w", err)
|
||||||
|
}
|
||||||
|
|
||||||
|
placements = append(placements, placement)
|
||||||
|
}
|
||||||
|
|
||||||
|
if err = rows.Err(); err != nil {
|
||||||
|
return nil, fmt.Errorf("error iterating location placements: %w", err)
|
||||||
|
}
|
||||||
|
|
||||||
|
return placements, nil
|
||||||
|
}
|
||||||
|
|
||||||
|
func (r *operationsRepository) GetByID(ctx context.Context, id uuid.UUID, orgID uuid.UUID) (*models.ItemPlacement, error) {
|
||||||
|
query := `
|
||||||
|
SELECT id, organization_id, item_id, location_id, quantity, created_at
|
||||||
|
FROM item_placements
|
||||||
|
WHERE id = $1 AND organization_id = $2
|
||||||
|
`
|
||||||
|
|
||||||
|
placement := &models.ItemPlacement{}
|
||||||
|
err := r.db.QueryRowContext(ctx, query, id, orgID).Scan(
|
||||||
|
&placement.ID,
|
||||||
|
&placement.OrganizationID,
|
||||||
|
&placement.ItemID,
|
||||||
|
&placement.LocationID,
|
||||||
|
&placement.Quantity,
|
||||||
|
&placement.CreatedAt,
|
||||||
|
)
|
||||||
|
|
||||||
|
if err != nil {
|
||||||
|
if err == sql.ErrNoRows {
|
||||||
|
return nil, fmt.Errorf("item placement not found")
|
||||||
|
}
|
||||||
|
return nil, fmt.Errorf("failed to get item placement: %w", err)
|
||||||
|
}
|
||||||
|
|
||||||
|
return placement, nil
|
||||||
|
}
|
||||||
|
|
||||||
|
func (r *operationsRepository) UpdateQuantity(ctx context.Context, id uuid.UUID, quantity int, orgID uuid.UUID) error {
|
||||||
|
query := `
|
||||||
|
UPDATE item_placements
|
||||||
|
SET quantity = $2
|
||||||
|
WHERE id = $1 AND organization_id = $3
|
||||||
|
`
|
||||||
|
|
||||||
|
result, err := r.db.ExecContext(ctx, query, id, quantity, orgID)
|
||||||
|
if err != nil {
|
||||||
|
return fmt.Errorf("failed to update quantity: %w", err)
|
||||||
|
}
|
||||||
|
|
||||||
|
rowsAffected, err := result.RowsAffected()
|
||||||
|
if err != nil {
|
||||||
|
return fmt.Errorf("failed to get rows affected: %w", err)
|
||||||
|
}
|
||||||
|
|
||||||
|
if rowsAffected == 0 {
|
||||||
|
return fmt.Errorf("item placement not found")
|
||||||
|
}
|
||||||
|
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
|
||||||
|
func (r *operationsRepository) Delete(ctx context.Context, id uuid.UUID, orgID uuid.UUID) error {
|
||||||
|
query := `
|
||||||
|
DELETE FROM item_placements
|
||||||
|
WHERE id = $1 AND organization_id = $2
|
||||||
|
`
|
||||||
|
|
||||||
|
result, err := r.db.ExecContext(ctx, query, id, orgID)
|
||||||
|
if err != nil {
|
||||||
|
return fmt.Errorf("failed to delete item placement: %w", err)
|
||||||
|
}
|
||||||
|
|
||||||
|
rowsAffected, err := result.RowsAffected()
|
||||||
|
if err != nil {
|
||||||
|
return fmt.Errorf("failed to get rows affected: %w", err)
|
||||||
|
}
|
||||||
|
|
||||||
|
if rowsAffected == 0 {
|
||||||
|
return fmt.Errorf("item placement not found")
|
||||||
|
}
|
||||||
|
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
|
||||||
|
func (r *operationsRepository) Search(ctx context.Context, orgID uuid.UUID, query string, category string, address string) ([]*models.ItemWithLocation, error) {
|
||||||
|
baseQuery := `
|
||||||
|
SELECT
|
||||||
|
i.id, i.organization_id, i.name, i.description, i.category, i.created_at,
|
||||||
|
sl.id, sl.organization_id, sl.parent_id, sl.name, sl.address, sl.type, sl.coordinates, sl.created_at,
|
||||||
|
ip.quantity
|
||||||
|
FROM items i
|
||||||
|
JOIN item_placements ip ON i.id = ip.item_id
|
||||||
|
JOIN storage_locations sl ON ip.location_id = sl.id
|
||||||
|
WHERE i.organization_id = $1 AND sl.organization_id = $1
|
||||||
|
`
|
||||||
|
|
||||||
|
var args []interface{}
|
||||||
|
args = append(args, orgID)
|
||||||
|
argIndex := 2
|
||||||
|
|
||||||
|
if query != "" {
|
||||||
|
baseQuery += fmt.Sprintf(" AND (i.name ILIKE $%d OR i.description ILIKE $%d)", argIndex, argIndex)
|
||||||
|
args = append(args, "%"+query+"%")
|
||||||
|
argIndex++
|
||||||
|
}
|
||||||
|
|
||||||
|
if category != "" {
|
||||||
|
baseQuery += fmt.Sprintf(" AND i.category = $%d", argIndex)
|
||||||
|
args = append(args, category)
|
||||||
|
argIndex++
|
||||||
|
}
|
||||||
|
|
||||||
|
if address != "" {
|
||||||
|
baseQuery += fmt.Sprintf(" AND sl.address ILIKE $%d", argIndex)
|
||||||
|
args = append(args, "%"+address+"%")
|
||||||
|
argIndex++
|
||||||
|
}
|
||||||
|
|
||||||
|
baseQuery += " ORDER BY i.name, sl.name"
|
||||||
|
|
||||||
|
rows, err := r.db.QueryContext(ctx, baseQuery, args...)
|
||||||
|
if err != nil {
|
||||||
|
return nil, fmt.Errorf("failed to search items with locations: %w", err)
|
||||||
|
}
|
||||||
|
defer rows.Close()
|
||||||
|
|
||||||
|
var results []*models.ItemWithLocation
|
||||||
|
for rows.Next() {
|
||||||
|
var coordinatesJSON []byte
|
||||||
|
itemWithLocation := &models.ItemWithLocation{}
|
||||||
|
|
||||||
|
err := rows.Scan(
|
||||||
|
&itemWithLocation.Item.ID,
|
||||||
|
&itemWithLocation.Item.OrganizationID,
|
||||||
|
&itemWithLocation.Item.Name,
|
||||||
|
&itemWithLocation.Item.Description,
|
||||||
|
&itemWithLocation.Item.Category,
|
||||||
|
&itemWithLocation.Item.CreatedAt,
|
||||||
|
&itemWithLocation.Location.ID,
|
||||||
|
&itemWithLocation.Location.OrganizationID,
|
||||||
|
&itemWithLocation.Location.ParentID,
|
||||||
|
&itemWithLocation.Location.Name,
|
||||||
|
&itemWithLocation.Location.Address,
|
||||||
|
&itemWithLocation.Location.Type,
|
||||||
|
&coordinatesJSON,
|
||||||
|
&itemWithLocation.Location.CreatedAt,
|
||||||
|
&itemWithLocation.Quantity,
|
||||||
|
)
|
||||||
|
if err != nil {
|
||||||
|
return nil, fmt.Errorf("failed to scan item with location: %w", err)
|
||||||
|
}
|
||||||
|
|
||||||
|
// Конвертируем JSON строку в map
|
||||||
|
if len(coordinatesJSON) > 0 {
|
||||||
|
err = json.Unmarshal(coordinatesJSON, &itemWithLocation.Location.Coordinates)
|
||||||
|
if err != nil {
|
||||||
|
return nil, fmt.Errorf("failed to unmarshal coordinates: %w", err)
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
itemWithLocation.Location.Coordinates = make(models.JSON)
|
||||||
|
}
|
||||||
|
|
||||||
|
results = append(results, itemWithLocation)
|
||||||
|
}
|
||||||
|
|
||||||
|
if err = rows.Err(); err != nil {
|
||||||
|
return nil, fmt.Errorf("error iterating search results: %w", err)
|
||||||
|
}
|
||||||
|
|
||||||
|
return results, nil
|
||||||
|
}
|
||||||
113
core-service/internal/repository/organizations.go
Normal file
113
core-service/internal/repository/organizations.go
Normal file
@@ -0,0 +1,113 @@
|
|||||||
|
package repository
|
||||||
|
|
||||||
|
import (
|
||||||
|
"context"
|
||||||
|
"database/sql"
|
||||||
|
"encoding/json"
|
||||||
|
"fmt"
|
||||||
|
|
||||||
|
"erp-mvp/core-service/internal/models"
|
||||||
|
|
||||||
|
"github.com/google/uuid"
|
||||||
|
)
|
||||||
|
|
||||||
|
type OrganizationRepository interface {
|
||||||
|
Create(ctx context.Context, org *models.Organization) error
|
||||||
|
GetByID(ctx context.Context, id uuid.UUID) (*models.Organization, error)
|
||||||
|
Update(ctx context.Context, org *models.Organization) error
|
||||||
|
}
|
||||||
|
|
||||||
|
type organizationRepository struct {
|
||||||
|
db *sql.DB
|
||||||
|
}
|
||||||
|
|
||||||
|
func NewOrganizationRepository(db *sql.DB) OrganizationRepository {
|
||||||
|
return &organizationRepository{db: db}
|
||||||
|
}
|
||||||
|
|
||||||
|
func (r *organizationRepository) Create(ctx context.Context, org *models.Organization) error {
|
||||||
|
query := `
|
||||||
|
INSERT INTO organizations (id, name, type, settings, created_at)
|
||||||
|
VALUES ($1, $2, $3, $4, $5)
|
||||||
|
`
|
||||||
|
|
||||||
|
// Конвертируем JSON в строку
|
||||||
|
settingsJSON, err := json.Marshal(org.Settings)
|
||||||
|
if err != nil {
|
||||||
|
return fmt.Errorf("failed to marshal settings: %w", err)
|
||||||
|
}
|
||||||
|
|
||||||
|
_, err = r.db.ExecContext(ctx, query, org.ID, org.Name, org.Type, string(settingsJSON), org.CreatedAt)
|
||||||
|
if err != nil {
|
||||||
|
return fmt.Errorf("failed to create organization: %w", err)
|
||||||
|
}
|
||||||
|
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
|
||||||
|
func (r *organizationRepository) GetByID(ctx context.Context, id uuid.UUID) (*models.Organization, error) {
|
||||||
|
query := `
|
||||||
|
SELECT id, name, type, settings, created_at
|
||||||
|
FROM organizations
|
||||||
|
WHERE id = $1
|
||||||
|
`
|
||||||
|
|
||||||
|
var settingsJSON []byte
|
||||||
|
org := &models.Organization{}
|
||||||
|
err := r.db.QueryRowContext(ctx, query, id).Scan(
|
||||||
|
&org.ID,
|
||||||
|
&org.Name,
|
||||||
|
&org.Type,
|
||||||
|
&settingsJSON,
|
||||||
|
&org.CreatedAt,
|
||||||
|
)
|
||||||
|
|
||||||
|
if err != nil {
|
||||||
|
if err == sql.ErrNoRows {
|
||||||
|
return nil, fmt.Errorf("organization not found")
|
||||||
|
}
|
||||||
|
return nil, fmt.Errorf("failed to get organization: %w", err)
|
||||||
|
}
|
||||||
|
|
||||||
|
// Конвертируем JSON строку в map
|
||||||
|
if len(settingsJSON) > 0 {
|
||||||
|
err = json.Unmarshal(settingsJSON, &org.Settings)
|
||||||
|
if err != nil {
|
||||||
|
return nil, fmt.Errorf("failed to unmarshal settings: %w", err)
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
org.Settings = make(models.JSON)
|
||||||
|
}
|
||||||
|
|
||||||
|
return org, nil
|
||||||
|
}
|
||||||
|
|
||||||
|
func (r *organizationRepository) Update(ctx context.Context, org *models.Organization) error {
|
||||||
|
query := `
|
||||||
|
UPDATE organizations
|
||||||
|
SET name = $2, type = $3, settings = $4
|
||||||
|
WHERE id = $1
|
||||||
|
`
|
||||||
|
|
||||||
|
// Конвертируем JSON в строку
|
||||||
|
settingsJSON, err := json.Marshal(org.Settings)
|
||||||
|
if err != nil {
|
||||||
|
return fmt.Errorf("failed to marshal settings: %w", err)
|
||||||
|
}
|
||||||
|
|
||||||
|
result, err := r.db.ExecContext(ctx, query, org.ID, org.Name, org.Type, string(settingsJSON))
|
||||||
|
if err != nil {
|
||||||
|
return fmt.Errorf("failed to update organization: %w", err)
|
||||||
|
}
|
||||||
|
|
||||||
|
rowsAffected, err := result.RowsAffected()
|
||||||
|
if err != nil {
|
||||||
|
return fmt.Errorf("failed to get rows affected: %w", err)
|
||||||
|
}
|
||||||
|
|
||||||
|
if rowsAffected == 0 {
|
||||||
|
return fmt.Errorf("organization not found")
|
||||||
|
}
|
||||||
|
|
||||||
|
return nil
|
||||||
|
}
|
||||||
99
core-service/internal/repository/users.go
Normal file
99
core-service/internal/repository/users.go
Normal file
@@ -0,0 +1,99 @@
|
|||||||
|
package repository
|
||||||
|
|
||||||
|
import (
|
||||||
|
"context"
|
||||||
|
"database/sql"
|
||||||
|
"fmt"
|
||||||
|
|
||||||
|
"erp-mvp/core-service/internal/models"
|
||||||
|
"github.com/google/uuid"
|
||||||
|
)
|
||||||
|
|
||||||
|
type UserRepository interface {
|
||||||
|
Create(ctx context.Context, user *models.User, password string) error
|
||||||
|
GetByEmail(ctx context.Context, email string) (*models.User, error)
|
||||||
|
GetByID(ctx context.Context, id uuid.UUID) (*models.User, error)
|
||||||
|
}
|
||||||
|
|
||||||
|
type userRepository struct {
|
||||||
|
db *sql.DB
|
||||||
|
}
|
||||||
|
|
||||||
|
func NewUserRepository(db *sql.DB) UserRepository {
|
||||||
|
return &userRepository{db: db}
|
||||||
|
}
|
||||||
|
|
||||||
|
func (r *userRepository) Create(ctx context.Context, user *models.User, password string) error {
|
||||||
|
query := `
|
||||||
|
INSERT INTO users (id, organization_id, email, password_hash, role, created_at)
|
||||||
|
VALUES ($1, $2, $3, $4, $5, $6)
|
||||||
|
`
|
||||||
|
|
||||||
|
_, err := r.db.ExecContext(ctx, query,
|
||||||
|
user.ID,
|
||||||
|
user.OrganizationID,
|
||||||
|
user.Email,
|
||||||
|
password,
|
||||||
|
user.Role,
|
||||||
|
user.CreatedAt,
|
||||||
|
)
|
||||||
|
if err != nil {
|
||||||
|
return fmt.Errorf("failed to create user: %w", err)
|
||||||
|
}
|
||||||
|
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
|
||||||
|
func (r *userRepository) GetByEmail(ctx context.Context, email string) (*models.User, error) {
|
||||||
|
query := `
|
||||||
|
SELECT id, organization_id, email, password_hash, role, created_at
|
||||||
|
FROM users
|
||||||
|
WHERE email = $1
|
||||||
|
`
|
||||||
|
|
||||||
|
user := &models.User{}
|
||||||
|
err := r.db.QueryRowContext(ctx, query, email).Scan(
|
||||||
|
&user.ID,
|
||||||
|
&user.OrganizationID,
|
||||||
|
&user.Email,
|
||||||
|
&user.PasswordHash,
|
||||||
|
&user.Role,
|
||||||
|
&user.CreatedAt,
|
||||||
|
)
|
||||||
|
|
||||||
|
if err != nil {
|
||||||
|
if err == sql.ErrNoRows {
|
||||||
|
return nil, fmt.Errorf("user not found")
|
||||||
|
}
|
||||||
|
return nil, fmt.Errorf("failed to get user: %w", err)
|
||||||
|
}
|
||||||
|
|
||||||
|
return user, nil
|
||||||
|
}
|
||||||
|
|
||||||
|
func (r *userRepository) GetByID(ctx context.Context, id uuid.UUID) (*models.User, error) {
|
||||||
|
query := `
|
||||||
|
SELECT id, organization_id, email, password_hash, role, created_at
|
||||||
|
FROM users
|
||||||
|
WHERE id = $1
|
||||||
|
`
|
||||||
|
|
||||||
|
user := &models.User{}
|
||||||
|
err := r.db.QueryRowContext(ctx, query, id).Scan(
|
||||||
|
&user.ID,
|
||||||
|
&user.OrganizationID,
|
||||||
|
&user.Email,
|
||||||
|
&user.PasswordHash,
|
||||||
|
&user.Role,
|
||||||
|
&user.CreatedAt,
|
||||||
|
)
|
||||||
|
|
||||||
|
if err != nil {
|
||||||
|
if err == sql.ErrNoRows {
|
||||||
|
return nil, fmt.Errorf("user not found")
|
||||||
|
}
|
||||||
|
return nil, fmt.Errorf("failed to get user: %w", err)
|
||||||
|
}
|
||||||
|
|
||||||
|
return user, nil
|
||||||
|
}
|
||||||
157
core-service/internal/service/auth_service.go
Normal file
157
core-service/internal/service/auth_service.go
Normal file
@@ -0,0 +1,157 @@
|
|||||||
|
package service
|
||||||
|
|
||||||
|
import (
|
||||||
|
"context"
|
||||||
|
"time"
|
||||||
|
|
||||||
|
"erp-mvp/core-service/internal/auth"
|
||||||
|
"erp-mvp/core-service/internal/models"
|
||||||
|
"erp-mvp/core-service/internal/repository"
|
||||||
|
|
||||||
|
"github.com/google/uuid"
|
||||||
|
"github.com/sirupsen/logrus"
|
||||||
|
)
|
||||||
|
|
||||||
|
type AuthService interface {
|
||||||
|
Register(ctx context.Context, req *models.RegisterRequest) (*models.LoginResponse, error)
|
||||||
|
Login(ctx context.Context, req *models.LoginRequest) (*models.LoginResponse, error)
|
||||||
|
}
|
||||||
|
|
||||||
|
type authService struct {
|
||||||
|
orgRepo repository.OrganizationRepository
|
||||||
|
userRepo repository.UserRepository
|
||||||
|
jwtService *auth.JWTService
|
||||||
|
logger *logrus.Logger
|
||||||
|
}
|
||||||
|
|
||||||
|
func NewAuthService(orgRepo repository.OrganizationRepository, userRepo repository.UserRepository, jwtService *auth.JWTService) AuthService {
|
||||||
|
return &authService{
|
||||||
|
orgRepo: orgRepo,
|
||||||
|
userRepo: userRepo,
|
||||||
|
jwtService: jwtService,
|
||||||
|
logger: logrus.New(),
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func (s *authService) Register(ctx context.Context, req *models.RegisterRequest) (*models.LoginResponse, error) {
|
||||||
|
s.logger.Info("Starting registration process")
|
||||||
|
|
||||||
|
// Проверяем, что пользователь с таким email не существует
|
||||||
|
existingUser, err := s.userRepo.GetByEmail(ctx, req.UserEmail)
|
||||||
|
if err == nil && existingUser != nil {
|
||||||
|
s.logger.Error("User with this email already exists")
|
||||||
|
return nil, &ValidationError{Message: "User with this email already exists"}
|
||||||
|
}
|
||||||
|
|
||||||
|
// Создаем организацию
|
||||||
|
orgID := uuid.New()
|
||||||
|
org := &models.Organization{
|
||||||
|
ID: orgID,
|
||||||
|
Name: req.OrganizationName,
|
||||||
|
Type: req.OrganizationType,
|
||||||
|
Settings: models.JSON{"created_at": time.Now().Unix()},
|
||||||
|
CreatedAt: time.Now(),
|
||||||
|
}
|
||||||
|
|
||||||
|
s.logger.Info("Creating organization with ID: ", orgID)
|
||||||
|
if err := s.orgRepo.Create(ctx, org); err != nil {
|
||||||
|
s.logger.Error("Failed to create organization: ", err)
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
s.logger.Info("Organization created successfully")
|
||||||
|
|
||||||
|
// Хешируем пароль
|
||||||
|
passwordHash, err := auth.HashPassword(req.UserPassword)
|
||||||
|
if err != nil {
|
||||||
|
s.logger.Error("Failed to hash password: ", err)
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
s.logger.Info("Password hashed successfully")
|
||||||
|
|
||||||
|
// Создаем пользователя
|
||||||
|
userID := uuid.New()
|
||||||
|
user := &models.User{
|
||||||
|
ID: userID,
|
||||||
|
OrganizationID: orgID,
|
||||||
|
Email: req.UserEmail,
|
||||||
|
Role: "admin", // Первый пользователь становится админом
|
||||||
|
CreatedAt: time.Now(),
|
||||||
|
}
|
||||||
|
|
||||||
|
s.logger.Info("Creating user with ID: ", userID)
|
||||||
|
if err := s.userRepo.Create(ctx, user, passwordHash); err != nil {
|
||||||
|
s.logger.Error("Failed to create user: ", err)
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
s.logger.Info("User created successfully")
|
||||||
|
|
||||||
|
// Генерируем JWT токен
|
||||||
|
token, err := s.jwtService.GenerateToken(user.ID, org.ID, user.Email, user.Role)
|
||||||
|
if err != nil {
|
||||||
|
s.logger.Error("Failed to generate token: ", err)
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
s.logger.Info("JWT token generated successfully")
|
||||||
|
|
||||||
|
return &models.LoginResponse{
|
||||||
|
Token: token,
|
||||||
|
User: models.UserResponse{
|
||||||
|
ID: user.ID,
|
||||||
|
Email: user.Email,
|
||||||
|
Role: user.Role,
|
||||||
|
},
|
||||||
|
Organization: models.OrganizationResponse{
|
||||||
|
ID: org.ID,
|
||||||
|
Name: org.Name,
|
||||||
|
Type: org.Type,
|
||||||
|
},
|
||||||
|
}, nil
|
||||||
|
}
|
||||||
|
|
||||||
|
func (s *authService) Login(ctx context.Context, req *models.LoginRequest) (*models.LoginResponse, error) {
|
||||||
|
// Получаем пользователя по email
|
||||||
|
user, err := s.userRepo.GetByEmail(ctx, req.Email)
|
||||||
|
if err != nil {
|
||||||
|
return nil, &ValidationError{Message: "Invalid email or password"}
|
||||||
|
}
|
||||||
|
|
||||||
|
// Проверяем пароль
|
||||||
|
if !auth.CheckPassword(req.Password, user.PasswordHash) {
|
||||||
|
return nil, &ValidationError{Message: "Invalid email or password"}
|
||||||
|
}
|
||||||
|
|
||||||
|
// Генерируем JWT токен
|
||||||
|
token, err := s.jwtService.GenerateToken(user.ID, user.OrganizationID, user.Email, user.Role)
|
||||||
|
if err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
|
||||||
|
// Получаем организацию для ответа
|
||||||
|
org, err := s.orgRepo.GetByID(ctx, user.OrganizationID)
|
||||||
|
if err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
|
||||||
|
return &models.LoginResponse{
|
||||||
|
Token: token,
|
||||||
|
User: models.UserResponse{
|
||||||
|
ID: user.ID,
|
||||||
|
Email: user.Email,
|
||||||
|
Role: user.Role,
|
||||||
|
},
|
||||||
|
Organization: models.OrganizationResponse{
|
||||||
|
ID: org.ID,
|
||||||
|
Name: org.Name,
|
||||||
|
Type: org.Type,
|
||||||
|
},
|
||||||
|
}, nil
|
||||||
|
}
|
||||||
|
|
||||||
|
// ValidationError ошибка валидации
|
||||||
|
type ValidationError struct {
|
||||||
|
Message string
|
||||||
|
}
|
||||||
|
|
||||||
|
func (e *ValidationError) Error() string {
|
||||||
|
return e.Message
|
||||||
|
}
|
||||||
126
core-service/internal/service/item_service.go
Normal file
126
core-service/internal/service/item_service.go
Normal file
@@ -0,0 +1,126 @@
|
|||||||
|
package service
|
||||||
|
|
||||||
|
import (
|
||||||
|
"context"
|
||||||
|
"time"
|
||||||
|
|
||||||
|
"erp-mvp/core-service/internal/models"
|
||||||
|
"erp-mvp/core-service/internal/repository"
|
||||||
|
|
||||||
|
"github.com/google/uuid"
|
||||||
|
"github.com/sirupsen/logrus"
|
||||||
|
)
|
||||||
|
|
||||||
|
type ItemService interface {
|
||||||
|
CreateItem(ctx context.Context, orgID uuid.UUID, req *models.CreateItemRequest) (*models.Item, error)
|
||||||
|
GetItem(ctx context.Context, id uuid.UUID, orgID uuid.UUID) (*models.Item, error)
|
||||||
|
GetItems(ctx context.Context, orgID uuid.UUID) ([]*models.Item, error)
|
||||||
|
UpdateItem(ctx context.Context, id uuid.UUID, orgID uuid.UUID, req *models.CreateItemRequest) (*models.Item, error)
|
||||||
|
DeleteItem(ctx context.Context, id uuid.UUID, orgID uuid.UUID) error
|
||||||
|
SearchItems(ctx context.Context, orgID uuid.UUID, query string, category string) ([]*models.Item, error)
|
||||||
|
}
|
||||||
|
|
||||||
|
type itemService struct {
|
||||||
|
itemRepo repository.ItemRepository
|
||||||
|
logger *logrus.Logger
|
||||||
|
}
|
||||||
|
|
||||||
|
func NewItemService(itemRepo repository.ItemRepository) ItemService {
|
||||||
|
return &itemService{
|
||||||
|
itemRepo: itemRepo,
|
||||||
|
logger: logrus.New(),
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func (s *itemService) CreateItem(ctx context.Context, orgID uuid.UUID, req *models.CreateItemRequest) (*models.Item, error) {
|
||||||
|
s.logger.Info("Creating item for organization: ", orgID)
|
||||||
|
|
||||||
|
item := &models.Item{
|
||||||
|
ID: uuid.New(),
|
||||||
|
OrganizationID: orgID,
|
||||||
|
Name: req.Name,
|
||||||
|
Description: req.Description,
|
||||||
|
Category: req.Category,
|
||||||
|
CreatedAt: time.Now(),
|
||||||
|
}
|
||||||
|
|
||||||
|
if err := s.itemRepo.Create(ctx, item); err != nil {
|
||||||
|
s.logger.Error("Failed to create item: ", err)
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
|
||||||
|
s.logger.Info("Item created successfully: ", item.ID)
|
||||||
|
return item, nil
|
||||||
|
}
|
||||||
|
|
||||||
|
func (s *itemService) GetItem(ctx context.Context, id uuid.UUID, orgID uuid.UUID) (*models.Item, error) {
|
||||||
|
s.logger.Info("Getting item: ", id, " for organization: ", orgID)
|
||||||
|
|
||||||
|
item, err := s.itemRepo.GetByID(ctx, id, orgID)
|
||||||
|
if err != nil {
|
||||||
|
s.logger.Error("Failed to get item: ", err)
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
|
||||||
|
return item, nil
|
||||||
|
}
|
||||||
|
|
||||||
|
func (s *itemService) GetItems(ctx context.Context, orgID uuid.UUID) ([]*models.Item, error) {
|
||||||
|
s.logger.Info("Getting all items for organization: ", orgID)
|
||||||
|
|
||||||
|
items, err := s.itemRepo.GetByOrganization(ctx, orgID)
|
||||||
|
if err != nil {
|
||||||
|
s.logger.Error("Failed to get items: ", err)
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
|
||||||
|
return items, nil
|
||||||
|
}
|
||||||
|
|
||||||
|
func (s *itemService) UpdateItem(ctx context.Context, id uuid.UUID, orgID uuid.UUID, req *models.CreateItemRequest) (*models.Item, error) {
|
||||||
|
s.logger.Info("Updating item: ", id, " for organization: ", orgID)
|
||||||
|
|
||||||
|
// Сначала получаем существующий товар
|
||||||
|
item, err := s.itemRepo.GetByID(ctx, id, orgID)
|
||||||
|
if err != nil {
|
||||||
|
s.logger.Error("Failed to get item for update: ", err)
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
|
||||||
|
// Обновляем поля
|
||||||
|
item.Name = req.Name
|
||||||
|
item.Description = req.Description
|
||||||
|
item.Category = req.Category
|
||||||
|
|
||||||
|
if err := s.itemRepo.Update(ctx, item); err != nil {
|
||||||
|
s.logger.Error("Failed to update item: ", err)
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
|
||||||
|
s.logger.Info("Item updated successfully: ", item.ID)
|
||||||
|
return item, nil
|
||||||
|
}
|
||||||
|
|
||||||
|
func (s *itemService) DeleteItem(ctx context.Context, id uuid.UUID, orgID uuid.UUID) error {
|
||||||
|
s.logger.Info("Deleting item: ", id, " for organization: ", orgID)
|
||||||
|
|
||||||
|
if err := s.itemRepo.Delete(ctx, id, orgID); err != nil {
|
||||||
|
s.logger.Error("Failed to delete item: ", err)
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
|
||||||
|
s.logger.Info("Item deleted successfully: ", id)
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
|
||||||
|
func (s *itemService) SearchItems(ctx context.Context, orgID uuid.UUID, query string, category string) ([]*models.Item, error) {
|
||||||
|
s.logger.Info("Searching items for organization: ", orgID, " query: ", query, " category: ", category)
|
||||||
|
|
||||||
|
items, err := s.itemRepo.Search(ctx, orgID, query, category)
|
||||||
|
if err != nil {
|
||||||
|
s.logger.Error("Failed to search items: ", err)
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
|
||||||
|
return items, nil
|
||||||
|
}
|
||||||
130
core-service/internal/service/location_service.go
Normal file
130
core-service/internal/service/location_service.go
Normal file
@@ -0,0 +1,130 @@
|
|||||||
|
package service
|
||||||
|
|
||||||
|
import (
|
||||||
|
"context"
|
||||||
|
"time"
|
||||||
|
|
||||||
|
"erp-mvp/core-service/internal/models"
|
||||||
|
"erp-mvp/core-service/internal/repository"
|
||||||
|
|
||||||
|
"github.com/google/uuid"
|
||||||
|
"github.com/sirupsen/logrus"
|
||||||
|
)
|
||||||
|
|
||||||
|
type LocationService interface {
|
||||||
|
CreateLocation(ctx context.Context, orgID uuid.UUID, req *models.CreateLocationRequest) (*models.StorageLocation, error)
|
||||||
|
GetLocation(ctx context.Context, id uuid.UUID, orgID uuid.UUID) (*models.StorageLocation, error)
|
||||||
|
GetLocations(ctx context.Context, orgID uuid.UUID) ([]*models.StorageLocation, error)
|
||||||
|
UpdateLocation(ctx context.Context, id uuid.UUID, orgID uuid.UUID, req *models.CreateLocationRequest) (*models.StorageLocation, error)
|
||||||
|
DeleteLocation(ctx context.Context, id uuid.UUID, orgID uuid.UUID) error
|
||||||
|
GetChildren(ctx context.Context, parentID uuid.UUID, orgID uuid.UUID) ([]*models.StorageLocation, error)
|
||||||
|
}
|
||||||
|
|
||||||
|
type locationService struct {
|
||||||
|
locationRepo repository.LocationRepository
|
||||||
|
logger *logrus.Logger
|
||||||
|
}
|
||||||
|
|
||||||
|
func NewLocationService(locationRepo repository.LocationRepository) LocationService {
|
||||||
|
return &locationService{
|
||||||
|
locationRepo: locationRepo,
|
||||||
|
logger: logrus.New(),
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func (s *locationService) CreateLocation(ctx context.Context, orgID uuid.UUID, req *models.CreateLocationRequest) (*models.StorageLocation, error) {
|
||||||
|
s.logger.Info("Creating location for organization: ", orgID)
|
||||||
|
|
||||||
|
location := &models.StorageLocation{
|
||||||
|
ID: uuid.New(),
|
||||||
|
OrganizationID: orgID,
|
||||||
|
ParentID: req.ParentID,
|
||||||
|
Name: req.Name,
|
||||||
|
Address: req.Address,
|
||||||
|
Type: req.Type,
|
||||||
|
Coordinates: req.Coordinates,
|
||||||
|
CreatedAt: time.Now(),
|
||||||
|
}
|
||||||
|
|
||||||
|
if err := s.locationRepo.Create(ctx, location); err != nil {
|
||||||
|
s.logger.Error("Failed to create location: ", err)
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
|
||||||
|
s.logger.Info("Location created successfully: ", location.ID)
|
||||||
|
return location, nil
|
||||||
|
}
|
||||||
|
|
||||||
|
func (s *locationService) GetLocation(ctx context.Context, id uuid.UUID, orgID uuid.UUID) (*models.StorageLocation, error) {
|
||||||
|
s.logger.Info("Getting location: ", id, " for organization: ", orgID)
|
||||||
|
|
||||||
|
location, err := s.locationRepo.GetByID(ctx, id, orgID)
|
||||||
|
if err != nil {
|
||||||
|
s.logger.Error("Failed to get location: ", err)
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
|
||||||
|
return location, nil
|
||||||
|
}
|
||||||
|
|
||||||
|
func (s *locationService) GetLocations(ctx context.Context, orgID uuid.UUID) ([]*models.StorageLocation, error) {
|
||||||
|
s.logger.Info("Getting all locations for organization: ", orgID)
|
||||||
|
|
||||||
|
locations, err := s.locationRepo.GetByOrganization(ctx, orgID)
|
||||||
|
if err != nil {
|
||||||
|
s.logger.Error("Failed to get locations: ", err)
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
|
||||||
|
return locations, nil
|
||||||
|
}
|
||||||
|
|
||||||
|
func (s *locationService) UpdateLocation(ctx context.Context, id uuid.UUID, orgID uuid.UUID, req *models.CreateLocationRequest) (*models.StorageLocation, error) {
|
||||||
|
s.logger.Info("Updating location: ", id, " for organization: ", orgID)
|
||||||
|
|
||||||
|
// Сначала получаем существующую локацию
|
||||||
|
location, err := s.locationRepo.GetByID(ctx, id, orgID)
|
||||||
|
if err != nil {
|
||||||
|
s.logger.Error("Failed to get location for update: ", err)
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
|
||||||
|
// Обновляем поля
|
||||||
|
location.ParentID = req.ParentID
|
||||||
|
location.Name = req.Name
|
||||||
|
location.Address = req.Address
|
||||||
|
location.Type = req.Type
|
||||||
|
location.Coordinates = req.Coordinates
|
||||||
|
|
||||||
|
if err := s.locationRepo.Update(ctx, location); err != nil {
|
||||||
|
s.logger.Error("Failed to update location: ", err)
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
|
||||||
|
s.logger.Info("Location updated successfully: ", location.ID)
|
||||||
|
return location, nil
|
||||||
|
}
|
||||||
|
|
||||||
|
func (s *locationService) DeleteLocation(ctx context.Context, id uuid.UUID, orgID uuid.UUID) error {
|
||||||
|
s.logger.Info("Deleting location: ", id, " for organization: ", orgID)
|
||||||
|
|
||||||
|
if err := s.locationRepo.Delete(ctx, id, orgID); err != nil {
|
||||||
|
s.logger.Error("Failed to delete location: ", err)
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
|
||||||
|
s.logger.Info("Location deleted successfully: ", id)
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
|
||||||
|
func (s *locationService) GetChildren(ctx context.Context, parentID uuid.UUID, orgID uuid.UUID) ([]*models.StorageLocation, error) {
|
||||||
|
s.logger.Info("Getting children for location: ", parentID, " in organization: ", orgID)
|
||||||
|
|
||||||
|
children, err := s.locationRepo.GetChildren(ctx, parentID, orgID)
|
||||||
|
if err != nil {
|
||||||
|
s.logger.Error("Failed to get children: ", err)
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
|
||||||
|
return children, nil
|
||||||
|
}
|
||||||
192
core-service/internal/service/operations_service.go
Normal file
192
core-service/internal/service/operations_service.go
Normal file
@@ -0,0 +1,192 @@
|
|||||||
|
package service
|
||||||
|
|
||||||
|
import (
|
||||||
|
"context"
|
||||||
|
"time"
|
||||||
|
|
||||||
|
"erp-mvp/core-service/internal/models"
|
||||||
|
"erp-mvp/core-service/internal/repository"
|
||||||
|
|
||||||
|
"github.com/google/uuid"
|
||||||
|
"github.com/sirupsen/logrus"
|
||||||
|
)
|
||||||
|
|
||||||
|
type OperationsService interface {
|
||||||
|
PlaceItem(ctx context.Context, orgID uuid.UUID, req *models.PlaceItemRequest) (*models.ItemPlacement, error)
|
||||||
|
MoveItem(ctx context.Context, placementID uuid.UUID, newLocationID uuid.UUID, orgID uuid.UUID) error
|
||||||
|
GetItemPlacements(ctx context.Context, itemID uuid.UUID, orgID uuid.UUID) ([]*models.ItemPlacement, error)
|
||||||
|
GetLocationPlacements(ctx context.Context, locationID uuid.UUID, orgID uuid.UUID) ([]*models.ItemPlacement, error)
|
||||||
|
UpdateQuantity(ctx context.Context, placementID uuid.UUID, quantity int, orgID uuid.UUID) error
|
||||||
|
DeletePlacement(ctx context.Context, placementID uuid.UUID, orgID uuid.UUID) error
|
||||||
|
Search(ctx context.Context, orgID uuid.UUID, req *models.SearchRequest) (*models.SearchResponse, error)
|
||||||
|
}
|
||||||
|
|
||||||
|
type operationsService struct {
|
||||||
|
operationsRepo repository.OperationsRepository
|
||||||
|
itemRepo repository.ItemRepository
|
||||||
|
locationRepo repository.LocationRepository
|
||||||
|
logger *logrus.Logger
|
||||||
|
}
|
||||||
|
|
||||||
|
func NewOperationsService(operationsRepo repository.OperationsRepository, itemRepo repository.ItemRepository, locationRepo repository.LocationRepository) OperationsService {
|
||||||
|
return &operationsService{
|
||||||
|
operationsRepo: operationsRepo,
|
||||||
|
itemRepo: itemRepo,
|
||||||
|
locationRepo: locationRepo,
|
||||||
|
logger: logrus.New(),
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func (s *operationsService) PlaceItem(ctx context.Context, orgID uuid.UUID, req *models.PlaceItemRequest) (*models.ItemPlacement, error) {
|
||||||
|
s.logger.Info("Placing item: ", req.ItemID, " in location: ", req.LocationID, " for organization: ", orgID)
|
||||||
|
|
||||||
|
// Проверяем, что товар существует и принадлежит организации
|
||||||
|
_, err := s.itemRepo.GetByID(ctx, req.ItemID, orgID)
|
||||||
|
if err != nil {
|
||||||
|
s.logger.Error("Item not found or not accessible: ", err)
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
|
||||||
|
// Проверяем, что место хранения существует и принадлежит организации
|
||||||
|
_, err = s.locationRepo.GetByID(ctx, req.LocationID, orgID)
|
||||||
|
if err != nil {
|
||||||
|
s.logger.Error("Location not found or not accessible: ", err)
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
|
||||||
|
placement := &models.ItemPlacement{
|
||||||
|
ID: uuid.New(),
|
||||||
|
OrganizationID: orgID,
|
||||||
|
ItemID: req.ItemID,
|
||||||
|
LocationID: req.LocationID,
|
||||||
|
Quantity: req.Quantity,
|
||||||
|
CreatedAt: time.Now(),
|
||||||
|
}
|
||||||
|
|
||||||
|
if err := s.operationsRepo.PlaceItem(ctx, placement); err != nil {
|
||||||
|
s.logger.Error("Failed to place item: ", err)
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
|
||||||
|
s.logger.Info("Item placed successfully: ", placement.ID)
|
||||||
|
return placement, nil
|
||||||
|
}
|
||||||
|
|
||||||
|
func (s *operationsService) MoveItem(ctx context.Context, placementID uuid.UUID, newLocationID uuid.UUID, orgID uuid.UUID) error {
|
||||||
|
s.logger.Info("Moving item placement: ", placementID, " to location: ", newLocationID, " for organization: ", orgID)
|
||||||
|
|
||||||
|
// Проверяем, что размещение существует и принадлежит организации
|
||||||
|
placement, err := s.operationsRepo.GetByID(ctx, placementID, orgID)
|
||||||
|
if err != nil {
|
||||||
|
s.logger.Error("Item placement not found or not accessible: ", err)
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
|
||||||
|
// Проверяем, что новое место хранения существует и принадлежит организации
|
||||||
|
_, err = s.locationRepo.GetByID(ctx, newLocationID, orgID)
|
||||||
|
if err != nil {
|
||||||
|
s.logger.Error("New location not found or not accessible: ", err)
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
|
||||||
|
if err := s.operationsRepo.MoveItem(ctx, placementID, newLocationID, orgID); err != nil {
|
||||||
|
s.logger.Error("Failed to move item: ", err)
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
|
||||||
|
s.logger.Info("Item moved successfully from location: ", placement.LocationID, " to: ", newLocationID)
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
|
||||||
|
func (s *operationsService) GetItemPlacements(ctx context.Context, itemID uuid.UUID, orgID uuid.UUID) ([]*models.ItemPlacement, error) {
|
||||||
|
s.logger.Info("Getting placements for item: ", itemID, " in organization: ", orgID)
|
||||||
|
|
||||||
|
// Проверяем, что товар существует и принадлежит организации
|
||||||
|
_, err := s.itemRepo.GetByID(ctx, itemID, orgID)
|
||||||
|
if err != nil {
|
||||||
|
s.logger.Error("Item not found or not accessible: ", err)
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
|
||||||
|
placements, err := s.operationsRepo.GetByItem(ctx, itemID, orgID)
|
||||||
|
if err != nil {
|
||||||
|
s.logger.Error("Failed to get item placements: ", err)
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
|
||||||
|
return placements, nil
|
||||||
|
}
|
||||||
|
|
||||||
|
func (s *operationsService) GetLocationPlacements(ctx context.Context, locationID uuid.UUID, orgID uuid.UUID) ([]*models.ItemPlacement, error) {
|
||||||
|
s.logger.Info("Getting placements for location: ", locationID, " in organization: ", orgID)
|
||||||
|
|
||||||
|
// Проверяем, что место хранения существует и принадлежит организации
|
||||||
|
_, err := s.locationRepo.GetByID(ctx, locationID, orgID)
|
||||||
|
if err != nil {
|
||||||
|
s.logger.Error("Location not found or not accessible: ", err)
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
|
||||||
|
placements, err := s.operationsRepo.GetByLocation(ctx, locationID, orgID)
|
||||||
|
if err != nil {
|
||||||
|
s.logger.Error("Failed to get location placements: ", err)
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
|
||||||
|
return placements, nil
|
||||||
|
}
|
||||||
|
|
||||||
|
func (s *operationsService) UpdateQuantity(ctx context.Context, placementID uuid.UUID, quantity int, orgID uuid.UUID) error {
|
||||||
|
s.logger.Info("Updating quantity for placement: ", placementID, " to: ", quantity, " in organization: ", orgID)
|
||||||
|
|
||||||
|
// Проверяем, что размещение существует и принадлежит организации
|
||||||
|
_, err := s.operationsRepo.GetByID(ctx, placementID, orgID)
|
||||||
|
if err != nil {
|
||||||
|
s.logger.Error("Item placement not found or not accessible: ", err)
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
|
||||||
|
if err := s.operationsRepo.UpdateQuantity(ctx, placementID, quantity, orgID); err != nil {
|
||||||
|
s.logger.Error("Failed to update quantity: ", err)
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
|
||||||
|
s.logger.Info("Quantity updated successfully for placement: ", placementID)
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
|
||||||
|
func (s *operationsService) DeletePlacement(ctx context.Context, placementID uuid.UUID, orgID uuid.UUID) error {
|
||||||
|
s.logger.Info("Deleting placement: ", placementID, " for organization: ", orgID)
|
||||||
|
|
||||||
|
// Проверяем, что размещение существует и принадлежит организации
|
||||||
|
_, err := s.operationsRepo.GetByID(ctx, placementID, orgID)
|
||||||
|
if err != nil {
|
||||||
|
s.logger.Error("Item placement not found or not accessible: ", err)
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
|
||||||
|
if err := s.operationsRepo.Delete(ctx, placementID, orgID); err != nil {
|
||||||
|
s.logger.Error("Failed to delete placement: ", err)
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
|
||||||
|
s.logger.Info("Placement deleted successfully: ", placementID)
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
|
||||||
|
func (s *operationsService) Search(ctx context.Context, orgID uuid.UUID, req *models.SearchRequest) (*models.SearchResponse, error) {
|
||||||
|
s.logger.Info("Searching items with locations for organization: ", orgID, " query: ", req.Query)
|
||||||
|
|
||||||
|
results, err := s.operationsRepo.Search(ctx, orgID, req.Query, req.Category, req.Address)
|
||||||
|
if err != nil {
|
||||||
|
s.logger.Error("Failed to search items with locations: ", err)
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
|
||||||
|
response := &models.SearchResponse{
|
||||||
|
Items: results,
|
||||||
|
TotalCount: len(results),
|
||||||
|
}
|
||||||
|
|
||||||
|
return response, nil
|
||||||
|
}
|
||||||
72
core-service/migrations/001_initial_schema.sql
Normal file
72
core-service/migrations/001_initial_schema.sql
Normal file
@@ -0,0 +1,72 @@
|
|||||||
|
-- Initial schema for ERP MVP
|
||||||
|
-- Migration: 001_initial_schema.sql
|
||||||
|
|
||||||
|
-- Enable UUID extension
|
||||||
|
CREATE EXTENSION IF NOT EXISTS "uuid-ossp";
|
||||||
|
|
||||||
|
-- Organizations table
|
||||||
|
CREATE TABLE organizations (
|
||||||
|
id UUID PRIMARY KEY DEFAULT uuid_generate_v4(),
|
||||||
|
name VARCHAR(255) NOT NULL,
|
||||||
|
type VARCHAR(100),
|
||||||
|
settings JSONB,
|
||||||
|
created_at TIMESTAMP DEFAULT NOW()
|
||||||
|
);
|
||||||
|
|
||||||
|
-- Users table
|
||||||
|
CREATE TABLE users (
|
||||||
|
id UUID PRIMARY KEY DEFAULT uuid_generate_v4(),
|
||||||
|
organization_id UUID REFERENCES organizations(id) ON DELETE CASCADE,
|
||||||
|
email VARCHAR(255) UNIQUE NOT NULL,
|
||||||
|
password_hash VARCHAR(255) NOT NULL,
|
||||||
|
role VARCHAR(50) DEFAULT 'user',
|
||||||
|
created_at TIMESTAMP DEFAULT NOW()
|
||||||
|
);
|
||||||
|
|
||||||
|
-- Storage locations table
|
||||||
|
CREATE TABLE storage_locations (
|
||||||
|
id UUID PRIMARY KEY DEFAULT uuid_generate_v4(),
|
||||||
|
organization_id UUID REFERENCES organizations(id) ON DELETE CASCADE,
|
||||||
|
parent_id UUID REFERENCES storage_locations(id) ON DELETE CASCADE,
|
||||||
|
name VARCHAR(255) NOT NULL,
|
||||||
|
address VARCHAR(100) NOT NULL,
|
||||||
|
type VARCHAR(50) NOT NULL,
|
||||||
|
coordinates JSONB,
|
||||||
|
created_at TIMESTAMP DEFAULT NOW()
|
||||||
|
);
|
||||||
|
|
||||||
|
-- Items table
|
||||||
|
CREATE TABLE items (
|
||||||
|
id UUID PRIMARY KEY DEFAULT uuid_generate_v4(),
|
||||||
|
organization_id UUID REFERENCES organizations(id) ON DELETE CASCADE,
|
||||||
|
name VARCHAR(255) NOT NULL,
|
||||||
|
description TEXT,
|
||||||
|
category VARCHAR(100),
|
||||||
|
created_at TIMESTAMP DEFAULT NOW()
|
||||||
|
);
|
||||||
|
|
||||||
|
-- Item placements table
|
||||||
|
CREATE TABLE item_placements (
|
||||||
|
id UUID PRIMARY KEY DEFAULT uuid_generate_v4(),
|
||||||
|
organization_id UUID REFERENCES organizations(id) ON DELETE CASCADE,
|
||||||
|
item_id UUID REFERENCES items(id) ON DELETE CASCADE,
|
||||||
|
location_id UUID REFERENCES storage_locations(id) ON DELETE CASCADE,
|
||||||
|
quantity INTEGER DEFAULT 1,
|
||||||
|
created_at TIMESTAMP DEFAULT NOW()
|
||||||
|
);
|
||||||
|
|
||||||
|
-- Indexes for better performance
|
||||||
|
CREATE INDEX idx_users_organization_id ON users(organization_id);
|
||||||
|
CREATE INDEX idx_users_email ON users(email);
|
||||||
|
CREATE INDEX idx_storage_locations_organization_id ON storage_locations(organization_id);
|
||||||
|
CREATE INDEX idx_storage_locations_parent_id ON storage_locations(parent_id);
|
||||||
|
CREATE INDEX idx_storage_locations_address ON storage_locations(address);
|
||||||
|
CREATE INDEX idx_items_organization_id ON items(organization_id);
|
||||||
|
CREATE INDEX idx_items_name ON items(name);
|
||||||
|
CREATE INDEX idx_items_category ON items(category);
|
||||||
|
CREATE INDEX idx_item_placements_organization_id ON item_placements(organization_id);
|
||||||
|
CREATE INDEX idx_item_placements_item_id ON item_placements(item_id);
|
||||||
|
CREATE INDEX idx_item_placements_location_id ON item_placements(location_id);
|
||||||
|
|
||||||
|
-- Unique constraints
|
||||||
|
CREATE UNIQUE INDEX idx_storage_locations_org_address ON storage_locations(organization_id, address);
|
||||||
Reference in New Issue
Block a user