initial commit 2
This commit is contained in:
230
application/applicationservice/organization.go
Normal file
230
application/applicationservice/organization.go
Normal file
@@ -0,0 +1,230 @@
|
||||
package applicationservice
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"strconv"
|
||||
"strings"
|
||||
|
||||
"bitbucket.org/nemt/nemt-portal-api/application/entitymapping"
|
||||
"bitbucket.org/nemt/nemt-portal-api/application/third/npd/npdmodel"
|
||||
"bitbucket.org/nemt/nemt-portal-api/application/viewmodel"
|
||||
"bitbucket.org/nemt/nemt-portal-api/domain/entity"
|
||||
"bitbucket.org/nemt/nemt-portal-api/domain/service"
|
||||
"github.com/pquerna/ffjson/ffjson"
|
||||
)
|
||||
|
||||
// providerService holds methods to provider application service
|
||||
type organizationService struct {
|
||||
svc *service.Service
|
||||
mapEntity *entitymapping.Mapper
|
||||
}
|
||||
|
||||
// newProviderService returns a providerService instance
|
||||
func newOrganizationService(svc *service.Service, mapper *entitymapping.Mapper) *organizationService {
|
||||
return &organizationService{
|
||||
svc: svc,
|
||||
mapEntity: mapper,
|
||||
}
|
||||
}
|
||||
|
||||
func (s *organizationService) GetAllTypes() ([]viewmodel.OrganizationType, error) {
|
||||
result, err := s.svc.Organization.GetAllTypes()
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
return s.mapEntity.Organization.ToOrganizationTypeModelSlice(result), nil
|
||||
}
|
||||
|
||||
func (s *organizationService) GetByType(organizationTypeKey string) ([]viewmodel.Organization, error) {
|
||||
result, err := s.svc.Organization.GetByType(organizationTypeKey)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
return s.mapEntity.Organization.ToOrganizationModelSlice(result), nil
|
||||
}
|
||||
|
||||
func (s *organizationService) GetByUUID(organizationUUID string) (viewmodel.Organization, error) {
|
||||
result, err := s.svc.Organization.GetByUUID(organizationUUID)
|
||||
if err != nil {
|
||||
return viewmodel.Organization{}, err
|
||||
}
|
||||
return s.mapEntity.Organization.ToOrganizationModel(result), nil
|
||||
}
|
||||
|
||||
func (s *organizationService) GetByName(name string, searchType string) ([]viewmodel.Organization, error) {
|
||||
result, err := s.svc.Organization.GetByName(name, searchType)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
return s.mapEntity.Organization.ToOrganizationModelSlice(result), nil
|
||||
}
|
||||
|
||||
func (s *organizationService) SetParentOrganization(organizationUUID string, parentOrganizationUUID string) (viewmodel.Organization, error) {
|
||||
child, err := s.svc.Organization.GetByUUID(organizationUUID)
|
||||
if err != nil {
|
||||
return viewmodel.Organization{}, err
|
||||
}
|
||||
|
||||
parent, err := s.svc.Organization.GetByUUID(parentOrganizationUUID)
|
||||
if err != nil {
|
||||
return viewmodel.Organization{}, err
|
||||
}
|
||||
|
||||
if err := s.svc.Organization.SetParentOrganization(child.ID, parent.ID); err != nil {
|
||||
return viewmodel.Organization{}, err
|
||||
}
|
||||
|
||||
return s.GetByUUID(organizationUUID)
|
||||
}
|
||||
|
||||
func (s *organizationService) InactivateOrganizationAddress(organizationUUID string, address viewmodel.OrganizationAddress) error {
|
||||
entityAddress := s.mapEntity.Organization.ToOrganizationAddressEntity(address)
|
||||
entityAddress.Organization = &entity.Organization{
|
||||
UUID: organizationUUID,
|
||||
}
|
||||
|
||||
user, err := s.svc.Users.GetByUUID(address.UpdatedUser.ID, "")
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
entityAddress.UpdatedUser = user
|
||||
|
||||
if err := s.svc.Organization.InactivateOrganizationAddress(entityAddress); err != nil {
|
||||
return err
|
||||
} else {
|
||||
return nil
|
||||
}
|
||||
}
|
||||
|
||||
func (s *organizationService) SetOrganizationAddress(organizationUUID string, address viewmodel.OrganizationAddress) (viewmodel.OrganizationAddress, error) {
|
||||
entityAddress := s.mapEntity.Organization.ToOrganizationAddressEntity(address)
|
||||
entityAddress.Organization = &entity.Organization{
|
||||
UUID: organizationUUID,
|
||||
}
|
||||
|
||||
user, err := s.svc.Users.GetByUUID(address.CreatedUser.ID, "")
|
||||
if err != nil {
|
||||
return viewmodel.OrganizationAddress{}, err
|
||||
}
|
||||
|
||||
entityAddress.CreatedUser = user
|
||||
entityAddress.UpdatedUser = user
|
||||
|
||||
entityAddress, err = s.svc.Organization.SetOrganizationAddress(entityAddress)
|
||||
if err != nil {
|
||||
return viewmodel.OrganizationAddress{}, err
|
||||
}
|
||||
|
||||
return s.mapEntity.Organization.ToOrganizationAddressModel(entityAddress), nil
|
||||
}
|
||||
|
||||
func (s *organizationService) InactivateOrganizationContact(organizationUUID string, contact viewmodel.OrganizationContact) error {
|
||||
entityContact := s.mapEntity.Organization.ToOrganizationContactEntity(contact)
|
||||
entityContact.Organization = &entity.Organization{
|
||||
UUID: organizationUUID,
|
||||
}
|
||||
|
||||
user, err := s.svc.Users.GetByUUID(contact.CreatedUser.ID, "")
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
entityContact.UpdatedUser = user
|
||||
|
||||
if err := s.svc.Organization.InactivateOrganizationContact(entityContact); err != nil {
|
||||
return err
|
||||
} else {
|
||||
return nil
|
||||
}
|
||||
}
|
||||
|
||||
func (s *organizationService) SetOrganizationContact(organizationUUID string, contact viewmodel.OrganizationContact) (viewmodel.OrganizationContact, error) {
|
||||
entityContact := s.mapEntity.Organization.ToOrganizationContactEntity(contact)
|
||||
entityContact.Organization = &entity.Organization{
|
||||
UUID: organizationUUID,
|
||||
}
|
||||
|
||||
user, err := s.svc.Users.GetByUUID(contact.CreatedUser.ID, "")
|
||||
if err != nil {
|
||||
return viewmodel.OrganizationContact{}, err
|
||||
}
|
||||
|
||||
entityContact.CreatedUser = user
|
||||
entityContact.UpdatedUser = user
|
||||
|
||||
entityContact, err = s.svc.Organization.SetOrganizationContact(entityContact)
|
||||
if err != nil {
|
||||
return viewmodel.OrganizationContact{}, err
|
||||
}
|
||||
|
||||
return s.mapEntity.Organization.ToOrganizationContactModel(entityContact), nil
|
||||
}
|
||||
|
||||
func (s *organizationService) AddOrganization(organization viewmodel.Organization, user viewmodel.User) (viewmodel.Organization, error) {
|
||||
enOrg := s.mapEntity.Organization.ToOrganizationEntity(organization)
|
||||
enUser := s.mapEntity.User.ToUserEntity(user)
|
||||
|
||||
author, err := s.svc.Users.GetByUUID(organization.Author.ID, "")
|
||||
if err != nil {
|
||||
return viewmodel.Organization{}, nil
|
||||
}
|
||||
enOrg.Author = author
|
||||
enOrg.LastEditor = enOrg.Author
|
||||
|
||||
if organization.Reference != nil {
|
||||
switch organization.Type.Key {
|
||||
case "provider":
|
||||
var provider npdmodel.ProviderResponse
|
||||
bProvider, err := ffjson.Marshal(organization.Reference)
|
||||
if err != nil {
|
||||
fmt.Println("Error to marshal provider")
|
||||
return viewmodel.Organization{}, nil
|
||||
}
|
||||
if err := ffjson.Unmarshal(bProvider, &provider); err != nil {
|
||||
fmt.Println("Error to convert provider")
|
||||
return viewmodel.Organization{}, nil
|
||||
}
|
||||
|
||||
eProviders, err := s.svc.Provider.Save(s.mapEntity.Provider.ToProviderEntitySlice([]npdmodel.ProviderResponse{provider}), enUser)
|
||||
if err != nil {
|
||||
fmt.Println("Error to save provider")
|
||||
return viewmodel.Organization{}, nil
|
||||
}
|
||||
respProvider := eProviders[0]
|
||||
enOrg.ReferenceID = respProvider.ProviderID
|
||||
|
||||
lat, _ := strconv.ParseFloat(provider.Latitude, 64)
|
||||
long, _ := strconv.ParseFloat(provider.Longitude, 64)
|
||||
address := entity.OrganizationAddress{
|
||||
InternalID: provider.MukID,
|
||||
Name: "Main Address",
|
||||
Address: fmt.Sprintf("%s %s - %s, %s (%s)", provider.StreetName1, provider.StreetName2, provider.CityName, provider.State, provider.ZipCode),
|
||||
Latitude: lat,
|
||||
Longitude: long,
|
||||
CreatedUser: enOrg.Author,
|
||||
UpdatedUser: enOrg.LastEditor,
|
||||
}
|
||||
enOrg.Addresses = append(enOrg.Addresses, address)
|
||||
fmt.Println("Phone Number: ", provider.PhoneNumber)
|
||||
if provider.PhoneNumber != "" {
|
||||
formattedPhone := "+1" + strings.Replace(provider.PhoneNumber, "-", "", -1)
|
||||
contact := entity.OrganizationContact{
|
||||
Type: entity.ContactType{
|
||||
Key: "phone",
|
||||
},
|
||||
Contact: formattedPhone,
|
||||
Name: "Main Phone",
|
||||
CreatedUser: enOrg.Author,
|
||||
UpdatedUser: enOrg.LastEditor,
|
||||
}
|
||||
enOrg.Contacts = append(enOrg.Contacts, contact)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
enOrg, err = s.svc.Organization.AddOrganization(enOrg)
|
||||
if err != nil {
|
||||
return viewmodel.Organization{}, nil
|
||||
}
|
||||
|
||||
return s.GetByUUID(enOrg.UUID)
|
||||
}
|
||||
Reference in New Issue
Block a user