first modify

master
unknown 2024-05-03 15:41:51 +03:00
parent 5ceca6af39
commit b780bb569b
2 changed files with 47 additions and 27 deletions

View File

@ -1,6 +1,19 @@
package models
import (
"time"
)
type BranchRegisterBody struct {
BranchName string `json:"branch_name" validate:"required"`
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"`
}

View File

@ -1,50 +1,57 @@
package services
import (
"bytes"
"context"
"encoding/json"
"fmt"
"io"
"net/http"
"errors"
"go.mongodb.org/mongo-driver/bson"
"go.mongodb.org/mongo-driver/bson/primitive"
"go.mongodb.org/mongo-driver/mongo"
"pkg.jojmatic.com/root/rpaas-core-service/pkg/logging"
"pkg.jojmatic.com/root/rpaas-core-service/pkg/models"
"time"
)
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 {
ctx context.Context
branchcollection *mongo.Collection
ctx context.Context
}
func NewBranchService(ctx context.Context) *BranchServiceImpl {
func NewBranchService(collection *mongo.Collection, ctx context.Context) *BranchServiceImpl {
return &BranchServiceImpl{
ctx: ctx,
branchcollection: collection,
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"
branchInfo, err := json.Marshal(body)
var branchexisting *models.BranchInfo
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 {
logging.Logger().Error(err)
return nil, err
}
defer resp.Body.Close()
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
return branchInfo, nil
}