227 lines
6.6 KiB
Go
227 lines
6.6 KiB
Go
package domain
|
|
|
|
import (
|
|
dbgen "github.com/SamuelTariku/FortuneBet-Backend/gen/db"
|
|
"github.com/jackc/pgx/v5/pgtype"
|
|
)
|
|
|
|
// Company represents the client that we will contract the services with
|
|
// they are the ones that manage the branches and branch managers
|
|
// they will have their own wallet that they will use to distribute to the branch wallets
|
|
type Company struct {
|
|
ID int64
|
|
Name string
|
|
AdminID int64
|
|
WalletID int64
|
|
DeductedPercentage float32
|
|
IsActive bool
|
|
}
|
|
|
|
type CompanyFilter struct {
|
|
IsActive ValidBool
|
|
Query ValidString
|
|
CreatedBefore ValidTime
|
|
CreatedAfter ValidTime
|
|
}
|
|
|
|
type GetCompany struct {
|
|
ID int64
|
|
Name string
|
|
AdminID int64
|
|
AdminFirstName string
|
|
AdminLastName string
|
|
AdminPhoneNumber string
|
|
WalletID int64
|
|
WalletBalance Currency
|
|
IsWalletActive bool
|
|
DeductedPercentage float32
|
|
IsActive bool
|
|
}
|
|
|
|
type CreateCompany struct {
|
|
Name string
|
|
AdminID int64
|
|
WalletID int64
|
|
DeductedPercentage float32
|
|
}
|
|
|
|
type UpdateCompany struct {
|
|
ID int64
|
|
Name ValidString
|
|
AdminID ValidInt64
|
|
IsActive ValidBool
|
|
DeductedPercentage ValidFloat32
|
|
}
|
|
|
|
type CreateCompanyReq struct {
|
|
Name string `json:"name" example:"CompanyName"`
|
|
AdminID int64 `json:"admin_id" example:"1"`
|
|
}
|
|
type UpdateCompanyReq struct {
|
|
Name *string `json:"name,omitempty" example:"CompanyName"`
|
|
AdminID *int64 `json:"admin_id,omitempty" example:"1"`
|
|
IsActive *bool `json:"is_active,omitempty" example:"true"`
|
|
DeductedPercentage *float32 `json:"deducted_percentage,omitempty" example:"0.1" validate:"lt=1"`
|
|
}
|
|
|
|
type CompanyRes struct {
|
|
ID int64 `json:"id" example:"1"`
|
|
Name string `json:"name" example:"CompanyName"`
|
|
AdminID int64 `json:"admin_id" example:"1"`
|
|
WalletID int64 `json:"wallet_id" example:"1"`
|
|
DeductedPercentage float32 `json:"deducted_percentage" example:"0.1"`
|
|
IsActive bool `json:"is_active" example:"true"`
|
|
}
|
|
|
|
type GetCompanyRes struct {
|
|
ID int64 `json:"id" example:"1"`
|
|
Name string `json:"name" example:"CompanyName"`
|
|
AdminID int64 `json:"admin_id" example:"1"`
|
|
WalletID int64 `json:"wallet_id" example:"1"`
|
|
WalletBalance float32 `json:"balance" example:"1"`
|
|
WalletIsActive bool `json:"is_wallet_active" example:"false"`
|
|
IsActive bool `json:"is_active" example:"false"`
|
|
DeductedPercentage float32 `json:"deducted_percentage" example:"0.1"`
|
|
AdminFirstName string `json:"admin_first_name" example:"John"`
|
|
AdminLastName string `json:"admin_last_name" example:"Doe"`
|
|
AdminPhoneNumber string `json:"admin_phone_number" example:"1234567890"`
|
|
}
|
|
|
|
func ConvertCompany(company Company) CompanyRes {
|
|
return CompanyRes{
|
|
ID: company.ID,
|
|
Name: company.Name,
|
|
AdminID: company.AdminID,
|
|
WalletID: company.WalletID,
|
|
IsActive: company.IsActive,
|
|
DeductedPercentage: company.DeductedPercentage,
|
|
}
|
|
}
|
|
|
|
func ConvertGetCompany(company GetCompany) GetCompanyRes {
|
|
return GetCompanyRes{
|
|
ID: company.ID,
|
|
Name: company.Name,
|
|
AdminID: company.AdminID,
|
|
WalletID: company.WalletID,
|
|
WalletBalance: company.WalletBalance.Float32(),
|
|
IsActive: company.IsActive,
|
|
WalletIsActive: company.IsWalletActive,
|
|
DeductedPercentage: company.DeductedPercentage,
|
|
AdminFirstName: company.AdminFirstName,
|
|
AdminLastName: company.AdminLastName,
|
|
AdminPhoneNumber: company.AdminPhoneNumber,
|
|
}
|
|
}
|
|
|
|
func ConvertCreateCompany(company CreateCompany) dbgen.CreateCompanyParams {
|
|
return dbgen.CreateCompanyParams{
|
|
Name: company.Name,
|
|
AdminID: company.AdminID,
|
|
WalletID: company.WalletID,
|
|
DeductedPercentage: company.DeductedPercentage,
|
|
}
|
|
}
|
|
|
|
func ConvertDBCompany(dbCompany dbgen.Company) Company {
|
|
return Company{
|
|
ID: dbCompany.ID,
|
|
Name: dbCompany.Name,
|
|
AdminID: dbCompany.AdminID,
|
|
WalletID: dbCompany.WalletID,
|
|
DeductedPercentage: dbCompany.DeductedPercentage,
|
|
IsActive: dbCompany.IsActive,
|
|
}
|
|
}
|
|
|
|
func ConvertDBCompanyDetails(dbCompany dbgen.CompaniesDetail) GetCompany {
|
|
return GetCompany{
|
|
ID: dbCompany.ID,
|
|
Name: dbCompany.Name,
|
|
AdminID: dbCompany.AdminID,
|
|
WalletID: dbCompany.WalletID,
|
|
WalletBalance: Currency(dbCompany.Balance),
|
|
IsWalletActive: dbCompany.WalletIsActive,
|
|
AdminFirstName: dbCompany.AdminFirstName,
|
|
AdminLastName: dbCompany.AdminLastName,
|
|
AdminPhoneNumber: dbCompany.AdminPhoneNumber.String,
|
|
DeductedPercentage: dbCompany.DeductedPercentage,
|
|
IsActive: dbCompany.IsActive,
|
|
}
|
|
}
|
|
|
|
func ConvertUpdateCompany(updateCompany UpdateCompany) dbgen.UpdateCompanyParams {
|
|
newUpdateCompany := dbgen.UpdateCompanyParams{
|
|
ID: updateCompany.ID,
|
|
Name: pgtype.Text{
|
|
String: updateCompany.Name.Value,
|
|
Valid: updateCompany.Name.Valid,
|
|
},
|
|
AdminID: pgtype.Int8{
|
|
Int64: updateCompany.AdminID.Value,
|
|
Valid: updateCompany.AdminID.Valid,
|
|
},
|
|
IsActive: pgtype.Bool{
|
|
Bool: updateCompany.IsActive.Value,
|
|
Valid: updateCompany.IsActive.Valid,
|
|
},
|
|
DeductedPercentage: pgtype.Float4{
|
|
Float32: updateCompany.DeductedPercentage.Value,
|
|
Valid: updateCompany.DeductedPercentage.Valid,
|
|
},
|
|
}
|
|
|
|
return newUpdateCompany
|
|
}
|
|
|
|
func ConvertUpdateCompanyReq(req UpdateCompanyReq) UpdateCompany {
|
|
var updateCompany UpdateCompany
|
|
|
|
if req.Name != nil {
|
|
updateCompany.Name = ValidString{
|
|
Value: *req.Name,
|
|
Valid: true,
|
|
}
|
|
}
|
|
|
|
if req.AdminID != nil {
|
|
updateCompany.AdminID = ValidInt64{
|
|
Value: *req.AdminID,
|
|
Valid: true,
|
|
}
|
|
}
|
|
|
|
if req.IsActive != nil {
|
|
updateCompany.IsActive = ValidBool{
|
|
Value: *req.IsActive,
|
|
Valid: true,
|
|
}
|
|
}
|
|
|
|
if req.DeductedPercentage != nil {
|
|
updateCompany.DeductedPercentage = ValidFloat32{
|
|
Value: *req.DeductedPercentage,
|
|
Valid: true,
|
|
}
|
|
}
|
|
|
|
return updateCompany
|
|
}
|
|
|
|
func ConvertGetAllCompaniesParams(filter CompanyFilter) dbgen.GetAllCompaniesParams {
|
|
return dbgen.GetAllCompaniesParams{
|
|
Query: pgtype.Text{
|
|
String: filter.Query.Value,
|
|
Valid: filter.Query.Valid,
|
|
},
|
|
CreatedBefore: pgtype.Timestamp{
|
|
Time: filter.CreatedBefore.Value,
|
|
Valid: filter.CreatedBefore.Valid,
|
|
},
|
|
CreatedAfter: pgtype.Timestamp{
|
|
Time: filter.CreatedAfter.Value,
|
|
Valid: filter.CreatedAfter.Valid,
|
|
},
|
|
}
|
|
}
|