Skip to content

Commit 1a832a4

Browse files
authored
Merge pull request #8 from IFRCGo/feature/WN-252
WN-252
2 parents fc44782 + 3de1205 commit 1a832a4

12 files changed

Lines changed: 739 additions & 4 deletions

File tree

app/Classes/Repositories/OrganisationRepository.php

Lines changed: 22 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
namespace App\Classes\Repositories;
44

55
use App\Models\Organisation;
6+
use App\Models\Contributor;
67
use Illuminate\Support\Facades\DB;
78
use Illuminate\Support\Facades\Log;
89

@@ -87,16 +88,35 @@ public function updateDetailsWithInput(Organisation $org, array $input)
8788
if (count($input['translations'])) {
8889
DB::transaction(function () use ($org, $input) {
8990
$org->details()->delete();
90-
91+
9192
foreach ($input['translations'] as $lang => $data) {
92-
$org->details()->updateOrCreate([
93+
$currentDetail = $org->details()->updateOrCreate([
9394
'language_code' => strtolower($lang),
9495
], [
9596
'language_code' => strtolower($lang),
9697
'org_name' => $data['name'] ?? '',
9798
'attribution_message' => $data['attributionMessage'] ?? '',
9899
'published' => $data['published'] ?? false,
99100
]);
101+
102+
if (array_key_exists('contributors', $data)) {
103+
$currentDetail->contributors()->delete();
104+
105+
foreach ($data['contributors'] as $contributor) {
106+
$contributor = new Contributor([
107+
'name' => $contributor['name'],
108+
'logo' => $contributor['logo'],
109+
]);
110+
111+
$isValid = $contributor->validate($contributor->toArray());
112+
113+
if (!$isValid) {
114+
Log::error('Contributor validation failed', ['errors' => $contributor->errors()->toArray()]);
115+
}
116+
117+
$currentDetail->contributors()->save($contributor);
118+
}
119+
}
100120
}
101121
});
102122
}

app/Classes/Transformers/OrganisationTransformer.php

Lines changed: 17 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -37,21 +37,37 @@ public function transform(Organisation $model)
3737
'name' => $model->org_name,
3838
'url' => $model->attribution_url,
3939
'imageUrl' => $model->attribution_file_name ? $model->getAttributionImageUrl() : null,
40-
'translations' => null
40+
'translations' => null,
4141
];
4242

4343
if ($model->details->count()) {
4444
$response['translations'] = [];
4545

4646
foreach ($model->details as $detail) {
4747

48+
$model->details->each(function ($detail) {
49+
$detail->load('contributors');
50+
});
51+
4852
if($this->unpublished || $detail->published) {
4953
$response['translations'][$detail->language_code] = [
5054
'languageCode' => $detail->language_code,
5155
'name' => $detail->org_name,
5256
'attributionMessage' => $detail->attribution_message,
5357
];
5458

59+
$response['translations'][$detail->language_code]['contributors'] = [];
60+
if ($detail->contributors->count()) {
61+
62+
$response['translations'][$detail->language_code]['contributors'] = $detail->contributors->map(function ($contributor) {
63+
return [
64+
'id' => $contributor->id,
65+
'name' => $contributor->name,
66+
'logo' => $contributor->logo,
67+
];
68+
});
69+
}
70+
5571
$response['translations'][$detail->language_code]['published'] = (bool) $detail->published;
5672
}
5773
}

app/Classes/Transformers/WhatNowEntityTransformer.php

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -70,20 +70,34 @@ public function transform(WhatNowEntity $model)
7070
'countryCode' => $model->organisation->country_code,
7171
'url' => $model->organisation->attribution_url,
7272
'imageUrl' => $model->organisation->attribution_file_name ? $model->organisation->getAttributionImageUrl() : null,
73-
'translations' => null
73+
'translations' => null,
7474
],
7575
];
7676

7777
if ($model->organisation->details->count()) {
7878
$response['attribution']['translations'] = [];
7979

8080
foreach ($model->organisation->details as $detail) {
81+
$model->organisation->details->each(function ($detail) {
82+
$detail->load('contributors');
83+
});
8184
$response['attribution']['translations'][$detail->language_code] = [
8285
'languageCode' => $detail->language_code,
8386
'name' => $detail->org_name,
8487
'attributionMessage' => $detail->attribution_message,
88+
'contributors' => []
8589
];
8690

91+
if ($detail->contributors->count()) {
92+
$response['attribution']['translations'][$detail->language_code]['contributors'] = $detail->contributors->map(function ($contributor) {
93+
return [
94+
'id' => $contributor->id,
95+
'name' => $contributor->name,
96+
'logo' => $contributor->logo,
97+
];
98+
});
99+
}
100+
87101
$response['attribution']['translations'][$detail->language_code]['published'] = (bool) $detail->published;
88102
}
89103
}

app/Http/Controllers/Controller.php

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,13 @@
77
use Illuminate\Foundation\Validation\ValidatesRequests;
88
use Illuminate\Foundation\Auth\Access\AuthorizesRequests;
99

