update
This commit is contained in:
@@ -0,0 +1,267 @@
|
||||
package repository
|
||||
|
||||
import (
|
||||
"enaklo-pos-be/internal/common/mycontext"
|
||||
"enaklo-pos-be/internal/constants"
|
||||
"enaklo-pos-be/internal/entity"
|
||||
"enaklo-pos-be/internal/repository/models"
|
||||
"github.com/pkg/errors"
|
||||
"gorm.io/gorm"
|
||||
time2 "time"
|
||||
)
|
||||
|
||||
type InProgressOrderRepository interface {
|
||||
CreateOrUpdate(ctx mycontext.Context, order *entity.InProgressOrder) (*entity.InProgressOrder, error)
|
||||
GetListByPartnerID(ctx mycontext.Context, partnerID int64, limit, offset int) ([]*entity.InProgressOrder, error)
|
||||
}
|
||||
|
||||
type inprogressOrderRepository struct {
|
||||
db *gorm.DB
|
||||
}
|
||||
|
||||
func NewInProgressOrderRepository(db *gorm.DB) *inprogressOrderRepository {
|
||||
return &inprogressOrderRepository{db: db}
|
||||
}
|
||||
|
||||
func (r *inprogressOrderRepository) CreateOrUpdate(ctx mycontext.Context, order *entity.InProgressOrder) (*entity.InProgressOrder, error) {
|
||||
isUpdate := order.ID != ""
|
||||
|
||||
tx := r.db.Begin()
|
||||
if tx.Error != nil {
|
||||
return nil, errors.Wrap(tx.Error, "failed to begin transaction")
|
||||
}
|
||||
defer func() {
|
||||
if r := recover(); r != nil {
|
||||
tx.Rollback()
|
||||
}
|
||||
}()
|
||||
|
||||
orderDB := r.toInProgressOrderDBModel(order)
|
||||
|
||||
if isUpdate {
|
||||
var existingOrder models.InProgressOrderDB
|
||||
if err := tx.First(&existingOrder, order.ID).Error; err != nil {
|
||||
tx.Rollback()
|
||||
return nil, errors.Wrap(err, "order not found for update")
|
||||
}
|
||||
|
||||
if err := tx.Model(&orderDB).Updates(orderDB).Error; err != nil {
|
||||
tx.Rollback()
|
||||
return nil, errors.Wrap(err, "failed to update order")
|
||||
}
|
||||
|
||||
if err := tx.Where("in_progress_order_id = ?", order.ID).Delete(&models.InProgressOrderItemDB{}).Error; err != nil {
|
||||
tx.Rollback()
|
||||
return nil, errors.Wrap(err, "failed to delete existing order items")
|
||||
}
|
||||
} else {
|
||||
if err := tx.Create(&orderDB).Error; err != nil {
|
||||
tx.Rollback()
|
||||
return nil, errors.Wrap(err, "failed to insert order")
|
||||
}
|
||||
|
||||
order.ID = orderDB.ID
|
||||
}
|
||||
|
||||
var itemIDs []int64
|
||||
for i := range order.OrderItems {
|
||||
itemIDs = append(itemIDs, order.OrderItems[i].ItemID)
|
||||
}
|
||||
|
||||
var products []models.ProductDB
|
||||
if len(itemIDs) > 0 {
|
||||
if err := tx.Where("id IN ?", itemIDs).Find(&products).Error; err != nil {
|
||||
tx.Rollback()
|
||||
return nil, errors.Wrap(err, "failed to fetch products")
|
||||
}
|
||||
}
|
||||
|
||||
productMap := make(map[int64]models.ProductDB)
|
||||
for _, product := range products {
|
||||
productMap[product.ID] = product
|
||||
}
|
||||
|
||||
for i := range order.OrderItems {
|
||||
item := &order.OrderItems[i]
|
||||
|
||||
itemDB := r.toOrderItemDBModel(item, orderDB.ID)
|
||||
|
||||
if err := tx.Create(&itemDB).Error; err != nil {
|
||||
tx.Rollback()
|
||||
return nil, errors.Wrap(err, "failed to insert order item")
|
||||
}
|
||||
|
||||
item.ID = itemDB.ID
|
||||
|
||||
if product, exists := productMap[item.ItemID]; exists {
|
||||
item.Product = r.toDomainProductModel(&product)
|
||||
}
|
||||
}
|
||||
|
||||
if err := tx.Commit().Error; err != nil {
|
||||
return nil, errors.Wrap(err, "failed to commit transaction")
|
||||
}
|
||||
|
||||
return order, nil
|
||||
}
|
||||
|
||||
func (r *inprogressOrderRepository) GetListByPartnerID(ctx mycontext.Context, partnerID int64, limit, offset int) ([]*entity.InProgressOrder, error) {
|
||||
var ordersDB []models.InProgressOrderDB
|
||||
query := r.db.Where("partner_id = ?", partnerID).Order("created_at DESC")
|
||||
|
||||
if limit > 0 {
|
||||
query = query.Limit(limit)
|
||||
}
|
||||
|
||||
if offset > 0 {
|
||||
query = query.Offset(offset)
|
||||
}
|
||||
|
||||
if err := query.Preload("OrderItems.Product").Find(&ordersDB).Error; err != nil {
|
||||
return nil, errors.Wrap(err, "failed to find orders by partner ID")
|
||||
}
|
||||
|
||||
orders := make([]*entity.InProgressOrder, 0, len(ordersDB))
|
||||
for _, orderDB := range ordersDB {
|
||||
order := r.toDomainOrderModel(&orderDB)
|
||||
order.OrderItems = make([]entity.InProgressOrderItem, 0, len(orderDB.OrderItems))
|
||||
|
||||
for _, itemDB := range orderDB.OrderItems {
|
||||
item := r.toDomainOrderItemModel(&itemDB)
|
||||
|
||||
orderItem := entity.InProgressOrderItem{
|
||||
ID: item.ID,
|
||||
ItemID: item.ItemID,
|
||||
Quantity: item.Quantity,
|
||||
}
|
||||
|
||||
if itemDB.Product.ID > 0 {
|
||||
productDomain := r.toDomainProductModel(&itemDB.Product)
|
||||
orderItem.Product = productDomain
|
||||
}
|
||||
|
||||
order.OrderItems = append(order.OrderItems, orderItem)
|
||||
}
|
||||
|
||||
orders = append(orders, order)
|
||||
}
|
||||
|
||||
return orders, nil
|
||||
}
|
||||
|
||||
func (r *inprogressOrderRepository) toInProgressOrderDBModel(order *entity.InProgressOrder) models.InProgressOrderDB {
|
||||
now := time2.Now()
|
||||
return models.InProgressOrderDB{
|
||||
ID: constants.GenerateUUID(),
|
||||
PartnerID: order.PartnerID,
|
||||
CustomerID: order.CustomerID,
|
||||
CustomerName: order.CustomerName,
|
||||
PaymentType: order.PaymentType,
|
||||
CreatedBy: order.CreatedBy,
|
||||
CreatedAt: now,
|
||||
UpdatedAt: now,
|
||||
TableNumber: order.TableNumber,
|
||||
OrderType: order.OrderType,
|
||||
}
|
||||
}
|
||||
|
||||
func (r *inprogressOrderRepository) toDomainOrderModel(dbModel *models.InProgressOrderDB) *entity.InProgressOrder {
|
||||
return &entity.InProgressOrder{
|
||||
ID: dbModel.ID,
|
||||
PartnerID: dbModel.PartnerID,
|
||||
CustomerID: dbModel.CustomerID,
|
||||
CustomerName: dbModel.CustomerName,
|
||||
PaymentType: dbModel.PaymentType,
|
||||
CreatedBy: dbModel.CreatedBy,
|
||||
OrderItems: []entity.InProgressOrderItem{},
|
||||
TableNumber: dbModel.TableNumber,
|
||||
OrderType: dbModel.OrderType,
|
||||
CreatedAt: dbModel.CreatedAt,
|
||||
UpdatedAt: dbModel.UpdatedAt,
|
||||
}
|
||||
}
|
||||
|
||||
func (r *inprogressOrderRepository) toOrderItemDBModel(item *entity.InProgressOrderItem, inprogressOrderID string) models.InProgressOrderItemDB {
|
||||
return models.InProgressOrderItemDB{
|
||||
ID: item.ID,
|
||||
InProgressOrderIO: inprogressOrderID,
|
||||
ItemID: item.ItemID,
|
||||
Quantity: item.Quantity,
|
||||
}
|
||||
}
|
||||
|
||||
func (r *inprogressOrderRepository) toDomainOrderItemModel(dbModel *models.InProgressOrderItemDB) *entity.OrderItem {
|
||||
return &entity.OrderItem{
|
||||
ID: dbModel.ID,
|
||||
ItemID: dbModel.ItemID,
|
||||
Quantity: dbModel.Quantity,
|
||||
CreatedBy: dbModel.CreatedBy,
|
||||
CreatedAt: dbModel.CreatedAt,
|
||||
}
|
||||
}
|
||||
|
||||
func (r *inprogressOrderRepository) toOrderInquiryDBModel(inquiry *entity.OrderInquiry) models.OrderInquiryDB {
|
||||
return models.OrderInquiryDB{
|
||||
ID: inquiry.ID,
|
||||
PartnerID: inquiry.PartnerID,
|
||||
CustomerID: &inquiry.CustomerID,
|
||||
Status: inquiry.Status,
|
||||
Amount: inquiry.Amount,
|
||||
Fee: inquiry.Fee,
|
||||
Total: inquiry.Total,
|
||||
PaymentType: inquiry.PaymentType,
|
||||
Source: inquiry.Source,
|
||||
CreatedBy: inquiry.CreatedBy,
|
||||
CreatedAt: inquiry.CreatedAt,
|
||||
UpdatedAt: inquiry.UpdatedAt,
|
||||
ExpiresAt: inquiry.ExpiresAt,
|
||||
CustomerName: inquiry.CustomerName,
|
||||
CustomerPhoneNumber: inquiry.CustomerPhoneNumber,
|
||||
CustomerEmail: inquiry.CustomerEmail,
|
||||
PaymentProvider: inquiry.PaymentProvider,
|
||||
OrderType: inquiry.OrderType,
|
||||
TableNumber: inquiry.TableNumber,
|
||||
}
|
||||
}
|
||||
|
||||
func (r *inprogressOrderRepository) toDomainOrderInquiryModel(dbModel *models.OrderInquiryDB) *entity.OrderInquiry {
|
||||
inquiry := &entity.OrderInquiry{
|
||||
ID: dbModel.ID,
|
||||
PartnerID: dbModel.PartnerID,
|
||||
Status: dbModel.Status,
|
||||
Amount: dbModel.Amount,
|
||||
Fee: dbModel.Fee,
|
||||
Total: dbModel.Total,
|
||||
PaymentType: dbModel.PaymentType,
|
||||
Source: dbModel.Source,
|
||||
CreatedBy: dbModel.CreatedBy,
|
||||
CreatedAt: dbModel.CreatedAt,
|
||||
ExpiresAt: dbModel.ExpiresAt,
|
||||
OrderItems: []entity.OrderItem{},
|
||||
}
|
||||
|
||||
if dbModel.CustomerID != nil {
|
||||
inquiry.CustomerID = *dbModel.CustomerID
|
||||
}
|
||||
|
||||
inquiry.UpdatedAt = dbModel.UpdatedAt
|
||||
|
||||
return inquiry
|
||||
}
|
||||
|
||||
func (r *inprogressOrderRepository) toDomainProductModel(productDB *models.ProductDB) *entity.Product {
|
||||
if productDB == nil {
|
||||
return nil
|
||||
}
|
||||
|
||||
return &entity.Product{
|
||||
ID: productDB.ID,
|
||||
Name: productDB.Name,
|
||||
Description: productDB.Description,
|
||||
Price: productDB.Price,
|
||||
CreatedAt: productDB.CreatedAt,
|
||||
UpdatedAt: productDB.UpdatedAt,
|
||||
Type: productDB.Type,
|
||||
Image: productDB.Image,
|
||||
}
|
||||
}
|
||||
@@ -4,8 +4,11 @@ import (
|
||||
"enaklo-pos-be/internal/common/mycontext"
|
||||
"enaklo-pos-be/internal/entity"
|
||||
"enaklo-pos-be/internal/repository/models"
|
||||
"fmt"
|
||||
"github.com/google/uuid"
|
||||
"github.com/pkg/errors"
|
||||
"gorm.io/gorm"
|
||||
"math/rand"
|
||||
"time"
|
||||
)
|
||||
|
||||
@@ -14,9 +17,10 @@ type CustomerRepo interface {
|
||||
FindByID(ctx mycontext.Context, id int64) (*entity.Customer, error)
|
||||
FindByPhone(ctx mycontext.Context, phone string) (*entity.Customer, error)
|
||||
FindByEmail(ctx mycontext.Context, email string) (*entity.Customer, error)
|
||||
AddPoints(ctx mycontext.Context, id int64, points int) error
|
||||
AddPoints(ctx mycontext.Context, id int64, points int, reference string) error
|
||||
FindSequence(ctx mycontext.Context, partnerID int64) (int64, error)
|
||||
GetAllCustomers(ctx mycontext.Context, req entity.MemberSearch) (entity.MemberList, int, error)
|
||||
VerifyOTP(ctx mycontext.Context, verificationHash string, otpCode string) (int64, error)
|
||||
}
|
||||
|
||||
type customerRepository struct {
|
||||
@@ -28,13 +32,53 @@ func NewCustomerRepository(db *gorm.DB) *customerRepository {
|
||||
}
|
||||
|
||||
func (r *customerRepository) Create(ctx mycontext.Context, customer *entity.Customer) (*entity.Customer, error) {
|
||||
customerDB := r.toCustomerDBModel(customer)
|
||||
tx := r.db.Begin()
|
||||
if tx.Error != nil {
|
||||
return nil, errors.Wrap(tx.Error, "failed to begin transaction")
|
||||
}
|
||||
|
||||
if err := r.db.Create(&customerDB).Error; err != nil {
|
||||
customerDB := r.toCustomerDBModel(customer)
|
||||
if err := tx.Omit("CustomerID").Create(&customerDB).Error; err != nil {
|
||||
tx.Rollback()
|
||||
return nil, errors.Wrap(err, "failed to insert customer")
|
||||
}
|
||||
|
||||
customerPoints := models.CustomerPointsDB{
|
||||
CustomerID: uint64(customerDB.ID),
|
||||
TotalPoints: 0,
|
||||
AvailablePoints: 0,
|
||||
LastUpdated: time.Now(),
|
||||
}
|
||||
|
||||
if err := tx.Create(&customerPoints).Error; err != nil {
|
||||
tx.Rollback()
|
||||
return nil, errors.Wrap(err, "failed to create initial customer points")
|
||||
}
|
||||
|
||||
otpCode := r.generateOTPCode()
|
||||
expiresAt := time.Now().Add(15 * time.Minute)
|
||||
|
||||
verificationCode := models.CustomerVerificationCodeDB{
|
||||
CustomerID: uint64(customerDB.ID),
|
||||
Code: otpCode,
|
||||
Type: "EMAIL",
|
||||
ExpiresAt: expiresAt,
|
||||
IsUsed: false,
|
||||
VerificationID: uuid.New(),
|
||||
}
|
||||
|
||||
if err := tx.Create(&verificationCode).Error; err != nil {
|
||||
tx.Rollback()
|
||||
return nil, errors.Wrap(err, "failed to create verification code")
|
||||
}
|
||||
|
||||
if err := tx.Commit().Error; err != nil {
|
||||
return nil, errors.Wrap(err, "failed to commit transaction")
|
||||
}
|
||||
|
||||
customer.ID = customerDB.ID
|
||||
customer.VerificationID = verificationCode.VerificationID.String()
|
||||
customer.OTP = otpCode
|
||||
|
||||
return customer, nil
|
||||
}
|
||||
@@ -84,22 +128,45 @@ func (r *customerRepository) FindByEmail(ctx mycontext.Context, email string) (*
|
||||
return customer, nil
|
||||
}
|
||||
|
||||
func (r *customerRepository) AddPoints(ctx mycontext.Context, id int64, points int) error {
|
||||
now := time.Now()
|
||||
func (r *customerRepository) AddPoints(ctx mycontext.Context, customerID int64, points int, reference string) error {
|
||||
tx := r.db.Begin()
|
||||
if tx.Error != nil {
|
||||
return errors.Wrap(tx.Error, "failed to begin transaction")
|
||||
}
|
||||
|
||||
result := r.db.Model(&models.CustomerDB{}).
|
||||
Where("id = ?", id).
|
||||
result := tx.Model(&models.CustomerPointsDB{}).
|
||||
Where("customer_id = ?", customerID).
|
||||
Updates(map[string]interface{}{
|
||||
"points": gorm.Expr("points + ?", points),
|
||||
"updated_at": now,
|
||||
"total_points": gorm.Expr("total_points + ?", points),
|
||||
"available_points": gorm.Expr("available_points + ?", points),
|
||||
"last_updated": time.Now(),
|
||||
})
|
||||
|
||||
if result.Error != nil {
|
||||
return errors.Wrap(result.Error, "failed to add points to customer")
|
||||
tx.Rollback()
|
||||
return errors.Wrap(result.Error, "failed to update customer points")
|
||||
}
|
||||
|
||||
if result.RowsAffected == 0 {
|
||||
return errors.New("customer not found")
|
||||
tx.Rollback()
|
||||
return errors.New("customer points record not found")
|
||||
}
|
||||
|
||||
pointTransaction := models.CustomerPointTransactionDB{
|
||||
CustomerID: customerID,
|
||||
Reference: reference,
|
||||
PointsEarned: points,
|
||||
TransactionDate: time.Now(),
|
||||
Status: "SUCCESS",
|
||||
}
|
||||
|
||||
if err := tx.Create(&pointTransaction).Error; err != nil {
|
||||
tx.Rollback()
|
||||
return errors.Wrap(err, "failed to create point transaction record")
|
||||
}
|
||||
|
||||
if err := tx.Commit().Error; err != nil {
|
||||
return errors.Wrap(err, "failed to commit transaction")
|
||||
}
|
||||
|
||||
return nil
|
||||
@@ -107,15 +174,15 @@ func (r *customerRepository) AddPoints(ctx mycontext.Context, id int64, points i
|
||||
|
||||
func (r *customerRepository) toCustomerDBModel(customer *entity.Customer) models.CustomerDB {
|
||||
return models.CustomerDB{
|
||||
ID: customer.ID,
|
||||
Name: customer.Name,
|
||||
Email: customer.Email,
|
||||
Phone: customer.Phone,
|
||||
Points: customer.Points,
|
||||
CreatedAt: customer.CreatedAt,
|
||||
UpdatedAt: customer.UpdatedAt,
|
||||
CustomerID: customer.CustomerID,
|
||||
BirthDate: customer.BirthDate,
|
||||
ID: customer.ID,
|
||||
Name: customer.Name,
|
||||
Email: customer.Email,
|
||||
Phone: customer.Phone,
|
||||
Points: customer.Points,
|
||||
CreatedAt: customer.CreatedAt,
|
||||
UpdatedAt: customer.UpdatedAt,
|
||||
BirthDate: customer.BirthDate,
|
||||
Password: customer.Password,
|
||||
}
|
||||
}
|
||||
|
||||
@@ -232,3 +299,59 @@ func (r *customerRepository) GetAllCustomers(ctx mycontext.Context, req entity.M
|
||||
|
||||
return customers, int(totalCount), nil
|
||||
}
|
||||
|
||||
func (r *customerRepository) generateOTPCode() string {
|
||||
rand.Seed(time.Now().UnixNano())
|
||||
otpCode := fmt.Sprintf("%06d", rand.Intn(1000000))
|
||||
return otpCode
|
||||
}
|
||||
|
||||
func (r *customerRepository) VerifyOTP(ctx mycontext.Context, verificationHash string, otpCode string) (int64, error) {
|
||||
var verificationCode models.CustomerVerificationCodeDB
|
||||
if err := r.db.Where("verification_id = ? AND is_used = false", verificationHash).First(&verificationCode).Error; err != nil {
|
||||
if errors.Is(err, gorm.ErrRecordNotFound) {
|
||||
return 0, errors.New("invalid or expired verification code")
|
||||
}
|
||||
return 0, errors.Wrap(err, "failed to find verification code")
|
||||
}
|
||||
|
||||
if time.Now().After(verificationCode.ExpiresAt) {
|
||||
return 0, errors.New("verification code has expired")
|
||||
}
|
||||
|
||||
if verificationCode.Code != otpCode {
|
||||
return 0, errors.New("invalid verification code")
|
||||
}
|
||||
|
||||
tx := r.db.Begin()
|
||||
if tx.Error != nil {
|
||||
return 0, errors.Wrap(tx.Error, "failed to begin transaction")
|
||||
}
|
||||
|
||||
if err := tx.Model(&verificationCode).Updates(map[string]interface{}{
|
||||
"is_used": true,
|
||||
}).Error; err != nil {
|
||||
tx.Rollback()
|
||||
return 0, errors.Wrap(err, "failed to mark verification code as used")
|
||||
}
|
||||
|
||||
if verificationCode.Type == "EMAIL" {
|
||||
if err := tx.Model(&models.CustomerDB{}).Where("id = ?", verificationCode.CustomerID).
|
||||
Update("is_email_verified", true).Error; err != nil {
|
||||
tx.Rollback()
|
||||
return 0, errors.Wrap(err, "failed to update customer verification status")
|
||||
}
|
||||
} else if verificationCode.Type == "PHONE" {
|
||||
if err := tx.Model(&models.CustomerDB{}).Where("id = ?", verificationCode.CustomerID).
|
||||
Update("is_phone_verified", true).Error; err != nil {
|
||||
tx.Rollback()
|
||||
return 0, errors.Wrap(err, "failed to update customer verification status")
|
||||
}
|
||||
}
|
||||
|
||||
if err := tx.Commit().Error; err != nil {
|
||||
return 0, errors.Wrap(err, "failed to commit transaction")
|
||||
}
|
||||
|
||||
return int64(verificationCode.CustomerID), nil
|
||||
}
|
||||
|
||||
@@ -1,19 +1,23 @@
|
||||
package models
|
||||
|
||||
import (
|
||||
"github.com/google/uuid"
|
||||
"time"
|
||||
)
|
||||
|
||||
type CustomerDB struct {
|
||||
ID int64 `gorm:"primaryKey;column:id"`
|
||||
Name string `gorm:"column:name"`
|
||||
Email string `gorm:"column:email"`
|
||||
Phone string `gorm:"column:phone"`
|
||||
Points int `gorm:"column:points"`
|
||||
CreatedAt time.Time `gorm:"column:created_at"`
|
||||
UpdatedAt time.Time `gorm:"column:updated_at"`
|
||||
CustomerID string `gorm:"column:customer_id"`
|
||||
BirthDate time.Time `gorm:"column:birth_date"`
|
||||
ID int64 `gorm:"primaryKey;column:id"`
|
||||
Name string `gorm:"column:name"`
|
||||
Email string `gorm:"column:email"`
|
||||
Phone string `gorm:"column:phone"`
|
||||
Points int `gorm:"column:points"`
|
||||
CreatedAt time.Time `gorm:"column:created_at"`
|
||||
UpdatedAt time.Time `gorm:"column:updated_at"`
|
||||
CustomerID string `gorm:"column:customer_id"`
|
||||
BirthDate time.Time `gorm:"column:birth_date"`
|
||||
Password string `gorm:"column:password"`
|
||||
IsEmailVerified bool `gorm:"column:is_email_verified"`
|
||||
IsPhoneVerified bool `gorm:"column:is_phone_verified"`
|
||||
}
|
||||
|
||||
func (CustomerDB) TableName() string {
|
||||
@@ -30,3 +34,45 @@ type PartnerMemberSequence struct {
|
||||
func (PartnerMemberSequence) TableName() string {
|
||||
return "partner_member_sequences"
|
||||
}
|
||||
|
||||
type CustomerPointsDB struct {
|
||||
ID uint64 `gorm:"column:id;primaryKey;autoIncrement"`
|
||||
CustomerID uint64 `gorm:"column:customer_id;not null"`
|
||||
TotalPoints int `gorm:"column:total_points;not null;default:0"`
|
||||
AvailablePoints int `gorm:"column:available_points;not null;default:0"`
|
||||
LastUpdated time.Time `gorm:"column:last_updated;default:CURRENT_TIMESTAMP"`
|
||||
}
|
||||
|
||||
func (CustomerPointsDB) TableName() string {
|
||||
return "customer_points"
|
||||
}
|
||||
|
||||
type CustomerPointTransactionDB struct {
|
||||
ID uint64 `gorm:"column:id;primaryKey;autoIncrement"`
|
||||
CustomerID int64 `gorm:"column:customer_id;not null"`
|
||||
Reference string `gorm:"column:transaction_id"`
|
||||
PointsEarned int `gorm:"column:points_earned;not null"`
|
||||
TransactionDate time.Time `gorm:"column:transaction_date;not null"`
|
||||
ExpirationDate *time.Time `gorm:"column:expiration_date"`
|
||||
Status string `gorm:"column:status;default:active"`
|
||||
CreatedAt time.Time `gorm:"column:created_at;default:CURRENT_TIMESTAMP"`
|
||||
}
|
||||
|
||||
func (CustomerPointTransactionDB) TableName() string {
|
||||
return "customer_point_transactions"
|
||||
}
|
||||
|
||||
type CustomerVerificationCodeDB struct {
|
||||
ID uint64 `gorm:"column:id;primaryKey;autoIncrement"`
|
||||
CustomerID uint64 `gorm:"column:customer_id;not null"`
|
||||
Code string `gorm:"column:code;not null"`
|
||||
Type string `gorm:"column:type;not null"`
|
||||
ExpiresAt time.Time `gorm:"column:expires_at;not null"`
|
||||
IsUsed bool `gorm:"column:is_used;default:false"`
|
||||
CreatedAt time.Time `gorm:"column:created_at;default:CURRENT_TIMESTAMP"`
|
||||
VerificationID uuid.UUID `gorm:"column:verification_id;type:uuid;default:uuid_generate_v4()"`
|
||||
}
|
||||
|
||||
func (CustomerVerificationCodeDB) TableName() string {
|
||||
return "customer_verification_codes"
|
||||
}
|
||||
|
||||
@@ -0,0 +1,35 @@
|
||||
package models
|
||||
|
||||
import "time"
|
||||
|
||||
type InProgressOrderDB struct {
|
||||
ID string `gorm:"primaryKey;column:id"`
|
||||
PartnerID int64 `gorm:"column:partner_id"`
|
||||
CustomerID *int64 `gorm:"column:customer_id"`
|
||||
CustomerName string `gorm:"column:customer_name"`
|
||||
PaymentType string `gorm:"column:payment_type"`
|
||||
CreatedBy int64 `gorm:"column:created_by"`
|
||||
CreatedAt time.Time `gorm:"column:created_at"`
|
||||
UpdatedAt time.Time `gorm:"column:updated_at"`
|
||||
TableNumber string `gorm:"column:table_number"`
|
||||
OrderItems []InProgressOrderItemDB `gorm:"foreignKey:InProgressOrderIO"`
|
||||
OrderType string `gorm:"column:order_type"`
|
||||
}
|
||||
|
||||
type InProgressOrderItemDB struct {
|
||||
ID int64 `gorm:"primaryKey;column:id"`
|
||||
InProgressOrderIO string `gorm:"column:in_progress_order_id"`
|
||||
ItemID int64 `gorm:"column:item_id"`
|
||||
Quantity int `gorm:"column:quantity"`
|
||||
CreatedBy int64 `gorm:"column:created_by"`
|
||||
CreatedAt time.Time `gorm:"column:created_at"`
|
||||
Product ProductDB `gorm:"foreignKey:ItemID;references:ID"`
|
||||
}
|
||||
|
||||
func (InProgressOrderItemDB) TableName() string {
|
||||
return "in_progress_order_items"
|
||||
}
|
||||
|
||||
func (InProgressOrderDB) TableName() string {
|
||||
return "in_progress_order"
|
||||
}
|
||||
@@ -58,6 +58,9 @@ type OrderInquiryDB struct {
|
||||
UpdatedAt time.Time `gorm:"column:updated_at"`
|
||||
ExpiresAt time.Time `gorm:"column:expires_at"`
|
||||
InquiryItems []InquiryItemDB `gorm:"foreignKey:InquiryID"`
|
||||
PaymentProvider string `gorm:"column:payment_provider"`
|
||||
TableNumber string `gorm:"column:table_number"`
|
||||
OrderType string `gorm:"column:order_type"`
|
||||
}
|
||||
|
||||
func (OrderInquiryDB) TableName() string {
|
||||
|
||||
@@ -15,6 +15,7 @@ type ProductDB struct {
|
||||
Status string `gorm:"column:status"`
|
||||
CreatedAt time.Time `gorm:"column:created_at"`
|
||||
UpdatedAt time.Time `gorm:"column:updated_at"`
|
||||
Image string `gorm:"column:image"`
|
||||
}
|
||||
|
||||
func (ProductDB) TableName() string {
|
||||
|
||||
@@ -61,6 +61,18 @@ func (r *orderRepository) Create(ctx mycontext.Context, order *entity.Order) (*e
|
||||
item.ID = itemDB.ID
|
||||
}
|
||||
|
||||
if order.InProgressOrderID != "" {
|
||||
if err := tx.Where("in_progress_order_id = ?", order.InProgressOrderID).Delete(&models.InProgressOrderItemDB{}).Error; err != nil {
|
||||
tx.Rollback()
|
||||
return nil, errors.Wrap(err, "failed to delete in-progress order items")
|
||||
}
|
||||
|
||||
if err := tx.Where("id = ?", order.InProgressOrderID).Delete(&models.InProgressOrderDB{}).Error; err != nil {
|
||||
tx.Rollback()
|
||||
return nil, errors.Wrap(err, "failed to delete in-progress order")
|
||||
}
|
||||
}
|
||||
|
||||
if err := tx.Commit().Error; err != nil {
|
||||
return nil, errors.Wrap(err, "failed to commit transaction")
|
||||
}
|
||||
@@ -263,6 +275,9 @@ func (r *orderRepository) toOrderInquiryDBModel(inquiry *entity.OrderInquiry) mo
|
||||
CustomerName: inquiry.CustomerName,
|
||||
CustomerPhoneNumber: inquiry.CustomerPhoneNumber,
|
||||
CustomerEmail: inquiry.CustomerEmail,
|
||||
PaymentProvider: inquiry.PaymentProvider,
|
||||
OrderType: inquiry.OrderType,
|
||||
TableNumber: inquiry.TableNumber,
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -141,9 +141,8 @@ func (b *OrderRepository) GetAllHystoryOrders(ctx context.Context, req entity.Or
|
||||
|
||||
query := b.db.Table("orders").
|
||||
Select("orders.id as id, users.name as employee, sites.name as site, orders.created_at as timestamp, orders.created_at as booking_time, STRING_AGG(ticket_summary.name || ' x' || ticket_summary.total_qty, ', ') AS tickets, orders.payment_type as payment_type, orders.status as status, orders.amount as amount, orders.visit_date as visit_date, orders.ticket_status as ticket_status, orders.source as source").
|
||||
Joins("left join (SELECT items.order_id, products.name, SUM(items.qty) AS total_qty FROM order_items items LEFT JOIN products ON items.item_id = products.id GROUP BY items.order_id, products.name) AS ticket_summary ON orders.id = ticket_summary.order_id").
|
||||
Joins("left join (SELECT items.order_id, products.name, SUM(items.quantity) AS total_qty FROM order_items items LEFT JOIN products ON items.item_id = products.id GROUP BY items.order_id, products.name) AS ticket_summary ON orders.id = ticket_summary.order_id").
|
||||
Joins("left join users on orders.created_by = users.id").
|
||||
Joins("left join sites on orders.site_id = sites.id").
|
||||
Where("orders.payment_type != ?", "NEW")
|
||||
|
||||
if req.PaymentType != "" {
|
||||
@@ -176,7 +175,7 @@ func (b *OrderRepository) GetAllHystoryOrders(ctx context.Context, req entity.Or
|
||||
}
|
||||
|
||||
if req.SiteID != nil {
|
||||
query = query.Where("orders.site_id = ?", req.SiteID)
|
||||
query = query.Where("orders.partner_id = ?", req.SiteID)
|
||||
}
|
||||
|
||||
if req.Source != "" {
|
||||
@@ -253,10 +252,6 @@ func (r *OrderRepository) SumAmount(ctx mycontext.Context, req entity.OrderSearc
|
||||
query = query.Where("orders.partner_id = ?", req.PartnerID)
|
||||
}
|
||||
|
||||
if req.SiteID != nil {
|
||||
query = query.Where("orders.site_id = ?", req.SiteID)
|
||||
}
|
||||
|
||||
if err := query.Scan(&amount).Error; err != nil {
|
||||
logger.ContextLogger(ctx).Error("error when get cash amount", zap.Error(err))
|
||||
return nil, err
|
||||
|
||||
@@ -52,11 +52,12 @@ type RepoManagerImpl struct {
|
||||
PG PaymentGateway
|
||||
LinkQu LinkQu
|
||||
|
||||
OrderRepo OrderRepository
|
||||
CustomerRepo CustomerRepo
|
||||
ProductRepo ProductRepository
|
||||
TransactionRepo TransactionRepo
|
||||
MemberRepository MemberRepository
|
||||
OrderRepo OrderRepository
|
||||
InProgressOrderRepo InProgressOrderRepository
|
||||
CustomerRepo CustomerRepo
|
||||
ProductRepo ProductRepository
|
||||
TransactionRepo TransactionRepo
|
||||
MemberRepository MemberRepository
|
||||
}
|
||||
|
||||
func NewRepoManagerImpl(db *gorm.DB, cfg *config.Config) *RepoManagerImpl {
|
||||
@@ -81,11 +82,12 @@ func NewRepoManagerImpl(db *gorm.DB, cfg *config.Config) *RepoManagerImpl {
|
||||
PG: pg.NewPaymentGatewayRepo(&cfg.Midtrans, &cfg.LinkQu),
|
||||
LinkQu: linkqu.NewLinkQuService(&cfg.LinkQu),
|
||||
|
||||
OrderRepo: NeworderRepository(db),
|
||||
CustomerRepo: NewCustomerRepository(db),
|
||||
ProductRepo: NewproductRepository(db),
|
||||
TransactionRepo: NewTransactionRepository(db),
|
||||
MemberRepository: NewMemberRepository(db),
|
||||
OrderRepo: NeworderRepository(db),
|
||||
CustomerRepo: NewCustomerRepository(db),
|
||||
ProductRepo: NewproductRepository(db),
|
||||
TransactionRepo: NewTransactionRepository(db),
|
||||
MemberRepository: NewMemberRepository(db),
|
||||
InProgressOrderRepo: NewInProgressOrderRepository(db),
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user