emberflow 1.3.74 → 1.3.77
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/lib/index-utils.d.ts +1 -0
- package/lib/index-utils.js +18 -2
- package/lib/index-utils.js.map +1 -1
- package/lib/index.d.ts +3 -3
- package/lib/index.js +209 -46
- package/lib/index.js.map +1 -1
- package/lib/tests/index-utils.test.js +5 -0
- package/lib/tests/index-utils.test.js.map +1 -1
- package/lib/tests/index.test.js +476 -11
- package/lib/tests/index.test.js.map +1 -1
- package/lib/types.d.ts +14 -0
- package/lib/utils/distribution.d.ts +9 -0
- package/lib/utils/distribution.js +65 -60
- package/lib/utils/distribution.js.map +1 -1
- package/package.json +1 -1
package/lib/tests/index.test.js
CHANGED
|
@@ -33,6 +33,8 @@ const paths = __importStar(require("../utils/paths"));
|
|
|
33
33
|
const adminClient = __importStar(require("emberflow-admin-client/lib"));
|
|
34
34
|
var Timestamp = firebase_admin_1.firestore.Timestamp;
|
|
35
35
|
const index_utils_1 = require("../index-utils");
|
|
36
|
+
const distribution = __importStar(require("../utils/distribution"));
|
|
37
|
+
var FieldValue = firebase_admin_1.firestore.FieldValue;
|
|
36
38
|
const projectConfig = {
|
|
37
39
|
projectId: "your-project-id",
|
|
38
40
|
budgetAlertTopicName: "budget-alerts",
|
|
@@ -54,8 +56,9 @@ const getMock = {
|
|
|
54
56
|
data: dataMock,
|
|
55
57
|
};
|
|
56
58
|
const updateMock = jest.fn();
|
|
59
|
+
const setMock = jest.fn();
|
|
57
60
|
const docMock = {
|
|
58
|
-
set:
|
|
61
|
+
set: setMock,
|
|
59
62
|
get: jest.fn().mockResolvedValue(getMock),
|
|
60
63
|
update: updateMock,
|
|
61
64
|
collection: jest.fn(() => collectionMock),
|
|
@@ -63,11 +66,21 @@ const docMock = {
|
|
|
63
66
|
const collectionMock = {
|
|
64
67
|
doc: jest.fn(() => docMock),
|
|
65
68
|
};
|
|
69
|
+
const transactionGetMock = jest.fn();
|
|
70
|
+
const transactionSetMock = jest.fn();
|
|
71
|
+
const transactionUpdateMock = jest.fn();
|
|
72
|
+
const transactionMock = {
|
|
73
|
+
get: transactionGetMock,
|
|
74
|
+
set: transactionSetMock,
|
|
75
|
+
update: transactionUpdateMock,
|
|
76
|
+
delete: jest.fn(),
|
|
77
|
+
};
|
|
66
78
|
jest.spyOn(admin, "firestore")
|
|
67
79
|
.mockImplementation(() => {
|
|
68
80
|
return {
|
|
69
81
|
collection: jest.fn(() => collectionMock),
|
|
70
82
|
doc: jest.fn(() => docMock),
|
|
83
|
+
runTransaction: jest.fn((fn) => fn(transactionMock)),
|
|
71
84
|
};
|
|
72
85
|
});
|
|
73
86
|
jest.spyOn(admin, "database")
|
|
@@ -78,8 +91,9 @@ jest.spyOn(admin, "database")
|
|
|
78
91
|
});
|
|
79
92
|
admin.initializeApp();
|
|
80
93
|
(0, index_1.initializeEmberFlow)(projectConfig, admin, db_structure_1.dbStructure, db_structure_1.Entity, {}, {}, []);
|
|
94
|
+
const refUpdateMock = jest.fn();
|
|
81
95
|
const refMock = {
|
|
82
|
-
update:
|
|
96
|
+
update: refUpdateMock,
|
|
83
97
|
};
|
|
84
98
|
function createDocumentSnapshot(form) {
|
|
85
99
|
return {
|
|
@@ -121,6 +135,41 @@ describe("onFormSubmit", () => {
|
|
|
121
135
|
nestedField2: "oldValue",
|
|
122
136
|
},
|
|
123
137
|
};
|
|
138
|
+
const equation = "totalTodos = notStartedCount + inProgressCount + toVerifyCount + requestChangesCount + doneCount";
|
|
139
|
+
const now = Timestamp.fromDate(new Date());
|
|
140
|
+
const journalEntry = {
|
|
141
|
+
date: now,
|
|
142
|
+
ledgerEntries: [
|
|
143
|
+
{
|
|
144
|
+
account: "totalTodos",
|
|
145
|
+
debit: 1,
|
|
146
|
+
credit: 0,
|
|
147
|
+
},
|
|
148
|
+
{
|
|
149
|
+
account: "notStartedCount",
|
|
150
|
+
debit: 0,
|
|
151
|
+
credit: 1,
|
|
152
|
+
},
|
|
153
|
+
],
|
|
154
|
+
equation: equation,
|
|
155
|
+
};
|
|
156
|
+
const recordedJournalEntry = {
|
|
157
|
+
date: now,
|
|
158
|
+
ledgerEntries: [
|
|
159
|
+
{
|
|
160
|
+
account: "inProgressCount",
|
|
161
|
+
debit: 1,
|
|
162
|
+
credit: 0,
|
|
163
|
+
},
|
|
164
|
+
{
|
|
165
|
+
account: "doneCount",
|
|
166
|
+
debit: 0,
|
|
167
|
+
credit: 1,
|
|
168
|
+
},
|
|
169
|
+
],
|
|
170
|
+
equation: equation,
|
|
171
|
+
recordEntry: true,
|
|
172
|
+
};
|
|
124
173
|
beforeEach(() => {
|
|
125
174
|
jest.spyOn(index_1._mockable, "createNowTimestamp").mockReturnValue(Timestamp.now());
|
|
126
175
|
jest.spyOn(console, "log").mockImplementation();
|
|
@@ -131,6 +180,14 @@ describe("onFormSubmit", () => {
|
|
|
131
180
|
});
|
|
132
181
|
refMock.update.mockReset();
|
|
133
182
|
updateMock.mockReset();
|
|
183
|
+
transactionGetMock.mockResolvedValue({
|
|
184
|
+
data: () => ({
|
|
185
|
+
totalTodos: 3,
|
|
186
|
+
notStartedCount: 1,
|
|
187
|
+
inProgressCount: 1,
|
|
188
|
+
doneCount: 1,
|
|
189
|
+
}),
|
|
190
|
+
});
|
|
134
191
|
});
|
|
135
192
|
afterEach(() => {
|
|
136
193
|
jest.restoreAllMocks();
|
|
@@ -495,7 +552,326 @@ describe("onFormSubmit", () => {
|
|
|
495
552
|
docMock.set.mockReset();
|
|
496
553
|
docMock.update.mockReset();
|
|
497
554
|
});
|
|
555
|
+
it("should write journal entries first", async () => {
|
|
556
|
+
const consoleErrorSpy = jest.spyOn(console, "error").mockImplementation();
|
|
557
|
+
const consoleInfoSpy = jest.spyOn(console, "info").mockImplementation();
|
|
558
|
+
jest.spyOn(indexutils, "runBusinessLogics").mockImplementation(async (actionRef, action, distributeFn) => {
|
|
559
|
+
await distributeFn(actionRef, logicResults, 0);
|
|
560
|
+
return "done";
|
|
561
|
+
});
|
|
562
|
+
let logicResults = [
|
|
563
|
+
{
|
|
564
|
+
name: "logic 1",
|
|
565
|
+
timeFinished: index_1._mockable.createNowTimestamp(),
|
|
566
|
+
status: "finished",
|
|
567
|
+
documents: [
|
|
568
|
+
{ action: "merge", priority: "normal", dstPath: "path1/doc1" },
|
|
569
|
+
],
|
|
570
|
+
},
|
|
571
|
+
];
|
|
572
|
+
const docPath = "users/user-1";
|
|
573
|
+
const form = {
|
|
574
|
+
"formData": JSON.stringify({
|
|
575
|
+
"@actionType": "create",
|
|
576
|
+
"name": "test",
|
|
577
|
+
"@docPath": docPath,
|
|
578
|
+
}),
|
|
579
|
+
"@status": "submit",
|
|
580
|
+
};
|
|
581
|
+
const event = createEvent(form);
|
|
582
|
+
await (0, index_1.onFormSubmit)(event);
|
|
583
|
+
expect(consoleInfoSpy).toHaveBeenCalledWith("No journal entries to write");
|
|
584
|
+
expect(transactionUpdateMock).not.toHaveBeenCalled();
|
|
585
|
+
expect(transactionSetMock).not.toHaveBeenCalled();
|
|
586
|
+
expect(transactionGetMock).not.toHaveBeenCalled();
|
|
587
|
+
logicResults = [
|
|
588
|
+
{
|
|
589
|
+
name: "logic 1",
|
|
590
|
+
timeFinished: index_1._mockable.createNowTimestamp(),
|
|
591
|
+
status: "finished",
|
|
592
|
+
documents: [
|
|
593
|
+
{ action: "merge", priority: "normal", dstPath: "", journalEntries: [journalEntry] },
|
|
594
|
+
],
|
|
595
|
+
},
|
|
596
|
+
];
|
|
597
|
+
await (0, index_1.onFormSubmit)(event);
|
|
598
|
+
expect(consoleErrorSpy).toHaveBeenCalledWith("Dst path has no docId");
|
|
599
|
+
expect(transactionUpdateMock).not.toHaveBeenCalled();
|
|
600
|
+
expect(transactionSetMock).not.toHaveBeenCalled();
|
|
601
|
+
expect(transactionGetMock).not.toHaveBeenCalled();
|
|
602
|
+
logicResults = [
|
|
603
|
+
{
|
|
604
|
+
name: "logic 1",
|
|
605
|
+
timeFinished: index_1._mockable.createNowTimestamp(),
|
|
606
|
+
status: "finished",
|
|
607
|
+
documents: [
|
|
608
|
+
{ action: "merge", priority: "high", dstPath: "path1/doc5", doc: { totalTodos: 1 }, journalEntries: [journalEntry] },
|
|
609
|
+
],
|
|
610
|
+
},
|
|
611
|
+
];
|
|
612
|
+
await (0, index_1.onFormSubmit)(event);
|
|
613
|
+
expect(consoleErrorSpy).toHaveBeenCalledWith("Doc cannot have keys that are the same as account names");
|
|
614
|
+
expect(transactionUpdateMock).not.toHaveBeenCalled();
|
|
615
|
+
expect(transactionSetMock).not.toHaveBeenCalled();
|
|
616
|
+
expect(transactionGetMock).not.toHaveBeenCalled();
|
|
617
|
+
logicResults = [
|
|
618
|
+
{
|
|
619
|
+
name: "logic 1",
|
|
620
|
+
timeFinished: index_1._mockable.createNowTimestamp(),
|
|
621
|
+
status: "finished",
|
|
622
|
+
documents: [
|
|
623
|
+
{ action: "merge", priority: "high", dstPath: "path1/doc5", instructions: { totalTodos: "+1" }, journalEntries: [journalEntry] },
|
|
624
|
+
],
|
|
625
|
+
},
|
|
626
|
+
];
|
|
627
|
+
await (0, index_1.onFormSubmit)(event);
|
|
628
|
+
expect(consoleErrorSpy).toHaveBeenCalledWith("Instructions cannot have keys that are the same as account names");
|
|
629
|
+
expect(transactionUpdateMock).not.toHaveBeenCalled();
|
|
630
|
+
expect(transactionSetMock).not.toHaveBeenCalled();
|
|
631
|
+
expect(transactionGetMock).not.toHaveBeenCalled();
|
|
632
|
+
const unbalancedJournalEntry = {
|
|
633
|
+
date: index_1._mockable.createNowTimestamp(),
|
|
634
|
+
ledgerEntries: [
|
|
635
|
+
{
|
|
636
|
+
account: "totalTodos",
|
|
637
|
+
debit: 1,
|
|
638
|
+
credit: 0,
|
|
639
|
+
},
|
|
640
|
+
{
|
|
641
|
+
account: "notStartedCount",
|
|
642
|
+
debit: 0,
|
|
643
|
+
credit: 1,
|
|
644
|
+
},
|
|
645
|
+
{
|
|
646
|
+
account: "inProgressCount",
|
|
647
|
+
debit: 0,
|
|
648
|
+
credit: 1,
|
|
649
|
+
},
|
|
650
|
+
],
|
|
651
|
+
equation: equation,
|
|
652
|
+
};
|
|
653
|
+
logicResults = [
|
|
654
|
+
{
|
|
655
|
+
name: "logic 1",
|
|
656
|
+
timeFinished: index_1._mockable.createNowTimestamp(),
|
|
657
|
+
status: "finished",
|
|
658
|
+
documents: [
|
|
659
|
+
{ action: "merge", priority: "high", dstPath: "path1/doc5", journalEntries: [unbalancedJournalEntry] },
|
|
660
|
+
],
|
|
661
|
+
},
|
|
662
|
+
];
|
|
663
|
+
await (0, index_1.onFormSubmit)(event);
|
|
664
|
+
expect(consoleErrorSpy).toHaveBeenCalledWith("Debit and credit should be equal");
|
|
665
|
+
expect(transactionUpdateMock).not.toHaveBeenCalled();
|
|
666
|
+
expect(transactionSetMock).not.toHaveBeenCalled();
|
|
667
|
+
expect(transactionGetMock).not.toHaveBeenCalled();
|
|
668
|
+
transactionGetMock.mockRestore();
|
|
669
|
+
transactionSetMock.mockRestore();
|
|
670
|
+
transactionUpdateMock.mockRestore();
|
|
671
|
+
transactionGetMock.mockResolvedValueOnce({
|
|
672
|
+
data: () => undefined,
|
|
673
|
+
});
|
|
674
|
+
const docRef = index_1.db.doc("path1/doc1");
|
|
675
|
+
logicResults = [
|
|
676
|
+
{
|
|
677
|
+
name: "logic 1",
|
|
678
|
+
timeFinished: index_1._mockable.createNowTimestamp(),
|
|
679
|
+
status: "finished",
|
|
680
|
+
documents: [
|
|
681
|
+
{ action: "merge", priority: "normal", dstPath: "path1/doc1", doc: { field: "value" }, journalEntries: [journalEntry] },
|
|
682
|
+
],
|
|
683
|
+
},
|
|
684
|
+
];
|
|
685
|
+
await (0, index_1.onFormSubmit)(event);
|
|
686
|
+
expect(transactionGetMock).toHaveBeenCalledTimes(1);
|
|
687
|
+
expect(transactionGetMock).toHaveBeenCalledWith(docRef);
|
|
688
|
+
expect(transactionSetMock).toHaveBeenCalledTimes(1);
|
|
689
|
+
expect(transactionSetMock).toHaveBeenCalledWith(docRef, { "field": "value", "@forDeletionLater": true });
|
|
690
|
+
expect(transactionUpdateMock).toHaveBeenCalledTimes(2);
|
|
691
|
+
expect(transactionUpdateMock).toHaveBeenNthCalledWith(1, docRef, {
|
|
692
|
+
"totalTodos": 1,
|
|
693
|
+
"@forDeletionLater": FieldValue.delete(),
|
|
694
|
+
});
|
|
695
|
+
expect(transactionUpdateMock).toHaveBeenNthCalledWith(2, docRef, {
|
|
696
|
+
"notStartedCount": 1,
|
|
697
|
+
"@forDeletionLater": FieldValue.delete(),
|
|
698
|
+
});
|
|
699
|
+
transactionGetMock.mockRestore();
|
|
700
|
+
transactionSetMock.mockRestore();
|
|
701
|
+
transactionUpdateMock.mockRestore();
|
|
702
|
+
transactionGetMock.mockResolvedValueOnce({
|
|
703
|
+
data: () => ({
|
|
704
|
+
"totalTodos": 1,
|
|
705
|
+
"notStartedCount": 1,
|
|
706
|
+
}),
|
|
707
|
+
});
|
|
708
|
+
logicResults = [
|
|
709
|
+
{
|
|
710
|
+
name: "logic 1",
|
|
711
|
+
timeFinished: index_1._mockable.createNowTimestamp(),
|
|
712
|
+
status: "finished",
|
|
713
|
+
documents: [
|
|
714
|
+
{ action: "merge", priority: "normal", dstPath: "path1/doc1", doc: { field: "value" }, journalEntries: [journalEntry] },
|
|
715
|
+
],
|
|
716
|
+
},
|
|
717
|
+
];
|
|
718
|
+
await (0, index_1.onFormSubmit)(event);
|
|
719
|
+
expect(transactionGetMock).toHaveBeenCalledTimes(1);
|
|
720
|
+
expect(transactionGetMock).toHaveBeenCalledWith(docRef);
|
|
721
|
+
expect(transactionSetMock).not.toHaveBeenCalled();
|
|
722
|
+
expect(transactionUpdateMock).toHaveBeenCalledTimes(3);
|
|
723
|
+
expect(transactionUpdateMock).toHaveBeenNthCalledWith(1, docRef, { "field": "value", "@forDeletionLater": true });
|
|
724
|
+
expect(transactionUpdateMock).toHaveBeenNthCalledWith(2, docRef, {
|
|
725
|
+
"totalTodos": 2,
|
|
726
|
+
"@forDeletionLater": FieldValue.delete(),
|
|
727
|
+
});
|
|
728
|
+
expect(transactionUpdateMock).toHaveBeenNthCalledWith(3, docRef, {
|
|
729
|
+
"notStartedCount": 2,
|
|
730
|
+
"@forDeletionLater": FieldValue.delete(),
|
|
731
|
+
});
|
|
732
|
+
transactionGetMock.mockRestore();
|
|
733
|
+
transactionSetMock.mockRestore();
|
|
734
|
+
transactionUpdateMock.mockRestore();
|
|
735
|
+
transactionGetMock.mockResolvedValueOnce({
|
|
736
|
+
data: () => ({
|
|
737
|
+
"totalTodos": 2,
|
|
738
|
+
"notStartedCount": 0,
|
|
739
|
+
"inProgressCount": 2,
|
|
740
|
+
}),
|
|
741
|
+
});
|
|
742
|
+
const zeroJournalEntry = {
|
|
743
|
+
date: index_1._mockable.createNowTimestamp(),
|
|
744
|
+
ledgerEntries: [
|
|
745
|
+
{
|
|
746
|
+
account: "notStartedCount",
|
|
747
|
+
debit: 1,
|
|
748
|
+
credit: 1,
|
|
749
|
+
},
|
|
750
|
+
{
|
|
751
|
+
account: "inProgressCount",
|
|
752
|
+
debit: 1,
|
|
753
|
+
credit: 1,
|
|
754
|
+
},
|
|
755
|
+
],
|
|
756
|
+
equation: equation,
|
|
757
|
+
};
|
|
758
|
+
logicResults = [
|
|
759
|
+
{
|
|
760
|
+
name: "logic 1",
|
|
761
|
+
timeFinished: index_1._mockable.createNowTimestamp(),
|
|
762
|
+
status: "finished",
|
|
763
|
+
documents: [
|
|
764
|
+
{ action: "merge", priority: "normal", dstPath: "path1/doc1", journalEntries: [zeroJournalEntry] },
|
|
765
|
+
],
|
|
766
|
+
},
|
|
767
|
+
];
|
|
768
|
+
await (0, index_1.onFormSubmit)(event);
|
|
769
|
+
expect(transactionGetMock).toHaveBeenCalledTimes(1);
|
|
770
|
+
expect(transactionGetMock).toHaveBeenCalledWith(docRef);
|
|
771
|
+
expect(transactionSetMock).not.toHaveBeenCalled();
|
|
772
|
+
expect(transactionUpdateMock).toHaveBeenCalledTimes(3);
|
|
773
|
+
expect(transactionUpdateMock).toHaveBeenNthCalledWith(1, docRef, { "@forDeletionLater": true });
|
|
774
|
+
expect(transactionUpdateMock).toHaveBeenNthCalledWith(2, docRef, { "@forDeletionLater": FieldValue.delete() });
|
|
775
|
+
expect(transactionUpdateMock).toHaveBeenNthCalledWith(3, docRef, { "@forDeletionLater": FieldValue.delete() });
|
|
776
|
+
const errorMock = jest.spyOn(global, "Error")
|
|
777
|
+
.mockImplementation();
|
|
778
|
+
transactionGetMock.mockRestore();
|
|
779
|
+
transactionSetMock.mockRestore();
|
|
780
|
+
transactionUpdateMock.mockRestore();
|
|
781
|
+
transactionGetMock.mockResolvedValueOnce({
|
|
782
|
+
data: () => ({
|
|
783
|
+
"totalTodos": 2,
|
|
784
|
+
"notStartedCount": 0,
|
|
785
|
+
"inProgressCount": 2,
|
|
786
|
+
}),
|
|
787
|
+
});
|
|
788
|
+
const changeStatusJournalEntry = {
|
|
789
|
+
date: index_1._mockable.createNowTimestamp(),
|
|
790
|
+
ledgerEntries: [
|
|
791
|
+
{
|
|
792
|
+
account: "notStartedCount",
|
|
793
|
+
debit: 1,
|
|
794
|
+
credit: 0,
|
|
795
|
+
},
|
|
796
|
+
{
|
|
797
|
+
account: "inProgressCount",
|
|
798
|
+
debit: 0,
|
|
799
|
+
credit: 1,
|
|
800
|
+
},
|
|
801
|
+
],
|
|
802
|
+
equation: equation,
|
|
803
|
+
};
|
|
804
|
+
logicResults = [
|
|
805
|
+
{
|
|
806
|
+
name: "logic 1",
|
|
807
|
+
timeFinished: index_1._mockable.createNowTimestamp(),
|
|
808
|
+
status: "finished",
|
|
809
|
+
documents: [
|
|
810
|
+
{ action: "merge", priority: "normal", dstPath: "path1/doc1", journalEntries: [changeStatusJournalEntry] },
|
|
811
|
+
],
|
|
812
|
+
},
|
|
813
|
+
];
|
|
814
|
+
await (0, index_1.onFormSubmit)(event);
|
|
815
|
+
expect(transactionGetMock).toHaveBeenCalledTimes(1);
|
|
816
|
+
expect(transactionGetMock).toHaveBeenCalledWith(docRef);
|
|
817
|
+
expect(transactionSetMock).not.toHaveBeenCalled();
|
|
818
|
+
expect(transactionUpdateMock).toHaveBeenCalledTimes(1);
|
|
819
|
+
expect(transactionUpdateMock).toHaveBeenCalledWith(docRef, { "@forDeletionLater": true });
|
|
820
|
+
expect(errorMock).toHaveBeenCalledTimes(1);
|
|
821
|
+
expect(errorMock).toHaveBeenCalledWith("Account value cannot be negative");
|
|
822
|
+
transactionGetMock.mockRestore();
|
|
823
|
+
transactionSetMock.mockRestore();
|
|
824
|
+
transactionUpdateMock.mockRestore();
|
|
825
|
+
transactionGetMock.mockResolvedValueOnce({
|
|
826
|
+
data: () => ({
|
|
827
|
+
"totalTodos": 2,
|
|
828
|
+
"notStartedCount": 1,
|
|
829
|
+
"inProgressCount": 1,
|
|
830
|
+
"doneCount": 0,
|
|
831
|
+
}),
|
|
832
|
+
});
|
|
833
|
+
logicResults = [
|
|
834
|
+
{
|
|
835
|
+
name: "logic 1",
|
|
836
|
+
timeFinished: index_1._mockable.createNowTimestamp(),
|
|
837
|
+
status: "finished",
|
|
838
|
+
documents: [
|
|
839
|
+
{ action: "merge", priority: "normal", dstPath: "path1/doc1", journalEntries: [recordedJournalEntry] },
|
|
840
|
+
],
|
|
841
|
+
},
|
|
842
|
+
];
|
|
843
|
+
await (0, index_1.onFormSubmit)(event);
|
|
844
|
+
expect(transactionGetMock).toHaveBeenCalledTimes(1);
|
|
845
|
+
expect(transactionGetMock).toHaveBeenCalledWith(docRef);
|
|
846
|
+
expect(transactionUpdateMock).toHaveBeenCalledTimes(3);
|
|
847
|
+
expect(transactionUpdateMock).toHaveBeenNthCalledWith(1, docRef, { "@forDeletionLater": true });
|
|
848
|
+
expect(transactionUpdateMock).toHaveBeenNthCalledWith(2, docRef, {
|
|
849
|
+
"inProgressCount": 0,
|
|
850
|
+
"@forDeletionLater": FieldValue.delete(),
|
|
851
|
+
});
|
|
852
|
+
expect(transactionUpdateMock).toHaveBeenNthCalledWith(3, docRef, {
|
|
853
|
+
"doneCount": 1,
|
|
854
|
+
"@forDeletionLater": FieldValue.delete(),
|
|
855
|
+
});
|
|
856
|
+
expect(transactionSetMock).toHaveBeenCalledTimes(2);
|
|
857
|
+
expect(transactionSetMock).toHaveBeenNthCalledWith(1, index_1.db.doc("path/doc1/@ledgers/doc100"), {
|
|
858
|
+
"journalEntryId": "doc10",
|
|
859
|
+
"account": "inProgressCount",
|
|
860
|
+
"debit": 1,
|
|
861
|
+
"credit": 0,
|
|
862
|
+
});
|
|
863
|
+
expect(transactionSetMock).toHaveBeenNthCalledWith(2, index_1.db.doc("path/doc1/@ledgers/doc101"), {
|
|
864
|
+
"journalEntryId": "doc10",
|
|
865
|
+
"account": "doneCount",
|
|
866
|
+
"debit": 0,
|
|
867
|
+
"credit": 1,
|
|
868
|
+
});
|
|
869
|
+
});
|
|
498
870
|
it("should execute the sequence of operations correctly", async () => {
|
|
871
|
+
refUpdateMock.mockRestore();
|
|
872
|
+
setMock.mockRestore();
|
|
873
|
+
transactionSetMock.mockRestore();
|
|
874
|
+
transactionUpdateMock.mockRestore();
|
|
499
875
|
jest.spyOn(indexutils, "validateForm").mockResolvedValue([false, {}]);
|
|
500
876
|
jest.spyOn(indexutils, "getFormModifiedFields").mockReturnValue({ "field1": "value1", "field2": "value2" });
|
|
501
877
|
jest.spyOn(indexutils, "getSecurityFn").mockReturnValue(() => Promise.resolve({ status: "allowed" }));
|
|
@@ -503,6 +879,12 @@ describe("onFormSubmit", () => {
|
|
|
503
879
|
jest.spyOn(indexutils, "distribute").mockResolvedValue();
|
|
504
880
|
jest.spyOn(indexutils, "distributeLater").mockResolvedValue();
|
|
505
881
|
jest.spyOn(viewLogics, "queueRunViewLogics").mockResolvedValue();
|
|
882
|
+
jest.spyOn(distribution, "convertInstructionsToDbValues").mockReturnValue({
|
|
883
|
+
updateData: {
|
|
884
|
+
transactions: FieldValue.increment(-1),
|
|
885
|
+
},
|
|
886
|
+
removeData: {},
|
|
887
|
+
});
|
|
506
888
|
const highPriorityDocs = [
|
|
507
889
|
{
|
|
508
890
|
action: "merge",
|
|
@@ -561,7 +943,7 @@ describe("onFormSubmit", () => {
|
|
|
561
943
|
priority: "low",
|
|
562
944
|
},
|
|
563
945
|
];
|
|
564
|
-
const
|
|
946
|
+
const additionalNormalPriorityDocs = [
|
|
565
947
|
{
|
|
566
948
|
action: "merge",
|
|
567
949
|
dstPath: "users/user-1/activities/activity-1",
|
|
@@ -571,6 +953,66 @@ describe("onFormSubmit", () => {
|
|
|
571
953
|
priority: "normal",
|
|
572
954
|
},
|
|
573
955
|
];
|
|
956
|
+
const transactionalDocs = [
|
|
957
|
+
{
|
|
958
|
+
action: "create",
|
|
959
|
+
dstPath: "transactions/transaction-1",
|
|
960
|
+
doc: {
|
|
961
|
+
title: "Transaction doc for user 1",
|
|
962
|
+
},
|
|
963
|
+
priority: "high",
|
|
964
|
+
},
|
|
965
|
+
{
|
|
966
|
+
action: "merge",
|
|
967
|
+
dstPath: "users/user-1",
|
|
968
|
+
doc: {
|
|
969
|
+
lastActivity: index_1._mockable.createNowTimestamp(),
|
|
970
|
+
},
|
|
971
|
+
instructions: {
|
|
972
|
+
transactions: "--",
|
|
973
|
+
},
|
|
974
|
+
priority: "normal",
|
|
975
|
+
},
|
|
976
|
+
{
|
|
977
|
+
action: "delete",
|
|
978
|
+
dstPath: "messages/message-1",
|
|
979
|
+
doc: {
|
|
980
|
+
title: "Message doc for transaction 1",
|
|
981
|
+
},
|
|
982
|
+
priority: "low",
|
|
983
|
+
},
|
|
984
|
+
];
|
|
985
|
+
const recordedJournalEntry = {
|
|
986
|
+
date: index_1._mockable.createNowTimestamp(),
|
|
987
|
+
ledgerEntries: [
|
|
988
|
+
{
|
|
989
|
+
account: "inProgressCount",
|
|
990
|
+
debit: 1,
|
|
991
|
+
credit: 0,
|
|
992
|
+
},
|
|
993
|
+
{
|
|
994
|
+
account: "doneCount",
|
|
995
|
+
debit: 0,
|
|
996
|
+
credit: 1,
|
|
997
|
+
},
|
|
998
|
+
],
|
|
999
|
+
equation: equation,
|
|
1000
|
+
recordEntry: true,
|
|
1001
|
+
};
|
|
1002
|
+
const journalDocs = [
|
|
1003
|
+
{
|
|
1004
|
+
action: "merge",
|
|
1005
|
+
priority: "normal",
|
|
1006
|
+
dstPath: "journal/doc1",
|
|
1007
|
+
journalEntries: [recordedJournalEntry, journalEntry],
|
|
1008
|
+
},
|
|
1009
|
+
{
|
|
1010
|
+
action: "merge",
|
|
1011
|
+
priority: "normal",
|
|
1012
|
+
dstPath: "journal/doc2",
|
|
1013
|
+
journalEntries: [journalEntry],
|
|
1014
|
+
},
|
|
1015
|
+
];
|
|
574
1016
|
const logicResults = [
|
|
575
1017
|
{
|
|
576
1018
|
name: "testLogic",
|
|
@@ -578,10 +1020,22 @@ describe("onFormSubmit", () => {
|
|
|
578
1020
|
timeFinished: index_1._mockable.createNowTimestamp(),
|
|
579
1021
|
documents: [...highPriorityDocs, ...normalPriorityDocs, ...lowPriorityDocs],
|
|
580
1022
|
}, {
|
|
581
|
-
name: "
|
|
1023
|
+
name: "additionalLogic",
|
|
1024
|
+
status: "finished",
|
|
1025
|
+
timeFinished: index_1._mockable.createNowTimestamp(),
|
|
1026
|
+
documents: [...additionalNormalPriorityDocs],
|
|
1027
|
+
}, {
|
|
1028
|
+
name: "transactionalLogic",
|
|
1029
|
+
status: "finished",
|
|
1030
|
+
timeFinished: index_1._mockable.createNowTimestamp(),
|
|
1031
|
+
documents: [...transactionalDocs],
|
|
1032
|
+
transactional: true,
|
|
1033
|
+
}, {
|
|
1034
|
+
name: "journalLogic",
|
|
582
1035
|
status: "finished",
|
|
583
1036
|
timeFinished: index_1._mockable.createNowTimestamp(),
|
|
584
|
-
documents: [...
|
|
1037
|
+
documents: [...journalDocs],
|
|
1038
|
+
transactional: true,
|
|
585
1039
|
},
|
|
586
1040
|
];
|
|
587
1041
|
const runBusinessLogicsSpy = jest.spyOn(indexutils, "runBusinessLogics").mockImplementation(async (actionRef, action, distributeFn) => {
|
|
@@ -600,10 +1054,12 @@ describe("onFormSubmit", () => {
|
|
|
600
1054
|
const event = createEvent(form);
|
|
601
1055
|
const highPriorityDstPathLogicDocsMap = await (0, index_utils_1.expandConsolidateAndGroupByDstPath)(highPriorityDocs);
|
|
602
1056
|
const { docsByDocPath: highPriorityDocsByDocPath, otherDocsByDocPath: highPriorityOtherDocsByDocPath, } = (0, index_utils_1.groupDocsByTargetDocPath)(highPriorityDstPathLogicDocsMap, docPath);
|
|
603
|
-
const normalPriorityDstPathLogicDocsMap = await (0, index_utils_1.expandConsolidateAndGroupByDstPath)([...normalPriorityDocs, ...
|
|
1057
|
+
const normalPriorityDstPathLogicDocsMap = await (0, index_utils_1.expandConsolidateAndGroupByDstPath)([...normalPriorityDocs, ...additionalNormalPriorityDocs]);
|
|
604
1058
|
const { docsByDocPath: normalPriorityDocsByDocPath, otherDocsByDocPath: normalPriorityOtherDocsByDocPath, } = (0, index_utils_1.groupDocsByTargetDocPath)(normalPriorityDstPathLogicDocsMap, docPath);
|
|
605
1059
|
const lowPriorityDstPathLogicDocsMap = await (0, index_utils_1.expandConsolidateAndGroupByDstPath)(lowPriorityDocs);
|
|
606
1060
|
const { docsByDocPath: lowPriorityDocsByDocPath, otherDocsByDocPath: lowPriorityOtherDocsByDocPath, } = (0, index_utils_1.groupDocsByTargetDocPath)(lowPriorityDstPathLogicDocsMap, docPath);
|
|
1061
|
+
const transactionalDstPathLogicDocsMap = await (0, index_utils_1.expandConsolidateAndGroupByDstPath)(transactionalDocs);
|
|
1062
|
+
const journalDstPathLogicDocsMap = await (0, index_utils_1.expandConsolidateAndGroupByDstPath)(journalDocs);
|
|
607
1063
|
const viewLogicResults = [{
|
|
608
1064
|
name: "User ViewLogic",
|
|
609
1065
|
status: "finished",
|
|
@@ -611,6 +1067,8 @@ describe("onFormSubmit", () => {
|
|
|
611
1067
|
documents: logicResults.map((result) => result.documents).flat(),
|
|
612
1068
|
}];
|
|
613
1069
|
jest.spyOn(indexutils, "expandConsolidateAndGroupByDstPath")
|
|
1070
|
+
.mockResolvedValueOnce(journalDstPathLogicDocsMap)
|
|
1071
|
+
.mockResolvedValueOnce(transactionalDstPathLogicDocsMap)
|
|
614
1072
|
.mockResolvedValueOnce(highPriorityDstPathLogicDocsMap)
|
|
615
1073
|
.mockResolvedValueOnce(normalPriorityDstPathLogicDocsMap)
|
|
616
1074
|
.mockResolvedValueOnce(lowPriorityDstPathLogicDocsMap);
|
|
@@ -633,25 +1091,32 @@ describe("onFormSubmit", () => {
|
|
|
633
1091
|
await (0, index_1.onFormSubmit)(event);
|
|
634
1092
|
// Test that the runBusinessLogics function was called with the correct parameters
|
|
635
1093
|
expect(runBusinessLogicsSpy).toHaveBeenCalled();
|
|
636
|
-
expect(
|
|
1094
|
+
expect(setMock).toHaveBeenCalledTimes(18);
|
|
637
1095
|
expect(refMock.update).toHaveBeenCalledTimes(3);
|
|
638
1096
|
expect(refMock.update).toHaveBeenNthCalledWith(1, { "@status": "processing" });
|
|
639
1097
|
expect(refMock.update).toHaveBeenNthCalledWith(2, { "@status": "submitted" });
|
|
640
1098
|
expect(refMock.update).toHaveBeenNthCalledWith(3, { "@status": "finished" });
|
|
641
1099
|
// Test that the functions are called in the correct sequence
|
|
642
|
-
expect(indexutils.expandConsolidateAndGroupByDstPath).toHaveBeenNthCalledWith(1,
|
|
1100
|
+
expect(indexutils.expandConsolidateAndGroupByDstPath).toHaveBeenNthCalledWith(1, journalDocs);
|
|
1101
|
+
console.debug("set", transactionSetMock.mock.calls);
|
|
1102
|
+
console.debug("update", transactionUpdateMock.mock.calls);
|
|
1103
|
+
expect(transactionMock.set).toHaveBeenCalledTimes(3);
|
|
1104
|
+
expect(transactionMock.update).toHaveBeenCalledTimes(11);
|
|
1105
|
+
expect(indexutils.expandConsolidateAndGroupByDstPath).toHaveBeenNthCalledWith(2, transactionalDocs);
|
|
1106
|
+
expect(transactionMock.delete).toHaveBeenCalledTimes(1);
|
|
1107
|
+
expect(indexutils.expandConsolidateAndGroupByDstPath).toHaveBeenNthCalledWith(3, highPriorityDocs);
|
|
643
1108
|
expect(indexutils.groupDocsByTargetDocPath).toHaveBeenNthCalledWith(1, highPriorityDstPathLogicDocsMap, docPath);
|
|
644
1109
|
expect(indexutils.distribute).toHaveBeenNthCalledWith(1, highPriorityDocsByDocPath);
|
|
645
1110
|
expect(indexutils.distribute).toHaveBeenNthCalledWith(2, highPriorityOtherDocsByDocPath);
|
|
646
|
-
expect(indexutils.expandConsolidateAndGroupByDstPath).toHaveBeenNthCalledWith(
|
|
1111
|
+
expect(indexutils.expandConsolidateAndGroupByDstPath).toHaveBeenNthCalledWith(4, [...normalPriorityDocs, ...additionalNormalPriorityDocs]);
|
|
647
1112
|
expect(indexutils.groupDocsByTargetDocPath).toHaveBeenNthCalledWith(2, normalPriorityDstPathLogicDocsMap, docPath);
|
|
648
1113
|
expect(indexutils.distribute).toHaveBeenNthCalledWith(3, normalPriorityDocsByDocPath);
|
|
649
1114
|
expect(indexutils.distributeLater).toHaveBeenNthCalledWith(1, normalPriorityOtherDocsByDocPath);
|
|
650
|
-
expect(indexutils.expandConsolidateAndGroupByDstPath).toHaveBeenNthCalledWith(
|
|
1115
|
+
expect(indexutils.expandConsolidateAndGroupByDstPath).toHaveBeenNthCalledWith(5, lowPriorityDocs);
|
|
651
1116
|
expect(indexutils.groupDocsByTargetDocPath).toHaveBeenNthCalledWith(3, lowPriorityDstPathLogicDocsMap, docPath);
|
|
652
1117
|
expect(indexutils.distributeLater).toHaveBeenNthCalledWith(2, lowPriorityDocsByDocPath);
|
|
653
1118
|
expect(indexutils.distributeLater).toHaveBeenNthCalledWith(3, lowPriorityOtherDocsByDocPath);
|
|
654
|
-
expect(indexutils.expandConsolidateAndGroupByDstPath).toHaveBeenCalledTimes(
|
|
1119
|
+
expect(indexutils.expandConsolidateAndGroupByDstPath).toHaveBeenCalledTimes(5);
|
|
655
1120
|
expect(updateMock).toHaveBeenCalledTimes(1);
|
|
656
1121
|
expect(updateMock.mock.calls[0][0]).toEqual({ status: "finished" });
|
|
657
1122
|
});
|