|
4 | 4 | http://api.docs.membersuite.com/#References/Objects/Organization.htm |
5 | 5 |
|
6 | 6 | """ |
| 7 | +import datetime |
7 | 8 |
|
| 9 | +from ..exceptions import ExecuteMSQLError |
8 | 10 | from ..mixins import ChunkQueryMixin |
| 11 | +from ..security.models import Individual |
| 12 | +from ..utils import convert_ms_object, get_new_client |
9 | 13 | from .models import Organization, OrganizationType |
10 | | -from ..utils import convert_ms_object |
11 | | -from zeep.exceptions import TransportError |
12 | | -import datetime |
13 | 14 |
|
14 | 15 |
|
15 | 16 | class OrganizationService(ChunkQueryMixin, object): |
16 | 17 |
|
17 | | - def __init__(self, client): |
| 18 | + def __init__(self, client=None): |
18 | 19 | """ |
19 | 20 | Accepts a ConciergeClient to connect with MemberSuite |
20 | 21 | """ |
21 | | - self.client = client |
| 22 | + self.client = client or get_new_client(request_session=True) |
22 | 23 |
|
23 | 24 | def get_orgs( |
24 | 25 | self, limit_to=100, max_calls=None, parameters=None, |
@@ -104,3 +105,42 @@ def package_org_types(self, obj_list): |
104 | 105 | org = OrganizationType(sane_obj) |
105 | 106 | org_type_list.append(org) |
106 | 107 | return org_type_list |
| 108 | + |
| 109 | + def get_individuals_for_primary_organization(self, organization): |
| 110 | + """ |
| 111 | + Returns all Individuals that have `organization` as a primary org. |
| 112 | +
|
| 113 | + """ |
| 114 | + if not self.client.session_id: |
| 115 | + self.client.request_session() |
| 116 | + |
| 117 | + query = ("SELECT Objects() FROM Individual WHERE " |
| 118 | + "PrimaryOrganization = '{}'".format( |
| 119 | + organization.membersuite_account_num)) |
| 120 | + |
| 121 | + result = self.client.runSQL(query) |
| 122 | + |
| 123 | + msql_result = result["body"]["ExecuteMSQLResult"] |
| 124 | + |
| 125 | + if msql_result["Success"]: |
| 126 | + membersuite_objects = (msql_result["ResultValue"] |
| 127 | + ["ObjectSearchResult"] |
| 128 | + ["Objects"]) |
| 129 | + else: |
| 130 | + raise ExecuteMSQLError(result=result) |
| 131 | + |
| 132 | + individuals = [] |
| 133 | + |
| 134 | + for membersuite_object in membersuite_objects["MemberSuiteObject"]: |
| 135 | + individuals.append( |
| 136 | + Individual(membersuite_object_data=membersuite_object)) |
| 137 | + |
| 138 | + return individuals |
| 139 | + |
| 140 | + def get_stars_liaison_for_organization(self, organization): |
| 141 | + candidates = self.get_individuals_for_primary_organization( |
| 142 | + organization=organization) |
| 143 | + for candidate in candidates: |
| 144 | + if "StarsPrimaryContact__rt" in candidate.fields: |
| 145 | + return candidate |
| 146 | + return None |
0 commit comments