From 8f78dd25cf2b69c44634b6506824aefac9b66399 Mon Sep 17 00:00:00 2001 From: nochill Date: Tue, 21 Mar 2023 13:15:14 +0700 Subject: [PATCH] add created by params for sale and purchase --- api/purchase_product.go | 13 ++++++++++++- api/user.go | 2 +- db/migrations/000001_init_schema.up.sql | 4 +++- db/mock/store.go | 15 +++++++++++++++ db/query/purchase_order.sql | 5 +++-- db/sqlc/models.go | 2 ++ db/sqlc/product_category_test.go | 2 +- db/sqlc/product_test.go | 2 +- db/sqlc/purchase_order.sql.go | 16 +++++++++++----- db/sqlc/querier.go | 1 + db/sqlc/store.go | 1 + db/sqlc/suppliers_test.go | 2 +- 12 files changed, 52 insertions(+), 13 deletions(-) diff --git a/api/purchase_product.go b/api/purchase_product.go index adf8977..7560f2b 100644 --- a/api/purchase_product.go +++ b/api/purchase_product.go @@ -5,12 +5,15 @@ import ( "net/http" db "git.nochill.in/nochill/naice_pos/db/sqlc" + "git.nochill.in/nochill/naice_pos/util" "github.com/gin-gonic/gin" "github.com/google/uuid" ) type CreatePurchaseOrderRequest struct { MerchantID uuid.UUID `json:"merchant_id" binding:"required"` + MerchantIdx int64 `json:"merchant_index" binding:"required,number"` + CreatedBy uuid.UUID `json:"created_by" binding:"required"` SupplierID uuid.UUID `json:"supplier_id" binding:"required"` Code string `json:"code"` IsPaid bool `json:"is_paid" binding:"required"` @@ -22,6 +25,13 @@ type CreatePurchaseOrderRequest struct { func (server Server) createPurchase(ctx *gin.Context) { var req CreatePurchaseOrderRequest + var code sql.NullString + + if len(req.Code) > 0 { + code = sql.NullString{Valid: true, String: req.Code} + } else { + code = sql.NullString{Valid: true, String: util.RandomTransactionCode("P", req.MerchantIdx)} + } if err := ctx.ShouldBindJSON(&req); err != nil { ctx.JSON(http.StatusBadRequest, errorResponse(err)) @@ -30,8 +40,9 @@ func (server Server) createPurchase(ctx *gin.Context) { arg := db.PurchasoOrderTxParams{ MerchantID: req.MerchantID, + CreatedBy: req.CreatedBy, SupplierID: req.SupplierID, - Code: sql.NullString{String: req.Code, Valid: len(req.Code) > 0}, + Code: code, IsPaid: req.IsPaid, Total: req.Total, PaidNominal: req.PaidNominal, diff --git a/api/user.go b/api/user.go index 30f51ec..10464bb 100644 --- a/api/user.go +++ b/api/user.go @@ -16,7 +16,7 @@ type createUserMerchantRequest struct { Email string `json:"email" binding:"required,email"` Fullname string `json:"fullname" binding:"required,alphanum"` Password string `json:"password" binding:"required"` - OutletName string `json:"outlet_name" binding:"required,alphanum"` + OutletName string `json:"outlet_name" binding:"required"` } type userMerchantResponse struct { diff --git a/db/migrations/000001_init_schema.up.sql b/db/migrations/000001_init_schema.up.sql index 0e9a64c..c7f2502 100644 --- a/db/migrations/000001_init_schema.up.sql +++ b/db/migrations/000001_init_schema.up.sql @@ -54,6 +54,7 @@ CREATE TABLE purchase_order ( "supplier_id" uuid references "suppliers"("id") not null, "merchant_id" uuid references "merchants"("id") not null, "index_id" bigserial not null, + "created_by" uuid references "users"("id") not null, "code" varchar(100), "is_paid" boolean not null, "total" double precision not null, @@ -61,7 +62,7 @@ CREATE TABLE purchase_order ( "note" text, "created_at" timestamp default(now()), - "updated_at" timestamp default(now()) + "updated_at" timestamp default(now()) ); CREATE TABLE purchase_order_detail ( @@ -82,6 +83,7 @@ CREATE TABLE sale_order ( "id" uuid default gen_random_uuid() primary key not null, "index_id" bigserial not null, "code" text, + "created_by" uuid references "users"("id") not null, "merchant_id" uuid references "merchants"("id") not null, "customer_id" uuid references "customers"("id"), "is_paid" boolean, diff --git a/db/mock/store.go b/db/mock/store.go index c971e94..671da28 100644 --- a/db/mock/store.go +++ b/db/mock/store.go @@ -126,6 +126,21 @@ func (mr *MockStoreMockRecorder) CreatePurchaseOrderDetail(arg0, arg1 interface{ return mr.mock.ctrl.RecordCallWithMethodType(mr.mock, "CreatePurchaseOrderDetail", reflect.TypeOf((*MockStore)(nil).CreatePurchaseOrderDetail), arg0, arg1) } +// CreateSaleOrder mocks base method. +func (m *MockStore) CreateSaleOrder(arg0 context.Context, arg1 db.CreateSaleOrderParams) (db.PurchaseOrder, error) { + m.ctrl.T.Helper() + ret := m.ctrl.Call(m, "CreateSaleOrder", arg0, arg1) + ret0, _ := ret[0].(db.PurchaseOrder) + ret1, _ := ret[1].(error) + return ret0, ret1 +} + +// CreateSaleOrder indicates an expected call of CreateSaleOrder. +func (mr *MockStoreMockRecorder) CreateSaleOrder(arg0, arg1 interface{}) *gomock.Call { + mr.mock.ctrl.T.Helper() + return mr.mock.ctrl.RecordCallWithMethodType(mr.mock, "CreateSaleOrder", reflect.TypeOf((*MockStore)(nil).CreateSaleOrder), arg0, arg1) +} + // CreateSession mocks base method. func (m *MockStore) CreateSession(arg0 context.Context, arg1 db.CreateSessionParams) (db.UserSession, error) { m.ctrl.T.Helper() diff --git a/db/query/purchase_order.sql b/db/query/purchase_order.sql index e5f8ccf..906dcdf 100644 --- a/db/query/purchase_order.sql +++ b/db/query/purchase_order.sql @@ -6,9 +6,10 @@ INSERT INTO purchase_order ( is_paid, total, paid_nominal, - note + note, + created_by ) VALUES ( - $1, $2, $3, $4, $5, $6, $7 + $1, $2, $3, $4, $5, $6, $7, $8 ) RETURNING *; diff --git a/db/sqlc/models.go b/db/sqlc/models.go index d3005dc..a16816a 100644 --- a/db/sqlc/models.go +++ b/db/sqlc/models.go @@ -101,6 +101,7 @@ type PurchaseOrder struct { SupplierID uuid.UUID `json:"supplier_id"` MerchantID uuid.UUID `json:"merchant_id"` IndexID int64 `json:"index_id"` + CreatedBy uuid.UUID `json:"created_by"` Code sql.NullString `json:"code"` IsPaid bool `json:"is_paid"` Total float64 `json:"total"` @@ -128,6 +129,7 @@ type SaleOrder struct { ID uuid.UUID `json:"id"` IndexID int64 `json:"index_id"` Code sql.NullString `json:"code"` + CreatedBy uuid.UUID `json:"created_by"` MerchantID uuid.UUID `json:"merchant_id"` CustomerID uuid.NullUUID `json:"customer_id"` IsPaid sql.NullBool `json:"is_paid"` diff --git a/db/sqlc/product_category_test.go b/db/sqlc/product_category_test.go index 923dc98..851375e 100644 --- a/db/sqlc/product_category_test.go +++ b/db/sqlc/product_category_test.go @@ -9,7 +9,7 @@ import ( "github.com/stretchr/testify/require" ) -var merchantID = uuid.MustParse("a848090f-0409-4386-9caa-929ae6874dbb") +var merchantID = uuid.MustParse("04a1b0a7-69b4-41da-a053-2f6b95c93195") func createRandomProductCategory(t *testing.T) ProductCategory { arg := CreateProductCategoryParams{ diff --git a/db/sqlc/product_test.go b/db/sqlc/product_test.go index a590e28..3e2e098 100644 --- a/db/sqlc/product_test.go +++ b/db/sqlc/product_test.go @@ -19,7 +19,7 @@ func createRandomProduct(t *testing.T) (Product, CreateProductParams) { productCategory := createRandomProductCategory(t) arg := CreateProductParams{ - MerchantID: uuid.MustParse("a848090f-0409-4386-9caa-929ae6874dbb"), + MerchantID: uuid.MustParse("04a1b0a7-69b4-41da-a053-2f6b95c93195"), Name: util.RandomString(10), SellingPrice: sellingPrice, PurchasePrice: purchasePrice, diff --git a/db/sqlc/purchase_order.sql.go b/db/sqlc/purchase_order.sql.go index e9bd5d4..a5cb16d 100644 --- a/db/sqlc/purchase_order.sql.go +++ b/db/sqlc/purchase_order.sql.go @@ -20,11 +20,12 @@ INSERT INTO purchase_order ( is_paid, total, paid_nominal, - note + note, + created_by ) VALUES ( - $1, $2, $3, $4, $5, $6, $7 + $1, $2, $3, $4, $5, $6, $7, $8 ) -RETURNING id, supplier_id, merchant_id, index_id, code, is_paid, total, paid_nominal, note, created_at, updated_at +RETURNING id, supplier_id, merchant_id, index_id, created_by, code, is_paid, total, paid_nominal, note, created_at, updated_at ` type CreatePurchaseOrderParams struct { @@ -35,6 +36,7 @@ type CreatePurchaseOrderParams struct { Total float64 `json:"total"` PaidNominal float64 `json:"paid_nominal"` Note sql.NullString `json:"note"` + CreatedBy uuid.UUID `json:"created_by"` } func (q *Queries) CreatePurchaseOrder(ctx context.Context, arg CreatePurchaseOrderParams) (PurchaseOrder, error) { @@ -46,6 +48,7 @@ func (q *Queries) CreatePurchaseOrder(ctx context.Context, arg CreatePurchaseOrd arg.Total, arg.PaidNominal, arg.Note, + arg.CreatedBy, ) var i PurchaseOrder err := row.Scan( @@ -53,6 +56,7 @@ func (q *Queries) CreatePurchaseOrder(ctx context.Context, arg CreatePurchaseOrd &i.SupplierID, &i.MerchantID, &i.IndexID, + &i.CreatedBy, &i.Code, &i.IsPaid, &i.Total, @@ -65,7 +69,7 @@ func (q *Queries) CreatePurchaseOrder(ctx context.Context, arg CreatePurchaseOrd } const getPurchaseOrderById = `-- name: GetPurchaseOrderById :one -SELECT id, supplier_id, merchant_id, index_id, code, is_paid, total, paid_nominal, note, created_at, updated_at +SELECT id, supplier_id, merchant_id, index_id, created_by, code, is_paid, total, paid_nominal, note, created_at, updated_at FROM purchase_order WHERE id = $1 ` @@ -78,6 +82,7 @@ func (q *Queries) GetPurchaseOrderById(ctx context.Context, id uuid.UUID) (Purch &i.SupplierID, &i.MerchantID, &i.IndexID, + &i.CreatedBy, &i.Code, &i.IsPaid, &i.Total, @@ -90,7 +95,7 @@ func (q *Queries) GetPurchaseOrderById(ctx context.Context, id uuid.UUID) (Purch } const listPurchaseOrderByMerchantId = `-- name: ListPurchaseOrderByMerchantId :many -SELECT id, supplier_id, merchant_id, index_id, code, is_paid, total, paid_nominal, note, created_at, updated_at +SELECT id, supplier_id, merchant_id, index_id, created_by, code, is_paid, total, paid_nominal, note, created_at, updated_at FROM purchase_order WHERE merchant_id = $1 ` @@ -109,6 +114,7 @@ func (q *Queries) ListPurchaseOrderByMerchantId(ctx context.Context, merchantID &i.SupplierID, &i.MerchantID, &i.IndexID, + &i.CreatedBy, &i.Code, &i.IsPaid, &i.Total, diff --git a/db/sqlc/querier.go b/db/sqlc/querier.go index 2d5ac68..035f9f7 100644 --- a/db/sqlc/querier.go +++ b/db/sqlc/querier.go @@ -17,6 +17,7 @@ type Querier interface { CreateProductCategory(ctx context.Context, arg CreateProductCategoryParams) (ProductCategory, error) CreatePurchaseOrder(ctx context.Context, arg CreatePurchaseOrderParams) (PurchaseOrder, error) CreatePurchaseOrderDetail(ctx context.Context, arg CreatePurchaseOrderDetailParams) (PurchaseOrderDetail, error) + CreateSaleOrder(ctx context.Context, arg CreateSaleOrderParams) (PurchaseOrder, error) CreateSession(ctx context.Context, arg CreateSessionParams) (UserSession, error) CreateStockLogs(ctx context.Context, arg CreateStockLogsParams) (StockLog, error) CreateSuppliers(ctx context.Context, arg CreateSuppliersParams) (Supplier, error) diff --git a/db/sqlc/store.go b/db/sqlc/store.go index ba83003..70e227e 100644 --- a/db/sqlc/store.go +++ b/db/sqlc/store.go @@ -81,6 +81,7 @@ func (store *SQLStore) PurchaseOrderTx(ctx context.Context, arg PurchasoOrderTxP MerchantID: arg.MerchantID, SupplierID: arg.SupplierID, Code: arg.Code, + CreatedBy: arg.CreatedBy, IsPaid: arg.IsPaid, Total: arg.Total, PaidNominal: arg.PaidNominal, diff --git a/db/sqlc/suppliers_test.go b/db/sqlc/suppliers_test.go index 6a6d361..d72c7b4 100644 --- a/db/sqlc/suppliers_test.go +++ b/db/sqlc/suppliers_test.go @@ -11,7 +11,7 @@ import ( func createRandomSupplier(t *testing.T) (Supplier, CreateSuppliersParams) { arg := CreateSuppliersParams{ - MerchantID: uuid.MustParse("a848090f-0409-4386-9caa-929ae6874dbb"), + MerchantID: uuid.MustParse("04a1b0a7-69b4-41da-a053-2f6b95c93195"), Name: util.RandomString(10), }