diff --git a/database/factories/AppNotesFactory.php b/database/factories/AppNotesFactory.php new file mode 100644 index 000000000..0f6cdd446 --- /dev/null +++ b/database/factories/AppNotesFactory.php @@ -0,0 +1,40 @@ +define(\ProcessMaker\Model\AppNotes::class, function (Faker $faker) { + return [ + 'APP_UID' => G::generateUniqueID(), + 'USR_UID' => G::generateUniqueID(), + 'NOTE_DATE' => $faker->dateTime(), + 'NOTE_CONTENT' => $faker->sentence(3), + 'NOTE_TYPE' => 'USER', + 'NOTE_AVAILABILITY' => 'PUBLIC', + 'NOTE_ORIGIN_OBJ' => '', + 'NOTE_AFFECTED_OBJ1' => '', + 'NOTE_AFFECTED_OBJ2' => '', + 'NOTE_RECIPIENTS' => '', + ]; +}); + +// Create a case notes with the foreign keys +$factory->state(\ProcessMaker\Model\AppNotes::class, 'foreign_keys', function (Faker $faker) { + // Create values in the foreign key relations + $application = factory(\ProcessMaker\Model\Application::class)->create(); + $user = factory(\ProcessMaker\Model\User::class)->create(); + + // Return with default values + return [ + 'APP_UID' => $application->APP_UID, + 'USR_UID' => $user->USR_UID, + 'NOTE_DATE' => $faker->dateTime(), + 'NOTE_CONTENT' => $faker->sentence(3), + 'NOTE_TYPE' => 'USER', + 'NOTE_AVAILABILITY' => 'PUBLIC', + 'NOTE_ORIGIN_OBJ' => '', + 'NOTE_AFFECTED_OBJ1' => '', + 'NOTE_AFFECTED_OBJ2' => '', + 'NOTE_RECIPIENTS' => '', + ]; +}); + diff --git a/tests/unit/workflow/engine/classes/model/AppNotesTest.php b/tests/unit/workflow/engine/classes/model/AppNotesTest.php new file mode 100644 index 000000000..e6529b4f4 --- /dev/null +++ b/tests/unit/workflow/engine/classes/model/AppNotesTest.php @@ -0,0 +1,104 @@ +create(); + $user = factory(User::class)->create(); + $reason = "The case was canceled due to:"; + $appNotes = new ModelAppNotes(); + $noteContent = addslashes($reason); + $appNotes->postNewNote( + $application->APP_UID, $user->USR_UID, $noteContent, false + ); + + // Query to get the cases notes + $query = AppNotes::query(); + $query->select()->where('APP_UID', $application->APP_UID)->where('USR_UID', $user->USR_UID); + $result = $query->get()->values()->toArray(); + $this->assertNotEmpty($result); + } + + /** + * It test the cases notes creation and send a email to specific user + * + * @test + */ + public function it_test_case_notes_creation_and_send_email_to_user() + { + $application = factory(Application::class)->create(); + $user = factory(User::class)->create(); + $reason = "The case was canceled due to:"; + $appNotes = new ModelAppNotes(); + $noteContent = addslashes($reason); + $appNotes->postNewNote( + $application->APP_UID, $user->USR_UID, $noteContent, true, 'PUBLIC', $user->USR_UID + ); + + // Query to get the cases notes + $query = AppNotes::query(); + $query->select()->where('APP_UID', $application->APP_UID)->where('USR_UID', $user->USR_UID); + $result = $query->get()->values()->toArray(); + $this->assertNotEmpty($result); + + // Query to get the emails + $query = AppMessage::query(); + $query->select()->where('APP_UID', $application->APP_UID)->where('APP_MSG_TYPE', 'CASE_NOTE'); + $result = $query->get()->values()->toArray(); + $this->assertNotEmpty($result); + } + + /** + * It test the cases notes creation and send a email to user with participaion in the case + * + * @test + */ + public function it_test_case_notes_creation_and_send_email() + { + $application = factory(Application::class)->create(); + $user = factory(User::class)->create(); + factory(Delegation::class)->create([ + 'APP_UID' => $application->APP_UID, + 'USR_UID' => $user->USR_UID + ]); + $reason = "The case was canceled due to:"; + $appNotes = new ModelAppNotes(); + $noteContent = addslashes($reason); + $appNotes->postNewNote( + $application->APP_UID, $user->USR_UID, $noteContent, true, 'PUBLIC' + ); + + // Query to get the cases notes + $query = AppNotes::query(); + $query->select()->where('APP_UID', $application->APP_UID)->where('USR_UID', $user->USR_UID); + $result = $query->get()->values()->toArray(); + $this->assertNotEmpty($result); + + // Query to get the emails + $query = AppMessage::query(); + $query->select()->where('APP_UID', $application->APP_UID)->where('APP_MSG_TYPE', 'CASE_NOTE'); + $result = $query->get()->values()->toArray(); + $this->assertNotEmpty($result); + } +} \ No newline at end of file diff --git a/workflow/engine/methods/cases/ajaxListener.php b/workflow/engine/methods/cases/ajaxListener.php index 4dd2cee01..a966c5c1e 100644 --- a/workflow/engine/methods/cases/ajaxListener.php +++ b/workflow/engine/methods/cases/ajaxListener.php @@ -571,6 +571,14 @@ class Ajax // Review if the case was cancelled, true if the case was cancelled $result->status = ($response->status_code == 0) ? true : false; $result->msg = $response->message; + // Register in cases notes + if (!empty($_POST['NOTE_REASON'])) { + $appNotes = new AppNotes(); + $noteContent = addslashes($_POST['NOTE_REASON']); + $appNotes->postNewNote( + $appUid, $usrUid, $noteContent, $_POST['NOTIFY_CANCEL'] + ); + } } else { $result->status = false; $result->msg = G::LoadTranslation("ID_CASE_USER_INVALID_CANCEL_CASE", [$usrUid]); diff --git a/workflow/engine/src/ProcessMaker/Model/AppNotes.php b/workflow/engine/src/ProcessMaker/Model/AppNotes.php new file mode 100644 index 000000000..2db007ad5 --- /dev/null +++ b/workflow/engine/src/ProcessMaker/Model/AppNotes.php @@ -0,0 +1,11 @@ +