From 876b46b10c4ae8a1b6dd580688126cb64ee56844 Mon Sep 17 00:00:00 2001 From: "aditya.siregar" Date: Fri, 20 Dec 2024 16:52:48 +0700 Subject: [PATCH] enaklo pos backend --- infra/furtuna.development.yaml | 8 +-- internal/app/server.go | 6 +- internal/entity/product.go | 72 ++++++++------------ internal/handlers/http/discovery/discover.go | 14 ++-- internal/handlers/http/order/order.go | 11 ++- internal/handlers/http/product/product.go | 34 ++++----- internal/handlers/http/sites/sites.go | 23 +++---- internal/handlers/request/product.go | 6 +- internal/handlers/request/site.go | 19 +++--- internal/handlers/response/order.go | 11 ++- internal/handlers/response/product.go | 24 +++---- internal/middlewares/cors.go | 1 - migrations/000006_add_table_products.up.sql | 2 - 13 files changed, 101 insertions(+), 130 deletions(-) diff --git a/infra/furtuna.development.yaml b/infra/furtuna.development.yaml index 849caf0..48022d5 100644 --- a/infra/furtuna.development.yaml +++ b/infra/furtuna.development.yaml @@ -16,11 +16,11 @@ jwt: postgresql: host: 103.96.146.124 - port: 1960 + port: 2010 driver: postgres - db: fortuna-staging - username: fortuna_admin - password: 'Z4G827t9428QFQ^SZXW#43dB%!4Bmh80' + db: enaklo-pos-staging + username: admin + password: '4W^he3^BBBmPisWa$J#2' ssl-mode: disable max-idle-connections-in-second: 600 max-open-connections-in-second: 600 diff --git a/internal/app/server.go b/internal/app/server.go index c4186a0..b7e0526 100644 --- a/internal/app/server.go +++ b/internal/app/server.go @@ -13,8 +13,12 @@ import ( func NewServer(cfg *config.Config) *Server { gin.SetMode(gin.ReleaseMode) + engine := gin.New() + engine.RedirectTrailingSlash = true + engine.RedirectFixedPath = true + server := &Server{ - gin.New(), + engine, } server.Use(middlewares.Cors()) diff --git a/internal/entity/product.go b/internal/entity/product.go index 9bfd8ea..eab7f3b 100644 --- a/internal/entity/product.go +++ b/internal/entity/product.go @@ -6,22 +6,20 @@ import ( ) type Product struct { - ID int64 `gorm:"primaryKey;autoIncrement;column:id"` - PartnerID int64 `gorm:"type:int;column:partner_id"` - SiteID int64 `gorm:"type:int;column:site_id"` - Name string `gorm:"type:varchar(255);not null;column:name"` - Type string `gorm:"type:varchar;column:type"` - Price float64 `gorm:"type:decimal;column:price"` - IsWeekendTicket bool `gorm:"type:bool;column:is_weekend_ticket"` - IsSeasonTicket bool `gorm:"type:bool;column:is_season_ticket"` - Status string `gorm:"type:varchar;column:status"` - Description string `gorm:"type:varchar(255);not null;column:description"` - CreatedAt time.Time `gorm:"autoCreateTime;column:created_at"` - UpdatedAt time.Time `gorm:"autoUpdateTime;column:updated_at"` - DeletedAt *time.Time `gorm:"column:deleted_at"` - CreatedBy int64 `gorm:"type:int;column:created_by"` - UpdatedBy int64 `gorm:"type:int;column:updated_by"` - Stock int64 `gorm:"type:int;column:stock"` + ID int64 `gorm:"primaryKey;autoIncrement;column:id"` + PartnerID int64 `gorm:"type:int;column:partner_id"` + SiteID int64 `gorm:"type:int;column:site_id"` + Name string `gorm:"type:varchar(255);not null;column:name"` + Type string `gorm:"type:varchar;column:type"` + Price float64 `gorm:"type:decimal;column:price"` + Status string `gorm:"type:varchar;column:status"` + Description string `gorm:"type:varchar(255);not null;column:description"` + CreatedAt time.Time `gorm:"autoCreateTime;column:created_at"` + UpdatedAt time.Time `gorm:"autoUpdateTime;column:updated_at"` + DeletedAt *time.Time `gorm:"column:deleted_at"` + CreatedBy int64 `gorm:"type:int;column:created_by"` + UpdatedBy int64 `gorm:"type:int;column:updated_by"` + Image string `gorm:"type:varchar;column:type"` } func (Product) TableName() string { @@ -61,22 +59,19 @@ func (ProductDB) TableName() string { func (e *ProductDB) ToProduct() *Product { return &Product{ - ID: e.ID, - Name: e.Name, - Type: e.Type, - Price: e.Price, - Status: e.Status, - Description: e.Description, - PartnerID: e.PartnerID, - CreatedAt: e.CreatedAt, - UpdatedAt: e.UpdatedAt, - DeletedAt: e.DeletedAt, - CreatedBy: e.CreatedBy, - UpdatedBy: e.UpdatedBy, - SiteID: e.SiteID, - IsSeasonTicket: e.IsSeasonTicket, - IsWeekendTicket: e.IsWeekendTicket, - Stock: e.Stock, + ID: e.ID, + Name: e.Name, + Type: e.Type, + Price: e.Price, + Status: e.Status, + Description: e.Description, + PartnerID: e.PartnerID, + CreatedAt: e.CreatedAt, + UpdatedAt: e.UpdatedAt, + DeletedAt: e.DeletedAt, + CreatedBy: e.CreatedBy, + UpdatedBy: e.UpdatedBy, + SiteID: e.SiteID, } } @@ -94,20 +89,9 @@ func (b *ProductList) ToProductList() []*Product { func (b *ProductList) ToProductListPOS() []*Product { var Products []*Product - location, _ := time.LoadLocation("Asia/Jakarta") - today := time.Now().In(location).Weekday() - isWeekend := today == time.Saturday || today == time.Sunday for _, p := range *b { - if p.Status != "Available" { - continue - } - - if isWeekend && p.IsWeekendTicket { - Products = append(Products, p.ToProduct()) - } else if !isWeekend && !p.IsWeekendTicket { - Products = append(Products, p.ToProduct()) - } + Products = append(Products, p.ToProduct()) } return Products diff --git a/internal/handlers/http/discovery/discover.go b/internal/handlers/http/discovery/discover.go index 05cff71..5f5c29c 100644 --- a/internal/handlers/http/discovery/discover.go +++ b/internal/handlers/http/discovery/discover.go @@ -212,14 +212,12 @@ func ConvertToProductResp(resp []*entity.Product) *response.SearchProductSiteRes partnerID := int64(0) for _, res := range resp { productResp = append(productResp, response.SearchProductSiteByIDResponse{ - ID: res.ID, - Name: res.Name, - SiteID: res.SiteID, - Price: res.Price, - IsWeekendTicket: res.IsWeekendTicket, - IsSeasonTicket: res.IsSeasonTicket, - Description: res.Description, - Type: res.Type, + ID: res.ID, + Name: res.Name, + SiteID: res.SiteID, + Price: res.Price, + Description: res.Description, + Type: res.Type, }) partnerID = res.PartnerID diff --git a/internal/handlers/http/order/order.go b/internal/handlers/http/order/order.go index aebf8e5..64d4a57 100644 --- a/internal/handlers/http/order/order.go +++ b/internal/handlers/http/order/order.go @@ -446,12 +446,11 @@ func MapOrderToPrintDetailResponse(order *entity.OrderPrintDetail, casherName st orderItems := make([]response.CreateOrderItemResponse, len(order.OrderItems)) for i, item := range order.OrderItems { orderItems[i] = response.CreateOrderItemResponse{ - ID: item.ID, - ItemID: item.ItemID, - Quantity: item.Quantity, - Price: item.Price, - Name: item.Product.Name, - IsSeasonTicket: item.Product.IsSeasonTicket, + ID: item.ID, + ItemID: item.ItemID, + Quantity: item.Quantity, + Price: item.Price, + Name: item.Product.Name, } } diff --git a/internal/handlers/http/product/product.go b/internal/handlers/http/product/product.go index bad35ea..deeba45 100644 --- a/internal/handlers/http/product/product.go +++ b/internal/handlers/http/product/product.go @@ -19,9 +19,9 @@ type Handler struct { } func (h *Handler) Route(group *gin.RouterGroup, jwt gin.HandlerFunc) { - route := group.Group("/product") + route := group.Group("/products") - route.POST("/", jwt, h.Create) + route.POST("/create", jwt, h.Create) route.GET("/pos", jwt, h.GetPOSProduct) route.GET("/list", jwt, h.GetAll) route.PUT("/:id", jwt, h.Update) @@ -56,11 +56,8 @@ func (h *Handler) Create(c *gin.Context) { return } - validate := validator.New() - if err := validate.Struct(req); err != nil { - response.ErrorWrapper(c, err) - return - } + req.PartnerID = *ctx.GetPartnerID() + req.SiteID = *ctx.GetSiteID() res, err := h.service.Create(ctx, req.ToEntity()) @@ -272,19 +269,16 @@ func (h *Handler) GetByID(c *gin.Context) { func (h *Handler) toProductResponse(resp *entity.Product) response.Product { return response.Product{ - ID: resp.ID, - Name: resp.Name, - Type: resp.Type, - Price: resp.Price, - Status: resp.Status, - Description: resp.Description, - CreatedAt: resp.CreatedAt.Format(time.RFC3339), - UpdatedAt: resp.CreatedAt.Format(time.RFC3339), - PartnerID: resp.PartnerID, - SiteID: resp.SiteID, - IsSeasonTicket: resp.IsSeasonTicket, - IsWeekendTicket: resp.IsWeekendTicket, - Stock: resp.Stock, + ID: resp.ID, + Name: resp.Name, + Type: resp.Type, + Price: resp.Price, + Status: resp.Status, + Description: resp.Description, + CreatedAt: resp.CreatedAt.Format(time.RFC3339), + UpdatedAt: resp.CreatedAt.Format(time.RFC3339), + PartnerID: resp.PartnerID, + SiteID: resp.SiteID, } } diff --git a/internal/handlers/http/sites/sites.go b/internal/handlers/http/sites/sites.go index 8eda8dc..3b3851f 100644 --- a/internal/handlers/http/sites/sites.go +++ b/internal/handlers/http/sites/sites.go @@ -312,19 +312,16 @@ func (h *Handler) toProductResponseList(products []entity.Product) []response.Pr var res []response.Product for _, product := range products { res = append(res, response.Product{ - ID: product.ID, - PartnerID: product.PartnerID, - SiteID: product.SiteID, - Name: product.Name, - Type: product.Type, - Price: product.Price, - IsWeekendTicket: product.IsWeekendTicket, - IsSeasonTicket: product.IsSeasonTicket, - Status: product.Status, - Description: product.Description, - CreatedAt: product.CreatedAt.Format(time.RFC3339), - UpdatedAt: product.UpdatedAt.Format(time.RFC3339), - Stock: product.Stock, + ID: product.ID, + PartnerID: product.PartnerID, + SiteID: product.SiteID, + Name: product.Name, + Type: product.Type, + Price: product.Price, + Status: product.Status, + Description: product.Description, + CreatedAt: product.CreatedAt.Format(time.RFC3339), + UpdatedAt: product.UpdatedAt.Format(time.RFC3339), }) } return res diff --git a/internal/handlers/request/product.go b/internal/handlers/request/product.go index 6b12a06..7e5a730 100644 --- a/internal/handlers/request/product.go +++ b/internal/handlers/request/product.go @@ -30,6 +30,7 @@ func (p *ProductParam) ToEntity() entity.ProductSearch { type Product struct { ID int64 `json:"id,omitempty"` PartnerID int64 `json:"partner_id"` + SiteID int64 `json:"site_id"` Name string `json:"name" validate:"required"` Type string `json:"type"` Price float64 `json:"price" validate:"required"` @@ -38,6 +39,7 @@ type Product struct { Status string `json:"status"` Description string `json:"description" validate:"required"` Stock int64 `json:"stock"` + Image string `json:"image"` } func (e *Product) ToEntity() *entity.Product { @@ -47,6 +49,8 @@ func (e *Product) ToEntity() *entity.Product { Price: e.Price, Status: e.Status, Description: e.Description, - Stock: e.Stock, + PartnerID: e.PartnerID, + SiteID: e.SiteID, + Image: e.Image, } } diff --git a/internal/handlers/request/site.go b/internal/handlers/request/site.go index d83726f..6e3e442 100644 --- a/internal/handlers/request/site.go +++ b/internal/handlers/request/site.go @@ -31,17 +31,14 @@ func (r *Site) ToEntity(createdBy int64) *entity.Site { var products []entity.Product for _, p := range r.Products { products = append(products, entity.Product{ - ID: p.ID, - PartnerID: *r.PartnerID, - Name: p.Name, - Type: p.Type, - Price: p.Price, - IsWeekendTicket: p.IsWeekendTicket, - IsSeasonTicket: p.IsSeasonTicket, - Status: p.Status, - Description: p.Description, - CreatedBy: createdBy, - Stock: p.Stock, + ID: p.ID, + PartnerID: *r.PartnerID, + Name: p.Name, + Type: p.Type, + Price: p.Price, + Status: p.Status, + Description: p.Description, + CreatedBy: createdBy, }) } diff --git a/internal/handlers/response/order.go b/internal/handlers/response/order.go index 53f2ab8..d700875 100644 --- a/internal/handlers/response/order.go +++ b/internal/handlers/response/order.go @@ -151,12 +151,11 @@ type CheckingInquiryResponse struct { } type CreateOrderItemResponse struct { - ID int64 `json:"id"` - ItemID int64 `json:"item_id"` - Quantity int64 `json:"quantity"` - Price float64 `json:"price"` - Name string `json:"name"` - IsSeasonTicket bool `json:"is_season_ticket"` + ID int64 `json:"id"` + ItemID int64 `json:"item_id"` + Quantity int64 `json:"quantity"` + Price float64 `json:"price"` + Name string `json:"name"` } type ProductDailySales struct { diff --git a/internal/handlers/response/product.go b/internal/handlers/response/product.go index 3f62b8b..57bd004 100644 --- a/internal/handlers/response/product.go +++ b/internal/handlers/response/product.go @@ -1,19 +1,17 @@ package response type Product struct { - ID int64 `json:"id"` - PartnerID int64 `json:"partner_id"` - SiteID int64 `json:"site_id"` - Name string `json:"name"` - Type string `json:"type"` - Price float64 `json:"price"` - IsWeekendTicket bool `json:"is_weekend_ticket"` - IsSeasonTicket bool `json:"is_season_ticket"` - Status string `json:"status"` - Description string `json:"description"` - CreatedAt string `json:"created_at"` - UpdatedAt string `json:"updated_at"` - Stock int64 `json:"stock"` + ID int64 `json:"id"` + PartnerID int64 `json:"partner_id"` + SiteID int64 `json:"site_id"` + Name string `json:"name"` + Type string `json:"type"` + Price float64 `json:"price"` + Status string `json:"status"` + Description string `json:"description"` + CreatedAt string `json:"created_at"` + UpdatedAt string `json:"updated_at"` + Image string `json:"image"` } type ProductList struct { diff --git a/internal/middlewares/cors.go b/internal/middlewares/cors.go index d21e318..0d1f783 100644 --- a/internal/middlewares/cors.go +++ b/internal/middlewares/cors.go @@ -12,7 +12,6 @@ import ( func Cors() gin.HandlerFunc { return func(c *gin.Context) { c.Header("Access-Control-Allow-Origin", "*") - c.Header("Access-Control-Allow-Origin", "http://localhost:3000") c.Header("Access-Control-Allow-Credentials", "true") c.Header("Access-Control-Allow-Headers", "Content-Type, Content-Length, Accept-Encoding, X-CSRF-Token, Authorization, Accept, origin, Referer, Cache-Control, X-Requested-With") c.Header("Access-Control-Allow-Methods", "POST,HEAD,PATCH, OPTIONS, GET, PUT, DELETE") diff --git a/migrations/000006_add_table_products.up.sql b/migrations/000006_add_table_products.up.sql index 7000684..721c0cb 100644 --- a/migrations/000006_add_table_products.up.sql +++ b/migrations/000006_add_table_products.up.sql @@ -6,8 +6,6 @@ CREATE TABLE products name VARCHAR(255) NOT NULL, type VARCHAR, price decimal, - is_weekend_ticket boolean, - is_season_ticket boolean, status VARCHAR, description VARCHAR(255) NOT NULL, created_at TIMESTAMP NOT NULL DEFAULT NOW(),