44
55use Livewire \Component ;
66use Ramsey \Uuid \Uuid ;
7+ use VentureDrake \LaravelCrm \Models \Contact ;
8+ use VentureDrake \LaravelCrm \Models \Organisation ;
9+ use VentureDrake \LaravelCrm \Models \Person ;
710
811class LiveNote extends Component
912{
@@ -23,10 +26,34 @@ public function create()
2326 'content ' => 'required ' ,
2427 ]);
2528
26- $ this ->model ->notes ()->create ([
29+ $ note = $ this ->model ->notes ()->create ([
2730 'external_id ' => Uuid::uuid4 ()->toString (),
2831 'content ' => $ data ['content ' ],
2932 ]);
33+
34+ // Add to any upstream related models
35+ if ($ this ->model instanceof Person) {
36+ if ($ this ->model ->organisation ) {
37+ $ this ->model ->organisation ->notes ()->create ([
38+ 'external_id ' => Uuid::uuid4 ()->toString (),
39+ 'content ' => $ data ['content ' ],
40+ 'related_note_id ' => $ note ->id ,
41+ ]);
42+ }
43+ }
44+
45+ if ($ this ->model instanceof Organisation || $ this ->model instanceof Person) {
46+ foreach (Contact::where ([
47+ 'entityable_type ' => $ this ->model ->getMorphClass (),
48+ 'entityable_id ' => $ this ->model ->id ,
49+ ])->get () as $ contact ) {
50+ $ contact ->contactable ->notes ()->create ([
51+ 'external_id ' => Uuid::uuid4 ()->toString (),
52+ 'content ' => $ data ['content ' ],
53+ 'related_note_id ' => $ note ->id ,
54+ ]);
55+ }
56+ }
3057
3158 $ this ->resetFields ();
3259 }
0 commit comments