-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathMongoEventRequestHistoryReadModelComponent.scala
1356 lines (1154 loc) · 56.9 KB
/
MongoEventRequestHistoryReadModelComponent.scala
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
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
package com.jumpshot.patternmanagement.scalamongo
import java.time.{Instant, LocalDate}
import java.util.UUID
import org.bson.codecs.Codec // do not remove, Macros depend on it
import com.jumpshot.mongocommon.scalamongo.PolymorphicCodecProvider
import com.jumpshot.patternmanagement.EventRepositoryBindingKind
import com.jumpshot.patternmanagement.event.EventRequestBlockTransferred.{PatternTransfer, UrlTransfer}
import com.jumpshot.patternmanagement.event.GenericHandleableEvent
import com.jumpshot.patternmanagement.execution.ExecutionContextProvider
import com.jumpshot.patternmanagement.model._
import com.jumpshot.patternmanagement.event._
import com.jumpshot.patternmanagement.model.EventRequestHistoryReadModel.EventHistoryItems._
import com.typesafe.scalalogging.StrictLogging
import org.bson.codecs.configuration.CodecRegistries.{fromProviders, fromRegistries}
import org.mongodb.scala.MongoCollection
import org.mongodb.scala.bson.codecs.Macros
import org.mongodb.scala.bson.conversions.Bson
import org.mongodb.scala.model.Sorts._
import org.mongodb.scala.model.Filters._
import org.mongodb.scala.model.Filters.equal
import scala.async.Async._
import scala.concurrent.Future
import scala.reflect.ClassTag
trait MongoEventRequestHistoryReadModelComponent extends EventRequestHistoryReadModelComponent
with ExecutionContextProvider.Implicit {
this: ScalaMongoDbProvider
with StrictLogging
with ExecutionContextProvider =>
override def eventHistoryReadModel: EventRequestHistoryReadModel = new MongoEventRequestHistoryReadModel
class MongoEventRequestHistoryReadModel extends EventRequestHistoryReadModel {
import MongoEventRequestHistoryReadModel._
import EventUpgradeImplicits._
import implicits._
override def handle: PartialFunction[GenericHandleableEvent, Future[Unit]] = {
case HandleableEvent(evt: EventRequestAdded) => handleEventRequestAdded(evt)
case HandleableEvent(evt: EventRequestAddedV2) => handleEventRequestAdded(evt)
case HandleableEvent(evt: EventRequestEnabled) => handleEventRequestEnabled(evt)
case HandleableEvent(evt: EventRequestDisabled) => handleEventRequestDisabled(evt)
case HandleableEvent(evt: EventRequestTypeUpdated) => handleEventRequestTypeUpdated(evt)
case HandleableEvent(evt: EventRequestExistenceUpdated) => handleEventRequestExistenceUpdated(evt)
case HandleableEvent(evt: EventRequestExistenceUpdatedV2) => handleEventRequestExistenceUpdated(evt)
case HandleableEvent(evt: EventRequestBlockAdded) => handleEventRequestBlockAdded(evt)
case HandleableEvent(evt: EventRequestBlockAddedV2) => handleEventRequestBlockAdded(evt)
case HandleableEvent(evt: EventRequestBlockRemoved) => handleEventRequestBlockRemoved(evt)
case HandleableEvent(evt: EventRequestBlockNoteSet) => handleEventRequestBlockNoteSet(evt)
case HandleableEvent(evt: EventRequestBlockAnalystNoteSet) => handleEventRequestBlockAnalystNoteSet(evt)
case HandleableEvent(evt: EventRequestBlockValidatorNoteSet) => handleEventRequestBlockValidatorNoteSet(evt)
case HandleableEvent(evt: EventRequestBlockTransferred) => handleEventRequestBlockTransferred(evt)
case HandleableEvent(evt: EventRequestBlockTitleSet) => handleEventRequestBlockTitleSet(evt)
case HandleableEvent(evt: EventRequestBlockLabelsSet) => handleEventRequestBlockLabelsSet(evt)
case HandleableEvent(evt: EventRequestBlockRangeSet) => handleEventRequestBlockRangeSet(evt)
case HandleableEvent(evt: EventRequestGlobalUrlAdded) => handleEventRequestGlobalUrlAdded(evt)
case HandleableEvent(evt: EventRequestGlobalUrlValueSet) => handleEventRequestGlobalUrlValueSet(evt)
case HandleableEvent(evt: EventRequestGlobalUrlRemoved) => handleEventRequestGlobalUrlRemoved(evt)
case HandleableEvent(evt: EventRequestGlobalUrlNoteSet) => handleEventRequestGlobalUrlNoteSet(evt)
case HandleableEvent(evt: EventRequestUrlAdded) => handleEventRequestBlockUrlAdded(evt)
case HandleableEvent(evt: EventRequestUrlAddedV2) => handleEventRequestBlockUrlAdded(evt)
case HandleableEvent(evt: EventRequestUrlAddedV3) => handleEventRequestBlockUrlAdded(evt)
case HandleableEvent(evt: EventRequestUrlValueSet) => handleEventRequestBlockUrlValueSet(evt)
case HandleableEvent(evt: EventRequestUrlRemoved) => handleEventRequestBlockUrlRemoved(evt)
case HandleableEvent(evt: EventRequestUrlNoteSet) => handleEventRequestUrlNoteSet(evt)
case HandleableEvent(evt: EventRequestUrlMoved) => handleEventRequestUrlMoved(evt)
case HandleableEvent(evt: EventRequestNoteUpdated) => handleEventRequestNoteUpdated(evt)
case HandleableEvent(evt: EventRequestNoteUpdatedV2) => handleEventRequestNoteUpdated(evt)
case HandleableEvent(evt: EventRequestSubtypeUpdated) => handleEventRequestSubtypeUpdated(evt)
case HandleableEvent(evt: EventRequestPatternAdded) => handleEventRequestPatternAdded(evt)
case HandleableEvent(evt: EventRequestPatternUpdated) => handleEventRequestPatternUpdated(evt)
case HandleableEvent(evt: EventRequestPatternRemoved) => handleEventRequestPatternRemoved(evt)
case HandleableEvent(evt: EventRequestAndRepositoryEventBound) => handleEventRequestRepositoryEventBound(evt)
case HandleableEvent(evt: EventRequestAndRepositoryEventBoundV2) => handleEventRequestRepositoryEventBound(evt)
case HandleableEvent(evt: EventRequestAndRepositoryEventBoundV3) => handleEventRequestRepositoryEventBound(evt)
case HandleableEvent(evt: EventRequestAndRepositoryPatternBound) => handleEventRequestRepositoryPatternBound(evt)
case HandleableEvent(evt: EventRequestPatternMoved) => handleEventRequestPatternMoved(evt)
}
private def handleEventRequestAdded(evt: EventRequestAdded): Future[Unit] = {
val request = BsonEventRequestAdded(
evt.requestId,
evt.domain,
evt.doneBy,
evt.doneOn.toInstant,
evt.eventTypeId,
evt.eventSubtypeId
)
eventRequestHistoryCollection flatMap (_.insertOne(request))
}
private def handleEventRequestEnabled(evt: EventRequestEnabled): Future[Unit] = {
val request = BsonEventRequestEnabled(
evt.requestId,
evt.doneBy,
evt.doneOn.toInstant,
)
eventRequestHistoryCollection flatMap (_.insertOne(request))
}
private def handleEventRequestDisabled(evt: EventRequestDisabled): Future[Unit] = {
val request = BsonEventRequestDisabled(
evt.requestId,
Option(evt.message),
evt.doneBy,
evt.doneOn.toInstant,
)
eventRequestHistoryCollection flatMap (_.insertOne(request))
}
private def handleEventRequestTypeUpdated(evt: EventRequestTypeUpdated): Future[Unit] = async {
val maybePreviousEvent = await (findPreviousOperation[BsonEventRequestTypeUpdated](Selectors.byRequestId(evt.requestId)))
val maybeAddEvent = await (findPreviousOperation[BsonEventRequestAdded](Selectors.byRequestId(evt.requestId)))
val request = BsonEventRequestTypeUpdated(
evt.requestId,
maybeAddEvent.flatMap(_.source),
maybePreviousEvent.map(_.to),
evt.eventType,
evt.doneBy,
evt.doneOn.toInstant,
)
await (eventRequestHistoryCollection flatMap (_.insertOne(request)))
}
private def handleEventRequestExistenceUpdated(evt: EventRequestExistenceUpdatedV2): Future[Unit] = async {
val maybePreviousEvent = await (findPreviousOperation[BsonEventRequestExistenceUpdated](Selectors.byRequestIdAndSource(evt.requestId, evt.source)))
val operation = BsonEventRequestExistenceUpdated(
evt.requestId,
Some(evt.source),
maybePreviousEvent.map(_.to),
evt.existence,
evt.doneBy,
evt.doneOn.toInstant)
await (eventRequestHistoryCollection flatMap (_.insertOne(operation)))
}
private def handleEventRequestBlockAdded(evt: EventRequestBlockAddedV2): Future[Unit] = {
val operation = BsonEventRequestBlockAdded(
evt.requestId,
evt.id,
Some(evt.blockSource),
evt.blockType,
evt.doneBy,
evt.doneOn.toInstant)
eventRequestHistoryCollection flatMap (_.insertOne(operation))
}
private def handleEventRequestBlockRemoved(evt: EventRequestBlockRemoved): Future[Unit] = async {
val blockInfo = await(getBlockInfo(evt.id))
val operation = BsonEventRequestBlockRemoved(
evt.requestId,
blockInfo.source,
blockInfo.blockType,
blockInfo.blockTitle,
evt.doneBy,
evt.doneOn.toInstant)
await (eventRequestHistoryCollection flatMap (_.insertOne(operation)))
}
private def handleEventRequestBlockNoteSet(evt: EventRequestBlockNoteSet): Future[Unit] = async {
val maybePreviousEvent = await (findPreviousOperation[BsonEventRequestBlockValidatorNoteSet](Selectors.byBlockId(evt.blockId)))
val blockInfo = await(getBlockInfo(evt.blockId))
val operation = BsonEventRequestBlockNoteSet(
evt.requestId,
Some(evt.blockId),
blockInfo.blockType,
blockInfo.source,
blockInfo.blockTitle,
maybePreviousEvent.map(_.to),
evt.note,
evt.doneBy,
evt.doneOn.toInstant)
await (eventRequestHistoryCollection flatMap (_.insertOne(operation)))
}
private def handleEventRequestBlockAnalystNoteSet(evt: EventRequestBlockAnalystNoteSet): Future[Unit] = async {
val maybePreviousEvent = await (findPreviousOperation[BsonEventRequestBlockAnalystNoteSet](Selectors.byBlockId(evt.blockId)))
val blockInfo = await(getBlockInfo(evt.blockId))
val operation = BsonEventRequestBlockAnalystNoteSet(
evt.requestId,
blockInfo.source,
Some(evt.blockId),
blockInfo.blockType,
blockInfo.blockTitle,
maybePreviousEvent.map(_.to),
evt.note,
evt.doneBy,
evt.doneOn.toInstant)
await (eventRequestHistoryCollection flatMap (_.insertOne(operation)))
}
private def handleEventRequestBlockValidatorNoteSet(evt: EventRequestBlockValidatorNoteSet): Future[Unit] = async {
val maybePreviousEvent = await (findPreviousOperation[BsonEventRequestBlockValidatorNoteSet](Selectors.byBlockId(evt.blockId)))
val blockInfo = await(getBlockInfo(evt.blockId))
val operation = BsonEventRequestBlockValidatorNoteSet(
evt.requestId,
Some(evt.blockId),
blockInfo.source,
blockInfo.blockType,
blockInfo.blockTitle,
maybePreviousEvent.map(_.to),
evt.note,
evt.doneBy,
evt.doneOn.toInstant)
await (eventRequestHistoryCollection flatMap (_.insertOne(operation)))
}
private def handleEventRequestBlockTransferred(evt: EventRequestBlockTransferred): Future[Unit] = async {
val blockInfo = await(getBlockInfo(evt.payload.originalId))
val urls = evt.payload.urls.map(_.id)
val patterns = evt.payload.patterns.map(_.id)
val operation = BsonEventRequestBlockTransferred(
evt.requestId,
evt.payload.id,
blockInfo.source,
evt.payload.blockType,
blockInfo.blockTitle,
urls,
evt.payload.urls,
patterns,
evt.payload.patterns,
blockInfo.source,
evt.payload.blockSource,
evt.doneBy,
evt.doneOn.toInstant)
await (eventRequestHistoryCollection flatMap (_.insertOne(operation)))
}
private def handleEventRequestBlockTitleSet(evt: EventRequestBlockTitleSet): Future[Unit] = async {
val blockInfo = await(getBlockInfo(evt.blockId))
val operation = BsonEventRequestBlockTitleSet(
evt.requestId,
evt.blockId,
blockInfo.source,
blockInfo.blockType,
blockInfo.blockTitle,
evt.title,
evt.doneBy,
evt.doneOn.toInstant)
await (eventRequestHistoryCollection flatMap (_.insertOne(operation)))
}
private def handleEventRequestBlockLabelsSet(evt: EventRequestBlockLabelsSet): Future[Unit] = async {
val maybePreviousEvent = await (findPreviousOperation[BsonEventRequestBlockLabelsSet](Selectors.byBlockId(evt.blockId)))
val blockInfo = await(getBlockInfo(evt.blockId))
val operation = BsonEventRequestBlockLabelsSet(
evt.requestId,
blockInfo.source,
evt.blockId,
blockInfo.blockType,
blockInfo.blockTitle,
maybePreviousEvent.map(_.to),
evt.labels,
evt.doneBy,
evt.doneOn.toInstant)
await (eventRequestHistoryCollection flatMap (_.insertOne(operation)))
}
private def handleEventRequestBlockRangeSet(evt: EventRequestBlockRangeSet): Future[Unit] = async {
val maybePreviousEvent = await (findPreviousOperation[BsonEventRequestBlockRangeSet](Selectors.byBlockId(evt.blockId)))
val blockInfo = await(getBlockInfo(evt.blockId))
val operation = BsonEventRequestBlockRangeSet(
evt.requestId,
blockInfo.source,
evt.blockId,
blockInfo.blockType,
blockInfo.blockTitle,
maybePreviousEvent.flatMap(_.toFrom),
maybePreviousEvent.flatMap(_.toTo),
evt.from,
evt.to,
evt.doneBy,
evt.doneOn.toInstant)
await (eventRequestHistoryCollection flatMap (_.insertOne(operation)))
}
private def handleEventRequestGlobalUrlAdded(evt: EventRequestGlobalUrlAdded): Future[Unit] = {
val operation = BsonEventRequestGlobalUrlAdded(
evt.requestId,
evt.id,
source = None,
evt.urlType,
evt.browser,
evt.url,
evt.doneBy,
evt.doneOn.toInstant)
eventRequestHistoryCollection flatMap (_.insertOne(operation))
}
private def handleEventRequestGlobalUrlRemoved(evt: EventRequestGlobalUrlRemoved): Future[Unit] = async {
val maybePreviousUpdateEvent = await (findPreviousOperation[BsonEventRequestUrlValueSet](Selectors.byRequestId(evt.requestId)))
val maybePreviousAddEvent = await (findPreviousOperation[BsonEventRequestGlobalUrlAdded](Selectors.byRequestId(evt.requestId)))
val operation = BsonEventRequestUrlRemoved(
evt.requestId,
evt.id,
maybePreviousUpdateEvent.flatMap(_.source).orElse(maybePreviousAddEvent.flatMap(_.source)),
blockId = None,
blockType = None,
blockTitle = None,
maybePreviousUpdateEvent.flatMap(_.urlType),
maybePreviousUpdateEvent.flatMap(_.browser),
maybePreviousUpdateEvent.map(_.to),
evt.doneBy,
evt.doneOn.toInstant)
await (eventRequestHistoryCollection flatMap (_.insertOne(operation)))
}
private def handleEventRequestGlobalUrlNoteSet(evt: EventRequestGlobalUrlNoteSet): Future[Unit] = async {
val maybePreviousEvent = await (findPreviousOperation[BsonEventRequestUrlNoteSet](Selectors.byUrlId(evt.id)))
val maybeAddUrlEvent = await (findPreviousOperation[BsonEventRequestGlobalUrlAdded](Selectors.byUrlId(evt.id)))
val operation = BsonEventRequestUrlNoteSet(
evt.requestId,
evt.id,
maybeAddUrlEvent.flatMap(_.source),
blockId = None,
blockType = None,
blockTitle = None,
maybePreviousEvent.flatMap(_.urlType),
maybePreviousEvent.flatMap(_.browser),
maybePreviousEvent.map(_.to),
evt.note,
evt.doneBy,
evt.doneOn.toInstant)
await (eventRequestHistoryCollection flatMap (_.insertOne(operation)))
}
private def handleEventRequestGlobalUrlValueSet(evt: EventRequestGlobalUrlValueSet): Future[Unit] = async {
val maybePreviousEvent = await (findPreviousOperation[BsonEventRequestGlobalUrlValueSet](Selectors.byUrlId(evt.id)))
val maybePreviousAddEvent = await (findPreviousOperation[BsonEventRequestGlobalUrlAdded](Selectors.byUrlId(evt.id)))
val operation = BsonEventRequestGlobalUrlValueSet(
evt.requestId,
evt.id,
maybePreviousAddEvent.flatMap(_.source),
maybePreviousAddEvent.flatMap(_.browser),
maybePreviousAddEvent.map(_.urlType),
maybePreviousEvent.map(_.newUrl),
evt.url,
evt.doneBy,
evt.doneOn.toInstant)
await (eventRequestHistoryCollection flatMap (_.insertOne(operation)))
}
private def handleEventRequestBlockUrlAdded(evt: EventRequestUrlAddedV3): Future[Unit] = async {
val blockInfo = await(getBlockInfo(evt.blockId))
val operation = BsonEventRequestUrlAdded(
evt.requestId,
evt.id,
blockInfo.source,
evt.blockId,
blockInfo.blockType,
blockInfo.blockTitle,
evt.urlType,
evt.browser,
evt.url,
evt.doneBy,
evt.doneOn.toInstant)
await (eventRequestHistoryCollection flatMap (_.insertOne(operation)))
}
private def handleEventRequestBlockUrlValueSet(evt: EventRequestUrlValueSet): Future[Unit] = async {
val urlInfo = await(getUrlInfo(evt.id))
val operation = BsonEventRequestUrlValueSet(
evt.requestId,
evt.id,
urlInfo.source,
urlInfo.blockId,
urlInfo.blockType,
urlInfo.blockTitle,
urlInfo.browser,
urlInfo.urlType,
urlInfo.url,
evt.url,
evt.doneBy,
evt.doneOn.toInstant)
await (eventRequestHistoryCollection flatMap (_.insertOne(operation)))
}
private def handleEventRequestBlockUrlRemoved(evt: EventRequestUrlRemoved): Future[Unit] = async {
val urlInfo = await(getUrlInfo(evt.id))
val operation = BsonEventRequestUrlRemoved(
evt.requestId,
evt.id,
urlInfo.source,
urlInfo.blockId,
urlInfo.blockType,
urlInfo.blockTitle,
urlInfo.urlType,
urlInfo.browser,
urlInfo.url,
evt.doneBy,
evt.doneOn.toInstant)
await (eventRequestHistoryCollection flatMap (_.insertOne(operation)))
}
private def handleEventRequestUrlNoteSet(evt: EventRequestUrlNoteSet): Future[Unit] = async {
val maybePreviousEvent = await (findPreviousOperation[BsonEventRequestUrlNoteSet](Selectors.byUrlId(evt.id)))
val urlInfo = await(getUrlInfo(evt.id))
val operation = BsonEventRequestUrlNoteSet(
evt.requestId,
evt.id,
urlInfo.source,
urlInfo.blockId,
urlInfo.blockType,
urlInfo.blockTitle,
urlInfo.urlType,
urlInfo.browser,
maybePreviousEvent.map(_.to),
evt.note,
evt.doneBy,
evt.doneOn.toInstant)
await (eventRequestHistoryCollection flatMap (_.insertOne(operation)))
}
private def handleEventRequestUrlMoved(evt: EventRequestUrlMoved): Future[Unit] = async {
val urlInfo = await(getUrlInfo(evt.id))
val toBlockInfo: BlockInfo = await(getBlockInfo(evt.targetBlockId))
val operation = BsonEventRequestUrlMoved(
evt.requestId,
evt.id,
urlInfo.url,
urlInfo.urlType,
urlInfo.browser,
urlInfo.blockId,
urlInfo.blockType,
urlInfo.blockTitle,
urlInfo.source,
evt.targetBlockId,
toBlockInfo.blockType,
toBlockInfo.blockTitle,
toBlockInfo.source,
evt.doneBy,
evt.doneOn.toInstant)
await (eventRequestHistoryCollection flatMap (_.insertOne(operation)))
}
private def handleEventRequestUrlBrowserSet(evt: EventRequestUrlBrowserSet): Future[Unit] = async {
val maybePreviousEvent = await (findPreviousOperation[BsonEventRequestUrlBrowserSet](Selectors.byId(evt.id)))
val maybeBlockUrlSetEvent = await (findPreviousOperation[BsonEventRequestUrlValueSet](Selectors.byId(evt.id)))
val operation = BsonEventRequestUrlBrowserSet(
evt.requestId,
evt.id,
maybeBlockUrlSetEvent.map(_.to),
maybeBlockUrlSetEvent.flatMap(_.urlType),
maybePreviousEvent.flatMap(_.to),
evt.browser,
evt.doneBy,
evt.doneOn.toInstant)
await (eventRequestHistoryCollection flatMap (_.insertOne(operation)))
}
private def handleEventRequestNoteUpdated(evt: EventRequestNoteUpdatedV2): Future[Unit] = async {
val maybePreviousOperation = await (findPreviousOperation[BsonEventRequestNoteUpdated](Selectors.byRequestIdAndSource(evt.requestId, evt.source)))
val operation = BsonEventRequestNoteUpdated(evt.requestId,
Some(evt.source),
maybePreviousOperation.map(_.newNote),
evt.note,
evt.doneBy,
evt.doneOn.toInstant)
await (eventRequestHistoryCollection flatMap (_.insertOne(operation)))
}
private def handleEventRequestSubtypeUpdated(evt: EventRequestSubtypeUpdated): Future[Unit] = async {
val maybePreviousOperation = await (findPreviousOperation[BsonEventRequestSubtypeUpdated](Selectors.byRequestId(evt.requestId)))
val operation = BsonEventRequestSubtypeUpdated(evt.requestId,
blockId = None,
maybePreviousOperation.map(_.newSubtype),
evt.subtype,
evt.doneBy,
evt.doneOn.toInstant)
await (eventRequestHistoryCollection flatMap (_.insertOne(operation)))
}
private def handleEventRequestPatternAdded(evt: EventRequestPatternAdded): Future[Unit] = async {
val blockInfo = await(getBlockInfo(evt.blockId))
val operation = BsonEventRequestPatternAdded(evt.requestId,
evt.id,
blockInfo.source,
evt.blockId,
blockInfo.blockType,
blockInfo.blockTitle,
evt.pattern,
evt.doneBy,
evt.doneOn.toInstant)
await (eventRequestHistoryCollection flatMap (_.insertOne(operation)))
}
private def handleEventRequestPatternUpdated(evt: EventRequestPatternUpdated): Future[Unit] = async {
val patternInfo = await (getPatternInfo(evt.id))
val operation = BsonEventRequestPatternUpdated(
evt.requestId,
evt.id,
patternInfo.source,
patternInfo.blockId,
patternInfo.blockType,
patternInfo.blockTitle,
patternInfo.pattern,
evt.pattern,
evt.doneBy,
evt.doneOn.toInstant)
await (eventRequestHistoryCollection flatMap (_.insertOne(operation)))
}
private def handleEventRequestPatternRemoved(evt: EventRequestPatternRemoved): Future[Unit] = async {
val patternInfo = await (getPatternInfo(evt.id))
val operation = BsonEventRequestPatternRemoved(evt.requestId,
evt.id,
patternInfo.source,
patternInfo.blockType,
patternInfo.blockTitle,
patternInfo.pattern,
evt.doneBy,
evt.doneOn.toInstant)
await (eventRequestHistoryCollection flatMap (_.insertOne(operation)))
}
private def handleEventRequestRepositoryEventBound(evt: EventRequestAndRepositoryEventBoundV3): Future[Unit] = {
val operation = BsonEventRequestRepositoryEventBound(evt.requestId,
Some(evt.source),
evt.kind,
evt.repositoryEventId,
evt.doneBy,
evt.doneOn.toInstant)
eventRequestHistoryCollection flatMap (_.insertOne(operation))
}
private def handleEventRequestRepositoryPatternBound(evt: EventRequestAndRepositoryPatternBound): Future[Unit] = async {
val patternInfo = await(getPatternInfo(evt.patternId))
val operation = BsonEventRequestRepositoryPatternBound(evt.requestId,
patternInfo.source,
patternInfo.blockId,
patternInfo.blockType,
patternInfo.blockTitle,
evt.patternId,
patternInfo.pattern,
evt.repositoryPatternId,
evt.doneBy,
evt.doneOn.toInstant)
await (eventRequestHistoryCollection flatMap (_.insertOne(operation)))
}
private def handleEventRequestPatternMoved(evt: EventRequestPatternMoved): Future[Unit] = async {
val patternInfo = await (getPatternInfo(evt.id))
val toBlockInfo = await(getBlockInfo(evt.targetBlockId))
val operation = BsonEventRequestPatternMoved(evt.requestId,
evt.id,
patternInfo.source,
patternInfo.pattern,
patternInfo.blockId,
patternInfo.blockType,
patternInfo.blockTitle,
patternInfo.source,
evt.targetBlockId,
toBlockInfo.blockType,
toBlockInfo.blockTitle,
toBlockInfo.source,
evt.doneBy,
evt.doneOn.toInstant)
await (eventRequestHistoryCollection flatMap (_.insertOne(operation)))
}
private def eventRequestHistoryCollection: Future[MongoCollection[BsonOperation]] =
Future.successful {
scalaMongoDb
.getCollection[BsonOperation](HistoryCollectionName)
.withCodecRegistry(codecRegistry)
}
private def findPreviousOperation[T <: BsonOperation : ClassTag](idSelector: Bson): Future[Option[T]] = async {
val clazz = implicitly[ClassTag[T]].runtimeClass
val operationTag = operationTags(clazz)
val selector = and(
idSelector,
Selectors.byOperationTag(operationTag)
)
val maybeOp = await(eventRequestHistoryCollection flatMap(_.find(selector).sort(Sorting.byDoneOn).first().toFutureOption()))
maybeOp map {
case op if op.getClass == clazz => op.asInstanceOf[T]
case other => sys.error(s"Expecting operation of $clazz but got $other")
}
}
private def getBlockInfo(blockId: UUID): Future[BlockInfo] = async {
val maybeBlockAddEvent = await (findPreviousOperation[BsonEventRequestBlockAdded](Selectors.byBlockId(blockId)))
val maybeBlockTransferEvent = await (findPreviousOperation[BsonEventRequestBlockTransferred](Selectors.byBlockId(blockId)))
val maybeBlockTitleSetEvent = await (findPreviousOperation[BsonEventRequestBlockTitleSet](Selectors.byBlockId(blockId)))
BlockInfo(
Some(blockId),
maybeBlockTransferEvent.map(_.to).orElse(maybeBlockAddEvent.flatMap(_.source)),
maybeBlockTransferEvent.map(_.blockType).orElse(maybeBlockAddEvent.map(_.blockType)),
maybeBlockTitleSetEvent.map(_.to).orElse(maybeBlockTransferEvent.flatMap(_.blockTitle)),
)
}
private def getUrlInfo(urlId: UUID): Future[UrlInfo] = async {
val maybeUrlUpdateEvent = await (findPreviousOperation[BsonEventRequestUrlValueSet](Selectors.byUrlId(urlId)))
val maybeUrlMoveEvent = await (findPreviousOperation[BsonEventRequestUrlMoved](Selectors.byUrlId(urlId)))
val maybeUrlAddEvent = await (findPreviousOperation[BsonEventRequestUrlAdded](Selectors.byUrlId(urlId)))
val maybeOriginalUrlId: Option[UUID] = (maybeUrlUpdateEvent, maybeUrlMoveEvent, maybeUrlAddEvent) match {
case (None, None, None) => await (findPreviousOperation[BsonEventRequestBlockTransferred](Selectors.byUrlIdIn(urlId)))
.flatMap(_.urlsTransferred.find(_.id == urlId))
.map(_.originalId)
case _ => None
}
val maybeOriginalUrlInfo: Option[UrlInfo] = maybeOriginalUrlId match {
case Some(originalUrlId) => Some(await (getUrlInfo(originalUrlId)))
case _ => None
}
val maybeBlockId = maybeUrlUpdateEvent.flatMap(_.blockId)
.orElse(maybeUrlMoveEvent.map(_.to))
.orElse(maybeUrlAddEvent.map(_.blockId))
.orElse(maybeOriginalUrlInfo.flatMap(_.blockId))
val maybeBlockInfo: Option[BlockInfo] = maybeBlockId match {
case Some(blockId) => Some(await (getBlockInfo(blockId)))
case _ => None
}
UrlInfo(
Some(urlId),
maybeUrlUpdateEvent.map(_.to)
.orElse(maybeUrlMoveEvent.flatMap(_.url))
.orElse(maybeUrlAddEvent.map(_.url))
.orElse(maybeOriginalUrlInfo.flatMap(_.url))
.filter(_.nonEmpty),
maybeUrlUpdateEvent.flatMap(_.urlType)
.orElse(maybeUrlMoveEvent.flatMap(_.urlType))
.orElse(maybeUrlAddEvent.map(_.urlType))
.orElse(maybeOriginalUrlInfo.flatMap(_.urlType)),
maybeUrlUpdateEvent.flatMap(_.browser)
.orElse(maybeUrlMoveEvent.flatMap(_.browser))
.orElse(maybeUrlAddEvent.flatMap(_.browser))
.orElse(maybeOriginalUrlInfo.flatMap(_.browser)),
maybeBlockId,
maybeBlockInfo.flatMap(_.blockType),
maybeBlockInfo.flatMap(_.blockTitle),
maybeBlockInfo.flatMap(_.source),
)
}
private def getPatternInfo(patternId: UUID): Future[PatternInfo] = async {
val maybePatternUpdateEvent = await (findPreviousOperation[BsonEventRequestPatternUpdated](Selectors.byPatternId(patternId)))
val maybePatternMoveEvent = await (findPreviousOperation[BsonEventRequestPatternMoved](Selectors.byPatternId(patternId)))
val maybePatternAddEvent = await (findPreviousOperation[BsonEventRequestPatternAdded](Selectors.byPatternId(patternId)))
val maybeOriginalPatternId: Option[UUID] = (maybePatternUpdateEvent, maybePatternMoveEvent, maybePatternAddEvent) match {
case (None, None, None) => await (findPreviousOperation[BsonEventRequestBlockTransferred](Selectors.byPatternIdIn(patternId)))
.flatMap(_.patternsTransferred.find(_.id == patternId))
.map(_.originalId)
case _ => None
}
val maybeOriginalPatternInfo: Option[PatternInfo] = maybeOriginalPatternId match {
case Some(originalPatternId) => Some(await (getPatternInfo(originalPatternId)))
case _ => None
}
val maybeBlockId = maybePatternUpdateEvent.flatMap(_.blockId)
.orElse(maybePatternMoveEvent.map(_.to))
.orElse(maybePatternAddEvent.map(_.blockId))
.orElse(maybeOriginalPatternInfo.flatMap(_.blockId))
val maybeBlockInfo: Option[BlockInfo] = maybeBlockId match {
case Some(blockId) => Some(await (getBlockInfo(blockId)))
case _ => None
}
PatternInfo(
Some(patternId),
maybePatternUpdateEvent.map(_.to)
.orElse(maybePatternMoveEvent.flatMap(_.pattern))
.orElse(maybePatternAddEvent.map(_.pattern))
.orElse(maybeOriginalPatternInfo.flatMap(_.pattern))
.filter(_.nonEmpty),
maybeBlockId,
maybeBlockInfo.flatMap(_.blockType),
maybeBlockInfo.flatMap(_.blockTitle),
maybeBlockInfo.flatMap(_.source),
)
}
private def historySelector(requestId: UUID, maybeSource: Option[PatternDataSource], maybeEventType: Option[String]): Bson = (maybeSource, maybeEventType) match {
case (Some(source), Some(eventType)) => Selectors.byRequestIdSourceAndEventType(requestId, source, typesToTags(eventType))
case (Some(source), None) => Selectors.byRequestIdAndSource(requestId, source)
case (None, Some(eventType)) => Selectors.byRequestIdAndEventType(requestId, typesToTags(eventType))
case _ => Selectors.byRequestId(requestId)
}
override def clear(): Future[Unit] = eventRequestHistoryCollection flatMap (_.drop())
override def history(eventRequestId: UUID, start: Int, limit: Int, source: Option[PatternDataSource], eventType: Option[String]): Future[PaginatedList[EventRequestHistoryReadModel.EventHistoryItem]] = async {
val selector = historySelector(eventRequestId, source, eventType)
val historyItems = await(eventRequestHistoryCollection flatMap (_.find(selector).sort(Sorting.byDoneOn).skip(start).limit(limit).toFuture()))
val count = await(eventRequestHistoryCollection flatMap (_.countDocuments(selector).toFuture()))
val jsonItems = historyItems
.map {
case i: BsonEventRequestAdded => EventHistoryEventRequestAdded(i.domain, i.eventType, i.eventSubtype, i.doneBy, i.doneOn)
case i: BsonEventRequestEnabled=> EventHistoryEventEnabled(i.doneBy, i.doneOn)
case i: BsonEventRequestDisabled => EventHistoryEventDisabled(i.message, i.doneBy, i.doneOn)
case i: BsonEventRequestExistenceUpdated => EventHistoryEventExistenceUpdated(i.source, i.from, i.to, i.doneBy, i.doneOn)
case i: BsonEventRequestTypeUpdated => EventHistoryEventTypeUpdated(i.source, i.from, i.to, i.doneBy, i.doneOn)
case i: BsonEventRequestBlockAdded => EventHistoryAddedBlock(i.blockType, i.source, i.doneBy, i.doneOn)
case i: BsonEventRequestBlockRemoved => EventHistoryRemovedBlock(i.blockType, i.source, i.doneBy, i.doneOn)
case i: BsonEventRequestBlockTitleSet => EventHistoryBlockTitleSet(i.blockType, i.source, i.from, i.to, i.doneBy, i.doneOn)
case i: BsonEventRequestBlockNoteSet => EventHistoryBlockNoteSet(i.blockId, i.blockType, i.oldNote, i.newNote, i.source, i.blockTitle, i.doneBy, i.doneOn)
case i: BsonEventRequestBlockAnalystNoteSet => EventHistoryBlockAnalystNoteSet(i.blockId, i.blockType, i.blockTitle, i.from, i.to, i.source, i.doneBy, i.doneOn)
case i: BsonEventRequestBlockValidatorNoteSet => EventHistoryBlockValidatorNoteSet(i.blockId, i.blockType, i.blockTitle, i.from, i.to, i.source, i.doneBy, i.doneOn)
case i: BsonEventRequestBlockTransferred => EventHistoryBlockTransferred(i.blockId, i.blockType, i.blockTitle, i.from, i.to, i.source, i.doneBy, i.doneOn)
case i: BsonEventRequestBlockLabelsSet => EventHistoryBlockLabelsSet(i.blockId, i.blockType, i.blockTitle, i.from, i.to, i.source, i.doneBy, i.doneOn)
case i: BsonEventRequestBlockRangeSet => EventHistoryBlockRangeSet(i.blockId, i.blockType, i.blockTitle, i.fromFrom, i.fromTo, i.toFrom, i.toTo, i.source, i.doneBy, i.doneOn)
case i: BsonEventRequestGlobalUrlAdded => EventHistoryGlobalUrlAdded(i.urlId, i.urlType, i.browser, i.url, i.source, i.doneBy, i.doneOn)
case i: BsonEventRequestGlobalUrlValueSet => EventHistoryGlobalUrlValueSet(i.urlType, i.oldUrl, i.newUrl, i.source, i.doneBy, i.doneOn)
case i: BsonEventRequestUrlAdded => EventHistoryUrlAdded(i.urlId, i.blockId, i.blockType, i.blockTitle, i.urlType, i.browser, i.url, i.source, i.doneBy, i.doneOn)
case i: BsonEventRequestUrlRemoved => EventHistoryUrlRemoved(i.urlId, i.blockId, i.blockType, i.blockTitle, i.urlType, i.browser, i.url, i.source, i.doneBy, i.doneOn)
case i: BsonEventRequestUrlValueSet => EventHistoryUrlValueSet(i.blockType, i.blockTitle, i.urlType, i.from, i.to, i.source, i.doneBy, i.doneOn)
case i: BsonEventRequestUrlNoteSet => EventHistoryUrlNoteSet(i.blockType, i.blockTitle, i.urlType, i.from, i.to, i.source, i.doneBy, i.doneOn)
case i: BsonEventRequestUrlMoved => EventHistoryUrlMoved(i.urlType, i.url, i.from, i.fromBlockType, i.fromBlockTitle, i.fromSource, i.to, i.toBlockType, i.toBlockTitle, i.toSource, i.doneBy, i.doneOn)
case i: BsonEventRequestUrlBrowserSet => EventHistoryUrlBrowserSet(i.urlType, i.url, i.from, i.to, i.doneBy, i.doneOn)
case i: BsonEventRequestNoteUpdated => EventHistoryNoteUpdated(i.oldNote, i.newNote, i.source, i.doneBy, i.doneOn)
case i: BsonEventRequestSubtypeUpdated => EventHistorySubtypeUpdated(i.oldSubtype, i.newSubtype, i.source, i.doneBy, i.doneOn)
case i: BsonEventRequestPatternAdded => EventHistoryPatternAdded(i.pattern, i.source, i.blockType, i.blockTitle, i.doneBy, i.doneOn)
case i: BsonEventRequestPatternUpdated => EventHistoryPatternUpdated(i.blockId, i.blockType, i.blockTitle, i.from, i.to, i.source, i.doneBy, i.doneOn)
case i: BsonEventRequestPatternRemoved => EventHistoryPatternRemoved(i.pattern, i.source, i.doneBy, i.doneOn)
case i: BsonEventRequestRepositoryEventBound => EventHistoryRequestRepositoryEventBound(i.kind, i.repositoryEventId, i.source, i.doneBy, i.doneOn)
case i: BsonEventRequestRepositoryPatternBound => EventHistoryRequestRepositoryPatternBound(i.source, i.blockType, i.blockTitle, i.patternId, i.pattern, i.repositoryPatternId, i.doneBy, i.doneOn)
case i: BsonEventRequestPatternMoved => EventHistoryRequestPatternMoved(i.patternId, i.pattern, i.from, i.fromBlockType, i.fromBlockTitle, i.fromSource, i.to, i.toBlockType, i.toBlockTitle, i.toSource, i.doneBy, i.doneOn)
}
PaginatedList(jsonItems, limit, start, count.toInt)
}
}
object MongoEventRequestHistoryReadModel {
sealed trait BsonOperation {
def requestId: UUID
def doneBy: UUID
def doneOn: Instant
def source: Option[PatternDataSource]
}
final case class BlockInfo(
blockId: Option[UUID],
source: Option[PatternDataSource],
blockType: Option[BlockType],
blockTitle: Option[String],
)
final case class UrlInfo(
urlId: Option[UUID],
url: Option[String],
urlType: Option[UrlType],
browser: Option[Browser],
blockId: Option[UUID],
blockType: Option[BlockType],
blockTitle: Option[String],
source: Option[PatternDataSource],
)
final case class PatternInfo(
patternId: Option[UUID],
pattern: Option[String],
blockId: Option[UUID],
blockType: Option[BlockType],
blockTitle: Option[String],
source: Option[PatternDataSource],
)
final case class BsonEventRequestAdded(
requestId: UUID,
domain: String,
doneBy: UUID,
doneOn: Instant,
eventType: UUID,
eventSubtype: UUID) extends BsonOperation {
override val source: Option[PatternDataSource] = None
}
final case class BsonEventRequestEnabled(
requestId: UUID,
doneBy: UUID,
doneOn: Instant) extends BsonOperation {
override val source: Option[PatternDataSource] = None
}
final case class BsonEventRequestDisabled(
requestId: UUID,
message: Option[String],
doneBy: UUID,
doneOn: Instant) extends BsonOperation {
override val source: Option[PatternDataSource] = None
}
final case class BsonEventRequestTypeUpdated(
requestId: UUID,
source: Option[PatternDataSource],
from: Option[UUID],
to: UUID,
doneBy: UUID,
doneOn: Instant) extends BsonOperation
final case class BsonEventRequestBlockAdded(
requestId: UUID,
blockId: UUID,
source: Option[PatternDataSource],
blockType: BlockType,
doneBy: UUID,
doneOn: Instant) extends BsonOperation
final case class BsonEventRequestNoteUpdated(
requestId: UUID,
source: Option[PatternDataSource],
oldNote: Option[String],
newNote: String,
doneBy: UUID,
doneOn: Instant) extends BsonOperation
final case class BsonEventRequestExistenceUpdated(
requestId: UUID,
source: Option[PatternDataSource],
from: Option[EventRequestExistence],
to: EventRequestExistence,
doneBy: UUID,
doneOn: Instant) extends BsonOperation
final case class BsonEventRequestBlockRemoved(
requestId: UUID,
source: Option[PatternDataSource],
blockType: Option[BlockType],
blockTitle: Option[String],
doneBy: UUID,
doneOn: Instant) extends BsonOperation
final case class BsonEventRequestBlockNoteSet(
requestId: UUID,
blockId: Option[UUID],
blockType: Option[BlockType],
source: Option[PatternDataSource],
blockTitle: Option[String],
oldNote: Option[String],
newNote: String,
doneBy: UUID,
doneOn: Instant) extends BsonOperation
final case class BsonEventRequestBlockAnalystNoteSet(
requestId: UUID,
source: Option[PatternDataSource],
blockId: Option[UUID],
blockType: Option[BlockType],
blockTitle: Option[String],
from: Option[String],
to: String,
doneBy: UUID,
doneOn: Instant) extends BsonOperation
final case class BsonEventRequestBlockValidatorNoteSet(
requestId: UUID,
blockId: Option[UUID],
source: Option[PatternDataSource],
blockType: Option[BlockType],
blockTitle: Option[String],
from: Option[String],
to: String,
doneBy: UUID,
doneOn: Instant) extends BsonOperation
final case class BsonEventRequestBlockTransferred(
requestId: UUID,
blockId: UUID,
source: Option[PatternDataSource],
blockType: BlockType,
blockTitle: Option[String],
urls: Seq[UUID],
urlsTransferred: Seq[UrlTransfer],
patterns: Seq[UUID],
patternsTransferred: Seq[PatternTransfer],
from: Option[PatternDataSource],
to: PatternDataSource,
doneBy: UUID,
doneOn: Instant) extends BsonOperation
final case class BsonEventRequestBlockTitleSet(
requestId: UUID,
blockId: UUID,
source: Option[PatternDataSource],
blockType: Option[BlockType],
from: Option[String],
to: String,
doneBy: UUID,
doneOn: Instant) extends BsonOperation
final case class BsonEventRequestBlockLabelsSet(
requestId: UUID,
source: Option[PatternDataSource],
blockId: UUID,
blockType: Option[BlockType],
blockTitle: Option[String],
from: Option[Set[String]],
to: Set[String],
doneBy: UUID,
doneOn: Instant) extends BsonOperation
final case class BsonEventRequestBlockRangeSet(
requestId: UUID,
source: Option[PatternDataSource],