File size: 7,343 Bytes
07c3cdd
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
<?php
namespace ProcessMaker\Services\Api;

use \ProcessMaker\Services\Api;
use \Luracast\Restler\RestException;


/**
 * Department Api Controller
 *
 * @author Brayan Pereyra (Cochalo) <brayan@colosa.com>
 * @copyright Colosa - Bolivia
 *
 * @protected
 */
class Department extends Api
{
    /**
     * Constructor of the class
     *
     * return void
     */
    public function __construct()
    {
        try {
            $user = new \ProcessMaker\BusinessModel\User();

            $usrUid = $this->getUserId();

            if (!$user->checkPermission($usrUid, "PM_USERS")) {
                throw new \Exception(\G::LoadTranslation("ID_USER_NOT_HAVE_PERMISSION", array($usrUid)));
            }
        } catch (\Exception $e) {
            throw new RestException(Api::STAT_APP_EXCEPTION, $e->getMessage());
        }
    }

    /**
     * @url GET
     *
     * @return array
     *
     */
    public function doGetDepartments()
    {
        try {
            $oDepartment = new \ProcessMaker\BusinessModel\Department();
            $response = $oDepartment->getDepartments();
            return $response;
        } catch (\Exception $e) {
            throw (new RestException(Api::STAT_APP_EXCEPTION, $e->getMessage()));
        }
    }

    /**
     * @url GET /:dep_uid/assigned-user
     *
     * @param string $dep_uid {@min 1}{@max 32}
     *
     * @return array
     *
     */
    public function doGetAssignedUser($dep_uid)
    {
        try {
            $oDepartment = new \ProcessMaker\BusinessModel\Department();

            $response = $oDepartment->getUsers(
                $dep_uid, 'ASSIGNED', null, null, null, null, null, false
            );

            return $response['data'];
        } catch (\Exception $e) {
            throw (new RestException(Api::STAT_APP_EXCEPTION, $e->getMessage()));
        }
    }

    /**
     * @url GET /:dep_uid/available-user
     *
     * @param string $dep_uid {@min 1}{@max 32}
     * @param string $start   {@from path}
     * @param string $limit   {@from path}
     * @param string $search  {@from path}
     *
     * @return array
     *
     */
    public function doGetAvailableUser($dep_uid, $start = null, $limit = null, $search = null)
    {
        try {
            $oDepartment = new \ProcessMaker\BusinessModel\Department();

            $response = $oDepartment->getUsers(
                $dep_uid, 'AVAILABLE', ['filter' => $search, 'filterOption' => ''], null, null, $start, $limit, false
            );

            return $response['data'];
        } catch (\Exception $e) {
            throw (new RestException(Api::STAT_APP_EXCEPTION, $e->getMessage()));
        }
    }

    /**
     * Assign a user to a specified department in version 3.0 and later. If the 
     * user is already a member of another department, the user will be transfered 
     * to the specified department.
     * 
     * @url POST /:dep_uid/assign-user
     * @status 201
     * 
     * @param string $dep_uid      {@min 32}{@max 32}
     * @param array  $request_data
     * 
     * @return array
     * @throws RestException 
     * 
     * @access protected
     * @class AccessControl {@permission PM_USERS}
     */
    public function doPostAssignUser($dep_uid, array $request_data)
    {
        try {
            $department = new \ProcessMaker\BusinessModel\Department();

            $arrayData = $department->assignUser($dep_uid, $request_data);
        } catch (\Exception $e) {
            throw new RestException(Api::STAT_APP_EXCEPTION, $e->getMessage());
        }
    }

    /**
     * @url DELETE /:dep_uid/unassign-user/:usr_uid
     * @access protected
     * @class AccessControl {@permission PM_USERS}
     *
     * @param string $dep_uid {@min 1}{@max 32}
     * @param string $usr_uid {@min 1}{@max 32}
     *
     * @status 200
     *
     */
    public function doDeleteUnassignUser($dep_uid, $usr_uid)
    {
        try {
            $oDepartment = new \ProcessMaker\BusinessModel\Department();
            $oDepartment->unassignUser($dep_uid, $usr_uid);
        } catch (\Exception $e) {
            throw (new RestException(Api::STAT_APP_EXCEPTION, $e->getMessage()));
        }
    }

    /**
     * Update manager user
     *
     * @url PUT /:dep_uid/set-manager/:usr_uid
     *
     * @param string $dep_uid {@min 1}{@max 32}
     * @param string $usr_uid {@min 1}{@max 32}
     *
     * @return array
     * @throws RestException
     *
     * @access protected
     * @class AccessControl {@permission PM_USERS}
     */
    public function doPutSetManager($dep_uid, $usr_uid)
    {
        try {
            $oDepartment = new \ProcessMaker\BusinessModel\Department();
            $response = $oDepartment->setManagerUser($dep_uid, $usr_uid);
            return $response;
        } catch (\Exception $e) {
            throw (new RestException(Api::STAT_APP_EXCEPTION, $e->getMessage()));
        }
    }

    /**
     * @url GET /:dep_uid
     *
     * @param string $dep_uid {@min 1}{@max 32}
     *
     * @return array
     *
     */
    public function doGetDepartment($dep_uid)
    {
        try {
            $oDepartment = new \ProcessMaker\BusinessModel\Department();
            $response = $oDepartment->getDepartment($dep_uid);
            return $response;
        } catch (\Exception $e) {
            throw (new RestException(Api::STAT_APP_EXCEPTION, $e->getMessage()));
        }
    }

    /**
     * Create a new department.
     * 
     * @url POST
     * @status 201
     * 
     * @param array $request_data
     * @param string $dep_title {@from body} {@min 1}
     * 
     * @return array
     * @throws RestException 
     * 
     * @access protected
     * @class AccessControl {@permission PM_USERS}
     */
    public function doPost($request_data, $dep_title)
    {
        try {
            $oDepartment = new \ProcessMaker\BusinessModel\Department();
            $response = $oDepartment->saveDepartment($request_data);
            return $response;
        } catch (\Exception $e) {
            throw (new RestException(Api::STAT_APP_EXCEPTION, $e->getMessage()));
        }
    }

    /**
     * Update department.
     *
     * @url PUT /:dep_uid
     *
     * @param string $dep_uid      {@min 1}{@max 32}
     * @param array  $request_data
     *
     * @throws RestException
     *
     * @access protected
     * @class AccessControl {@permission PM_USERS}
     */
    public function doPut($dep_uid, $request_data)
    {
        try {
            $request_data['dep_uid'] = $dep_uid;
            $oDepartment = new \ProcessMaker\BusinessModel\Department();
            $response = $oDepartment->saveDepartment($request_data, false);
        } catch (\Exception $e) {
            throw (new RestException(Api::STAT_APP_EXCEPTION, $e->getMessage()));
        }
    }

    /**
     * @url DELETE /:dep_uid
     * @access protected
     * @class AccessControl {@permission PM_USERS}
     *
     * @param string $dep_uid {@min 1}{@max 32}
     *
     * @return array
     *
     */
    public function doDelete($dep_uid)
    {
        try {
            $oDepartment = new \ProcessMaker\BusinessModel\Department();
            $oDepartment->deleteDepartment($dep_uid);
        } catch (\Exception $e) {
            throw (new RestException(Api::STAT_APP_EXCEPTION, $e->getMessage()));
        }
    }
}