-
Notifications
You must be signed in to change notification settings - Fork 2.9k
/
Task.js
964 lines (883 loc) · 34.8 KB
/
Task.js
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
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
import lodashGet from 'lodash/get';
import Onyx from 'react-native-onyx';
import _ from 'underscore';
import * as Expensicons from '@components/Icon/Expensicons';
import * as API from '@libs/API';
import DateUtils from '@libs/DateUtils';
import * as ErrorUtils from '@libs/ErrorUtils';
import * as LocalePhoneNumber from '@libs/LocalePhoneNumber';
import * as Localize from '@libs/Localize';
import Navigation from '@libs/Navigation/Navigation';
import * as OptionsListUtils from '@libs/OptionsListUtils';
import * as ReportActionsUtils from '@libs/ReportActionsUtils';
import * as ReportUtils from '@libs/ReportUtils';
import * as UserUtils from '@libs/UserUtils';
import CONST from '@src/CONST';
import ONYXKEYS from '@src/ONYXKEYS';
import ROUTES from '@src/ROUTES';
import * as Report from './Report';
let currentUserEmail;
let currentUserAccountID;
Onyx.connect({
key: ONYXKEYS.SESSION,
callback: (val) => {
currentUserEmail = lodashGet(val, 'email', '');
currentUserAccountID = lodashGet(val, 'accountID', 0);
},
});
let allPersonalDetails;
Onyx.connect({
key: ONYXKEYS.PERSONAL_DETAILS_LIST,
callback: (val) => (allPersonalDetails = val),
});
/**
* Clears out the task info from the store
*/
function clearOutTaskInfo() {
Onyx.set(ONYXKEYS.TASK, null);
}
/**
* A task needs two things to be created - a title and a parent report
* When you create a task report, there are a few things that happen:
* A task report is created, along with a CreatedReportAction for that new task report
* A reportAction indicating that a task was created is added to the parent report (share destination)
* If you assign the task to someone, a reportAction is created in the chat between you and the assignee to inform them of the task
*
* So you have the following optimistic items potentially being created:
* 1. The task report
* 1a. The CreatedReportAction for the task report
* 2. The TaskReportAction on the parent report
* 3. The chat report between you and the assignee
* 3a. The CreatedReportAction for the assignee chat report
* 3b. The TaskReportAction on the assignee chat report
*
* @param {String} parentReportID
* @param {String} title
* @param {String} description
* @param {String} assigneeEmail
* @param {Number} assigneeAccountID
* @param {Object} assigneeChatReport - The chat report between you and the assignee
* @param {String} policyID - the policyID of the parent report
*/
function createTaskAndNavigate(parentReportID, title, description, assigneeEmail, assigneeAccountID = 0, assigneeChatReport = null, policyID = CONST.POLICY.OWNER_EMAIL_FAKE) {
const optimisticTaskReport = ReportUtils.buildOptimisticTaskReport(currentUserAccountID, assigneeAccountID, parentReportID, title, description, policyID);
const assigneeChatReportID = assigneeChatReport ? assigneeChatReport.reportID : 0;
const taskReportID = optimisticTaskReport.reportID;
let assigneeChatReportOnyxData;
// Parent ReportAction indicating that a task has been created
const optimisticTaskCreatedAction = ReportUtils.buildOptimisticCreatedReportAction(currentUserEmail);
const optimisticAddCommentReport = ReportUtils.buildOptimisticTaskCommentReportAction(taskReportID, title, assigneeAccountID, `task for ${title}`, parentReportID);
optimisticTaskReport.parentReportActionID = optimisticAddCommentReport.reportAction.reportActionID;
const currentTime = DateUtils.getDBTime();
const lastCommentText = ReportUtils.formatReportLastMessageText(optimisticAddCommentReport.reportAction.message[0].text);
const optimisticParentReport = {
lastVisibleActionCreated: currentTime,
lastMessageText: lastCommentText,
lastActorAccountID: currentUserAccountID,
lastReadTime: currentTime,
lastMessageTranslationKey: '',
};
// We're only setting onyx data for the task report here because it's possible for the parent report to not exist yet (if you're assigning a task to someone you haven't chatted with before)
// So we don't want to set the parent report data until we've successfully created that chat report
// FOR TASK REPORT
const optimisticData = [
{
onyxMethod: Onyx.METHOD.SET,
key: `${ONYXKEYS.COLLECTION.REPORT}${optimisticTaskReport.reportID}`,
value: {
...optimisticTaskReport,
pendingFields: {
createChat: CONST.RED_BRICK_ROAD_PENDING_ACTION.ADD,
reportName: CONST.RED_BRICK_ROAD_PENDING_ACTION.ADD,
description: CONST.RED_BRICK_ROAD_PENDING_ACTION.ADD,
managerID: CONST.RED_BRICK_ROAD_PENDING_ACTION.ADD,
},
isOptimisticReport: true,
},
},
{
onyxMethod: Onyx.METHOD.SET,
key: `${ONYXKEYS.COLLECTION.REPORT_ACTIONS}${optimisticTaskReport.reportID}`,
value: {[optimisticTaskCreatedAction.reportActionID]: optimisticTaskCreatedAction},
},
];
// FOR TASK REPORT
const successData = [
{
onyxMethod: Onyx.METHOD.MERGE,
key: `${ONYXKEYS.COLLECTION.REPORT}${optimisticTaskReport.reportID}`,
value: {
pendingFields: {
createChat: null,
reportName: null,
description: null,
managerID: null,
},
isOptimisticReport: false,
},
},
{
onyxMethod: Onyx.METHOD.MERGE,
key: `${ONYXKEYS.COLLECTION.REPORT_ACTIONS}${optimisticTaskReport.reportID}`,
value: {[optimisticTaskCreatedAction.reportActionID]: {pendingAction: null}},
},
];
// FOR TASK REPORT
const failureData = [
{
onyxMethod: Onyx.METHOD.MERGE,
key: `${ONYXKEYS.COLLECTION.REPORT}${optimisticTaskReport.reportID}`,
value: {
errorFields: {
createTask: ErrorUtils.getMicroSecondOnyxError('task.genericCreateTaskFailureMessage'),
},
},
},
{
onyxMethod: Onyx.METHOD.MERGE,
key: `${ONYXKEYS.COLLECTION.REPORT_ACTIONS}${optimisticTaskReport.reportID}`,
value: {[optimisticTaskCreatedAction.reportActionID]: {pendingAction: null}},
},
];
if (assigneeChatReport) {
assigneeChatReportOnyxData = ReportUtils.getTaskAssigneeChatOnyxData(
currentUserAccountID,
assigneeAccountID,
taskReportID,
assigneeChatReportID,
parentReportID,
title,
assigneeChatReport,
);
optimisticData.push(...assigneeChatReportOnyxData.optimisticData);
successData.push(...assigneeChatReportOnyxData.successData);
failureData.push(...assigneeChatReportOnyxData.failureData);
}
// Now that we've created the optimistic chat report and chat reportActions, we can update the parent report data
// FOR PARENT REPORT (SHARE DESTINATION)
optimisticData.push(
{
onyxMethod: Onyx.METHOD.MERGE,
key: `${ONYXKEYS.COLLECTION.REPORT}${parentReportID}`,
value: optimisticParentReport,
},
{
onyxMethod: Onyx.METHOD.MERGE,
key: `${ONYXKEYS.COLLECTION.REPORT_ACTIONS}${parentReportID}`,
value: {[optimisticAddCommentReport.reportAction.reportActionID]: optimisticAddCommentReport.reportAction},
},
);
// FOR PARENT REPORT (SHARE DESTINATION)
successData.push({
onyxMethod: Onyx.METHOD.MERGE,
key: `${ONYXKEYS.COLLECTION.REPORT_ACTIONS}${parentReportID}`,
value: {[optimisticAddCommentReport.reportAction.reportActionID]: {pendingAction: null}},
});
// FOR PARENT REPORT (SHARE DESTINATION)
failureData.push({
onyxMethod: Onyx.METHOD.MERGE,
key: `${ONYXKEYS.COLLECTION.REPORT_ACTIONS}${parentReportID}`,
value: {
[optimisticAddCommentReport.reportAction.reportActionID]: {
errors: ErrorUtils.getMicroSecondOnyxError('task.genericCreateTaskFailureMessage'),
},
},
});
clearOutTaskInfo();
API.write(
'CreateTask',
{
parentReportActionID: optimisticAddCommentReport.reportAction.reportActionID,
parentReportID,
taskReportID: optimisticTaskReport.reportID,
createdTaskReportActionID: optimisticTaskCreatedAction.reportActionID,
title: optimisticTaskReport.reportName,
description: optimisticTaskReport.description,
assignee: assigneeEmail,
assigneeAccountID,
assigneeChatReportID,
assigneeChatReportActionID:
assigneeChatReportOnyxData && assigneeChatReportOnyxData.optimisticAssigneeAddComment
? assigneeChatReportOnyxData.optimisticAssigneeAddComment.reportAction.reportActionID
: 0,
assigneeChatCreatedReportActionID:
assigneeChatReportOnyxData && assigneeChatReportOnyxData.optimisticChatCreatedReportAction ? assigneeChatReportOnyxData.optimisticChatCreatedReportAction.reportActionID : 0,
},
{optimisticData, successData, failureData},
);
Navigation.dismissModal(parentReportID);
}
/**
* Complete a task
* @param {Object} taskReport task report
*/
function completeTask(taskReport) {
const taskReportID = taskReport.reportID;
const message = `marked as complete`;
const completedTaskReportAction = ReportUtils.buildOptimisticTaskReportAction(taskReportID, CONST.REPORT.ACTIONS.TYPE.TASKCOMPLETED, message);
const optimisticData = [
{
onyxMethod: Onyx.METHOD.MERGE,
key: `${ONYXKEYS.COLLECTION.REPORT}${taskReportID}`,
value: {
stateNum: CONST.REPORT.STATE_NUM.SUBMITTED,
statusNum: CONST.REPORT.STATUS.APPROVED,
},
},
{
onyxMethod: Onyx.METHOD.MERGE,
key: `${ONYXKEYS.COLLECTION.REPORT_ACTIONS}${taskReportID}`,
value: {[completedTaskReportAction.reportActionID]: completedTaskReportAction},
},
];
const successData = [
{
onyxMethod: Onyx.METHOD.MERGE,
key: `${ONYXKEYS.COLLECTION.REPORT_ACTIONS}${taskReportID}`,
value: {
[completedTaskReportAction.reportActionID]: {
pendingAction: null,
},
},
},
];
const failureData = [
{
onyxMethod: Onyx.METHOD.MERGE,
key: `${ONYXKEYS.COLLECTION.REPORT}${taskReportID}`,
value: {
stateNum: CONST.REPORT.STATE_NUM.OPEN,
statusNum: CONST.REPORT.STATUS.OPEN,
},
},
{
onyxMethod: Onyx.METHOD.MERGE,
key: `${ONYXKEYS.COLLECTION.REPORT_ACTIONS}${taskReportID}`,
value: {
[completedTaskReportAction.reportActionID]: {
errors: ErrorUtils.getMicroSecondOnyxError('task.messages.error'),
},
},
},
];
API.write(
'CompleteTask',
{
taskReportID,
completedTaskReportActionID: completedTaskReportAction.reportActionID,
},
{optimisticData, successData, failureData},
);
}
/**
* Reopen a closed task
* @param {Object} taskReport task report
*/
function reopenTask(taskReport) {
const taskReportID = taskReport.reportID;
const message = `marked as incomplete`;
const reopenedTaskReportAction = ReportUtils.buildOptimisticTaskReportAction(taskReportID, CONST.REPORT.ACTIONS.TYPE.TASKREOPENED, message);
const optimisticData = [
{
onyxMethod: Onyx.METHOD.MERGE,
key: `${ONYXKEYS.COLLECTION.REPORT}${taskReportID}`,
value: {
stateNum: CONST.REPORT.STATE_NUM.OPEN,
statusNum: CONST.REPORT.STATUS.OPEN,
lastVisibleActionCreated: reopenedTaskReportAction.created,
lastMessageText: message,
lastActorAccountID: reopenedTaskReportAction.actorAccountID,
lastReadTime: reopenedTaskReportAction.created,
},
},
{
onyxMethod: Onyx.METHOD.MERGE,
key: `${ONYXKEYS.COLLECTION.REPORT_ACTIONS}${taskReportID}`,
value: {[reopenedTaskReportAction.reportActionID]: reopenedTaskReportAction},
},
];
const successData = [
{
onyxMethod: Onyx.METHOD.MERGE,
key: `${ONYXKEYS.COLLECTION.REPORT_ACTIONS}${taskReportID}`,
value: {
[reopenedTaskReportAction.reportActionID]: {
pendingAction: null,
},
},
},
];
const failureData = [
{
onyxMethod: Onyx.METHOD.MERGE,
key: `${ONYXKEYS.COLLECTION.REPORT}${taskReportID}`,
value: {
stateNum: CONST.REPORT.STATE_NUM.SUBMITTED,
statusNum: CONST.REPORT.STATUS.APPROVED,
},
},
{
onyxMethod: Onyx.METHOD.MERGE,
key: `${ONYXKEYS.COLLECTION.REPORT_ACTIONS}${taskReportID}`,
value: {
[reopenedTaskReportAction.reportActionID]: {
errors: ErrorUtils.getMicroSecondOnyxError('task.messages.error'),
},
},
},
];
API.write(
'ReopenTask',
{
taskReportID,
reopenedTaskReportActionID: reopenedTaskReportAction.reportActionID,
},
{optimisticData, successData, failureData},
);
}
/**
* @param {object} report
* @param {Object} editedTask
*/
function editTaskAndNavigate(report, {title, description}) {
// Create the EditedReportAction on the task
const editTaskReportAction = ReportUtils.buildOptimisticEditedTaskReportAction(currentUserEmail);
// Sometimes title or description is undefined, so we need to check for that, and we provide it to multiple functions
const reportName = (title || report.reportName).trim();
// Description can be unset, so we default to an empty string if so
const reportDescription = (!_.isUndefined(description) ? description : lodashGet(report, 'description', '')).trim();
const optimisticData = [
{
onyxMethod: Onyx.METHOD.MERGE,
key: `${ONYXKEYS.COLLECTION.REPORT_ACTIONS}${report.reportID}`,
value: {[editTaskReportAction.reportActionID]: editTaskReportAction},
},
{
onyxMethod: Onyx.METHOD.MERGE,
key: `${ONYXKEYS.COLLECTION.REPORT}${report.reportID}`,
value: {
reportName,
description: reportDescription,
pendingFields: {
...(title && {reportName: CONST.RED_BRICK_ROAD_PENDING_ACTION.UPDATE}),
...(description && {description: CONST.RED_BRICK_ROAD_PENDING_ACTION.UPDATE}),
},
},
},
];
const successData = [
{
onyxMethod: Onyx.METHOD.MERGE,
key: `${ONYXKEYS.COLLECTION.REPORT}${report.reportID}`,
value: {
pendingFields: {
...(title && {reportName: null}),
...(description && {description: null}),
},
},
},
];
const failureData = [
{
onyxMethod: Onyx.METHOD.MERGE,
key: `${ONYXKEYS.COLLECTION.REPORT_ACTIONS}${report.reportID}`,
value: {[editTaskReportAction.reportActionID]: {pendingAction: null}},
},
{
onyxMethod: Onyx.METHOD.MERGE,
key: `${ONYXKEYS.COLLECTION.REPORT}${report.reportID}`,
value: {
reportName: report.reportName,
description: report.description,
},
},
];
API.write(
'EditTask',
{
taskReportID: report.reportID,
title: reportName,
description: reportDescription,
editedTaskReportActionID: editTaskReportAction.reportActionID,
},
{optimisticData, successData, failureData},
);
Navigation.dismissModal(report.reportID);
}
function editTaskAssigneeAndNavigate(report, ownerAccountID, assigneeEmail, assigneeAccountID = 0, assigneeChatReport = null) {
// Create the EditedReportAction on the task
const editTaskReportAction = ReportUtils.buildOptimisticEditedTaskReportAction(currentUserEmail);
const reportName = report.reportName.trim();
let assigneeChatReportOnyxData;
const assigneeChatReportID = assigneeChatReport ? assigneeChatReport.reportID : 0;
const optimisticReport = {
reportName,
managerID: assigneeAccountID || report.managerID,
pendingFields: {
...(assigneeAccountID && {managerID: CONST.RED_BRICK_ROAD_PENDING_ACTION.UPDATE}),
},
notificationPreference: [assigneeAccountID, ownerAccountID].includes(currentUserAccountID)
? CONST.REPORT.NOTIFICATION_PREFERENCE.ALWAYS
: CONST.REPORT.NOTIFICATION_PREFERENCE.HIDDEN,
};
const optimisticData = [
{
onyxMethod: Onyx.METHOD.MERGE,
key: `${ONYXKEYS.COLLECTION.REPORT_ACTIONS}${report.reportID}`,
value: {[editTaskReportAction.reportActionID]: editTaskReportAction},
},
{
onyxMethod: Onyx.METHOD.MERGE,
key: `${ONYXKEYS.COLLECTION.REPORT}${report.reportID}`,
value: optimisticReport,
},
];
const successData = [
{
onyxMethod: Onyx.METHOD.MERGE,
key: `${ONYXKEYS.COLLECTION.REPORT}${report.reportID}`,
value: {pendingFields: {...(assigneeAccountID && {managerID: null})}},
},
];
const failureData = [
{
onyxMethod: Onyx.METHOD.MERGE,
key: `${ONYXKEYS.COLLECTION.REPORT_ACTIONS}${report.reportID}`,
value: {[editTaskReportAction.reportActionID]: {pendingAction: null}},
},
{
onyxMethod: Onyx.METHOD.MERGE,
key: `${ONYXKEYS.COLLECTION.REPORT}${report.reportID}`,
value: {managerID: report.managerID},
},
];
// If we make a change to the assignee, we want to add a comment to the assignee's chat
// Check if the assignee actually changed
if (assigneeAccountID && assigneeAccountID !== report.managerID && assigneeAccountID !== ownerAccountID && assigneeChatReport) {
const participants = lodashGet(report, 'participantAccountIDs', []);
if (!participants.includes(assigneeAccountID)) {
optimisticReport.participantAccountIDs = [...participants, assigneeAccountID];
}
assigneeChatReportOnyxData = ReportUtils.getTaskAssigneeChatOnyxData(
currentUserAccountID,
assigneeAccountID,
report.reportID,
assigneeChatReportID,
report.parentReportID,
reportName,
assigneeChatReport,
);
optimisticData.push(...assigneeChatReportOnyxData.optimisticData);
successData.push(...assigneeChatReportOnyxData.successData);
failureData.push(...assigneeChatReportOnyxData.failureData);
}
API.write(
'EditTaskAssignee',
{
taskReportID: report.reportID,
assignee: assigneeEmail,
editedTaskReportActionID: editTaskReportAction.reportActionID,
assigneeChatReportID,
assigneeChatReportActionID:
assigneeChatReportOnyxData && assigneeChatReportOnyxData.optimisticAssigneeAddComment
? assigneeChatReportOnyxData.optimisticAssigneeAddComment.reportAction.reportActionID
: 0,
assigneeChatCreatedReportActionID:
assigneeChatReportOnyxData && assigneeChatReportOnyxData.optimisticChatCreatedReportAction ? assigneeChatReportOnyxData.optimisticChatCreatedReportAction.reportActionID : 0,
},
{optimisticData, successData, failureData},
);
Navigation.dismissModal(report.reportID);
}
/**
* Sets the report info for the task being viewed
*
* @param {Object} report
*/
function setTaskReport(report) {
Onyx.merge(ONYXKEYS.TASK, {report});
}
/**
* Sets the title and description values for the task
* @param {string} title
* @param {string} description
*/
function setDetailsValue(title, description) {
// This is only needed for creation of a new task and so it should only be stored locally
Onyx.merge(ONYXKEYS.TASK, {title: title.trim(), description: description.trim()});
}
/**
* Sets the title value for the task
* @param {string} title
*/
function setTitleValue(title) {
Onyx.merge(ONYXKEYS.TASK, {title: title.trim()});
}
/**
* Sets the description value for the task
* @param {string} description
*/
function setDescriptionValue(description) {
Onyx.merge(ONYXKEYS.TASK, {description: description.trim()});
}
/**
* Sets the shareDestination value for the task
* @param {string} shareDestination
*/
function setShareDestinationValue(shareDestination) {
// This is only needed for creation of a new task and so it should only be stored locally
Onyx.merge(ONYXKEYS.TASK, {shareDestination});
}
/* Sets the assigneeChatReport details for the task
* @param {Object} chatReport
*/
function setAssigneeChatReport(chatReport) {
Onyx.merge(ONYXKEYS.TASK, {assigneeChatReport: chatReport});
}
/**
* Sets the assignee value for the task and checks for an existing chat with the assignee
* If there is no existing chat, it creates an optimistic chat report
* It also sets the shareDestination as that chat report if a share destination isn't already set
* @param {string} assigneeEmail
* @param {Number} assigneeAccountID
* @param {string} shareDestination
* @param {boolean} isCurrentUser
*/
function setAssigneeValue(assigneeEmail, assigneeAccountID, shareDestination, isCurrentUser = false) {
let chatReport;
if (!isCurrentUser) {
chatReport = ReportUtils.getChatByParticipants([assigneeAccountID]);
if (!chatReport) {
chatReport = ReportUtils.buildOptimisticChatReport([assigneeAccountID]);
chatReport.isOptimisticReport = true;
// When assigning a task to a new user, by default we share the task in their DM
// However, the DM doesn't exist yet - and will be created optimistically once the task is created
// We don't want to show the new DM yet, because if you select an assignee and then change the assignee, the previous DM will still be shown
// So here, we create it optimistically to share it with the assignee, but we have to hide it until the task is created
chatReport.isHidden = true;
Onyx.set(`${ONYXKEYS.COLLECTION.REPORT}${chatReport.reportID}`, chatReport);
// If this is an optimistic report, we likely don't have their personal details yet so we set it here optimistically as well
const optimisticPersonalDetailsListAction = {
accountID: assigneeAccountID,
avatar: lodashGet(allPersonalDetails, [assigneeAccountID, 'avatar'], UserUtils.getDefaultAvatarURL(assigneeAccountID)),
displayName: lodashGet(allPersonalDetails, [assigneeAccountID, 'displayName'], assigneeEmail),
login: assigneeEmail,
};
Onyx.merge(ONYXKEYS.PERSONAL_DETAILS_LIST, {[assigneeAccountID]: optimisticPersonalDetailsListAction});
}
setAssigneeChatReport(chatReport);
// If there is no share destination set, automatically set it to the assignee chat report
// This allows for a much quicker process when creating a new task and is likely the desired share destination most times
if (!shareDestination) {
setShareDestinationValue(chatReport.reportID);
}
}
// This is only needed for creation of a new task and so it should only be stored locally
Onyx.merge(ONYXKEYS.TASK, {assignee: assigneeEmail, assigneeAccountID});
// When we're editing the assignee, we immediately call EditTaskAndNavigate. Since setting the assignee is async,
// the chatReport is not yet set when EditTaskAndNavigate is called. So we return the chatReport here so that
// EditTaskAndNavigate can use it.
return chatReport;
}
/**
* Sets the parentReportID value for the task
* @param {string} parentReportID
*/
function setParentReportID(parentReportID) {
// This is only needed for creation of a new task and so it should only be stored locally
Onyx.merge(ONYXKEYS.TASK, {parentReportID});
}
/**
* Clears out the task info from the store and navigates to the NewTaskDetails page
* @param {string} reportID
*/
function clearOutTaskInfoAndNavigate(reportID) {
clearOutTaskInfo();
if (reportID && reportID !== '0') {
setParentReportID(reportID);
}
Navigation.navigate(ROUTES.NEW_TASK_DETAILS);
}
/**
* Get the assignee data
*
* @param {Number} assigneeAccountID
* @param {Object} personalDetails
* @returns {Object}
*/
function getAssignee(assigneeAccountID, personalDetails) {
const details = personalDetails[assigneeAccountID];
if (!details) {
return {
icons: [],
displayName: '',
subtitle: '',
};
}
return {
icons: ReportUtils.getIconsForParticipants([details.accountID], personalDetails),
displayName: details.displayName,
subtitle: details.login,
};
}
/**
* Get the share destination data
* @param {Object} reportID
* @param {Object} reports
* @param {Object} personalDetails
* @returns {Object}
* */
function getShareDestination(reportID, reports, personalDetails) {
const report = lodashGet(reports, `report_${reportID}`, {});
const participantAccountIDs = lodashGet(report, 'participantAccountIDs');
const isMultipleParticipant = participantAccountIDs.length > 1;
const displayNamesWithTooltips = ReportUtils.getDisplayNamesWithTooltips(OptionsListUtils.getPersonalDetailsForAccountIDs(participantAccountIDs, personalDetails), isMultipleParticipant);
let subtitle = '';
if (ReportUtils.isChatReport(report) && ReportUtils.isDM(report) && ReportUtils.hasSingleParticipant(report)) {
const participantAccountID = lodashGet(report, 'participantAccountIDs[0]');
const displayName = lodashGet(personalDetails, [participantAccountID, 'displayName']);
const login = lodashGet(personalDetails, [participantAccountID, 'login']);
subtitle = LocalePhoneNumber.formatPhoneNumber(login || displayName);
} else {
subtitle = ReportUtils.getChatRoomSubtitle(report);
}
return {
icons: ReportUtils.getIcons(report, personalDetails, Expensicons.FallbackAvatar),
displayName: ReportUtils.getReportName(report),
subtitle,
displayNamesWithTooltips,
shouldUseFullTitleToDisplay: ReportUtils.shouldUseFullTitleToDisplay(report),
};
}
/**
* Cancels a task by setting the report state to SUBMITTED and status to CLOSED
* @param {string} taskReportID
* @param {string} taskTitle
* @param {number} originalStateNum
* @param {number} originalStatusNum
*/
function cancelTask(taskReportID, taskTitle, originalStateNum, originalStatusNum) {
const message = `deleted task: ${taskTitle}`;
const optimisticCancelReportAction = ReportUtils.buildOptimisticTaskReportAction(taskReportID, CONST.REPORT.ACTIONS.TYPE.TASKCANCELLED, message);
const optimisticReportActionID = optimisticCancelReportAction.reportActionID;
const taskReport = ReportUtils.getReport(taskReportID);
const parentReportAction = ReportActionsUtils.getParentReportAction(taskReport);
const parentReport = ReportUtils.getParentReport(taskReport);
const optimisticReportActions = {
[parentReportAction.reportActionID]: {
pendingAction: CONST.RED_BRICK_ROAD_PENDING_ACTION.UPDATE,
previousMessage: parentReportAction.message,
message: [
{
translationKey: '',
type: 'COMMENT',
html: '',
text: '',
isEdited: true,
isDeletedParentAction: true,
},
],
errors: null,
linkMetaData: [],
},
};
const optimisticData = [
{
onyxMethod: Onyx.METHOD.MERGE,
key: `${ONYXKEYS.COLLECTION.REPORT}${taskReportID}`,
value: {
lastVisibleActionCreated: optimisticCancelReportAction.created,
lastMessageText: message,
lastActorAccountID: optimisticCancelReportAction.actorAccountID,
updateReportInLHN: true,
isDeletedParentAction: true,
},
},
{
onyxMethod: Onyx.METHOD.MERGE,
key: `${ONYXKEYS.COLLECTION.REPORT}${parentReport.reportID}`,
value: {
lastMessageText: ReportActionsUtils.getLastVisibleMessage(parentReport.reportID, optimisticReportActions).lastMessageText,
lastVisibleActionCreated: lodashGet(ReportActionsUtils.getLastVisibleAction(parentReport.reportID, optimisticReportActions), 'created'),
},
},
{
onyxMethod: Onyx.METHOD.MERGE,
key: `${ONYXKEYS.COLLECTION.REPORT_ACTIONS}${taskReportID}`,
value: {
[optimisticReportActionID]: optimisticCancelReportAction,
},
},
{
onyxMethod: Onyx.METHOD.MERGE,
key: `${ONYXKEYS.COLLECTION.REPORT_ACTIONS}${parentReport.reportID}`,
value: optimisticReportActions,
},
];
const successData = [
{
onyxMethod: Onyx.METHOD.MERGE,
key: `${ONYXKEYS.COLLECTION.REPORT_ACTIONS}${taskReportID}`,
value: {
[optimisticReportActionID]: {
pendingAction: null,
},
},
},
{
onyxMethod: Onyx.METHOD.MERGE,
key: `${ONYXKEYS.COLLECTION.REPORT_ACTIONS}${parentReport.reportID}`,
value: {
[parentReportAction.reportActionID]: {
pendingAction: null,
},
},
},
];
const failureData = [
{
onyxMethod: Onyx.METHOD.MERGE,
key: `${ONYXKEYS.COLLECTION.REPORT}${taskReportID}`,
value: {
stateNum: originalStateNum,
statusNum: originalStatusNum,
},
},
{
onyxMethod: Onyx.METHOD.MERGE,
key: `${ONYXKEYS.COLLECTION.REPORT_ACTIONS}${taskReportID}`,
value: {
[optimisticReportActionID]: null,
},
},
{
onyxMethod: Onyx.METHOD.MERGE,
key: `${ONYXKEYS.COLLECTION.REPORT_ACTIONS}${parentReport.reportID}`,
value: {
[parentReportAction.reportActionID]: {
pendingAction: null,
},
},
},
];
API.write('CancelTask', {cancelledTaskReportActionID: optimisticReportActionID, taskReportID}, {optimisticData, successData, failureData});
}
/**
* Closes the current open task modal and clears out the task info from the store.
*/
function dismissModalAndClearOutTaskInfo() {
Navigation.dismissModal();
clearOutTaskInfo();
}
/**
* Returns Task assignee accountID
*
* @param {Object} taskReport
* @returns {Number|null}
*/
function getTaskAssigneeAccountID(taskReport) {
if (!taskReport) {
return null;
}
if (taskReport.managerID) {
return taskReport.managerID;
}
const reportAction = ReportActionsUtils.getParentReportAction(taskReport);
return lodashGet(reportAction, 'childManagerAccountID');
}
/**
* Returns Task owner accountID
*
* @param {Object} taskReport
* @returns {Number|null}
*/
function getTaskOwnerAccountID(taskReport) {
return lodashGet(taskReport, 'ownerAccountID', null);
}
/**
* Check if you're allowed to modify the task - anyone that has write access to the report can modify the task
* @param {Object} taskReport
* @param {Number} sessionAccountID
* @returns {Boolean}
*/
function canModifyTask(taskReport, sessionAccountID) {
if (ReportUtils.isCanceledTaskReport(taskReport)) {
return false;
}
if (sessionAccountID === getTaskOwnerAccountID(taskReport) || sessionAccountID === getTaskAssigneeAccountID(taskReport)) {
return true;
}
// If you don't have access to the task report (maybe haven't opened it yet), check if you can access the parent report
// - If the parent report is an #admins only room
// - If you are a policy admin
const parentReport = ReportUtils.getParentReport(taskReport);
return ReportUtils.isAllowedToComment(parentReport);
}
/**
* @param {String} reportID
*/
function clearTaskErrors(reportID) {
const report = ReportUtils.getReport(reportID);
// Delete the task preview in the parent report
if (lodashGet(report, 'pendingFields.createChat') === CONST.RED_BRICK_ROAD_PENDING_ACTION.ADD) {
Onyx.merge(`${ONYXKEYS.COLLECTION.REPORT_ACTIONS}${report.parentReportID}`, {
[report.parentReportActionID]: null,
});
Report.navigateToConciergeChatAndDeleteReport(reportID);
return;
}
Onyx.merge(`${ONYXKEYS.COLLECTION.REPORT}${reportID}`, {
pendingFields: null,
errorFields: null,
});
}
/**
* @param {string} actionName
* @param {string} reportID
* @param {boolean} isCreateTaskAction
* @returns {string}
*/
function getTaskReportActionMessage(actionName, reportID, isCreateTaskAction) {
const report = ReportUtils.getReport(reportID);
if (isCreateTaskAction) {
return `task for ${report.reportName}`;
}
let taskStatusText = '';
switch (actionName) {
case CONST.REPORT.ACTIONS.TYPE.TASKCOMPLETED:
taskStatusText = Localize.translateLocal('task.messages.completed');
break;
case CONST.REPORT.ACTIONS.TYPE.TASKCANCELLED:
taskStatusText = Localize.translateLocal('task.messages.canceled');
break;
case CONST.REPORT.ACTIONS.TYPE.TASKREOPENED:
taskStatusText = Localize.translateLocal('task.messages.reopened');
break;
default:
taskStatusText = Localize.translateLocal('task.task');
}
return `${taskStatusText}`;
}
export {
createTaskAndNavigate,
editTaskAndNavigate,
editTaskAssigneeAndNavigate,
setTitleValue,
setDescriptionValue,
setTaskReport,
setDetailsValue,
setAssigneeValue,
setShareDestinationValue,
clearOutTaskInfo,
reopenTask,
completeTask,
clearOutTaskInfoAndNavigate,
getAssignee,
getShareDestination,
cancelTask,
dismissModalAndClearOutTaskInfo,
getTaskAssigneeAccountID,
clearTaskErrors,
canModifyTask,
getTaskReportActionMessage,
};