From 5d209e8a360c1e91cb04fa3ea68c8928ef56aae1 Mon Sep 17 00:00:00 2001 From: smarcet Date: Sun, 30 Jun 2019 14:49:36 -0300 Subject: [PATCH] Update bookable room field types from float to int ( bc stripes use int) Change-Id: I15d3d9be2e46f9fd253feda94fbedf1db975948d --- ...tRoomReservationValidationRulesFactory.php | 2 +- ...enueBookableRoomValidationRulesFactory.php | 2 +- .../Traits/SummitBookableVenueRoomApi.php | 2 +- .../SummitBookableVenueRoomSerializer.php | 2 +- .../Factories/SummitLocationFactory.php | 2 +- .../SummitRoomReservationFactory.php | 5 +- .../Locations/SummitBookableVenueRoom.php | 14 ++--- .../Locations/SummitRoomReservation.php | 31 +++++------ app/Providers/AppServiceProvider.php | 6 ++- app/Services/Model/SummitLocationService.php | 23 ++++++-- .../model/Version20190629222739.php | 54 +++++++++++++++++++ tests/OAuth2SummitLocationsApiTest.php | 7 +-- 12 files changed, 108 insertions(+), 42 deletions(-) create mode 100644 database/migrations/model/Version20190629222739.php diff --git a/app/Http/Controllers/Apis/Protected/Summit/Factories/SummitRoomReservationValidationRulesFactory.php b/app/Http/Controllers/Apis/Protected/Summit/Factories/SummitRoomReservationValidationRulesFactory.php index bc6315b7..6d4af1c6 100644 --- a/app/Http/Controllers/Apis/Protected/Summit/Factories/SummitRoomReservationValidationRulesFactory.php +++ b/app/Http/Controllers/Apis/Protected/Summit/Factories/SummitRoomReservationValidationRulesFactory.php @@ -28,7 +28,7 @@ final class SummitRoomReservationValidationRulesFactory return [ 'currency' => 'required|string|currency_iso', - 'amount' => 'required|integer', + 'amount' => 'required|integer|greater_than:0', 'start_datetime' => 'required|date_format:U', 'end_datetime' => 'required|date_format:U|after:start_datetime', ]; diff --git a/app/Http/Controllers/Apis/Protected/Summit/Factories/SummitVenueBookableRoomValidationRulesFactory.php b/app/Http/Controllers/Apis/Protected/Summit/Factories/SummitVenueBookableRoomValidationRulesFactory.php index 07596051..3fdb14e5 100644 --- a/app/Http/Controllers/Apis/Protected/Summit/Factories/SummitVenueBookableRoomValidationRulesFactory.php +++ b/app/Http/Controllers/Apis/Protected/Summit/Factories/SummitVenueBookableRoomValidationRulesFactory.php @@ -29,7 +29,7 @@ final class SummitVenueBookableRoomValidationRulesFactory return array_merge([ 'capacity' => 'required|integer:min:0', - 'time_slot_cost' => 'required|numeric', + 'time_slot_cost' => 'required|integer|greater_than:0', 'currency' => 'required|string|currency_iso', ], $rules); } diff --git a/app/Http/Controllers/Apis/Protected/Summit/Traits/SummitBookableVenueRoomApi.php b/app/Http/Controllers/Apis/Protected/Summit/Traits/SummitBookableVenueRoomApi.php index df3d0d22..9f6bc71e 100644 --- a/app/Http/Controllers/Apis/Protected/Summit/Traits/SummitBookableVenueRoomApi.php +++ b/app/Http/Controllers/Apis/Protected/Summit/Traits/SummitBookableVenueRoomApi.php @@ -962,7 +962,7 @@ trait SummitBookableVenueRoomApi if(!Request::isJson()) return $this->error400(); $payload = Input::json()->all(); $rules = [ - 'amount' => 'required|numeric', + 'amount' => 'required|integer|greater_than:0', ]; // Creates a Validator instance and validates the data. diff --git a/app/ModelSerializers/Locations/SummitBookableVenueRoomSerializer.php b/app/ModelSerializers/Locations/SummitBookableVenueRoomSerializer.php index 4b46b748..aa6d5729 100644 --- a/app/ModelSerializers/Locations/SummitBookableVenueRoomSerializer.php +++ b/app/ModelSerializers/Locations/SummitBookableVenueRoomSerializer.php @@ -21,7 +21,7 @@ use ModelSerializers\SerializerRegistry; class SummitBookableVenueRoomSerializer extends SummitVenueRoomSerializer { protected static $array_mappings = [ - 'TimeSlotCost' => 'time_slot_cost:json_float', + 'TimeSlotCost' => 'time_slot_cost:json_int', 'Currency' => 'currency:json_string', ]; diff --git a/app/Models/Foundation/Summit/Factories/SummitLocationFactory.php b/app/Models/Foundation/Summit/Factories/SummitLocationFactory.php index b641df49..4fbf1c95 100644 --- a/app/Models/Foundation/Summit/Factories/SummitLocationFactory.php +++ b/app/Models/Foundation/Summit/Factories/SummitLocationFactory.php @@ -240,7 +240,7 @@ final class SummitLocationFactory self::populateSummitVenueRoom($room, $data); if(isset($data['time_slot_cost'])) - $room->setTimeSlotCost(floatval($data['time_slot_cost'])); + $room->setTimeSlotCost(intval($data['time_slot_cost'])); if(isset($data['currency'])) $room->setCurrency(trim($data['currency'])); diff --git a/app/Models/Foundation/Summit/Factories/SummitRoomReservationFactory.php b/app/Models/Foundation/Summit/Factories/SummitRoomReservationFactory.php index 9819f00a..220ffe98 100644 --- a/app/Models/Foundation/Summit/Factories/SummitRoomReservationFactory.php +++ b/app/Models/Foundation/Summit/Factories/SummitRoomReservationFactory.php @@ -11,7 +11,6 @@ * See the License for the specific language governing permissions and * limitations under the License. **/ - use Eluceo\iCal\Component\Timezone; use models\summit\Summit; use models\summit\SummitRoomReservation; @@ -33,10 +32,10 @@ final class SummitRoomReservationFactory if(isset($data['currency'])) $reservation->setCurrency(trim($data['currency'])); if(isset($data['amount'])) - $reservation->setAmount(floatval($data['amount'])); + $reservation->setAmount(intval($data['amount'])); // dates ( they came on local time epoch , so must be converted to utc using - // summit timezone + // summit timezonefloatval if(isset($data['start_datetime'])) { $val = intval($data['start_datetime']); $val = new \DateTime("@$val"); diff --git a/app/Models/Foundation/Summit/Locations/SummitBookableVenueRoom.php b/app/Models/Foundation/Summit/Locations/SummitBookableVenueRoom.php index 45b91556..f3d3db6b 100644 --- a/app/Models/Foundation/Summit/Locations/SummitBookableVenueRoom.php +++ b/app/Models/Foundation/Summit/Locations/SummitBookableVenueRoom.php @@ -27,8 +27,8 @@ class SummitBookableVenueRoom extends SummitVenueRoom const ClassName = 'SummitBookableVenueRoom'; /** - * @ORM\Column(name="TimeSlotCost", type="decimal") - * @var float + * @ORM\Column(name="TimeSlotCost", type="integer") + * @var int */ private $time_slot_cost; @@ -159,17 +159,17 @@ class SummitBookableVenueRoom extends SummitVenueRoom } /** - * @return float + * @return int */ - public function getTimeSlotCost(): float + public function getTimeSlotCost(): int { - return floatval($this->time_slot_cost); + return $this->time_slot_cost; } /** - * @param float $time_slot_cost + * @param int $time_slot_cost */ - public function setTimeSlotCost(float $time_slot_cost): void + public function setTimeSlotCost(int $time_slot_cost): void { $this->time_slot_cost = $time_slot_cost; } diff --git a/app/Models/Foundation/Summit/Locations/SummitRoomReservation.php b/app/Models/Foundation/Summit/Locations/SummitRoomReservation.php index f458b1ff..4a2c8f6c 100644 --- a/app/Models/Foundation/Summit/Locations/SummitRoomReservation.php +++ b/app/Models/Foundation/Summit/Locations/SummitRoomReservation.php @@ -77,13 +77,13 @@ class SummitRoomReservation extends SilverstripeBaseModel /** * @var float - * @ORM\Column(name="Amount", type="float") + * @ORM\Column(name="Amount", type="integer") */ private $amount; /** * @var float - * @ORM\Column(name="RefundedAmount", type="float") + * @ORM\Column(name="RefundedAmount", type="integer") */ private $refunded_amount; @@ -126,9 +126,9 @@ class SummitRoomReservation extends SilverstripeBaseModel } /** - * @param float $amount + * @param int $amount */ - public function refund(float $amount){ + public function refund(int $amount){ $this->status = self::RefundedStatus; $this->refunded_amount = $amount; Event::fire(new BookableRoomReservationRefundAccepted($this->getId())); @@ -271,17 +271,17 @@ class SummitRoomReservation extends SilverstripeBaseModel } /** - * @return float + * @return int */ - public function getAmount(): float + public function getAmount(): int { return $this->amount; } /** - * @param float $amount + * @param int $amount */ - public function setAmount(float $amount): void + public function setAmount(int $amount): void { $this->amount = $amount; } @@ -289,7 +289,8 @@ class SummitRoomReservation extends SilverstripeBaseModel public function __construct() { parent::__construct(); - $this->amount = 0.0; + $this->amount = 0; + $this->refunded_amount = 0; $this->status = self::ReservedStatus; } @@ -356,19 +357,11 @@ class SummitRoomReservation extends SilverstripeBaseModel } /** - * @return float + * @return int */ - public function getRefundedAmount(): float + public function getRefundedAmount(): int { return $this->refunded_amount; } - /** - * @param float $refunded_amount - */ - public function setRefundedAmount(float $refunded_amount): void - { - $this->refunded_amount = $refunded_amount; - } - } \ No newline at end of file diff --git a/app/Providers/AppServiceProvider.php b/app/Providers/AppServiceProvider.php index 7cb221a7..60a99b8b 100644 --- a/app/Providers/AppServiceProvider.php +++ b/app/Providers/AppServiceProvider.php @@ -614,10 +614,12 @@ class AppServiceProvider extends ServiceProvider $value = trim($value); return isset($currencies[$value]); }); + + Validator::extend('greater_than', function ($attribute, $value, $otherValue) { + return intval($value) > intval($otherValue[0]); + }); } - - /** * Register any application services. * @return void diff --git a/app/Services/Model/SummitLocationService.php b/app/Services/Model/SummitLocationService.php index 55809702..4c73e1ef 100644 --- a/app/Services/Model/SummitLocationService.php +++ b/app/Services/Model/SummitLocationService.php @@ -1717,13 +1717,30 @@ final class SummitLocationService $currency = trim($payload['currency']); if($room->getCurrency() != $currency){ - throw new ValidationException(sprintf("currency set %s is not allowed for room %s", $currency, $room->getId())); + throw new ValidationException + ( + sprintf + ( + "currency set %s is not allowed for room %s", + $currency, + $room->getId() + ) + ); } - $amount = floatval($payload['amount']); + $amount = intval($payload['amount']); if($room->getTimeSlotCost() != $amount){ - throw new ValidationException(sprintf("amount set %s does not match with time slot cost %s for room %s", $currency, $room->getTimeSlotCost(), $room->getId())); + throw new ValidationException + ( + sprintf + ( + "amount set %s does not match with time slot cost %s for room %s", + $amount, + $room->getTimeSlotCost(), + $room->getId() + ) + ); } $reservation = SummitRoomReservationFactory::build($summit, $payload); diff --git a/database/migrations/model/Version20190629222739.php b/database/migrations/model/Version20190629222739.php new file mode 100644 index 00000000..a4939534 --- /dev/null +++ b/database/migrations/model/Version20190629222739.php @@ -0,0 +1,54 @@ +hasTable("SummitRoomReservation")) { + $builder->table('SummitRoomReservation', function (Table $table) { + $table->dropColumn('RefundedAmount'); + $table->integer("RefundedAmount")->setDefault(0)->setNotnull(true); + $table->dropColumn('Amount'); + $table->integer("Amount")->setDefault(0)->setNotnull(true); + }); + } + if($schema->hasTable("SummitBookableVenueRoom")) { + $builder->table('SummitBookableVenueRoom', function (Table $table) { + $table->dropColumn('TimeSlotCost'); + $table->integer("TimeSlotCost")->setDefault(0)->setNotnull(true); + }); + } + } + + /** + * @param Schema $schema + */ + public function down(Schema $schema) + { + + } +} diff --git a/tests/OAuth2SummitLocationsApiTest.php b/tests/OAuth2SummitLocationsApiTest.php index 677dc6b7..143d6a37 100644 --- a/tests/OAuth2SummitLocationsApiTest.php +++ b/tests/OAuth2SummitLocationsApiTest.php @@ -1429,15 +1429,16 @@ final class OAuth2SummitLocationsApiTest extends ProtectedApiTest * @param int $start_date * @return mixed */ - public function testBookableRoomReservation($summit_id =27, $room_id = 922, $start_date = 1572919200, $end_date = 1572922800){ + public function testBookableRoomReservation($summit_id =27, $start_date = 1572919200, $end_date = 1572922800){ + $bookable_room = $this->testAddBookableRoom($summit_id); $params = [ 'id' => $summit_id, - 'room_id' => $room_id, + 'room_id' => $bookable_room->id, ]; $data = [ 'currency' => 'USD', - 'amount' => 325, + 'amount' => 200, 'start_datetime' => $start_date, 'end_datetime' => $end_date, ];