Skip to content

Commit b6ad504

Browse files
committed
Update
- Fixed: creating and updating coupon - Added: new tests
1 parent b7b7f27 commit b6ad504

File tree

8 files changed

+74
-22
lines changed

8 files changed

+74
-22
lines changed

app/Crud/CouponCrud.php

Lines changed: 13 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -433,19 +433,19 @@ public function get( $param )
433433
* @param object entry
434434
* @return void
435435
*/
436-
public function beforePut( $request, $entry )
436+
public function beforePut( $fields, $entry )
437437
{
438438
if ( $this->permissions[ 'update' ] !== false ) {
439439
ns()->restrict( $this->permissions[ 'update' ] );
440440

441-
foreach( $request->input( 'selected_products.products' ) as $product_id ) {
441+
foreach( $fields[ 'products' ] as $product_id ) {
442442
$product = Product::find( $product_id );
443443
if ( ! $product instanceof Product ) {
444444
throw new Exception( __( 'Unable to save the coupon product as this product doens\'t exists.' ) );
445445
}
446446
}
447447

448-
foreach( $request->input( 'selected_categories.categories' ) as $category_id ) {
448+
foreach( $fields[ 'categories' ] as $category_id ) {
449449
$category = ProductCategory::find( $category_id );
450450
if ( ! $category instanceof ProductCategory ) {
451451
throw new Exception( __( 'Unable to save the coupon category as this category doens\'t exists.' ) );
@@ -455,7 +455,7 @@ public function beforePut( $request, $entry )
455455
throw new NotAllowedException;
456456
}
457457

458-
return $request;
458+
return $fields;
459459
}
460460

461461
/**
@@ -464,21 +464,21 @@ public function beforePut( $request, $entry )
464464
* @param object entry
465465
* @return void
466466
*/
467-
public function afterPut( $request, $coupon )
467+
public function afterPut( $fields, $coupon )
468468
{
469-
$coupon->categories->each( function( $category ) use ( $request ) {
470-
if ( ! in_array( $category->category_id, $request->input( 'selected_categories.categories' ) ) ) {
469+
$coupon->categories->each( function( $category ) use ( $fields ) {
470+
if ( ! in_array( $category->category_id, $fields[ 'categories' ] ) ) {
471471
$category->delete();
472472
}
473473
});
474474

475-
$coupon->products->each( function( $product ) use ( $request ) {
476-
if ( ! in_array( $product->product_id, $request->input( 'selected_products.products' ) ) ) {
475+
$coupon->products->each( function( $product ) use ( $fields ) {
476+
if ( ! in_array( $product->product_id, $fields[ 'products' ] ) ) {
477477
$product->delete();
478478
}
479479
});
480480

481-
foreach( $request->input( 'selected_products.products' ) as $product_id ) {
481+
foreach( $fields[ 'products' ] as $product_id ) {
482482
$productRelation = CouponProduct::where( 'coupon_id', $coupon->id )
483483
->where( 'product_id', $product_id )
484484
->first();
@@ -492,7 +492,7 @@ public function afterPut( $request, $coupon )
492492
$productRelation->save();
493493
}
494494

495-
foreach( $request->input( 'selected_categories.categories' ) as $category_id ) {
495+
foreach( $fields[ 'categories' ] as $category_id ) {
496496
$categoryRelation = CouponCategory::where( 'coupon_id', $coupon->id )
497497
->where( 'category_id', $category_id )
498498
->first();
@@ -510,9 +510,9 @@ public function afterPut( $request, $coupon )
510510
* @var CustomerService
511511
*/
512512
$customersService = app()->make( CustomerService::class );
513-
$customersService->setCoupon( $request->all(), $coupon );
513+
$customersService->setCoupon( $fields, $coupon );
514514

515-
return $request;
515+
return $fields;
516516
}
517517

518518
/**

app/Services/CashRegistersService.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@ public function openRegister( Register $register, $amount, $description )
2929
);
3030
}
3131

32-
$registerHistory = new RegisterHistory;
32+
$registerHistory = new RegisterHistory;
3333
$registerHistory->register_id = $register->id;
3434
$registerHistory->action = RegisterHistory::ACTION_OPENING;
3535
$registerHistory->author = Auth::id();
@@ -73,7 +73,7 @@ public function closeRegister( Register $register, $amount, $description )
7373
);
7474
}
7575

76-
$registerHistory = new RegisterHistory;
76+
$registerHistory = new RegisterHistory;
7777
$registerHistory->register_id = $register->id;
7878
$registerHistory->action = RegisterHistory::ACTION_CLOSING;
7979
$registerHistory->author = Auth::id();

public/js/pos-init.min.js

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

public/mix-manifest.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
"/js/dashboard.min.js": "/js/dashboard.min.js?id=d84adbf74bac6693a9bc63d716a2ec4a",
66
"/js/cashier.min.js": "/js/cashier.min.js?id=1177672fac425ea64d2adf3cf92110fc",
77
"/js/update.min.js": "/js/update.min.js?id=359c7559d2e051df01ab3d8b1e8456bc",
8-
"/js/pos-init.min.js": "/js/pos-init.min.js?id=264dc8e29e9ac89967ef3699b5d0ba5a",
8+
"/js/pos-init.min.js": "/js/pos-init.min.js?id=4c01ea93e535d15a73de37dfa37c4749",
99
"/js/pos.min.js": "/js/pos.min.js?id=cf4204ed94965b0bb1128d38d7a8ef45",
1010
"/js/auth.min.js": "/js/auth.min.js?id=7acb5663107a1dedc67cdb4eb5a1e1ab",
1111
"/js/setup.min.js": "/js/setup.min.js?id=666d58849dcf16f6a5d892ecadde8a0f",

resources/ts/popups/ns-pos-cash-registers-action-popup.vue

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,9 +6,9 @@
66
<div><ns-close-button @click="close()"></ns-close-button></div>
77
</div>
88
<div class="p-2">
9-
<div v-if="settings !== null && settings.register" class="mb-2 p-3 bg-gray-400 font-bold text-white text-right flex justify-between">
9+
<div v-if="register !== null" class="mb-2 p-3 bg-gray-400 font-bold text-white text-right flex justify-between">
1010
<span>{{ __( 'Balance' ) }} </span>
11-
<span>{{ settings.register.balance | currency }}</span>
11+
<span>{{ register.balance | currency }}</span>
1212
</div>
1313
<div class="mb-2 p-3 bg-green-400 font-bold text-white text-right flex justify-between">
1414
<span>{{ __( 'Input' ) }}</span>
@@ -44,6 +44,7 @@ export default {
4444
settingsSubscription: null,
4545
settings: null,
4646
action: null,
47+
register: null,
4748
loaded: false,
4849
register_id: null, // conditionnally provider
4950
validation: new FormValidation,
@@ -53,6 +54,7 @@ export default {
5354
mounted() {
5455
this.title = this.$popupParams.title;
5556
this.identifier = this.$popupParams.identifier;
57+
this.register = this.$popupParams.register;
5658
this.action = this.$popupParams.action;
5759
this.register_id = this.$popupParams.register_id;
5860
this.settingsSubscription = POS.settings.subscribe( settings => {

resources/ts/popups/ns-pos-cash-registers-options-popup.vue

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -45,13 +45,14 @@ export default {
4545
});
4646
},
4747
48-
async closeCashRegister() {
48+
async closeCashRegister( register ) {
4949
try {
5050
const response = await new Promise( ( resolve, reject ) => {
5151
Popup.show( nsPosCashRegistersActionPopupVue, {
5252
title: __( 'Close Register' ),
5353
action: 'close',
5454
identifier: 'ns.cash-registers-closing',
55+
register,
5556
resolve,
5657
reject
5758
})
@@ -158,7 +159,7 @@ export default {
158159
</div>
159160
</div>
160161
<div class="grid grid-cols-2 text-gray-700">
161-
<div @click="closeCashRegister()" class="hover:bg-teal-100 border-gray-200 border-r border-b py-4 cursor-pointer px-2 flex items-center justify-center flex-col">
162+
<div @click="closeCashRegister( register )" class="hover:bg-teal-100 border-gray-200 border-r border-b py-4 cursor-pointer px-2 flex items-center justify-center flex-col">
162163
<i class="las la-sign-out-alt text-6xl"></i>
163164
<h3 class="text-xl font-bold">{{ __( 'Close' ) }}</h3>
164165
</div>

tests/Feature/CreateCouponTest.php

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,4 +27,15 @@ public function testCreateCoupon()
2727
$this->attemptAuthenticate();
2828
$this->attemptCreatecoupon();
2929
}
30+
31+
/**
32+
* Let's now try to update the coupon.
33+
*
34+
* @return void
35+
*/
36+
public function testUpdateCoupon()
37+
{
38+
$this->attemptAuthenticate();
39+
$this->attemptUpdateCoupon();
40+
}
3041
}

tests/Traits/WithCouponTest.php

Lines changed: 39 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,9 @@
11
<?php
22
namespace Tests\Traits;
33

4+
use App\Models\Coupon;
45
use App\Models\Product;
6+
use App\Models\ProductCategory;
57
use Illuminate\Foundation\Testing\WithFaker;
68

79
trait WithCouponTest
@@ -26,7 +28,43 @@ protected function attemptCreatecoupon()
2628
->toArray()
2729
],
2830
'selected_categories' => [
29-
'categories' => Product::select( 'id' )
31+
'categories' => ProductCategory::select( 'id' )
32+
->get()
33+
->map( fn( $product ) => $product->id )
34+
->toArray()
35+
]
36+
]);
37+
38+
$response->assertJsonPath( 'status', 'success' );
39+
}
40+
41+
protected function attemptUpdateCoupon()
42+
{
43+
/**
44+
* just in case the function executes before
45+
* the coupon creation.
46+
*/
47+
$this->attemptCreatecoupon();
48+
49+
$coupon = Coupon::first();
50+
51+
$response = $this->withSession( $this->app[ 'session' ]->all() )
52+
->json( 'put', 'api/nexopos/v4/crud/ns.coupons/' . $coupon->id, [
53+
'name' => $this->faker->name,
54+
'general' => [
55+
'type' => 'percentage_discount',
56+
'code' => 'cp-' . $this->faker->numberBetween(0,9) . $this->faker->numberBetween(0,9),
57+
'discount_value' => $this->faker->randomElement([ 10, 15, 20, 25 ]),
58+
'limit_usage' => $this->faker->randomElement([ 100, 200, 400 ]),
59+
],
60+
'selected_products' => [
61+
'products' => Product::select( 'id' )
62+
->get()
63+
->map( fn( $product ) => $product->id )
64+
->toArray()
65+
],
66+
'selected_categories' => [
67+
'categories' => ProductCategory::select( 'id' )
3068
->get()
3169
->map( fn( $product ) => $product->id )
3270
->toArray()

0 commit comments

Comments
 (0)