Skip to content

Commit

Permalink
Merge branch 'develop' into RES-1936_sentry
Browse files Browse the repository at this point in the history
# Conflicts:
#	app/Device.php
#	lang/fr-BE/groups.php
#	lang/fr/groups.php
#	tests/Feature/Events/WordpressEventPushTest.php
  • Loading branch information
edwh committed Dec 19, 2023
2 parents 277bd39 + 7e64c29 commit e1133d0
Show file tree
Hide file tree
Showing 95 changed files with 1,098 additions and 554 deletions.
35 changes: 18 additions & 17 deletions app/Device.php
Original file line number Diff line number Diff line change
Expand Up @@ -430,34 +430,34 @@ public static function getItemTypes()
// This is a beast of a query, but the basic idea is to return a list of the categories most commonly
// used by the item types.
//
// ANY_VALUE is used to suppress errors when SQL mode is not set to ONLY_FULL_GROUP_BY.
// MAX is used to suppress errors when SQL mode is not set to ONLY_FULL_GROUP_BY.
//
// This is slow and the results don't change much, so we use a cache.
if (Cache::has('item_types')) {
$types = Cache::get('item_types');
} else {
$types = DB::select(DB::raw("
SELECT item_type,
ANY_VALUE(powered) AS powered,
ANY_VALUE(idcategories) AS idcategories,
ANY_VALUE(categoryname)
SELECT TRIM(item_type) AS item_type,
MAX(powered) AS powered,
MAX(idcategories) AS idcategories,
MAX(categoryname) AS categoryname
FROM (SELECT DISTINCT s.*
FROM (SELECT item_type,
ANY_VALUE(powered) AS powered,
ANY_VALUE(idcategories) AS idcategories,
FROM (SELECT TRIM(item_type) AS item_type,
MAX(powered) AS powered,
MAX(idcategories) AS idcategories,
categories.name AS categoryname,
COUNT(*) AS count
FROM devices
INNER JOIN categories
ON devices.category = categories.idcategories
WHERE item_type IS NOT NULL
GROUP BY categoryname,
item_type) s
JOIN (SELECT item_type,
UPPER(item_type)) s
JOIN (SELECT TRIM(item_type) AS item_type,
MAX(count) AS maxcount
FROM (SELECT item_type AS item_type,
ANY_VALUE(powered) AS powered,
ANY_VALUE(idcategories) AS idcategories,
FROM (SELECT TRIM(item_type) AS item_type,
MAX(powered) AS powered,
MAX(idcategories) AS idcategories,
categories.name AS categoryname,
COUNT(*) AS count
FROM devices
Expand All @@ -466,11 +466,12 @@ public static function getItemTypes()
categories.idcategories
WHERE item_type IS NOT NULL
GROUP BY categoryname,
item_type) s
GROUP BY s.item_type) AS m
ON s.item_type = m.item_type
UPPER(item_type)) s
GROUP BY UPPER(s.item_type)) AS m
ON UPPER(s.item_type) = UPPER(m.item_type)
AND s.count = m.maxcount) t
GROUP BY t.item_type
GROUP BY UPPER(t.item_type)
HAVING LENGTH(item_type) > 0
"));
\Cache::put('item_types', $types, 24 * 3600);
}
Expand Down
1 change: 1 addition & 0 deletions app/Events/ApproveGroup.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ class ApproveGroup
use Dispatchable, InteractsWithSockets, SerializesModels;

public $group;
public $data;

/**
* Create a new event instance.
Expand Down
1 change: 1 addition & 0 deletions app/Events/EditEvent.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ class EditEvent
use Dispatchable, InteractsWithSockets, SerializesModels;

public $party;
public $data;

/**
* Create a new event instance.
Expand Down
1 change: 1 addition & 0 deletions app/Events/EditGroup.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ class EditGroup
use Dispatchable, InteractsWithSockets, SerializesModels;

public $group;
public $data;

/**
* Create a new event instance.
Expand Down
9 changes: 6 additions & 3 deletions app/Http/Controllers/API/EventController.php
Original file line number Diff line number Diff line change
Expand Up @@ -548,7 +548,7 @@ public function createEventv2(Request $request)
* operationId="editEvent",
* tags={"Events"},
* summary="Edit Event",
* description="Edits an event.",
* description="Edits an event. The event of a group cannot be changed after creation.",
* @OA\Parameter(
* name="api_token",
* description="A valid user API token",
Expand Down Expand Up @@ -621,6 +621,10 @@ public function updateEventv2(Request $request, $idEvents)
false
);

if (!Fixometer::userHasEditPartyPermission($idEvents, $user->id)) {
abort(403);
}

// Convert the timezone to UTC, because the timezone is not itself stored in the DB.
$event_start_utc = Carbon::parse($start)->setTimezone('UTC')->toIso8601String();
$event_end_utc = Carbon::parse($end)->setTimezone('UTC')->toIso8601String();
Expand All @@ -645,8 +649,7 @@ public function updateEventv2(Request $request, $idEvents)
$party = Party::findOrFail($idEvents);
$party->update($update);

// REVIEW: In the old code we wouldn't generate EditEvent in the approval case. But I think that was a bug.
if ($request->has('moderate') && $request->input('moderate') == 'approve') {
if ($request->has('moderate') && $request->input('moderate') == 'approve' && Fixometer::userCanApproveEvent($idEvents, $user->id)) {
$party->approve();
}

Expand Down
21 changes: 0 additions & 21 deletions app/Http/Controllers/API/GroupController.php
Original file line number Diff line number Diff line change
Expand Up @@ -224,27 +224,6 @@ public static function getGroupList()
return response()->json($groups);
}

public static function getEventsForGroup(Request $request, Group $group)
{
// Used by old JS client.
$group = $group->load('parties');

$events = $group->parties->sortByDesc('event_start_utc');

if ($request->has('format') && $request->input('format') == 'location') {
$events = $events->map(function ($event) {
return (object) [
'id' => $event->idevents,
'location' => $event->FriendlyLocation,
];
});
}

return response()->json([
'events' => $events->values()->toJson(),
]);
}

/**
* @OA\Get(
* path="/api/v2/groups/names",
Expand Down
54 changes: 54 additions & 0 deletions app/Http/Controllers/API/ItemController.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
<?php

namespace App\Http\Controllers\API;

use App\Device;
use App\Http\Controllers\Controller;
use App\Http\Resources\ItemCollection;
use Auth;
use Carbon\Carbon;
use Illuminate\Auth\AuthenticationException;
use Illuminate\Database\QueryException;
use Illuminate\Http\Request;
use Notification;
use Illuminate\Validation\ValidationException;

class ItemController extends Controller
{
/**
* @OA\Get(
* path="/api/v2/items",
* operationId="listItemsv2",
* tags={"Items"},
* summary="Get list of items",
* @OA\Response(
* response=200,
* description="Successful operation",
* @OA\JsonContent(
* @OA\Property(
* property="data",
* title="data",
* description="An array of items",
* type="array",
* @OA\Items(
* ref="#/components/schemas/Item"
* )
* )
* )
* ),
* )
*/
public static function listItemsv2(Request $request) {
// Item types don't change often, so we can cache them.
if (\Cache::has('item_types')) {
$items = \Cache::get('item_types');
} else {
$items = Device::getItemTypes();
\Cache::put('item_types', $items, 7200);
}

return [
'data' => ItemCollection::make($items)
];
}
}
78 changes: 42 additions & 36 deletions app/Http/Controllers/ApiController.php
Original file line number Diff line number Diff line change
Expand Up @@ -50,44 +50,50 @@ public static function homepage_data()
{
$result = [];

$Device = new Device;

$allparties = Party::past()->get();

$participants = 0;
$hours_volunteered = 0;

foreach ($allparties as $party) {
$participants += $party->pax;

$hours_volunteered += $party->hoursVolunteered();
if (\Cache::has('homepage_data')) {
$result = \Cache::get('homepage_data');
} else {
$Device = new Device;

$allparties = Party::past()->get();

$participants = 0;
$hours_volunteered = 0;

foreach ($allparties as $party) {
$participants += $party->pax;

$hours_volunteered += $party->hoursVolunteered();
}

$result['participants'] = $participants;
$result['hours_volunteered'] = $hours_volunteered;
$fixed = $Device->statusCount();
$result['items_fixed'] = count($fixed) ? $fixed[0]->counter : 0;

$stats = \App\Helpers\LcaStats::getWasteStats();
$result['waste_powered'] = round($stats[0]->powered_waste);
$result['waste_unpowered'] = round($stats[0]->unpowered_waste);
$result['waste_total'] = round($stats[0]->powered_waste + $stats[0]->unpowered_waste);
$result['co2_powered'] = round($stats[0]->powered_footprint);
$result['co2_unpowered'] = round($stats[0]->unpowered_footprint);
$result['co2_total'] = round($stats[0]->powered_footprint + $stats[0]->unpowered_footprint);

$devices = new Device;
$result['fixed_powered'] = $devices->fixedPoweredCount();
$result['fixed_unpowered'] = $devices->fixedUnpoweredCount();
$result['total_powered'] = $devices->poweredCount();
$result['total_unpowered'] = $devices->unpoweredCount();

// for backward compatibility (don't break therestartproject.org)
$result['weights'] = round($result['waste_total']);
$result['ewaste'] = round($result['waste_powered']);
$result['unpowered_waste'] = round($result['waste_unpowered']);
$result['emissions'] = round($result['co2_total']);

\Cache::put('homepage_data', $result, 43200);
}

$result['participants'] = $participants;
$result['hours_volunteered'] = $hours_volunteered;
$fixed = $Device->statusCount();
$result['items_fixed'] = count($fixed) ? $fixed[0]->counter : 0;

$stats = \App\Helpers\LcaStats::getWasteStats();
$result['waste_powered'] = round($stats[0]->powered_waste);
$result['waste_unpowered'] = round($stats[0]->unpowered_waste);
$result['waste_total'] = round($stats[0]->powered_waste + $stats[0]->unpowered_waste);
$result['co2_powered'] = round($stats[0]->powered_footprint);
$result['co2_unpowered'] = round($stats[0]->unpowered_footprint);
$result['co2_total'] = round($stats[0]->powered_footprint + $stats[0]->unpowered_footprint);

$devices = new Device;
$result['fixed_powered'] = $devices->fixedPoweredCount();
$result['fixed_unpowered'] = $devices->fixedUnpoweredCount();
$result['total_powered'] = $devices->poweredCount();
$result['total_unpowered'] = $devices->unpoweredCount();

// for backward compatibility (don't break therestartproject.org)
$result['weights'] = round($result['waste_total']);
$result['ewaste'] = round($result['waste_powered']);
$result['unpowered_waste'] = round($result['waste_unpowered']);
$result['emissions'] = round($result['co2_total']);

return response()
->json($result, 200);
}
Expand Down
13 changes: 6 additions & 7 deletions app/Http/Controllers/DeviceController.php
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,6 @@ public function index($search = null)
'clusters' => $clusters,
'barriers' => \App\Helpers\Fixometer::allBarriers(),
'brands' => $brands,
'item_types' => Device::getItemTypes(),
]);
}