10+
/**
11+
* @OA\Info(
12+
* title="Whatnow API",
13+
* description="Whatnow API documentation",
14+
* version="1.0.0",
15+
* )
16+
*/
1017
class Controller extends BaseController
1118
{
1219
use AuthorizesRequests, DispatchesJobs, ValidatesRequests;

app/Http/Controllers/OrganisationController.php

Lines changed: 77 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,12 @@
1111
use Illuminate\Support\Facades\Storage;
1212
use League\Fractal\Manager;
1313

14+
/**
15+
* @OA\Tag(
16+
* name="Organisation",
17+
* description="Operations about Organisations"
18+
* )
19+
*/
1420
class OrganisationController extends Controller
1521
{
1622
/**
@@ -101,6 +107,77 @@ public function getById($code, Request $request)
101107
/**
102108
* @param $code
103109
* @return \Symfony\Component\HttpFoundation\Response
110+
*/
111+
/**
112+
* @OA\Put(
113+
* path="/organisation/{code}",
114+
* summary="Update organisation by country code",
115+
* tags={"Organisation"},
116+
* @OA\Parameter(
117+
* name="code",
118+
* in="path",
119+
* description="Country code of the organisation",
120+
* required=true,
121+
* @OA\Schema(type="string")
122+
* ),
123+
* @OA\RequestBody(
124+
* required=true,
125+
* @OA\JsonContent(
126+
* type="object",
127+
* @OA\Property(property="countryCode", type="string", example="USA"),
128+
* @OA\Property(property="name", type="string", example="American Red Cross"),
129+
* @OA\Property(property="url", type="string", nullable=true, example=null),
130+
* @OA\Property(
131+
* property="translations",
132+
* type="array",
133+
* @OA\Items(
134+
* type="object",
135+
* @OA\Property(property="languageCode", type="string", example="en"),
136+
* @OA\Property(property="name", type="string", example="Organization name"),
137+
* @OA\Property(property="attributionMessage", type="string", example="Attribution Message"),
138+
* @OA\Property(property="published", type="boolean", example=true),
139+
* @OA\Property(
140+
* property="contributors",
141+
* type="array",
142+
* @OA\Items(
143+
* type="object",
144+
* @OA\Property(property="name", type="string", example="Contributor name"),
145+
* @OA\Property(property="logo", type="string", example="logo.png")
146+
* )
147+
* )
148+
* )
149+
* )
150+
* )
151+
* ),
152+
* @OA\Response(
153+
* response=200,
154+
* description="Successful response",
155+
* @OA\JsonContent(
156+
* type="object",
157+
* @OA\Property(property="data", type="object")
158+
* )
159+
* ),
160+
* @OA\Response(
161+
* response=404,
162+
* description="Organisation not found",
163+
* @OA\JsonContent(
164+
* type="object",
165+
* @OA\Property(property="status", type="integer", example=404),
166+
* @OA\Property(property="error_message", type="string", example="Organisation does not exist"),
167+
* @OA\Property(property="errors", type="array", @OA\Items(type="string"))
168+
* )
169+
* ),
170+
* @OA\Response(
171+
* response=500,
172+
* description="Organisation could not be updated",
173+
* @OA\JsonContent(
174+
* type="object",
175+
* @OA\Property(property="status", type="integer", example=500),
176+
* @OA\Property(property="error_message", type="string", example="Organisation could not be updated"),
177+
* @OA\Property(property="errors", type="array", @OA\Items(type="string"))
178+
* )
179+
* )
180+
* )
104181
*/
105182
public function putById($code)
106183
{

app/Models/Contributor.php

Lines changed: 75 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,75 @@
1+
<?php
2+
3+
namespace App\Models;
4+
5+
use Illuminate\Database\Eloquent\Model;
6+
use Illuminate\Support\Facades\Validator;
7+
8+
class Contributor extends Model
9+
{
10+
11+
/**
12+
* table
13+
*
14+
* @var string
15+
*/
16+
protected $table = 'contributors';
17+
18+
19+
/**
20+
* @var
21+
*/
22+
protected $errors;
23+
24+
/**
25+
* @var bool
26+
*/
27+
public $timestamps = false;
28+
29+
/**
30+
* The attributes that are mass assignable.
31+
* @var array
32+
*/
33+
protected $fillable = ['name', 'logo', 'org_detail_id'];
34+
35+
/**
36+
* Model validation rules
37+
* @var array
38+
*/
39+
protected $rules = [
40+
'name' => 'required|string|between:2,100',
41+
'logo' => 'string|between:2,2048',
42+
];
43+
44+
45+
/**
46+
* @param $data
47+
* @return bool
48+
*/
49+
public function validate($data)
50+
{
51+
$v = Validator::make($data, $this->rules);
52+
53+
if ($v->fails()) {
54+
$this->errors = $v->errors();
55+
56+
return false;
57+
}
58+
59+
return true;
60+
}
61+
62+
/**
63+
* @return mixed
64+
*/
65+
public function errors()
66+
{
67+
return $this->errors;
68+
}
69+
70+
public function organisation()
71+
{
72+
return $this->belongsTo(OrganisationDetails::class, 'org_detail_id');
73+
}
74+
75+
}

app/Models/OrganisationDetails.php

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,5 +34,10 @@ public function organisation()
3434
{
3535
return $this->belongsTo('App\Models\Details', 'org_id', 'id');
3636
}
37+
38+
public function contributors()
39+
{
40+
return $this->hasMany(Contributor::class, 'org_detail_id');
41+
}
3742
}
3843

composer.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
"license": "MIT",
1010
"require": {
1111
"php": "^7.4",
12+
"darkaonline/l5-swagger": "^8.6",
1213
"fideloper/proxy": "^4.0",
1314
"laravel/framework": "^7.0",
1415
"laravel/helpers": "^1.6",

0 commit comments

Comments
 (0)