This repository has been archived by the owner on May 24, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 11
/
organization.go
93 lines (75 loc) · 2.49 KB
/
organization.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
package platform
import (
"fmt"
"strconv"
"github.com/LF-Engineering/dev-analytics-affiliation/gen/models"
log "github.com/LF-Engineering/dev-analytics-affiliation/logging"
"github.com/LF-Engineering/dev-analytics-libraries/orgs"
)
// Service - access platform org services
type Service interface {
GetListOrganizations(string, int64, int64) (*models.GetListOrganizationsServiceOutput, error)
LookupOrganization(name string) (*models.OrganizationServiceDataOutput, error)
}
type service struct {
client *orgs.Org
}
// New return ES connection
func New(client *orgs.Org) Service {
return &service{
client: client,
}
}
// GetListOrganizations ...
func (s *service) GetListOrganizations(q string, rows, page int64) (*models.GetListOrganizationsServiceOutput, error) {
getListOrganizations := &models.GetListOrganizationsServiceOutput{}
nRows := int64(0)
var orgs []*models.OrganizationServiceDataOutput
// lookup for exact org name match first
sfdcOrg, err := s.client.LookupOrganization(q)
if err != nil {
return nil, err
}
// append if found in sfdc
if sfdcOrg.Name != "" && sfdcOrg.ID != "" {
orgs = append(orgs, &models.OrganizationServiceDataOutput{ID: (sfdcOrg.ID), Name: sfdcOrg.Name, Domains: []*models.DomainDataOutput{}})
}
// next, search for org name match.
response, err := s.client.SearchOrganization(q, strconv.FormatInt(rows, 10), strconv.FormatInt(page-1, 10))
if err != nil {
return nil, err
}
for _, org := range response.Data {
if sfdcOrg.Name != org.Name {
orgs = append(orgs, &models.OrganizationServiceDataOutput{ID: (org.ID), Name: org.Name, Domains: []*models.DomainDataOutput{}})
}
}
log.Info(fmt.Sprintf("GetListOrganizations: q:%s rows:%d page:%d", q, rows, page))
getListOrganizations.Organizations = orgs
getListOrganizations.NRecords = nRows
getListOrganizations.Rows = int64(len(orgs))
if rows == 0 {
getListOrganizations.NPages = 1
} else {
pages := nRows / rows
if nRows%rows != 0 {
pages++
}
getListOrganizations.NPages = pages
}
getListOrganizations.Page = page
if q != "" {
getListOrganizations.Search = "q=" + q
}
return getListOrganizations, nil
}
// LookupOrganization ...
func (s *service) LookupOrganization(name string) (*models.OrganizationServiceDataOutput, error) {
org, err := s.client.LookupOrganization(name)
if err != nil {
return nil, err
}
return &models.OrganizationServiceDataOutput{ID: org.ID, Name: org.Name,
Domains: []*models.DomainDataOutput{{Name: org.Link, OrganizationName: org.Name}}},
nil
}