added table of co-woeker requests and new features 2
This commit is contained in:
@@ -36,8 +36,19 @@ class MassageController extends Controller
|
||||
throw new UnprocessableEntityHttpException('در هر درخواست حداکثر ۱۰ شماره مجاز است.');
|
||||
}
|
||||
|
||||
// یافتن مشتریان با شمارههای داده شده
|
||||
$customers = Customer::whereIn('mobile', $numbers)->get();
|
||||
// یافتن مشتریان با شمارههای داده شده — فقط مشتریان پنل
|
||||
$customers = Customer::whereIn('mobile', $numbers)
|
||||
->where('type', 2)
|
||||
->where('is_archive', false)
|
||||
->get();
|
||||
|
||||
$known = $customers->pluck('mobile')->map(static fn ($m) => (string) $m)->all();
|
||||
$unknown = array_values(array_diff($numbers, $known));
|
||||
if ($unknown !== []) {
|
||||
throw new UnprocessableEntityHttpException(
|
||||
'فقط به مشتریان پنل میتوان پیامک گروهی فرستاد. شماره خارج از لیست مشتری مجاز نیست.'
|
||||
);
|
||||
}
|
||||
|
||||
$filter = app(SmsProfanityFilter::class);
|
||||
$filter->assertClean((string) ($request->message ?? ''));
|
||||
@@ -50,15 +61,6 @@ class MassageController extends Controller
|
||||
$this->send($customer->mobile, $message);
|
||||
}
|
||||
|
||||
// شمارههای دستی که مشتری در DB ندارند
|
||||
$known = $customers->pluck('mobile')->all();
|
||||
foreach ($numbers as $mobile) {
|
||||
if (in_array($mobile, $known, true)) {
|
||||
continue;
|
||||
}
|
||||
$this->send($mobile, (string) $request->message);
|
||||
}
|
||||
|
||||
return response()->json(true);
|
||||
}
|
||||
|
||||
@@ -134,49 +136,36 @@ class MassageController extends Controller
|
||||
'function' => 'getActiveCustomers',
|
||||
'name' => 'کاربران فعال',
|
||||
],
|
||||
[
|
||||
'function' => 'getInactiveLast1Month',
|
||||
'name' => 'یک ماه اخیر وارد نشده',
|
||||
],
|
||||
[
|
||||
'function' => 'getInactiveLast3Months',
|
||||
'name' => 'سه ماه اخیر وارد نشده',
|
||||
],
|
||||
[
|
||||
'function' => 'getInactiveLast6Months',
|
||||
'name' => 'شش ماه گذشته وارد نشده',
|
||||
],
|
||||
[
|
||||
'function' => 'getInactiveLast1Year',
|
||||
'name' => 'سال گذشته وارد نشده',
|
||||
],
|
||||
]);
|
||||
}
|
||||
|
||||
public function getCustomerList(Request $request)
|
||||
{
|
||||
$customers = [];
|
||||
foreach ($request->functions as $action) {
|
||||
if ($action === 'getAllCustomers') {
|
||||
$customers = array_merge($customers, $this->getAllCustomers());
|
||||
}
|
||||
if ($action === 'getNoLoginCustomers') {
|
||||
$customers = array_merge($customers, $this->getNoLoginCustomers());
|
||||
}
|
||||
if ($action === 'getNoValidCustomers') {
|
||||
$customers = array_merge($customers, $this->getNoValidCustomers());
|
||||
}
|
||||
if ($action === 'getActiveCustomers') {
|
||||
$customers = array_merge($customers, $this->getActiveCustomers());
|
||||
}
|
||||
}
|
||||
// حذف شمارههای تکراری
|
||||
$customers = $this->resolveCustomersByFunctions((array) ($request->functions ?? []));
|
||||
$uniqueCustomers = $this->removeDuplicateNumbers($customers);
|
||||
|
||||
return response()->json(['customers' => $uniqueCustomers]);
|
||||
}
|
||||
|
||||
public function getCustomerListTable(Request $request)
|
||||
{
|
||||
$customers = [];
|
||||
foreach ($request->functions as $action) {
|
||||
if ($action === 'getAllCustomers') {
|
||||
$customers = array_merge($customers, $this->getAllCustomers());
|
||||
}
|
||||
if ($action === 'getNoLoginCustomers') {
|
||||
$customers = array_merge($customers, $this->getNoLoginCustomers());
|
||||
}
|
||||
if ($action === 'getNoValidCustomers') {
|
||||
$customers = array_merge($customers, $this->getNoValidCustomers());
|
||||
}
|
||||
if ($action === 'getActiveCustomers') {
|
||||
$customers = array_merge($customers, $this->getActiveCustomers());
|
||||
}
|
||||
}
|
||||
// Remove duplicate numbers
|
||||
$customers = $this->resolveCustomersByFunctions((array) ($request->functions ?? []));
|
||||
$uniqueCustomers = $this->removeDuplicateNumbers($customers);
|
||||
$uniqueCustomersCollection = collect($uniqueCustomers);
|
||||
|
||||
@@ -193,6 +182,30 @@ class MassageController extends Controller
|
||||
|
||||
}
|
||||
|
||||
/**
|
||||
* @param list<string> $actions
|
||||
* @return list<array{id:mixed,name:mixed,mobile:mixed}>
|
||||
*/
|
||||
private function resolveCustomersByFunctions(array $actions): array
|
||||
{
|
||||
$customers = [];
|
||||
foreach ($actions as $action) {
|
||||
$customers = array_merge($customers, match ($action) {
|
||||
'getAllCustomers' => $this->getAllCustomers(),
|
||||
'getNoLoginCustomers' => $this->getNoLoginCustomers(),
|
||||
'getNoValidCustomers' => $this->getNoValidCustomers(),
|
||||
'getActiveCustomers' => $this->getActiveCustomers(),
|
||||
'getInactiveLast1Month' => $this->getInactiveSinceMonths(1),
|
||||
'getInactiveLast3Months' => $this->getInactiveSinceMonths(3),
|
||||
'getInactiveLast6Months' => $this->getInactiveSinceMonths(6),
|
||||
'getInactiveLast1Year' => $this->getInactiveSinceMonths(12),
|
||||
default => [],
|
||||
});
|
||||
}
|
||||
|
||||
return $customers;
|
||||
}
|
||||
|
||||
private function getAllCustomers()
|
||||
{
|
||||
return Customer::where('type', 2)
|
||||
@@ -231,6 +244,25 @@ class MassageController extends Controller
|
||||
->toArray();
|
||||
}
|
||||
|
||||
/**
|
||||
* مشتریان خانگی که در N ماه اخیر وارد نشدهاند (یا هرگز وارد نشدهاند).
|
||||
*
|
||||
* @return list<array{id:mixed,name:mixed,mobile:mixed}>
|
||||
*/
|
||||
private function getInactiveSinceMonths(int $months): array
|
||||
{
|
||||
$cutoff = now()->subMonths($months);
|
||||
|
||||
return Customer::where('type', 2)
|
||||
->where('is_archive', false)
|
||||
->where(function ($q) use ($cutoff) {
|
||||
$q->whereNull('last_seen_at')
|
||||
->orWhere('last_seen_at', '<', $cutoff);
|
||||
})
|
||||
->get(['id', 'name', 'mobile'])
|
||||
->toArray();
|
||||
}
|
||||
|
||||
private function removeDuplicateNumbers(array $customers)
|
||||
{
|
||||
$uniqueCustomers = [];
|
||||
|
||||
Reference in New Issue
Block a user