feat: add invoice and alternative_componen management functionality
This commit is contained in:
67
internal/mapper/alternative_component_mapper.go
Normal file
67
internal/mapper/alternative_component_mapper.go
Normal file
@@ -0,0 +1,67 @@
|
||||
package mapper
|
||||
|
||||
import (
|
||||
"encoding/json"
|
||||
"wm-backend/internal/models"
|
||||
db "wm-backend/sqlc_gen"
|
||||
|
||||
"github.com/jackc/pgx/v5/pgtype"
|
||||
)
|
||||
|
||||
func numericToString(n pgtype.Numeric) string {
|
||||
if !n.Valid {
|
||||
return ""
|
||||
}
|
||||
b, _ := json.Marshal(n)
|
||||
return string(b)
|
||||
}
|
||||
|
||||
func stringToNumeric(s string) pgtype.Numeric {
|
||||
var n pgtype.Numeric
|
||||
if s == "" {
|
||||
return n
|
||||
}
|
||||
_ = json.Unmarshal([]byte(s), &n)
|
||||
return n
|
||||
}
|
||||
|
||||
func ToDomainAlternativeComponent(r db.AlternativeComponent) *models.AlternativeComponent {
|
||||
return &models.AlternativeComponent{
|
||||
ID: r.ID,
|
||||
InvoiceConfigItemID: r.InvoiceConfigItemID,
|
||||
AlternativeComponentID: r.AlternativeComponentID,
|
||||
ConversionRatio: numericToString(r.ConversionRatio),
|
||||
Priority: r.Priority,
|
||||
Note: r.Note.String,
|
||||
Metadata: r.Metadata,
|
||||
}
|
||||
}
|
||||
|
||||
func ToModelAlternativeComponent(r *models.AlternativeComponent) *db.CreateAlternativeComponentParams {
|
||||
return &db.CreateAlternativeComponentParams{
|
||||
InvoiceConfigItemID: r.InvoiceConfigItemID,
|
||||
AlternativeComponentID: r.AlternativeComponentID,
|
||||
ConversionRatio: stringToNumeric(r.ConversionRatio),
|
||||
Priority: r.Priority,
|
||||
Note: pgtype.Text{
|
||||
String: r.Note,
|
||||
Valid: r.Note != "",
|
||||
},
|
||||
Metadata: r.Metadata,
|
||||
}
|
||||
}
|
||||
|
||||
func ToUpdateModelAlternativeComponent(r *models.AlternativeComponent) *db.UpdateAlternativeComponentParams {
|
||||
return &db.UpdateAlternativeComponentParams{
|
||||
InvoiceConfigItemID: r.InvoiceConfigItemID,
|
||||
AlternativeComponentID: r.AlternativeComponentID,
|
||||
ConversionRatio: stringToNumeric(r.ConversionRatio),
|
||||
Priority: r.Priority,
|
||||
Note: pgtype.Text{
|
||||
String: r.Note,
|
||||
Valid: r.Note != "",
|
||||
},
|
||||
Metadata: r.Metadata,
|
||||
ID: r.ID,
|
||||
}
|
||||
}
|
||||
78
internal/mapper/invoice_mapper.go
Normal file
78
internal/mapper/invoice_mapper.go
Normal file
@@ -0,0 +1,78 @@
|
||||
package mapper
|
||||
|
||||
import (
|
||||
"wm-backend/internal/models"
|
||||
db "wm-backend/sqlc_gen"
|
||||
|
||||
"github.com/jackc/pgx/v5/pgtype"
|
||||
)
|
||||
|
||||
func timestampToTime(t pgtype.Timestamptz) string {
|
||||
if !t.Valid {
|
||||
return ""
|
||||
}
|
||||
return t.Time.String()
|
||||
}
|
||||
|
||||
func ToDomainInvoice(r db.Invoice) *models.Invoice {
|
||||
return &models.Invoice{
|
||||
ID: r.ID,
|
||||
InvoiceCode: r.InvoiceCode,
|
||||
Type: string(r.Type),
|
||||
Status: string(r.Status),
|
||||
InvoiceConfigID: r.InvoiceConfigID.Int64,
|
||||
TotalItems: r.TotalItems,
|
||||
Note: r.Note.String,
|
||||
CreatedBy: r.CreatedBy.String,
|
||||
ApprovedBy: r.ApprovedBy.String,
|
||||
CompletedAt: r.CompletedAt.Time,
|
||||
CreatedAt: r.CreatedAt,
|
||||
UpdatedAt: r.UpdatedAt,
|
||||
Metadata: r.Metadata,
|
||||
}
|
||||
}
|
||||
|
||||
func ToModelInvoice(r *models.Invoice) *db.CreateInvoiceParams {
|
||||
return &db.CreateInvoiceParams{
|
||||
Type: db.InvoiceTypeEnum(r.Type),
|
||||
Status: db.InvoiceStatusEnum(r.Status),
|
||||
InvoiceConfigID: pgtype.Int8{
|
||||
Int64: r.InvoiceConfigID,
|
||||
Valid: r.InvoiceConfigID != 0,
|
||||
},
|
||||
TotalItems: r.TotalItems,
|
||||
Note: pgtype.Text{
|
||||
String: r.Note,
|
||||
Valid: r.Note != "",
|
||||
},
|
||||
CreatedBy: pgtype.Text{
|
||||
String: r.CreatedBy,
|
||||
Valid: r.CreatedBy != "",
|
||||
},
|
||||
ApprovedBy: pgtype.Text{
|
||||
String: r.ApprovedBy,
|
||||
Valid: r.ApprovedBy != "",
|
||||
},
|
||||
CreatedAt: r.CreatedAt,
|
||||
Metadata: r.Metadata,
|
||||
}
|
||||
}
|
||||
|
||||
func ToUpdateModelInvoice(r *models.Invoice) *db.UpdateInvoiceParams {
|
||||
return &db.UpdateInvoiceParams{
|
||||
Type: db.InvoiceTypeEnum(r.Type),
|
||||
Status: db.InvoiceStatusEnum(r.Status),
|
||||
InvoiceConfigID: pgtype.Int8{
|
||||
Int64: r.InvoiceConfigID,
|
||||
Valid: r.InvoiceConfigID != 0,
|
||||
},
|
||||
TotalItems: r.TotalItems,
|
||||
Note: pgtype.Text{
|
||||
String: r.Note,
|
||||
Valid: r.Note != "",
|
||||
},
|
||||
Metadata: r.Metadata,
|
||||
UpdatedAt: r.UpdatedAt,
|
||||
ID: r.ID,
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user