Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
6.90% covered (danger)
6.90%
8 / 116
9.09% covered (danger)
9.09%
1 / 11
CRAP
0.00% covered (danger)
0.00%
0 / 2
InstancyUserDTO
0.00% covered (danger)
0.00%
0 / 1
0.00% covered (danger)
0.00%
0 / 1
2
0.00% covered (danger)
0.00%
0 / 1
 __construct
0.00% covered (danger)
0.00%
0 / 1
0.00% covered (danger)
0.00%
0 / 1
2
InstancyRepository
6.96% covered (danger)
6.96%
8 / 115
10.00% covered (danger)
10.00%
1 / 10
411.86
0.00% covered (danger)
0.00%
0 / 1
 __construct
100.00% covered (success)
100.00%
8 / 8
100.00% covered (success)
100.00%
1 / 1
3
 request
0.00% covered (danger)
0.00%
0 / 12
0.00% covered (danger)
0.00%
0 / 1
2
 updateUser
0.00% covered (danger)
0.00%
0 / 30
0.00% covered (danger)
0.00%
0 / 1
20
 updateUserEmail
0.00% covered (danger)
0.00%
0 / 11
0.00% covered (danger)
0.00%
0 / 1
12
 deleteGroup
0.00% covered (danger)
0.00%
0 / 9
0.00% covered (danger)
0.00%
0 / 1
6
 updateGroup
0.00% covered (danger)
0.00%
0 / 13
0.00% covered (danger)
0.00%
0 / 1
6
 createGroup
0.00% covered (danger)
0.00%
0 / 11
0.00% covered (danger)
0.00%
0 / 1
6
 findGroups
0.00% covered (danger)
0.00%
0 / 12
0.00% covered (danger)
0.00%
0 / 1
2
 getGroups
0.00% covered (danger)
0.00%
0 / 2
0.00% covered (danger)
0.00%
0 / 1
2
 getGroup
