Update users
This commit is contained in:
@@ -0,0 +1,238 @@
|
||||
package processor
|
||||
|
||||
import (
|
||||
"apskel-pos-be/internal/entities"
|
||||
"apskel-pos-be/internal/mappers"
|
||||
"apskel-pos-be/internal/models"
|
||||
"context"
|
||||
"time"
|
||||
|
||||
"github.com/google/uuid"
|
||||
)
|
||||
|
||||
type IngredientProcessorImpl struct {
|
||||
ingredientRepo IngredientRepository
|
||||
unitRepo UnitRepository
|
||||
}
|
||||
|
||||
func NewIngredientProcessor(ingredientRepo IngredientRepository, unitRepo UnitRepository) *IngredientProcessorImpl {
|
||||
return &IngredientProcessorImpl{
|
||||
ingredientRepo: ingredientRepo,
|
||||
unitRepo: unitRepo,
|
||||
}
|
||||
}
|
||||
|
||||
func (p *IngredientProcessorImpl) CreateIngredient(ctx context.Context, req *models.CreateIngredientRequest) (*models.IngredientResponse, error) {
|
||||
// Validate unit exists
|
||||
_, err := p.unitRepo.GetByID(ctx, req.UnitID, req.OrganizationID)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
// Create ingredient entity
|
||||
ingredient := &entities.Ingredient{
|
||||
ID: uuid.New(),
|
||||
OrganizationID: req.OrganizationID,
|
||||
OutletID: req.OutletID,
|
||||
Name: req.Name,
|
||||
UnitID: req.UnitID,
|
||||
Cost: req.Cost,
|
||||
Stock: req.Stock,
|
||||
IsSemiFinished: req.IsSemiFinished,
|
||||
IsActive: req.IsActive,
|
||||
Metadata: req.Metadata,
|
||||
CreatedAt: time.Now(),
|
||||
UpdatedAt: time.Now(),
|
||||
}
|
||||
|
||||
// Save to database
|
||||
err = p.ingredientRepo.Create(ctx, ingredient)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
// Get with relations
|
||||
ingredientWithUnit, err := p.ingredientRepo.GetByID(ctx, ingredient.ID, req.OrganizationID)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
// Map to response
|
||||
ingredientModel := mappers.MapIngredientEntityToModel(ingredientWithUnit)
|
||||
response := &models.IngredientResponse{
|
||||
ID: ingredientModel.ID,
|
||||
OrganizationID: ingredientModel.OrganizationID,
|
||||
OutletID: ingredientModel.OutletID,
|
||||
Name: ingredientModel.Name,
|
||||
UnitID: ingredientModel.UnitID,
|
||||
Cost: ingredientModel.Cost,
|
||||
Stock: ingredientModel.Stock,
|
||||
IsSemiFinished: ingredientModel.IsSemiFinished,
|
||||
IsActive: ingredientModel.IsActive,
|
||||
Metadata: ingredientModel.Metadata,
|
||||
CreatedAt: ingredientModel.CreatedAt,
|
||||
UpdatedAt: ingredientModel.UpdatedAt,
|
||||
Unit: ingredientModel.Unit,
|
||||
}
|
||||
|
||||
return response, nil
|
||||
}
|
||||
|
||||
func (p *IngredientProcessorImpl) GetIngredientByID(ctx context.Context, id uuid.UUID) (*models.IngredientResponse, error) {
|
||||
// For now, we'll need to get organizationID from context or request
|
||||
// This is a limitation of the current interface design
|
||||
organizationID := uuid.Nil // This should come from context
|
||||
|
||||
ingredient, err := p.ingredientRepo.GetByID(ctx, id, organizationID)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
ingredientModel := mappers.MapIngredientEntityToModel(ingredient)
|
||||
response := &models.IngredientResponse{
|
||||
ID: ingredientModel.ID,
|
||||
OrganizationID: ingredientModel.OrganizationID,
|
||||
OutletID: ingredientModel.OutletID,
|
||||
Name: ingredientModel.Name,
|
||||
UnitID: ingredientModel.UnitID,
|
||||
Cost: ingredientModel.Cost,
|
||||
Stock: ingredientModel.Stock,
|
||||
IsSemiFinished: ingredientModel.IsSemiFinished,
|
||||
IsActive: ingredientModel.IsActive,
|
||||
Metadata: ingredientModel.Metadata,
|
||||
CreatedAt: ingredientModel.CreatedAt,
|
||||
UpdatedAt: ingredientModel.UpdatedAt,
|
||||
Unit: ingredientModel.Unit,
|
||||
}
|
||||
|
||||
return response, nil
|
||||
}
|
||||
|
||||
func (p *IngredientProcessorImpl) ListIngredients(ctx context.Context, organizationID uuid.UUID, outletID *uuid.UUID, page, limit int, search string) (*models.PaginatedResponse[models.IngredientResponse], error) {
|
||||
// Set default values
|
||||
if page < 1 {
|
||||
page = 1
|
||||
}
|
||||
if limit < 1 {
|
||||
limit = 10
|
||||
}
|
||||
if limit > 100 {
|
||||
limit = 100
|
||||
}
|
||||
|
||||
ingredients, total, err := p.ingredientRepo.GetAll(ctx, organizationID, outletID, page, limit, search, nil)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
// Map to response models
|
||||
ingredientModels := mappers.MapIngredientEntitiesToModels(ingredients)
|
||||
ingredientResponses := make([]models.IngredientResponse, len(ingredientModels))
|
||||
|
||||
for i, ingredientModel := range ingredientModels {
|
||||
ingredientResponses[i] = models.IngredientResponse{
|
||||
ID: ingredientModel.ID,
|
||||
OrganizationID: ingredientModel.OrganizationID,
|
||||
OutletID: ingredientModel.OutletID,
|
||||
Name: ingredientModel.Name,
|
||||
UnitID: ingredientModel.UnitID,
|
||||
Cost: ingredientModel.Cost,
|
||||
Stock: ingredientModel.Stock,
|
||||
IsSemiFinished: ingredientModel.IsSemiFinished,
|
||||
IsActive: ingredientModel.IsActive,
|
||||
Metadata: ingredientModel.Metadata,
|
||||
CreatedAt: ingredientModel.CreatedAt,
|
||||
UpdatedAt: ingredientModel.UpdatedAt,
|
||||
Unit: ingredientModel.Unit,
|
||||
}
|
||||
}
|
||||
|
||||
// Create paginated response
|
||||
paginatedResponse := &models.PaginatedResponse[models.IngredientResponse]{
|
||||
Data: ingredientResponses,
|
||||
Pagination: models.Pagination{
|
||||
Page: page,
|
||||
Limit: limit,
|
||||
Total: int64(total),
|
||||
TotalPages: (total + limit - 1) / limit,
|
||||
},
|
||||
}
|
||||
|
||||
return paginatedResponse, nil
|
||||
}
|
||||
|
||||
func (p *IngredientProcessorImpl) UpdateIngredient(ctx context.Context, id uuid.UUID, req *models.UpdateIngredientRequest) (*models.IngredientResponse, error) {
|
||||
// For now, we'll need to get organizationID from context or request
|
||||
// This is a limitation of the current interface design
|
||||
organizationID := uuid.Nil // This should come from context
|
||||
|
||||
// Get existing ingredient
|
||||
existingIngredient, err := p.ingredientRepo.GetByID(ctx, id, organizationID)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
// Validate unit exists if changed
|
||||
if req.UnitID != existingIngredient.UnitID {
|
||||
_, err := p.unitRepo.GetByID(ctx, req.UnitID, organizationID)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
}
|
||||
|
||||
// Update fields
|
||||
existingIngredient.OutletID = req.OutletID
|
||||
existingIngredient.Name = req.Name
|
||||
existingIngredient.UnitID = req.UnitID
|
||||
existingIngredient.Cost = req.Cost
|
||||
existingIngredient.Stock = req.Stock
|
||||
existingIngredient.IsSemiFinished = req.IsSemiFinished
|
||||
existingIngredient.IsActive = req.IsActive
|
||||
existingIngredient.Metadata = req.Metadata
|
||||
existingIngredient.UpdatedAt = time.Now()
|
||||
|
||||
// Save to database
|
||||
err = p.ingredientRepo.Update(ctx, existingIngredient)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
// Get with relations
|
||||
ingredientWithUnit, err := p.ingredientRepo.GetByID(ctx, id, organizationID)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
// Map to response
|
||||
ingredientModel := mappers.MapIngredientEntityToModel(ingredientWithUnit)
|
||||
response := &models.IngredientResponse{
|
||||
ID: ingredientModel.ID,
|
||||
OrganizationID: ingredientModel.OrganizationID,
|
||||
OutletID: ingredientModel.OutletID,
|
||||
Name: ingredientModel.Name,
|
||||
UnitID: ingredientModel.UnitID,
|
||||
Cost: ingredientModel.Cost,
|
||||
Stock: ingredientModel.Stock,
|
||||
IsSemiFinished: ingredientModel.IsSemiFinished,
|
||||
IsActive: ingredientModel.IsActive,
|
||||
Metadata: ingredientModel.Metadata,
|
||||
CreatedAt: ingredientModel.CreatedAt,
|
||||
UpdatedAt: ingredientModel.UpdatedAt,
|
||||
Unit: ingredientModel.Unit,
|
||||
}
|
||||
|
||||
return response, nil
|
||||
}
|
||||
|
||||
func (p *IngredientProcessorImpl) DeleteIngredient(ctx context.Context, id uuid.UUID) error {
|
||||
// For now, we'll need to get organizationID from context or request
|
||||
// This is a limitation of the current interface design
|
||||
organizationID := uuid.Nil // This should come from context
|
||||
|
||||
err := p.ingredientRepo.Delete(ctx, id, organizationID)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
||||
@@ -0,0 +1,17 @@
|
||||
package processor
|
||||
|
||||
import (
|
||||
"apskel-pos-be/internal/entities"
|
||||
"context"
|
||||
|
||||
"github.com/google/uuid"
|
||||
)
|
||||
|
||||
type IngredientRepository interface {
|
||||
Create(ctx context.Context, ingredient *entities.Ingredient) error
|
||||
GetByID(ctx context.Context, id, organizationID uuid.UUID) (*entities.Ingredient, error)
|
||||
GetAll(ctx context.Context, organizationID uuid.UUID, outletID *uuid.UUID, page, limit int, search string, isSemiFinished *bool) ([]*entities.Ingredient, int, error)
|
||||
Update(ctx context.Context, ingredient *entities.Ingredient) error
|
||||
Delete(ctx context.Context, id, organizationID uuid.UUID) error
|
||||
UpdateStock(ctx context.Context, id uuid.UUID, newStock float64, organizationID uuid.UUID) error
|
||||
}
|
||||
@@ -47,8 +47,8 @@ func NewInventoryMovementProcessorImpl(
|
||||
}
|
||||
}
|
||||
|
||||
func (p *InventoryMovementProcessorImpl) CreateMovement(ctx context.Context, req *models.CreateInventoryMovementRequest) (*models.InventoryMovementResponse, error) {
|
||||
currentInventory, err := p.inventoryRepo.GetByProductAndOutlet(ctx, req.ProductID, req.OutletID)
|
||||
func (p *InventoryMovementProcessorImpl) CreateInventoryMovement(ctx context.Context, req *models.CreateInventoryMovementRequest) (*models.InventoryMovementResponse, error) {
|
||||
currentInventory, err := p.inventoryRepo.GetByProductAndOutlet(ctx, req.ItemID, req.OutletID)
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("failed to get current inventory: %w", err)
|
||||
}
|
||||
@@ -59,11 +59,12 @@ func (p *InventoryMovementProcessorImpl) CreateMovement(ctx context.Context, req
|
||||
movement := &entities.InventoryMovement{
|
||||
OrganizationID: req.OrganizationID,
|
||||
OutletID: req.OutletID,
|
||||
ProductID: req.ProductID,
|
||||
ItemID: req.ItemID,
|
||||
ItemType: req.ItemType,
|
||||
MovementType: entities.InventoryMovementType(req.MovementType),
|
||||
Quantity: req.Quantity,
|
||||
PreviousQuantity: previousQuantity,
|
||||
NewQuantity: newQuantity,
|
||||
Quantity: float64(req.Quantity),
|
||||
PreviousQuantity: float64(previousQuantity),
|
||||
NewQuantity: float64(newQuantity),
|
||||
UnitCost: req.UnitCost,
|
||||
TotalCost: float64(req.Quantity) * req.UnitCost,
|
||||
ReferenceType: (*entities.InventoryMovementReferenceType)(req.ReferenceType),
|
||||
@@ -89,7 +90,7 @@ func (p *InventoryMovementProcessorImpl) CreateMovement(ctx context.Context, req
|
||||
return response, nil
|
||||
}
|
||||
|
||||
func (p *InventoryMovementProcessorImpl) GetMovementByID(ctx context.Context, id uuid.UUID) (*models.InventoryMovementResponse, error) {
|
||||
func (p *InventoryMovementProcessorImpl) GetInventoryMovementByID(ctx context.Context, id uuid.UUID) (*models.InventoryMovementResponse, error) {
|
||||
movement, err := p.movementRepo.GetWithRelations(ctx, id)
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("movement not found: %w", err)
|
||||
@@ -99,44 +100,29 @@ func (p *InventoryMovementProcessorImpl) GetMovementByID(ctx context.Context, id
|
||||
return response, nil
|
||||
}
|
||||
|
||||
func (p *InventoryMovementProcessorImpl) ListMovements(ctx context.Context, req *models.ListInventoryMovementsRequest) (*models.ListInventoryMovementsResponse, error) {
|
||||
filters := make(map[string]interface{})
|
||||
if req.OrganizationID != nil {
|
||||
filters["organization_id"] = *req.OrganizationID
|
||||
func (p *InventoryMovementProcessorImpl) ListInventoryMovements(ctx context.Context, organizationID uuid.UUID, outletID *uuid.UUID, page, limit int, search string) (*models.PaginatedResponse[models.InventoryMovementResponse], error) {
|
||||
// Set default values
|
||||
if page < 1 {
|
||||
page = 1
|
||||
}
|
||||
if req.OutletID != nil {
|
||||
filters["outlet_id"] = *req.OutletID
|
||||
if limit < 1 {
|
||||
limit = 10
|
||||
}
|
||||
if req.ProductID != nil {
|
||||
filters["product_id"] = *req.ProductID
|
||||
}
|
||||
if req.MovementType != nil {
|
||||
filters["movement_type"] = string(*req.MovementType)
|
||||
}
|
||||
if req.ReferenceType != nil {
|
||||
filters["reference_type"] = string(*req.ReferenceType)
|
||||
}
|
||||
if req.ReferenceID != nil {
|
||||
filters["reference_id"] = *req.ReferenceID
|
||||
}
|
||||
if req.OrderID != nil {
|
||||
filters["order_id"] = *req.OrderID
|
||||
}
|
||||
if req.PaymentID != nil {
|
||||
filters["payment_id"] = *req.PaymentID
|
||||
}
|
||||
if req.UserID != nil {
|
||||
filters["user_id"] = *req.UserID
|
||||
}
|
||||
if req.DateFrom != nil {
|
||||
filters["date_from"] = *req.DateFrom
|
||||
}
|
||||
if req.DateTo != nil {
|
||||
filters["date_to"] = *req.DateTo
|
||||
if limit > 100 {
|
||||
limit = 100
|
||||
}
|
||||
|
||||
offset := (req.Page - 1) * req.Limit
|
||||
movements, total, err := p.movementRepo.List(ctx, filters, req.Limit, offset)
|
||||
filters := make(map[string]interface{})
|
||||
filters["organization_id"] = organizationID
|
||||
if outletID != nil {
|
||||
filters["outlet_id"] = *outletID
|
||||
}
|
||||
if search != "" {
|
||||
filters["search"] = search
|
||||
}
|
||||
|
||||
offset := (page - 1) * limit
|
||||
movements, total, err := p.movementRepo.List(ctx, filters, limit, offset)
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("failed to list movements: %w", err)
|
||||
}
|
||||
@@ -150,19 +136,18 @@ func (p *InventoryMovementProcessorImpl) ListMovements(ctx context.Context, req
|
||||
}
|
||||
}
|
||||
|
||||
// Calculate total pages
|
||||
totalPages := int(total) / req.Limit
|
||||
if int(total)%req.Limit > 0 {
|
||||
totalPages++
|
||||
// Create paginated response
|
||||
paginatedResponse := &models.PaginatedResponse[models.InventoryMovementResponse]{
|
||||
Data: movementResponses,
|
||||
Pagination: models.Pagination{
|
||||
Page: page,
|
||||
Limit: limit,
|
||||
Total: total,
|
||||
TotalPages: int((total + int64(limit) - 1) / int64(limit)),
|
||||
},
|
||||
}
|
||||
|
||||
return &models.ListInventoryMovementsResponse{
|
||||
Movements: movementResponses,
|
||||
TotalCount: int(total),
|
||||
Page: req.Page,
|
||||
Limit: req.Limit,
|
||||
TotalPages: totalPages,
|
||||
}, nil
|
||||
return paginatedResponse, nil
|
||||
}
|
||||
|
||||
func (p *InventoryMovementProcessorImpl) GetMovementsByProductAndOutlet(ctx context.Context, productID, outletID uuid.UUID, limit, offset int) (*models.ListInventoryMovementsResponse, error) {
|
||||
|
||||
@@ -0,0 +1,166 @@
|
||||
package processor
|
||||
|
||||
import (
|
||||
"apskel-pos-be/internal/entities"
|
||||
"apskel-pos-be/internal/mappers"
|
||||
"apskel-pos-be/internal/models"
|
||||
"context"
|
||||
"time"
|
||||
|
||||
"github.com/google/uuid"
|
||||
)
|
||||
|
||||
type UnitProcessorImpl struct {
|
||||
unitRepo UnitRepository
|
||||
}
|
||||
|
||||
func NewUnitProcessor(unitRepo UnitRepository) *UnitProcessorImpl {
|
||||
return &UnitProcessorImpl{
|
||||
unitRepo: unitRepo,
|
||||
}
|
||||
}
|
||||
|
||||
func (p *UnitProcessorImpl) CreateUnit(ctx context.Context, req *models.CreateUnitRequest) (*models.UnitResponse, error) {
|
||||
unit := &entities.Unit{
|
||||
ID: uuid.New(),
|
||||
OrganizationID: req.OrganizationID,
|
||||
OutletID: req.OutletID,
|
||||
Name: req.Name,
|
||||
Abbreviation: req.Abbreviation,
|
||||
IsActive: req.IsActive,
|
||||
CreatedAt: time.Now(),
|
||||
UpdatedAt: time.Now(),
|
||||
}
|
||||
|
||||
err := p.unitRepo.Create(ctx, unit)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
unitModel := mappers.MapUnitEntityToModel(unit)
|
||||
response := &models.UnitResponse{
|
||||
ID: unitModel.ID,
|
||||
OrganizationID: unitModel.OrganizationID,
|
||||
OutletID: unitModel.OutletID,
|
||||
Name: unitModel.Name,
|
||||
Abbreviation: unitModel.Abbreviation,
|
||||
IsActive: unitModel.IsActive,
|
||||
CreatedAt: unitModel.CreatedAt,
|
||||
UpdatedAt: unitModel.UpdatedAt,
|
||||
}
|
||||
|
||||
return response, nil
|
||||
}
|
||||
|
||||
func (p *UnitProcessorImpl) GetUnitByID(ctx context.Context, id uuid.UUID) (*models.UnitResponse, error) {
|
||||
organizationID := uuid.Nil // This should come from context
|
||||
|
||||
unit, err := p.unitRepo.GetByID(ctx, id, organizationID)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
unitModel := mappers.MapUnitEntityToModel(unit)
|
||||
response := &models.UnitResponse{
|
||||
ID: unitModel.ID,
|
||||
OrganizationID: unitModel.OrganizationID,
|
||||
OutletID: unitModel.OutletID,
|
||||
Name: unitModel.Name,
|
||||
Abbreviation: unitModel.Abbreviation,
|
||||
IsActive: unitModel.IsActive,
|
||||
CreatedAt: unitModel.CreatedAt,
|
||||
UpdatedAt: unitModel.UpdatedAt,
|
||||
}
|
||||
|
||||
return response, nil
|
||||
}
|
||||
|
||||
func (p *UnitProcessorImpl) ListUnits(ctx context.Context, organizationID uuid.UUID, outletID *uuid.UUID, page, limit int, search string) (*models.PaginatedResponse[models.UnitResponse], error) {
|
||||
if page < 1 {
|
||||
page = 1
|
||||
}
|
||||
if limit < 1 {
|
||||
limit = 10
|
||||
}
|
||||
if limit > 100 {
|
||||
limit = 100
|
||||
}
|
||||
|
||||
units, total, err := p.unitRepo.GetAll(ctx, organizationID, outletID, page, limit, search)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
unitModels := mappers.MapUnitEntitiesToModels(units)
|
||||
unitResponses := make([]models.UnitResponse, len(unitModels))
|
||||
|
||||
for i, unitModel := range unitModels {
|
||||
unitResponses[i] = models.UnitResponse{
|
||||
ID: unitModel.ID,
|
||||
OrganizationID: unitModel.OrganizationID,
|
||||
OutletID: unitModel.OutletID,
|
||||
Name: unitModel.Name,
|
||||
Abbreviation: unitModel.Abbreviation,
|
||||
IsActive: unitModel.IsActive,
|
||||
CreatedAt: unitModel.CreatedAt,
|
||||
UpdatedAt: unitModel.UpdatedAt,
|
||||
}
|
||||
}
|
||||
|
||||
paginatedResponse := &models.PaginatedResponse[models.UnitResponse]{
|
||||
Data: unitResponses,
|
||||
Pagination: models.Pagination{
|
||||
Page: page,
|
||||
Limit: limit,
|
||||
Total: int64(total),
|
||||
TotalPages: (total + limit - 1) / limit,
|
||||
},
|
||||
}
|
||||
|
||||
return paginatedResponse, nil
|
||||
}
|
||||
|
||||
func (p *UnitProcessorImpl) UpdateUnit(ctx context.Context, id uuid.UUID, req *models.UpdateUnitRequest) (*models.UnitResponse, error) {
|
||||
organizationID := uuid.Nil
|
||||
|
||||
existingUnit, err := p.unitRepo.GetByID(ctx, id, organizationID)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
existingUnit.OutletID = req.OutletID
|
||||
existingUnit.Name = req.Name
|
||||
existingUnit.Abbreviation = req.Abbreviation
|
||||
existingUnit.IsActive = req.IsActive
|
||||
existingUnit.UpdatedAt = time.Now()
|
||||
|
||||
err = p.unitRepo.Update(ctx, existingUnit)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
unitModel := mappers.MapUnitEntityToModel(existingUnit)
|
||||
response := &models.UnitResponse{
|
||||
ID: unitModel.ID,
|
||||
OrganizationID: unitModel.OrganizationID,
|
||||
OutletID: unitModel.OutletID,
|
||||
Name: unitModel.Name,
|
||||
Abbreviation: unitModel.Abbreviation,
|
||||
IsActive: unitModel.IsActive,
|
||||
CreatedAt: unitModel.CreatedAt,
|
||||
UpdatedAt: unitModel.UpdatedAt,
|
||||
}
|
||||
|
||||
return response, nil
|
||||
}
|
||||
|
||||
func (p *UnitProcessorImpl) DeleteUnit(ctx context.Context, id uuid.UUID) error {
|
||||
organizationID := uuid.Nil
|
||||
|
||||
err := p.unitRepo.Delete(ctx, id, organizationID)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
||||
@@ -0,0 +1,153 @@
|
||||
package processor
|
||||
|
||||
import (
|
||||
"apskel-pos-be/internal/models"
|
||||
"context"
|
||||
"testing"
|
||||
|
||||
"github.com/google/uuid"
|
||||
"github.com/stretchr/testify/assert"
|
||||
"github.com/stretchr/testify/mock"
|
||||
)
|
||||
|
||||
// MockUnitRepository is a mock implementation of the unit repository
|
||||
type MockUnitRepository struct {
|
||||
mock.Mock
|
||||
}
|
||||
|
||||
func (m *MockUnitRepository) Create(ctx context.Context, unit *models.Unit) error {
|
||||
args := m.Called(ctx, unit)
|
||||
return args.Error(0)
|
||||
}
|
||||
|
||||
func (m *MockUnitRepository) GetByID(ctx context.Context, id, organizationID uuid.UUID) (*models.Unit, error) {
|
||||
args := m.Called(ctx, id, organizationID)
|
||||
if args.Get(0) == nil {
|
||||
return nil, args.Error(1)
|
||||
}
|
||||
return args.Get(0).(*models.Unit), args.Error(1)
|
||||
}
|
||||
|
||||
func (m *MockUnitRepository) GetAll(ctx context.Context, organizationID uuid.UUID, outletID *uuid.UUID, page, limit int, search string) ([]*models.Unit, int, error) {
|
||||
args := m.Called(ctx, organizationID, outletID, page, limit, search)
|
||||
if args.Get(0) == nil {
|
||||
return nil, args.Int(1), args.Error(2)
|
||||
}
|
||||
return args.Get(0).([]*models.Unit), args.Int(1), args.Error(2)
|
||||
}
|
||||
|
||||
func (m *MockUnitRepository) Update(ctx context.Context, unit *models.Unit) error {
|
||||
args := m.Called(ctx, unit)
|
||||
return args.Error(0)
|
||||
}
|
||||
|
||||
func (m *MockUnitRepository) Delete(ctx context.Context, id, organizationID uuid.UUID) error {
|
||||
args := m.Called(ctx, id, organizationID)
|
||||
return args.Error(0)
|
||||
}
|
||||
|
||||
func TestUnitProcessor_Create(t *testing.T) {
|
||||
// Create mock repository
|
||||
mockRepo := &MockUnitRepository{}
|
||||
|
||||
// Create processor
|
||||
processor := NewUnitProcessor(mockRepo)
|
||||
|
||||
// Test data
|
||||
organizationID := uuid.New()
|
||||
request := &models.CreateUnitRequest{
|
||||
Name: "Gram",
|
||||
Abbreviation: "g",
|
||||
IsActive: true,
|
||||
}
|
||||
|
||||
// Mock expectations
|
||||
mockRepo.On("Create", mock.Anything, mock.AnythingOfType("*models.Unit")).Return(nil)
|
||||
|
||||
// Execute
|
||||
result, err := processor.Create(request, organizationID)
|
||||
|
||||
// Assertions
|
||||
assert.NoError(t, err)
|
||||
assert.NotNil(t, result)
|
||||
assert.Equal(t, request.Name, result.Name)
|
||||
assert.Equal(t, request.Abbreviation, result.Abbreviation)
|
||||
assert.Equal(t, request.IsActive, result.IsActive)
|
||||
assert.Equal(t, organizationID, result.OrganizationID)
|
||||
|
||||
mockRepo.AssertExpectations(t)
|
||||
}
|
||||
|
||||
func TestUnitProcessor_GetByID(t *testing.T) {
|
||||
// Create mock repository
|
||||
mockRepo := &MockUnitRepository{}
|
||||
|
||||
// Create processor
|
||||
processor := NewUnitProcessor(mockRepo)
|
||||
|
||||
// Test data
|
||||
unitID := uuid.New()
|
||||
organizationID := uuid.New()
|
||||
expectedUnit := &models.Unit{
|
||||
ID: unitID,
|
||||
OrganizationID: organizationID,
|
||||
Name: "Gram",
|
||||
Abbreviation: "g",
|
||||
IsActive: true,
|
||||
}
|
||||
|
||||
// Mock expectations
|
||||
mockRepo.On("GetByID", mock.Anything, unitID, organizationID).Return(expectedUnit, nil)
|
||||
|
||||
// Execute
|
||||
result, err := processor.GetByID(unitID, organizationID)
|
||||
|
||||
// Assertions
|
||||
assert.NoError(t, err)
|
||||
assert.NotNil(t, result)
|
||||
assert.Equal(t, expectedUnit.ID, result.ID)
|
||||
assert.Equal(t, expectedUnit.Name, result.Name)
|
||||
|
||||
mockRepo.AssertExpectations(t)
|
||||
}
|
||||
|
||||
func TestUnitProcessor_GetAll(t *testing.T) {
|
||||
// Create mock repository
|
||||
mockRepo := &MockUnitRepository{}
|
||||
|
||||
// Create processor
|
||||
processor := NewUnitProcessor(mockRepo)
|
||||
|
||||
// Test data
|
||||
organizationID := uuid.New()
|
||||
expectedUnits := []*models.Unit{
|
||||
{
|
||||
ID: uuid.New(),
|
||||
OrganizationID: organizationID,
|
||||
Name: "Gram",
|
||||
Abbreviation: "g",
|
||||
IsActive: true,
|
||||
},
|
||||
{
|
||||
ID: uuid.New(),
|
||||
OrganizationID: organizationID,
|
||||
Name: "Liter",
|
||||
Abbreviation: "L",
|
||||
IsActive: true,
|
||||
},
|
||||
}
|
||||
|
||||
// Mock expectations
|
||||
mockRepo.On("GetAll", mock.Anything, organizationID, (*uuid.UUID)(nil), 1, 10, "").Return(expectedUnits, 2, nil)
|
||||
|
||||
// Execute
|
||||
result, err := processor.GetAll(organizationID, nil, 1, 10, "")
|
||||
|
||||
// Assertions
|
||||
assert.NoError(t, err)
|
||||
assert.NotNil(t, result)
|
||||
assert.Len(t, result.Data, 2)
|
||||
assert.Equal(t, 2, result.Pagination.Total)
|
||||
|
||||
mockRepo.AssertExpectations(t)
|
||||
}
|
||||
@@ -0,0 +1,16 @@
|
||||
package processor
|
||||
|
||||
import (
|
||||
"apskel-pos-be/internal/entities"
|
||||
"context"
|
||||
|
||||
"github.com/google/uuid"
|
||||
)
|
||||
|
||||
type UnitRepository interface {
|
||||
Create(ctx context.Context, unit *entities.Unit) error
|
||||
GetByID(ctx context.Context, id, organizationID uuid.UUID) (*entities.Unit, error)
|
||||
GetAll(ctx context.Context, organizationID uuid.UUID, outletID *uuid.UUID, page, limit int, search string) ([]*entities.Unit, int, error)
|
||||
Update(ctx context.Context, unit *entities.Unit) error
|
||||
Delete(ctx context.Context, id, organizationID uuid.UUID) error
|
||||
}
|
||||
Reference in New Issue
Block a user