-
Notifications
You must be signed in to change notification settings - Fork 15
Expand file tree
/
Copy pathactions.js
More file actions
387 lines (354 loc) · 9.94 KB
/
actions.js
File metadata and controls
387 lines (354 loc) · 9.94 KB
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
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
import {
graphql,
formatPageQuery,
formatPageQueryWithCount,
formatMutation,
decodeId,
prepareMutation,
graphqlWithVariables,
fetchMutation,
} from "@openimis/fe-core";
import { mapUserValuesToInput } from "./utils";
const USER_SUMMARY_PROJECTION = [
"id",
"username",
"officer{id,dob,phone,lastName,otherNames,email}",
"iUser{id,phone,lastName,otherNames,email,roles{id,name}}",
"claimAdmin{id,phone,lastName,otherNames,emailId,dob}",
"validityTo",
"clientMutationId",
];
const DISTRICT_DATA_FETCH_PARAMS = "id, uuid, code, name, parent { id, uuid, name, code }";
export const USER_PICKER_PROJECTION = ["id", "username", "iUser{id otherNames lastName}"];
export function fetchUsers(mm, filters = [], restrictHealthFacility = true) {
return (dispatch, getState) => {
if (restrictHealthFacility) {
const state = getState();
const hf = state.loc.userHealthFacilityFullPath;
if (hf) {
filters.push(`healthFacilityId: ${decodeId(hf.id)}`);
}
}
const payload = formatPageQuery("users", filters, mm.getRef("admin.UserPicker.projection"));
return dispatch(graphql(payload, "ADMIN_USERS", filters));
};
}
export function fetchUsersSummaries(mm, filters) {
const payload = formatPageQueryWithCount("users", filters, USER_SUMMARY_PROJECTION);
return graphql(payload, "ADMIN_USERS_SUMMARIES");
}
export function fetchEnrolmentOfficers(mm, variables) {
return graphqlWithVariables(
`
query ($searchString: String, $first: Int) {
enrolmentOfficers(str: $searchString, first: $first) {
edges {
node {
id
code
lastName
otherNames
}
}
pageInfo {
hasNextPage
}
}
}
`,
variables,
"ADMIN_ENROLMENT_OFFICERS",
);
}
export function fetchSubstitutionEnrolmentOfficers(mm, variables) {
return graphqlWithVariables(
`
query SubstitutionEnrolmentOfficers ($str: String, $villagesUuids: [String!], $officerUuid: String) {
substitutionEnrolmentOfficers(str: $str, villagesUuids: $villagesUuids, officerUuid: $officerUuid) {
edges {
node {
id
uuid
code
lastName
otherNames
}
}
}
}
`,
variables,
"ADMIN_SUBSTITUTION_ENROLMENT_OFFICERS",
);
}
export function createUser(mm, user, clientMutationLabel) {
const mutation = prepareMutation(
`
mutation ($input: CreateUserMutationInput!) {
createUser(input: $input) {
clientMutationId
internalId
}
}
`,
mapUserValuesToInput(user),
{ clientMutationLabel },
);
// eslint-disable-next-line no-param-reassign
user.clientMutationId = mutation.clientMutationId;
return graphqlWithVariables(
mutation.operation,
mutation.variables,
["ADMIN_USER_MUTATION_REQ", "ADMIN_USER_CREATE_RESP", "ADMIN_USER_MUTATION_ERR"],
{ clientMutationId: mutation.clientMutationId, clientMutationLabel },
);
}
export function updateUser(mm, user, clientMutationLabel) {
const mutation = prepareMutation(
`
mutation ($input: UpdateUserMutationInput!) {
updateUser(input: $input) {
clientMutationId
internalId
}
}
`,
mapUserValuesToInput(user),
{ clientMutationLabel },
);
// eslint-disable-next-line no-param-reassign
user.clientMutationId = mutation.clientMutationId;
return graphqlWithVariables(
mutation.operation,
mutation.variables,
["ADMIN_USER_MUTATION_REQ", "ADMIN_USER_UPDATE_RESP", "ADMIN_USER_MUTATION_ERR"],
{ clientMutationId: mutation.clientMutationId, clientMutationLabel, userId: user.id },
);
}
export function deleteUser(mm, user, clientMutationLabel) {
const mutation = formatMutation("deleteUser", `uuids: ["${decodeId(user.id)}"]`, clientMutationLabel);
// eslint-disable-next-line no-param-reassign
user.clientMutationId = mutation.clientMutationId;
return (dispatch) => {
dispatch(
graphql(mutation.payload, ["ADMIN_USER_MUTATION_REQ", "ADMIN_USER_DELETE_RESP", "ADMIN_USER_MUTATION_ERR"], {
clientMutationId: mutation.clientMutationId,
clientMutationLabel,
userId: user.id,
}),
);
dispatch(fetchMutation(mutation.clientMutationId));
};
}
export function fetchUser(mm, userId, clientMutationId) {
const filters = [];
if (userId) {
filters.push(`id: "${decodeId(userId)}"`);
filters.push("showDeleted: true");
} else if (clientMutationId) {
filters.push(`clientMutationId: "${clientMutationId}"`);
}
return graphql(
`
{
users(${filters.join(" ")}) {
pageInfo { hasNextPage, hasPreviousPage, startCursor, endCursor}
edges {
node {
clientMutationId
id
username
validityTo
officer {
id
uuid
hasLogin
phone
dob
lastName
otherNames
address
substitutionOfficer { id lastName otherNames code }
worksTo
officerVillages {
id
location {
id
name
code
uuid
parent {
id
name
code
uuid
}
}
}
location {
id
name
uuid
code
parent {
id
name
uuid
code
}
}
}
iUser {
id
phone
languageId
lastName
otherNames
roles { id name isSystem}
programSet { edges{node{id idProgram nameProgram validityDateFrom}}}
healthFacility ${mm.getProjection("location.HealthFacilityPicker.projection")}
validityFrom
validityTo
email
districts: userdistrictSet { location { id name code uuid parent { id code uuid name }}}
}
claimAdmin{
id
hasLogin
emailId
phone
dob
lastName
otherNames
healthFacility ${mm.getProjection("location.HealthFacilityPicker.projection")}
}
}
}
}
}
`,
"ADMIN_USER_OVERVIEW",
);
}
export function newUser() {
return (dispatch) => {
dispatch({ type: "ADMIN_USER_NEW" });
};
}
export function fetchUserMutation(mm, clientMutationId) {
const payload = formatPageQuery(
"mutationLogs",
[`clientMutationId:"${clientMutationId}"`],
["id", "status", "users{coreUser{id}}"],
);
return graphql(payload, "ADMIN_USER");
}
export function fetchRegionDistricts(parent) {
const filters = [`type: "D"`];
if (parent) {
filters.push(`parent_Uuid: "${parent.uuid}"`);
}
const payload = formatPageQuery("locations", filters, [
"id",
"uuid",
"type",
"code",
"name",
"malePopulation",
"femalePopulation",
"otherPopulation",
"families",
"clientMutationId",
]);
return graphql(payload, `LOCATION_REGION_DISTRICTS`);
}
export function fetchDataFromDistrict(districtUuids) {
const filters = [];
if (districtUuids) {
filters.push(`parent_Uuid_In: ["${districtUuids.join('", "')}"]`);
}
const payload = formatPageQuery("locations", filters, [
`${DISTRICT_DATA_FETCH_PARAMS}, children { edges {node {${DISTRICT_DATA_FETCH_PARAMS}}}}`,
]);
return graphql(payload, `LOCATION_DISTRICT_DATA`);
}
export function fetchObligatoryUserFields() {
const payload = "query userObligatoryFields {userObligatoryFields}";
return graphql(payload, `OBLIGTORY_USER_FIELDS`);
}
export function fetchObligatoryEnrolmentOfficerFields() {
const payload = "query userObligatoryFields {eoObligatoryFields}";
return graphql(payload, `OBLIGTORY_EO_FIELDS`);
}
export function clearRegionDistricts() {
return (dispatch) => {
dispatch({ type: `LOCATION_REGION_DISTRICTS_CLEAR` });
};
}
export function clearDistrictData() {
return (dispatch) => {
dispatch({ type: `LOCATION_DISTRICT_DATA_CLEAR` });
};
}
export function usernameValidationCheck(mm, variables) {
return graphqlWithVariables(
`
query ($username: String!) {
isValid: validateUsername(username: $username)
}
`,
variables,
`USERNAME_FIELDS_VALIDATION`,
);
}
export function fetchUsernameLength() {
const payload = "query {usernameLength}";
return graphql(payload, `USERNAME_LENGTH_FIELDS`);
}
export function fetchPasswordPolicy() {
const payload = `query {
passwordPolicy
}`;
return graphql(payload, "PASSWORD_POLICY_FIELDS");
}
export function usernameValidationClear() {
return (dispatch) => {
dispatch({ type: `USERNAME_FIELDS_VALIDATION_CLEAR` });
};
}
export function setUsernameValid() {
return (dispatch) => {
dispatch({ type: "USERNAME_FIELDS_VALIDATION_SET_VALID" });
};
}
export function clearUser() {
return (dispatch) => {
dispatch({ type: "ADMIN_USER_OVERVIEW_CLEAR" });
};
}
export function userEmailValidationCheck(mm, variables) {
return graphqlWithVariables(
`
query ($userEmail: String!) {
isValid: validateUserEmail(userEmail: $userEmail)
}
`,
variables,
`USER_EMAIL_FIELDS_VALIDATION`,
);
}
export function userEmailValidationClear() {
return (dispatch) => {
dispatch({ type: `USER_EMAIL_FIELDS_VALIDATION_CLEAR` });
};
}
export function setUserEmailValid() {
return (dispatch) => {
dispatch({ type: "USER_EMAIL_FIELDS_VALIDATION_SET_VALID" });
};
}
export function saveEmailFormatValidity(isFormatInvalid) {
return (dispatch) => {
dispatch({ type: "USER_EMAIL_FORMAT_VALIDATION_CHECK", payload: { data: { isFormatInvalid } } });
};
}