mirror of
https://gitlab.com/mbugroup/lti-api.git
synced 2026-05-20 13:31:56 +00:00
Feat(BE-36,37,38,39): finish master data management api
This commit is contained in:
@@ -44,6 +44,16 @@ func setupIntegrationApp(t *testing.T) (*fiber.App, *gorm.DB) {
|
||||
&entities.Warehouse{},
|
||||
&entities.Uom{},
|
||||
&entities.Customer{},
|
||||
&entities.Supplier{},
|
||||
&entities.Flag{},
|
||||
&entities.ProductCategory{},
|
||||
&entities.Nonstock{},
|
||||
&entities.NonstockSupplier{},
|
||||
&entities.Product{},
|
||||
&entities.ProductSupplier{},
|
||||
&entities.Fcr{},
|
||||
&entities.FcrStandard{},
|
||||
&entities.Bank{},
|
||||
); err != nil {
|
||||
t.Fatalf("auto migrate failed: %v", err)
|
||||
}
|
||||
@@ -60,7 +70,7 @@ func setupIntegrationApp(t *testing.T) (*fiber.App, *gorm.DB) {
|
||||
t.Fatalf("failed to seed user: %v", err)
|
||||
}
|
||||
|
||||
app := fiber.New()
|
||||
app := fiber.New(fiber.Config{ErrorHandler: utils.ErrorHandler})
|
||||
route.Routes(app, db)
|
||||
return app, db
|
||||
}
|
||||
@@ -129,6 +139,15 @@ func createLocation(t *testing.T, app *fiber.App, name, address string, areaID u
|
||||
return parseID(t, body)
|
||||
}
|
||||
|
||||
func createUom(t *testing.T, app *fiber.App, name string) uint {
|
||||
t.Helper()
|
||||
resp, body := doJSONRequest(t, app, http.MethodPost, "/api/master-data/uoms", map[string]any{"name": name})
|
||||
if resp.StatusCode != fiber.StatusCreated {
|
||||
t.Fatalf("expected 201 when creating uom, got %d: %s", resp.StatusCode, string(body))
|
||||
}
|
||||
return parseID(t, body)
|
||||
}
|
||||
|
||||
func createKandang(t *testing.T, app *fiber.App, name string, locationID, picID uint) uint {
|
||||
t.Helper()
|
||||
resp, body := doJSONRequest(t, app, http.MethodPost, "/api/master-data/kandangs", map[string]any{
|
||||
@@ -169,6 +188,152 @@ func fetchCustomer(t *testing.T, db *gorm.DB, id uint) entities.Customer {
|
||||
return customer
|
||||
}
|
||||
|
||||
func createSupplier(t *testing.T, app *fiber.App, name, alias, category string) uint {
|
||||
t.Helper()
|
||||
identifier := strings.ToLower(strings.ReplaceAll(name, " ", "_"))
|
||||
resp, body := doJSONRequest(t, app, http.MethodPost, "/api/master-data/suppliers", map[string]any{
|
||||
"name": name,
|
||||
"alias": alias,
|
||||
"pic": "John Doe",
|
||||
"type": utils.CustomerSupplierTypeBisnis,
|
||||
"category": category,
|
||||
"hatchery": "Hatchery A",
|
||||
"phone": "081234567890",
|
||||
"email": fmt.Sprintf("%s@supplier.com", identifier),
|
||||
"address": "Supplier address",
|
||||
"npwp": "NPWP-123",
|
||||
"account_number": "ACC-SUPPLIER",
|
||||
"due_date": 30,
|
||||
})
|
||||
if resp.StatusCode != fiber.StatusCreated {
|
||||
t.Fatalf("expected 201 when creating supplier, got %d: %s", resp.StatusCode, string(body))
|
||||
}
|
||||
return parseID(t, body)
|
||||
}
|
||||
|
||||
func createProductCategory(t *testing.T, app *fiber.App, name, code string) uint {
|
||||
t.Helper()
|
||||
resp, body := doJSONRequest(t, app, http.MethodPost, "/api/master-data/product-categories", map[string]any{
|
||||
"name": name,
|
||||
"code": code,
|
||||
})
|
||||
if resp.StatusCode != fiber.StatusCreated {
|
||||
t.Fatalf("expected 201 when creating product category, got %d: %s", resp.StatusCode, string(body))
|
||||
}
|
||||
return parseID(t, body)
|
||||
}
|
||||
|
||||
func fetchProductCategory(t *testing.T, db *gorm.DB, id uint) entities.ProductCategory {
|
||||
t.Helper()
|
||||
var pc entities.ProductCategory
|
||||
if err := db.Preload("CreatedUser").First(&pc, id).Error; err != nil {
|
||||
t.Fatalf("failed to fetch product category: %v", err)
|
||||
}
|
||||
return pc
|
||||
}
|
||||
|
||||
func createProduct(t *testing.T, app *fiber.App, name, brand string, sku *string, uomID, categoryID uint, productPrice float64, supplierIDs []uint, flags []string) uint {
|
||||
t.Helper()
|
||||
payload := map[string]any{
|
||||
"name": name,
|
||||
"brand": brand,
|
||||
"uom_id": uomID,
|
||||
"product_category_id": categoryID,
|
||||
"product_price": productPrice,
|
||||
"supplier_ids": supplierIDs,
|
||||
}
|
||||
if sku != nil {
|
||||
payload["sku"] = *sku
|
||||
}
|
||||
if len(flags) > 0 {
|
||||
payload["flags"] = flags
|
||||
}
|
||||
resp, body := doJSONRequest(t, app, http.MethodPost, "/api/master-data/products", payload)
|
||||
if resp.StatusCode != fiber.StatusCreated {
|
||||
t.Fatalf("expected 201 when creating product, got %d: %s", resp.StatusCode, string(body))
|
||||
}
|
||||
return parseID(t, body)
|
||||
}
|
||||
|
||||
func fetchProduct(t *testing.T, db *gorm.DB, id uint) entities.Product {
|
||||
t.Helper()
|
||||
var product entities.Product
|
||||
if err := db.Preload("CreatedUser").
|
||||
Preload("Uom").
|
||||
Preload("ProductCategory").
|
||||
Preload("Suppliers", func(tx *gorm.DB) *gorm.DB { return tx.Order("suppliers.name ASC") }).
|
||||
Preload("Flags", func(tx *gorm.DB) *gorm.DB { return tx.Order("flags.name ASC") }).
|
||||
First(&product, id).Error; err != nil {
|
||||
t.Fatalf("failed to fetch product: %v", err)
|
||||
}
|
||||
return product
|
||||
}
|
||||
|
||||
func fetchSupplier(t *testing.T, db *gorm.DB, id uint) entities.Supplier {
|
||||
t.Helper()
|
||||
var supplier entities.Supplier
|
||||
if err := db.Preload("CreatedUser").First(&supplier, id).Error; err != nil {
|
||||
t.Fatalf("failed to fetch supplier: %v", err)
|
||||
}
|
||||
return supplier
|
||||
}
|
||||
|
||||
func createFcr(t *testing.T, app *fiber.App, name string, standards []map[string]any) uint {
|
||||
t.Helper()
|
||||
payload := map[string]any{
|
||||
"name": name,
|
||||
"fcr_standards": standards,
|
||||
}
|
||||
resp, body := doJSONRequest(t, app, http.MethodPost, "/api/master-data/fcrs", payload)
|
||||
if resp.StatusCode != fiber.StatusCreated {
|
||||
t.Fatalf("expected 201 when creating fcr, got %d: %s", resp.StatusCode, string(body))
|
||||
}
|
||||
return parseID(t, body)
|
||||
}
|
||||
|
||||
func fetchFcr(t *testing.T, db *gorm.DB, id uint) entities.Fcr {
|
||||
t.Helper()
|
||||
var fcr entities.Fcr
|
||||
if err := db.Preload("CreatedUser").
|
||||
Preload("Standards", func(tx *gorm.DB) *gorm.DB {
|
||||
return tx.Order("weight ASC")
|
||||
}).
|
||||
First(&fcr, id).Error; err != nil {
|
||||
t.Fatalf("failed to fetch fcr: %v", err)
|
||||
}
|
||||
return fcr
|
||||
}
|
||||
|
||||
func createNonstock(t *testing.T, app *fiber.App, name string, uomID uint, supplierIDs []uint, flags []string) uint {
|
||||
t.Helper()
|
||||
payload := map[string]any{
|
||||
"name": name,
|
||||
"uom_id": uomID,
|
||||
"supplier_ids": supplierIDs,
|
||||
}
|
||||
if len(flags) > 0 {
|
||||
payload["flags"] = flags
|
||||
}
|
||||
resp, body := doJSONRequest(t, app, http.MethodPost, "/api/master-data/nonstocks", payload)
|
||||
if resp.StatusCode != fiber.StatusCreated {
|
||||
t.Fatalf("expected 201 when creating nonstock, got %d: %s", resp.StatusCode, string(body))
|
||||
}
|
||||
return parseID(t, body)
|
||||
}
|
||||
|
||||
func fetchNonstock(t *testing.T, db *gorm.DB, id uint) entities.Nonstock {
|
||||
t.Helper()
|
||||
var nonstock entities.Nonstock
|
||||
if err := db.Preload("CreatedUser").
|
||||
Preload("Uom").
|
||||
Preload("Suppliers", func(tx *gorm.DB) *gorm.DB { return tx.Order("suppliers.name ASC") }).
|
||||
Preload("Flags", func(tx *gorm.DB) *gorm.DB { return tx.Order("flags.name ASC") }).
|
||||
First(&nonstock, id).Error; err != nil {
|
||||
t.Fatalf("failed to fetch nonstock: %v", err)
|
||||
}
|
||||
return nonstock
|
||||
}
|
||||
|
||||
func fetchAreaName(t *testing.T, db *gorm.DB, id uint) string {
|
||||
t.Helper()
|
||||
var area entities.Area
|
||||
@@ -186,3 +351,27 @@ func fetchWarehouse(t *testing.T, db *gorm.DB, id uint) entities.Warehouse {
|
||||
}
|
||||
return wh
|
||||
}
|
||||
|
||||
func createBank(t *testing.T, app *fiber.App, name, alias, accountNumber string, owner any) uint {
|
||||
t.Helper()
|
||||
payload := map[string]any{
|
||||
"name": name,
|
||||
"alias": alias,
|
||||
"account_number": accountNumber,
|
||||
"owner": owner,
|
||||
}
|
||||
resp, body := doJSONRequest(t, app, http.MethodPost, "/api/master-data/banks", payload)
|
||||
if resp.StatusCode != fiber.StatusCreated {
|
||||
t.Fatalf("expected 201 when creating bank, got %d: %s", resp.StatusCode, string(body))
|
||||
}
|
||||
return parseID(t, body)
|
||||
}
|
||||
|
||||
func fetchBank(t *testing.T, db *gorm.DB, id uint) entities.Bank {
|
||||
t.Helper()
|
||||
var bank entities.Bank
|
||||
if err := db.Preload("CreatedUser").First(&bank, id).Error; err != nil {
|
||||
t.Fatalf("failed to fetch bank: %v", err)
|
||||
}
|
||||
return bank
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user