Expand Down Expand Up @@ -170,10 +169,10 @@ public function ajaxCreate(Request $request)
event(new DeviceCreatedOrUpdated($device[$i]));

// Update barriers
if (isset($barrier) && ! empty($barrier) && $repair_status == 3) { // Only sync when repair status is end-of-life
if (isset($barrier) && ! empty($barrier) && $repair_status == Device::REPAIR_STATUS_ENDOFLIFE) {
Device::find($device[$i]->iddevices)->barriers()->sync($barrier);
} else {
Device::find($device[$i]->iddevices)->barriers()->sync([]);
Device::find($device[$i]->iddevices)->barriers()->sync([]);
}

// If the number of devices exceeds set amount then show the following message
Expand All @@ -195,8 +194,8 @@ public function ajaxCreate(Request $request)

$barriers = [];

foreach ($device[$i]->barriers as $barrier) {
$barriers[] = $barrier->id;
foreach ($device[$i]->barriers as $b) {
$barriers[] = $b->id;
}

$device[$i]->barrier = $barriers;
Expand Down Expand Up @@ -340,8 +339,8 @@ public function ajaxEdit(Request $request, $id)

$barriers = [];

foreach ($device->barriers as $barrier) {
$barriers[] = $barrier->id;
foreach ($device->barriers as $b) {
$barriers[] = $b->id;
}

$device->barrier = $barriers;
Expand Down
Loading

0 comments on commit e1133d0

Please sign in to comment.