first modify
parent
5ceca6af39
commit
b780bb569b
|
@ -1,6 +1,19 @@
|
||||||
package models
|
package models
|
||||||
|
|
||||||
|
import (
|
||||||
|
"time"
|
||||||
|
)
|
||||||
|
|
||||||
type BranchRegisterBody struct {
|
type BranchRegisterBody struct {
|
||||||
BranchName string `json:"branch_name" validate:"required"`
|
BranchName string `json:"branch_name" validate:"required"`
|
||||||
CompanyID string `json:"company_id" bson:"company_id"`
|
CompanyID string `json:"company_id" bson:"company_id"`
|
||||||
}
|
}
|
||||||
|
|
||||||
|
type BranchInfo struct {
|
||||||
|
BranchName string `json:"branch_name" bson:"branch_name"`
|
||||||
|
BranchID string `json:"branch_id" bson:"branch_id"`
|
||||||
|
CompanyID string `json:"company_id" bson:"company_id"`
|
||||||
|
Owner []string `json:"owner" bson:"owner"`
|
||||||
|
CreateAt time.Time `json:"create_at" bson:"create_at"`
|
||||||
|
UpdateAt time.Time `json:"update_at" bson:"update_at"`
|
||||||
|
}
|
||||||
|
|
|
@ -1,50 +1,57 @@
|
||||||
package services
|
package services
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"bytes"
|
|
||||||
"context"
|
"context"
|
||||||
"encoding/json"
|
"errors"
|
||||||
"fmt"
|
"go.mongodb.org/mongo-driver/bson"
|
||||||
"io"
|
"go.mongodb.org/mongo-driver/bson/primitive"
|
||||||
"net/http"
|
"go.mongodb.org/mongo-driver/mongo"
|
||||||
|
"pkg.jojmatic.com/root/rpaas-core-service/pkg/logging"
|
||||||
"pkg.jojmatic.com/root/rpaas-core-service/pkg/models"
|
"pkg.jojmatic.com/root/rpaas-core-service/pkg/models"
|
||||||
|
"time"
|
||||||
)
|
)
|
||||||
|
|
||||||
type BranchService interface {
|
type BranchService interface {
|
||||||
RegisterBranch(endpoint string, body *models.BranchRegisterBody) (map[string]interface{}, error)
|
RegisterBranch(body *models.BranchRegisterBody, info *models.CompanyInfo) (*models.BranchInfo, error)
|
||||||
}
|
}
|
||||||
|
|
||||||
type BranchServiceImpl struct {
|
type BranchServiceImpl struct {
|
||||||
|
branchcollection *mongo.Collection
|
||||||
ctx context.Context
|
ctx context.Context
|
||||||
}
|
}
|
||||||
|
|
||||||
func NewBranchService(ctx context.Context) *BranchServiceImpl {
|
func NewBranchService(collection *mongo.Collection, ctx context.Context) *BranchServiceImpl {
|
||||||
return &BranchServiceImpl{
|
return &BranchServiceImpl{
|
||||||
|
branchcollection: collection,
|
||||||
ctx: ctx,
|
ctx: ctx,
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
func (bc *BranchServiceImpl) RegisterBranch(endpoint string, body *models.BranchRegisterBody) (map[string]interface{}, error) {
|
func (bc *BranchServiceImpl) RegisterBranch(body *models.BranchRegisterBody, info *models.CompanyInfo) (*models.BranchInfo, error) {
|
||||||
|
|
||||||
url := endpoint + "v1/branch/register"
|
var branchexisting *models.BranchInfo
|
||||||
branchInfo, err := json.Marshal(body)
|
err := bc.branchcollection.FindOne(bc.ctx, bson.M{"$and": []bson.M{{"branch_name": body.BranchName}, {"company_id": body.CompanyID}}}).Decode(&branchexisting)
|
||||||
|
if err == nil {
|
||||||
|
logging.Logger().Warn("Branch name already exists", err)
|
||||||
|
return nil, errors.New("branch name already exists")
|
||||||
|
} else if !errors.Is(err, mongo.ErrNoDocuments) {
|
||||||
|
logging.Logger().Error(err)
|
||||||
|
return nil, errors.New("InternalServerError")
|
||||||
|
}
|
||||||
|
|
||||||
resp, err := http.Post(url, "application/json", bytes.NewBuffer(branchInfo))
|
branchInfo := &models.BranchInfo{
|
||||||
|
BranchName: body.BranchName,
|
||||||
|
BranchID: primitive.NewObjectID().Hex(),
|
||||||
|
CompanyID: body.CompanyID,
|
||||||
|
Owner: info.Owner,
|
||||||
|
CreateAt: time.Now().UTC().Round(time.Second),
|
||||||
|
UpdateAt: time.Now().UTC().Round(time.Second),
|
||||||
|
}
|
||||||
|
|
||||||
|
_, err = bc.branchcollection.InsertOne(bc.ctx, branchInfo)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
|
logging.Logger().Error(err)
|
||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
defer resp.Body.Close()
|
return branchInfo, nil
|
||||||
|
|
||||||
if resp.StatusCode != http.StatusOK {
|
|
||||||
return nil, fmt.Errorf("register company failed, status code: %d", resp.StatusCode)
|
|
||||||
}
|
|
||||||
|
|
||||||
var datajson map[string]interface{}
|
|
||||||
|
|
||||||
data, _ := io.ReadAll(resp.Body)
|
|
||||||
if err := json.Unmarshal(data, &datajson); err != nil {
|
|
||||||
return nil, err
|
|
||||||
}
|
|
||||||
return datajson, err
|
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in New Issue