diff --git a/app/Http/Controllers/Api/V1/UserController.php b/app/Http/Controllers/Api/V1/UserController.php index 3d510d056..a9829dffa 100644 --- a/app/Http/Controllers/Api/V1/UserController.php +++ b/app/Http/Controllers/Api/V1/UserController.php @@ -563,6 +563,8 @@ public function update(UpdateUser $request, int $id): JsonResponse $user->role = isset($input['role']) ? $input['role'] : $user->role; $user->save(); + + return response()->json([ 'message' => 'success', 'data' => $user, diff --git a/app/Http/Kernel.php b/app/Http/Kernel.php index e5704e167..f02b06c00 100644 --- a/app/Http/Kernel.php +++ b/app/Http/Kernel.php @@ -23,8 +23,8 @@ class Kernel extends HttpKernel \App\Http\Middleware\TrimStrings::class, \Illuminate\Foundation\Http\Middleware\ConvertEmptyStringsToNull::class, \Illuminate\Session\Middleware\StartSession::class, - \App\Http\Middleware\TerminateRequest::class, - \App\Http\Middleware\LogRequestResponse::class, + // \App\Http\Middleware\TerminateRequest::class, + // \App\Http\Middleware\LogRequestResponse::class, ]; /** diff --git a/app/Models/Affiliation.php b/app/Models/Affiliation.php index 3973a7fbf..3b59ee34a 100644 --- a/app/Models/Affiliation.php +++ b/app/Models/Affiliation.php @@ -180,6 +180,8 @@ class Affiliation extends Model ], State::STATE_AFFILIATION_LEFT => [] ]; + + protected static array $transitionsAutomated = []; public $table = 'affiliations'; @@ -216,6 +218,11 @@ public static function getTransitions(): array return static::$transitions; } + public function getTransitionsAutomated(): array + { + return static::$transitionsAutomated; + } + public function getActivitylogOptions(): LogOptions { return LogOptions::defaults() diff --git a/app/Models/CustodianHasProjectOrganisation.php b/app/Models/CustodianHasProjectOrganisation.php index 22a5b788c..c37d24878 100644 --- a/app/Models/CustodianHasProjectOrganisation.php +++ b/app/Models/CustodianHasProjectOrganisation.php @@ -60,6 +60,12 @@ class CustodianHasProjectOrganisation extends Model use FilterManager; protected static array $transitions = [ + State::STATE_ORG_INVITED => [ + State::STATE_ORG_AWAITING_APPROVAL, + ], + State::STATE_ORG_AWAITING_APPROVAL => [ + State::STATE_PENDING, // I think we need something to move to declined too + ], State::STATE_PENDING => [ State::STATE_VALIDATION_IN_PROGRESS, State::STATE_MORE_ORG_INFO_REQ_ESCALATION_MANAGER, @@ -74,6 +80,7 @@ class CustodianHasProjectOrganisation extends Model State::STATE_VALIDATION_COMPLETE => [ State::STATE_ORG_VALIDATION_DECLINED, State::STATE_VALIDATION_COMPLETE, + State::STATE_ORG_REMOVED_FROM_PROJECT, ], State::STATE_MORE_ORG_INFO_REQ_ESCALATION_MANAGER => [ State::STATE_MORE_ORG_INFO_REQ_ESCALATION_COMMITTEE, @@ -89,6 +96,13 @@ class CustodianHasProjectOrganisation extends Model State::STATE_VALIDATION_COMPLETE, ], State::STATE_ORG_LEFT_PROJECT => [], + State::STATE_ORG_REMOVED_FROM_PROJECT => [], + ]; + + protected static array $transitionsAutomated = [ + State::STATE_ORG_INVITED, + State::STATE_ORG_AWAITING_APPROVAL, + State::STATE_PENDING, ]; public static function getTransitions(): array @@ -96,6 +110,11 @@ public static function getTransitions(): array return static::$transitions; } + public function getTransitionsAutomated(): array + { + return static::$transitionsAutomated; + } + protected $table = 'custodian_has_project_has_organisation'; public $timestamps = true; diff --git a/app/Models/CustodianHasProjectUser.php b/app/Models/CustodianHasProjectUser.php index e3f9fccda..bd5746c00 100644 --- a/app/Models/CustodianHasProjectUser.php +++ b/app/Models/CustodianHasProjectUser.php @@ -80,11 +80,18 @@ class CustodianHasProjectUser extends Model State::STATE_USER_LEFT_PROJECT => [], ]; + protected static array $transitionsAutomated = []; + public static function getTransitions(): array { return static::$transitions; } + public function getTransitionsAutomated(): array + { + return static::$transitionsAutomated; + } + protected static array $searchableColumns = ['projects.title']; protected static array $sortableColumns = ['projects.title']; diff --git a/app/Models/Project.php b/app/Models/Project.php index 25b752af3..6e2511868 100644 --- a/app/Models/Project.php +++ b/app/Models/Project.php @@ -149,6 +149,8 @@ class Project extends Model ], ]; + protected static array $transitionsAutomated = []; + protected $table = 'projects'; public $timestamps = true; @@ -230,6 +232,11 @@ public static function getTransitions(): array return static::$transitions; } + public function getTransitionsAutomated(): array + { + return static::$transitionsAutomated; + } + public function projectHasOrganisations() { return $this->hasMany( diff --git a/app/Models/State.php b/app/Models/State.php index 8bbdaa5b4..bedd4f984 100644 --- a/app/Models/State.php +++ b/app/Models/State.php @@ -110,6 +110,11 @@ class State extends Model public const STATE_USER_LEFT_PROJECT = 'user_left_project'; // user left project public const STATE_ORG_LEFT_PROJECT = 'org_left_project'; // org left project + public const STATE_ORG_INVITED = 'org_invited'; + public const STATE_ORG_AWAITING_APPROVAL = 'org_awaiting_approval'; + public const STATE_ORG_REMOVED_FROM_PROJECT = 'org_removed_from_project'; + + public const STATES = [ self::STATE_INVITED, self::STATE_REGISTERED, @@ -140,5 +145,8 @@ class State extends Model self::STATE_ORG_VALIDATION_DECLINED, self::STATE_USER_LEFT_PROJECT, self::STATE_ORG_LEFT_PROJECT, + self::STATE_ORG_INVITED, + self::STATE_ORG_AWAITING_APPROVAL, + self::STATE_ORG_REMOVED_FROM_PROJECT, ]; } diff --git a/app/Models/User.php b/app/Models/User.php index e933e7515..38f2a84ec 100644 --- a/app/Models/User.php +++ b/app/Models/User.php @@ -167,6 +167,8 @@ class User extends Authenticatable State::STATE_VALIDATED => [], ]; + protected static array $transitionsAutomated = []; + /** * The attributes that are mass assignable. */ @@ -442,4 +444,9 @@ public static function getTransitions(): array { return static::$transitions; } + + public function getTransitionsAutomated(): array + { + return static::$transitionsAutomated; + } } diff --git a/app/Observers/FileObserver.php b/app/Observers/FileObserver.php index 8c50df1a0..8f0104fb0 100644 --- a/app/Observers/FileObserver.php +++ b/app/Observers/FileObserver.php @@ -3,12 +3,15 @@ namespace App\Observers; use App\Models\File; +use App\Models\User; +use App\Models\State; use App\Jobs\SendEmailJob; use App\Models\Organisation; -use Hdruk\LaravelMjml\Models\EmailTemplate; use App\Jobs\ProcessCSVSubmission; use App\Models\OrganisationHasFile; -use App\Models\User; +use App\Models\ProjectHasOrganisation; +use Hdruk\LaravelMjml\Models\EmailTemplate; +use App\Models\CustodianHasProjectOrganisation; class FileObserver { @@ -30,9 +33,27 @@ public function updated(File $file): void ])->first()->organisation_id; $this->sendEmail($file, $organisationId); + + $this->statusCustodianHasProjectOrganisation($organisationId); } } + protected function statusCustodianHasProjectOrganisation(int $organisationId): void + { + $projectHasOrganisations = ProjectHasOrganisation::where([ + 'organisation_id' => $organisationId, + ])->pluck('id')->toArray(); + + $custodianHasProjectOrganisationIds = CustodianHasProjectOrganisation::whereIn('project_has_organisation_id', $projectHasOrganisations)->pluck('id')->toArray(); + foreach ($custodianHasProjectOrganisationIds as $custodianHasProjectOrganisationId) { + $custodianHasProjectOrganisation = CustodianHasProjectOrganisation::where('id', $custodianHasProjectOrganisationId)->first(); + if ($custodianHasProjectOrganisation->getState() === State::STATE_ORG_INVITED) { + $custodianHasProjectOrganisation->setState(State::STATE_ORG_AWAITING_APPROVAL); + } + } + + } + protected function sendEmail(File $file, int $organisationId): void { $organisation = Organisation::where('id', $organisationId)->first(); diff --git a/app/Observers/ProjectHasOrganisationObserver.php b/app/Observers/ProjectHasOrganisationObserver.php index 1ca052d06..89a8b3c2c 100644 --- a/app/Observers/ProjectHasOrganisationObserver.php +++ b/app/Observers/ProjectHasOrganisationObserver.php @@ -2,10 +2,11 @@ namespace App\Observers; -use App\Models\CustodianHasProjectOrganisation; -use App\Models\ProjectHasOrganisation; -use App\Models\ProjectHasCustodian; +use App\Models\State; use App\Traits\ValidationManager; +use App\Models\ProjectHasCustodian; +use App\Models\ProjectHasOrganisation; +use App\Models\CustodianHasProjectOrganisation; class ProjectHasOrganisationObserver { @@ -20,10 +21,20 @@ public function created(ProjectHasOrganisation $pho): void $custodianIds = ProjectHasCustodian::where(['project_id' => $pho->project_id])->pluck("custodian_id")->toArray(); foreach ($custodianIds as $custodianId) { - CustodianHasProjectOrganisation::firstOrCreate([ + $checking = CustodianHasProjectOrganisation::where([ 'custodian_id' => $custodianId, 'project_has_organisation_id' => $projectHasOrganisationId - ]); + ])->first(); + + if (is_null($checking)) { + $create = CustodianHasProjectOrganisation::create([ + 'custodian_id' => $custodianId, + 'project_has_organisation_id' => $projectHasOrganisationId + ]); + + $custodianHasProjectOrganisation = CustodianHasProjectOrganisation::where('id', $create->id)->first(); + $custodianHasProjectOrganisation->setState(State::STATE_ORG_INVITED); + } } } } diff --git a/app/Traits/StateWorkflow.php b/app/Traits/StateWorkflow.php index d89a50179..1759da37f 100644 --- a/app/Traits/StateWorkflow.php +++ b/app/Traits/StateWorkflow.php @@ -76,4 +76,9 @@ public static function getAllStates(): array $allStates = array_unique(array_merge($keys, $values)); return array_values($allStates); } + + public static function getAutomatedTransitions(): array + { + return static::getAutomatedTransitions(); + } }