2
2
3
3
namespace Webkul \Admin \Http \Controllers \Contact \Persons ;
4
4
5
+ use Exception ;
5
6
use Illuminate \Http \JsonResponse ;
6
7
use Illuminate \Http \RedirectResponse ;
7
8
use Illuminate \Http \Resources \Json \JsonResource ;
@@ -138,17 +139,27 @@ public function destroy(int $id): JsonResponse
138
139
{
139
140
$ person = $ this ->personRepository ->findOrFail ($ id );
140
141
142
+ if (
143
+ $ person ->leads
144
+ && $ person ->leads ->count () > 0
145
+ ) {
146
+ return response ()->json ([
147
+ 'message ' => trans ('admin::app.contacts.persons.index.delete-failed ' ),
148
+ ], 400 );
149
+ }
150
+
141
151
try {
142
- Event::dispatch ('contacts.person.delete.before ' , $ id );
152
+ Event::dispatch ('contacts.person.delete.before ' , $ person );
143
153
144
- $ person ->delete ($ id );
154
+ $ person ->delete ();
145
155
146
- Event::dispatch ('contacts.person.delete.after ' , $ id );
156
+ Event::dispatch ('contacts.person.delete.after ' , $ person );
147
157
148
158
return response ()->json ([
149
159
'message ' => trans ('admin::app.contacts.persons.index.delete-success ' ),
150
160
], 200 );
151
- } catch (\Exception $ exception ) {
161
+
162
+ } catch (Exception $ exception ) {
152
163
return response ()->json ([
153
164
'message ' => trans ('admin::app.contacts.persons.index.delete-failed ' ),
154
165
], 400 );
@@ -158,20 +169,43 @@ public function destroy(int $id): JsonResponse
158
169
/**
159
170
* Mass Delete the specified resources.
160
171
*/
161
- public function massDestroy (MassDestroyRequest $ massDestroyRequest ): JsonResponse
172
+ public function massDestroy (MassDestroyRequest $ request ): JsonResponse
162
173
{
163
- $ persons = $ this ->personRepository ->findWhereIn ('id ' , $ massDestroyRequest ->input ('indices ' ));
174
+ try {
175
+ $ persons = $ this ->personRepository ->findWhereIn ('id ' , $ request ->input ('indices ' , []));
164
176
165
- foreach ($ persons as $ person ) {
166
- Event::dispatch ('contact.person.delete.before ' , $ person );
177
+ $ notDeleted = [];
167
178
168
- $ this ->personRepository ->delete ($ person ->id );
179
+ foreach ($ persons as $ person ) {
180
+ if (
181
+ $ person ->leads
182
+ && $ person ->leads ->count () > 0
183
+ ) {
184
+ $ notDeleted [] = $ person ->name ?? "ID: {$ person ->id }" ;
169
185
170
- Event::dispatch ('contact.person.delete.after ' , $ person );
171
- }
186
+ continue ;
187
+ }
188
+
189
+ Event::dispatch ('contact.person.delete.before ' , $ person );
172
190
173
- return response ()->json ([
174
- 'message ' => trans ('admin::app.contacts.persons.index.delete-success ' ),
175
- ]);
191
+ $ this ->personRepository ->delete ($ person ->id );
192
+
193
+ Event::dispatch ('contact.person.delete.after ' , $ person );
194
+ }
195
+
196
+ $ message = trans ('admin::app.contacts.persons.index.delete-success ' );
197
+
198
+ if (! empty ($ notDeleted )) {
199
+ $ message .= ' ' .trans ('admin::app.contacts.persons.index.delete-partial-warning ' , [
200
+ 'persons ' => implode (', ' , $ notDeleted ),
201
+ ]);
202
+ }
203
+
204
+ return response ()->json (['message ' => $ message ]);
205
+ } catch (Exception $ exception ) {
206
+ return response ()->json ([
207
+ 'message ' => trans ('admin::app.contacts.persons.index.delete-failed ' ),
208
+ ], 400 );
209
+ }
176
210
}
177
211
}
0 commit comments