0.00% covered (danger)
0.00%
0 / 7
0.00% covered (danger)
0.00%
0 / 1
12
1<?php
2
3namespace App\Http\Repositories;
4
5use App\Services\FlyMsgAI\SecretsAWS;
6use Illuminate\Support\Facades\Http;
7
8class InstancyUserDTO
9{
10    public function __construct(
11        public readonly string $userId,
12        public readonly string $groupId,
13        public readonly string $firstName,
14        public readonly string $lastName,
15        public readonly string $email,
16        public readonly ?string $companyName,
17    ) {}
18}
19
20class InstancyRepository
21{
22    protected $baseUrl;
23    protected $apiKey;
24    protected $sso_url;
25    protected $siteGroupId;
26    protected $newUserPassword = 'FlyMSGWelcome#2024';
27
28    public function __construct()
29    {
30        $this->baseUrl = "https://ondemand-admin.vengreso.com/InstancyService.asmx";
31        $this->apiKey = "22A9838A-2196-47ED-8126-4321B7A07EF1";
32        $this->sso_url = "https://ondemand-master-admin.vengreso.com/InstancyService.asmx";
33        $this->siteGroupId = "388";
34
35        if (!isProduction() && !isLocalProduction()) {
36            $this->baseUrl = "https://ondemand-admin.instancy.net/instancyservice.asmx";
37            $this->apiKey = "A58ABE98-1B0D-41FC-9805-0F34D6B99E80";
38            $this->sso_url = "https://ondemand-master-admin.instancy.net/InstancyService.asmx";
39        }
40    }
41
42    protected function request($endpoint, $data)
43    {
44        $url = $this->baseUrl . '/' . $endpoint;
45
46        $response = Http::withHeaders([
47            'Accept-Encoding' => '',
48            'Content-Type' => 'application/x-www-form-urlencoded',
49        ])->asForm()
50            ->post($url, [
51                'astrKey' => $this->apiKey,
52                'astrXML' => $data,
53            ])->body();
54
55        $response = parseInstancyXml($response);
56
57        $response = parseInstancyXml($response["value"]);
58
59        return $response;
60    }
61
62    public function updateUser(InstancyUserDTO $user)
63    {
64        $userId = $user->userId;
65        $groupId = $user->groupId;
66        $firstName = str_replace("'", " ", $user->firstName);
67        $lastName = str_replace("'", " ", $user->lastName);
68        $email = $user->email;
69        $status = 1;
70        $password = $this->newUserPassword;
71        $mobileNumber = '0000000000';
72        $jobTitle = 'User';
73        $companyName = $user->companyName ?? 'Vengreso';
74
75        if (!isProduction() && !isLocalProduction()) {
76            $email = 'staging.' . $email;
77        }
78
79        $xml = "<?xml version='1.0' encoding='iso-8859-1'?>
80                <InstancyWrapper><Request SiteID=\"$this->siteGroupId\"><UserDetails>
81                    <UserID><![CDATA[$userId]]></UserID>
82                    <GroupID><![CDATA[$groupId]]></GroupID>
83                    <First_Name><![CDATA[$firstName]]></First_Name>
84                    <Last_Name><![CDATA[$lastName]]></Last_Name>
85                    <User_name><![CDATA[$email]]></User_name>
86                    <Email><![CDATA[$email]]></Email>
87                    <Password><![CDATA[$password]]></Password>
88                    <Mobile_Number><![CDATA[$mobileNumber]]></Mobile_Number>
89                    <Job_title><![CDATA[$jobTitle]]></Job_title>
90                    <Company_Name><![CDATA[$companyName]]></Company_Name>
91                    <Status><![CDATA[$status]]></Status>
92                </UserDetails></Request></InstancyWrapper>";
93
94        $group = $this->request('UpdateUser', $xml);
95
96        if (isset($group['UserDetails'][0])) {
97            return $group['UserDetails'][0]['UserID'][0]['value'];
98        }
99
100        return null;
101    }
102
103    public function updateUserEmail($groupData)
104    {
105        $instancyId = $groupData->instancyId;
106        $newEmail = $groupData->email;
107
108        if (!isProduction() && !isLocalProduction()) {
109            $newEmail = 'staging.' . $newEmail;
110        }
111
112        $xml = "<?xml version='1.0' encoding='iso-8859-1'?>
113                <InstancyWrapper>
114                    <Request SiteID=\"$this->siteGroupId\">
115                        <UpdateUserEmail>
116                            <UserID><![CDATA[$instancyId]]></UserID>
117                            <EmailToBeUpdated><![CDATA[$newEmail]]></EmailToBeUpdated>
118                        </UpdateUserEmail>
119                    </Request>
120                </InstancyWrapper>";
121
122        $group = $this->request('/UpdateUserEmail', $xml);
123
124        // if (isset($group['Group'][0])) {
125        //     return $group['Group'][0]['GroupID'][0]['value'];
126        // }
127
128        return $group;
129    }
130
131    public function deleteGroup($groupData)
132    {
133        $groupId = $groupData->groupId;
134        $xml = "<?xml version='1.0' encoding='iso-8859-1'?>
135                <InstancyWrapper>
136                    <Request SiteID=\"$this->siteGroupId\">
137                        <DeleteGroup>
138                            <GroupID><![CDATA[$groupId]]></GroupID>
139                        </DeleteGroup>                        
140                    </Request>
141                </InstancyWrapper>";
142
143        $group = $this->request('/DeleteGroup', $xml);
144
145        if (isset($group['Group'][0])) {
146            return $group['Group'][0]['status'][0]['value'] == "Group deleted successfully";
147        }
148
149        return false;
150    }
151
152    public function updateGroup($groupData)
153    {
154        $parentId = $groupData->parentId ?? $this->siteGroupId;
155        $groupName = str_replace("'", " ", $groupData->name);
156        $groupId = $groupData->groupId;
157        $xml = "<?xml version='1.0' encoding='iso-8859-1'?>
158                <InstancyWrapper>
159                    <Request SiteID=\"$this->siteGroupId\">
160                        <UpdateGroup>
161                            <GroupID><![CDATA[$groupId]]></GroupID>
162                            <GroupName><![CDATA[$groupName]]></GroupName>
163                            <ParentID><![CDATA[$parentId]]></ParentID>
164                        </UpdateGroup>
165                    </Request>
166                </InstancyWrapper>";
167
168        $group = $this->request('/UpdateGroup', $xml);
169
170        if (isset($group['Group'][0])) {
171            return $group['Group'][0]['status'][0]['value'] == "Updated successfully";
172        }
173
174        return false;
175    }
176
177    public function createGroup($groupData)
178    {
179        $parentId = $groupData->parentId ?? $this->siteGroupId;
180        $groupName = str_replace("'", " ", $groupData->name);
181        $xml = "<?xml version='1.0' encoding='iso-8859-1'?>
182                <InstancyWrapper>
183                    <Request SiteID=\"$this->siteGroupId\">
184                        <CreateGroup>
185                            <GroupID><![CDATA[-1]]></GroupID>
186                            <GroupName><![CDATA[$groupName]]></GroupName>
187                            <ParentID><![CDATA[$parentId]]></ParentID>
188                        </CreateGroup>
189                    </Request>
190                </InstancyWrapper>";
191
192        $group = $this->request('/CreateGroup', $xml);
193
194        if (isset($group['Group'][0])) {
195            return $group['Group'][0]['GroupID'][0]['value'];
196        }
197
198        return null;
199    }
200
201    public function findGroups()
202    {
203        $xml = "<?xml version='1.0' encoding='iso-8859-1'?>
204                <InstancyWrapper>
205                    <Request SiteID=\"$this->siteGroupId\">
206                        <GroupList>
207                            <siteid>
208                                <![CDATA[$this->siteGroupId]]>
209                            </siteid>
210                        </GroupList>
211                    </Request>
212                </InstancyWrapper>";
213
214        $groups = $this->request('/GetGroupListing', $xml);
215
216        return array_map(function($group) {
217            return json_decode(json_encode([
218                'id' => $group['GroupID'][0]['value'],
219                'name' => $group['Name'][0]['value'],
220                'parentId' => $group['ParentID'][0]['value'],
221            ]));
222        }, $groups['GroupList'][0]['Group']);
223    }
224
225    public function getGroups($option, $searchName)
226    {
227        $groups = $this->findGroups();
228
229        return collect($groups)->where($option, $searchName);
230    }
231
232    public function getGroup($searchName, $company, $parentId)
233    {
234        $groups = $this->findGroups();
235
236        $query = collect($groups)->where('name', $searchName);
237
238        if ($company) {
239            $query = $query->where('parentId', $company);
240        } elseif ($parentId) {
241            $query = $query->where('parentId', $parentId);
242        }
243
244        return $query->first();
245    }
246}