add test batches

This commit is contained in:
root
2026-06-08 23:45:55 -04:00
parent c792b8be05
commit 8d4d610b82
1480 changed files with 22587 additions and 10762 deletions
@@ -8,7 +8,9 @@ use Illuminate\Support\Facades\Log;
class WhatsappContactService
{
public function __construct(private WhatsappContextService $context) {}
public function __construct(private WhatsappContextService $context)
{
}
public function listParentContacts(?string $schoolYear, ?string $semester): array
{
@@ -16,7 +18,7 @@ class WhatsappContactService
$semester = trim((string) $semester);
$builder = DB::table('parents as sp');
$builder->select('
$builder->select("
sp.id AS parents_row_id,
sp.firstparent_id AS firstparent_id,
sp.secondparent_firstname AS sp_firstname,
@@ -32,7 +34,7 @@ class WhatsappContactService
u.cellphone AS u_phone,
u.school_year AS u_school_year,
u.semester AS u_semester
');
");
$builder->leftJoin('users as u', 'u.id', '=', 'sp.firstparent_id');
if ($schoolYear !== '') {
@@ -47,7 +49,7 @@ class WhatsappContactService
$contacts = [];
foreach ($rows as $row) {
$r = (array) $row;
if (! empty($r['u_id'])) {
if (!empty($r['u_id'])) {
$contacts[] = [
'firstname' => (string) ($r['u_firstname'] ?? ''),
'lastname' => (string) ($r['u_lastname'] ?? ''),
@@ -57,7 +59,7 @@ class WhatsappContactService
];
}
$hasSecond = ! empty($r['sp_firstname']) || ! empty($r['sp_lastname']) || ! empty($r['sp_email']) || ! empty($r['sp_phone']);
$hasSecond = !empty($r['sp_firstname']) || !empty($r['sp_lastname']) || !empty($r['sp_email']) || !empty($r['sp_phone']);
if ($hasSecond) {
$contacts[] = [
'firstname' => (string) ($r['sp_firstname'] ?? ''),
@@ -70,9 +72,8 @@ class WhatsappContactService
}
$contacts = array_values(array_filter($contacts, function ($c) {
$name = trim(($c['firstname'] ?? '').($c['lastname'] ?? ''));
$contact = trim(($c['phone'] ?? '').($c['email'] ?? ''));
$name = trim(($c['firstname'] ?? '') . ($c['lastname'] ?? ''));
$contact = trim(($c['phone'] ?? '') . ($c['email'] ?? ''));
return $name !== '' || $contact !== '';
}));
@@ -89,7 +90,6 @@ class WhatsappContactService
}
$ta = ($a['type'] ?? '') === 'Primary Parent' ? 0 : 1;
$tb = ($b['type'] ?? '') === 'Primary Parent' ? 0 : 1;
return $ta <=> $tb;
});
@@ -115,7 +115,7 @@ class WhatsappContactService
))));
$namesById = [];
if (! empty($sectionIds)) {
if (!empty($sectionIds)) {
$nameRows = DB::table('classSection')
->select('class_section_id', 'class_section_name')
->whereIn('class_section_id', $sectionIds)
@@ -130,13 +130,13 @@ class WhatsappContactService
$classSections = [];
foreach ($sections as $s) {
$sid = (int) ($s->class_section_id ?? 0);
if (! $sid) {
if (!$sid) {
continue;
}
$classSections[$sid] = [
'class_section_id' => $sid,
'class_section_name' => $namesById[$sid] ?? ('Section '.$sid),
'class_section_name' => $namesById[$sid] ?? ('Section ' . $sid),
'semester' => $semester,
'school_year' => (string) ($s->school_year ?? ''),
'description' => '',
@@ -178,11 +178,11 @@ class WhatsappContactService
$r = (array) $row;
$sid = (int) ($r['class_section_id'] ?? 0);
$pid = (int) ($r['primary_id'] ?? 0);
if (! $sid || ! $pid || ! isset($classSections[$sid])) {
if (!$sid || !$pid || !isset($classSections[$sid])) {
continue;
}
$key = $sid.':'.$pid;
$key = $sid . ':' . $pid;
if (isset($seen[$key])) {
continue;
}
@@ -191,11 +191,11 @@ class WhatsappContactService
$classSections[$sid]['parents'][] = [
'class_section_id' => $sid,
'primary_id' => $pid,
'primary_name' => trim(($r['primary_lastname'] ?? '').', '.($r['primary_firstname'] ?? '')),
'primary_name' => trim(($r['primary_lastname'] ?? '') . ', ' . ($r['primary_firstname'] ?? '')),
'primary_phone' => (string) ($r['primary_phone'] ?? ''),
'primary_email' => (string) ($r['primary_email'] ?? ''),
'second_id' => (int) ($r['second_id'] ?? 0),
'second_name' => trim(($r['second_lastname'] ?? '').', '.($r['second_firstname'] ?? '')),
'second_name' => trim(($r['second_lastname'] ?? '') . ', ' . ($r['second_firstname'] ?? '')),
'second_phone' => (string) ($r['second_phone'] ?? ''),
'second_email' => (string) ($r['second_email'] ?? ''),
'primary_member' => null,
@@ -225,7 +225,7 @@ class WhatsappContactService
}
unset($section);
} catch (\Throwable $e) {
Log::warning('WhatsApp membership lookup failed: '.$e->getMessage());
Log::warning('WhatsApp membership lookup failed: ' . $e->getMessage());
}
try {
@@ -239,10 +239,10 @@ class WhatsappContactService
foreach ($teachers as $row) {
$r = (array) $row;
$sid = (int) ($r['class_section_id'] ?? 0);
if (! isset($classSections[$sid])) {
if (!isset($classSections[$sid])) {
continue;
}
$name = trim(($r['lastname'] ?? '').', '.($r['firstname'] ?? ''));
$name = trim(($r['lastname'] ?? '') . ', ' . ($r['firstname'] ?? ''));
$pos = strtolower((string) ($r['position'] ?? 'main'));
if ($pos === 'ta' || $pos === 'assistant') {
$classSections[$sid]['teacher_assistants'][] = $name;
@@ -251,7 +251,7 @@ class WhatsappContactService
}
}
} catch (\Throwable $e) {
Log::warning('WhatsApp teacher list lookup failed: '.$e->getMessage());
Log::warning('WhatsApp teacher list lookup failed: ' . $e->getMessage());
}
foreach ($classSections as &$section) {
@@ -47,11 +47,11 @@ class WhatsappContextService
}
$end = $start + 1;
$canonical = $start.'-'.$end;
$canonical = $start . '-' . $end;
$aliases = [
$canonical,
$start.'/'.$end,
$start . '/' . $end,
sprintf('%d-%02d', $start, $end % 100),
(string) $start,
];
@@ -13,12 +13,12 @@ class WhatsappInviteBundleService
$isPrimary = User::query()->whereKey($pickedId)->exists();
$primaryId = $pickedId;
if (! $isPrimary) {
if (!$isPrimary) {
$map = ParentModel::query()
->select('firstparent_id', 'id')
->where('id', $pickedId)
->first();
if (! $map || ! $map->firstparent_id) {
if (!$map || !$map->firstparent_id) {
return null;
}
$primaryId = (int) $map->firstparent_id;
@@ -28,7 +28,7 @@ class WhatsappInviteBundleService
->select('id', 'firstname', 'lastname', 'email')
->whereKey($primaryId)
->first();
if (! $primary) {
if (!$primary) {
return null;
}
@@ -50,7 +50,7 @@ class WhatsappInviteBundleService
$linksSet = [];
foreach ($sectionRows as $row) {
$cid = (int) ($row->class_section_id ?? 0);
if (! $cid) {
if (!$cid) {
continue;
}
$link = (string) ($linkBySection[$cid]['invite_link'] ?? '');
@@ -60,7 +60,7 @@ class WhatsappInviteBundleService
$sections[] = [
'class_section_id' => $cid,
'class_section_name' => (string) ($linkBySection[$cid]['class_section_name'] ?? ('Section '.$cid)),
'class_section_name' => (string) ($linkBySection[$cid]['class_section_name'] ?? ('Section ' . $cid)),
'invite_link' => $link,
];
$linksSet[$link] = true;
@@ -71,10 +71,10 @@ class WhatsappInviteBundleService
}
$emails = [];
if (! empty($primary->email)) {
if (!empty($primary->email)) {
$emails[$primary->email] = true;
}
if (! empty($secondary?->secondparent_email)) {
if (!empty($secondary?->secondparent_email)) {
$emails[$secondary->secondparent_email] = true;
}
if (empty($emails)) {
@@ -89,7 +89,7 @@ class WhatsappInviteBundleService
'email' => (string) ($primary->email ?? ''),
'source' => 'users',
],
'secondary_parents' => $secondary && ! empty($secondary->id) ? [[
'secondary_parents' => $secondary && !empty($secondary->id) ? [[
'id' => (int) $secondary->id,
'firstname' => (string) ($secondary->secondparent_firstname ?? ''),
'lastname' => (string) ($secondary->secondparent_lastname ?? ''),
@@ -186,15 +186,15 @@ class WhatsappInviteBundleService
$bundles = [];
foreach ($parentIds as $pid) {
$profile = $profiles[$pid] ?? null;
if (! $profile) {
if (!$profile) {
continue;
}
$emails = [];
if (! empty($profile['email'])) {
if (!empty($profile['email'])) {
$emails[$profile['email']] = true;
}
if (! empty($secondaryByPrimary[$pid]['email'])) {
if (!empty($secondaryByPrimary[$pid]['email'])) {
$emails[$secondaryByPrimary[$pid]['email']] = true;
}
if (empty($emails)) {
@@ -203,10 +203,10 @@ class WhatsappInviteBundleService
$bundles[] = [
'parent' => $profile,
'secondary_parents' => ! empty($secondaryByPrimary[$pid]['id']) ? [$secondaryByPrimary[$pid]] : [],
'secondary_parents' => !empty($secondaryByPrimary[$pid]['id']) ? [$secondaryByPrimary[$pid]] : [],
'sections' => [[
'class_section_id' => $classSectionId,
'class_section_name' => (string) ($linkBySection[$classSectionId]['class_section_name'] ?? ('Section '.$classSectionId)),
'class_section_name' => (string) ($linkBySection[$classSectionId]['class_section_name'] ?? ('Section ' . $classSectionId)),
'invite_link' => $link,
]],
'links' => [$link],
@@ -232,7 +232,7 @@ class WhatsappInviteBundleService
$bundles = [];
foreach ($sectionRows as $row) {
$cid = (int) ($row->class_section_id ?? 0);
if (! $cid) {
if (!$cid) {
continue;
}
@@ -255,7 +255,6 @@ class WhatsappInviteBundleService
$out[$e] = true;
}
}
return array_keys($out);
};
@@ -265,7 +264,6 @@ class WhatsappInviteBundleService
$k = is_array($v) ? md5(json_encode($v)) : (string) $v;
$map[$k] = $v;
}
return array_values($map);
};
@@ -275,10 +273,10 @@ class WhatsappInviteBundleService
if (is_array($row)) {
$id = $row['class_section_id'] ?? null;
$name = $row['class_section_name'] ?? null;
$key = $id !== null ? ('id:'.(int) $id) : ('name:'.(string) $name);
$key = $id !== null ? ('id:' . (int) $id) : ('name:' . (string) $name);
$byKey[$key] = $row;
} else {
$byKey['name:'.(string) $row] = $row;
$byKey['name:' . (string) $row] = $row;
}
};
@@ -297,11 +295,11 @@ class WhatsappInviteBundleService
$add = static function (&$index, $item) {
if (is_array($item)) {
$key = isset($item['class_section_id'])
? 'id:'.(int) $item['class_section_id']
: ('url:'.(string) ($item['invite_link'] ?? json_encode($item)));
? 'id:' . (int) $item['class_section_id']
: ('url:' . (string) ($item['invite_link'] ?? json_encode($item)));
$index[$key] = $item;
} else {
$index['url:'.(string) $item] = $item;
$index['url:' . (string) $item] = $item;
}
};
@@ -320,9 +318,9 @@ class WhatsappInviteBundleService
$parent = $bundle['parent'] ?? [];
$pid = (int) ($parent['id'] ?? 0);
$primaryEmails = $normalizeEmails((array) ($bundle['emails'] ?? []));
$groupKey = $pid > 0 ? ('pid:'.$pid) : ('mail:'.implode(',', $primaryEmails));
$groupKey = $pid > 0 ? ('pid:' . $pid) : ('mail:' . implode(',', $primaryEmails));
if (! isset($grouped[$groupKey])) {
if (!isset($grouped[$groupKey])) {
$grouped[$groupKey] = [
'parent' => $parent,
'emails' => $primaryEmails,
@@ -332,11 +330,10 @@ class WhatsappInviteBundleService
'items' => (array) ($bundle['items'] ?? []),
'secondary_parents' => (array) ($bundle['secondary_parents'] ?? []),
];
continue;
}
$grouped[$groupKey]['parent'] = ! empty($parent) ? $parent : $grouped[$groupKey]['parent'];
$grouped[$groupKey]['parent'] = !empty($parent) ? $parent : $grouped[$groupKey]['parent'];
$grouped[$groupKey]['emails'] = $normalizeEmails(array_merge($grouped[$groupKey]['emails'], $primaryEmails));
$grouped[$groupKey]['sections'] = $mergeSections($grouped[$groupKey]['sections'], (array) ($bundle['sections'] ?? []));
$grouped[$groupKey]['links'] = $mergeLinks($grouped[$groupKey]['links'], (array) ($bundle['links'] ?? []));
@@ -8,7 +8,9 @@ use Illuminate\Support\Facades\Log;
class WhatsappInviteEmailService
{
public function __construct(private EmailService $emailService) {}
public function __construct(private EmailService $emailService)
{
}
public function send(array $payload): array
{
@@ -16,13 +18,12 @@ class WhatsappInviteEmailService
$cc = array_values(array_unique(array_filter((array) ($payload['cc_emails'] ?? []))));
$cc = array_values(array_diff($cc, $to));
if (empty($to) && ! empty($cc)) {
if (empty($to) && !empty($cc)) {
$to = $cc;
$cc = [];
}
if (empty($to)) {
Log::warning('WhatsappInvite: no recipients after normalization.');
return ['ok' => false, 'message' => 'No recipients provided.'];
}
@@ -33,7 +34,6 @@ class WhatsappInviteEmailService
$items = $this->buildItems($sections, $links);
if (empty($items)) {
Log::warning('WhatsappInvite: no invite items built.');
return ['ok' => false, 'message' => 'No invite links found.'];
}
@@ -77,12 +77,12 @@ class WhatsappInviteEmailService
foreach ($sections as $section) {
$sid = isset($section['class_section_id']) ? (int) $section['class_section_id'] : 0;
$name = trim((string) ($section['class_section_name'] ?? ($sid ? ('Section '.$sid) : 'Class')));
$name = trim((string) ($section['class_section_name'] ?? ($sid ? ('Section ' . $sid) : 'Class')));
$url = trim((string) ($section['invite_link'] ?? ''));
if ($url === '') {
continue;
}
$key = $sid.'|'.$url;
$key = $sid . '|' . $url;
if (isset($seen[$key])) {
continue;
}
@@ -96,20 +96,20 @@ class WhatsappInviteEmailService
];
}
if (empty($items) && ! empty($links)) {
if (empty($items) && !empty($links)) {
foreach ($links as $i => $url) {
$url = trim((string) $url);
if ($url === '') {
continue;
}
$key = '0|'.$url;
$key = '0|' . $url;
if (isset($seen[$key])) {
continue;
}
$seen[$key] = true;
$items[] = [
'class_section_id' => 0,
'class_section_name' => 'Class Link '.($i + 1),
'class_section_name' => 'Class Link ' . ($i + 1),
'invite_link' => $url,
'qr_src' => $this->generateQrImage($url),
];
@@ -126,23 +126,21 @@ class WhatsappInviteEmailService
}
$dir = storage_path('app/qrcodes');
if (! is_dir($dir)) {
if (! @mkdir($dir, 0775, true) && ! is_dir($dir)) {
if (!is_dir($dir)) {
if (!@mkdir($dir, 0775, true) && !is_dir($dir)) {
Log::error('QR: cannot create dir', ['dir' => $dir]);
return $this->qrFallback($inviteLink);
}
}
if (! is_writable($dir)) {
if (!is_writable($dir)) {
Log::error('QR: dir not writable', ['dir' => $dir]);
return $this->qrFallback($inviteLink);
}
$filename = 'wa_'.md5($inviteLink).'.png';
$path = $dir.DIRECTORY_SEPARATOR.$filename;
$filename = 'wa_' . md5($inviteLink) . '.png';
$path = $dir . DIRECTORY_SEPARATOR . $filename;
if (! is_file($path)) {
if (!is_file($path)) {
$v5BuilderClass = '\\Endroid\\QrCode\\Builder\\Builder';
$v5WriterClass = '\\Endroid\\QrCode\\Writer\\PngWriter';
$v5Encoding = '\\Endroid\\QrCode\\Encoding\\Encoding';
@@ -159,7 +157,7 @@ class WhatsappInviteEmailService
if (class_exists($v5BuilderClass) && class_exists($v5WriterClass)) {
try {
$builder = $v5BuilderClass::create()
->writer(new $v5WriterClass)
->writer(new $v5WriterClass())
->data($inviteLink);
if (class_exists($v5Encoding)) {
$builder->encoding(new $v5Encoding('UTF-8'));
@@ -167,11 +165,11 @@ class WhatsappInviteEmailService
$result = $builder->size(500)->margin(2)->build();
$result->saveToFile($path);
} catch (\Throwable $e) {
$lastErr = 'Endroid v5: '.$e->getMessage();
$lastErr = 'Endroid v5: ' . $e->getMessage();
}
}
if (! is_file($path) && class_exists($writerClass)) {
if (!is_file($path) && class_exists($writerClass)) {
try {
if (class_exists($qrClass) && method_exists($qrClass, 'create')) {
$qr = $qrClass::create($inviteLink);
@@ -185,7 +183,7 @@ class WhatsappInviteEmailService
if (method_exists($qr, 'setMargin')) {
$qr->setMargin(2);
}
$writer = new $writerClass;
$writer = new $writerClass();
$result = $writer->write($qr);
if (method_exists($result, 'saveToFile')) {
$result->saveToFile($path);
@@ -195,21 +193,21 @@ class WhatsappInviteEmailService
}
}
} catch (\Throwable $e) {
$lastErr = 'Endroid v3/v4: '.$e->getMessage();
$lastErr = 'Endroid v3/v4: ' . $e->getMessage();
}
}
if (! is_file($path) && class_exists($chlQRCode) && class_exists($chlQROptions)) {
if (!is_file($path) && class_exists($chlQRCode) && class_exists($chlQROptions)) {
try {
$optsArr = [
'version' => 5,
'outputType' => constant($chlQRCode.'::OUTPUT_IMAGE_PNG'),
'outputType' => constant($chlQRCode . '::OUTPUT_IMAGE_PNG'),
'scale' => 6,
'imageBase64' => false,
'quietzoneSize' => 2,
];
if (class_exists($chlQRMatrix) && defined($chlQRMatrix.'::ECC_H')) {
$optsArr['eccLevel'] = constant($chlQRMatrix.'::ECC_H');
if (class_exists($chlQRMatrix) && defined($chlQRMatrix . '::ECC_H')) {
$optsArr['eccLevel'] = constant($chlQRMatrix . '::ECC_H');
}
$opts = new $chlQROptions($optsArr);
@@ -217,11 +215,11 @@ class WhatsappInviteEmailService
$png = $qrcode->render($inviteLink);
file_put_contents($path, $png);
} catch (\Throwable $e) {
$lastErr = 'chillerlan: '.$e->getMessage();
$lastErr = 'chillerlan: ' . $e->getMessage();
}
}
if (! is_file($path) && $lastErr) {
if (!is_file($path) && $lastErr) {
Log::error('QR generation failed', ['error' => $lastErr]);
}
}
@@ -229,7 +227,7 @@ class WhatsappInviteEmailService
if (is_file($path)) {
$data = @file_get_contents($path);
if ($data !== false) {
return 'data:image/png;base64,'.base64_encode($data);
return 'data:image/png;base64,' . base64_encode($data);
}
}
@@ -239,7 +237,6 @@ class WhatsappInviteEmailService
private function qrFallback(string $inviteLink): string
{
$link = rawurlencode($inviteLink);
return 'https://api.qrserver.com/v1/create-qr-code/?size=260x260&qzone=2&data='.$link;
return 'https://api.qrserver.com/v1/create-qr-code/?size=260x260&qzone=2&data=' . $link;
}
}
@@ -14,7 +14,6 @@ class WhatsappInviteNotificationService
$listenerCount = Event::hasListeners(WhatsappInvitesSend::class) ? 1 : 0;
if ($listenerCount === 0) {
Log::warning('WhatsappInvitesSend has no listeners registered.');
return [
'ok' => false,
'message' => 'No event listener registered for WhatsappInvitesSend.',
@@ -30,7 +29,6 @@ class WhatsappInviteNotificationService
$out[$e] = true;
}
}
return array_keys($out);
};
@@ -49,9 +47,9 @@ class WhatsappInviteNotificationService
$to = $primaryEmails;
$cc = [];
if (! empty($primaryEmails) && ! empty($secondaryEmails)) {
if (!empty($primaryEmails) && !empty($secondaryEmails)) {
$cc = array_values(array_diff($secondaryEmails, $primaryEmails));
} elseif (empty($primaryEmails) && ! empty($secondaryEmails)) {
} elseif (empty($primaryEmails) && !empty($secondaryEmails)) {
$to = $secondaryEmails;
}
@@ -59,7 +57,6 @@ class WhatsappInviteNotificationService
Log::warning('Skipping WhatsApp invite: no recipient email(s).', [
'parent_id' => $bundle['parent']['id'] ?? null,
]);
continue;
}
@@ -76,7 +73,7 @@ class WhatsappInviteNotificationService
$triggered++;
} catch (\Throwable $e) {
Log::error('WhatsappInvitesSend dispatch failed: '.$e->getMessage());
Log::error('WhatsappInvitesSend dispatch failed: ' . $e->getMessage());
}
}
@@ -90,12 +87,12 @@ class WhatsappInviteNotificationService
{
$collected = [];
if (! empty($bundle['emails_secondary'])) {
if (!empty($bundle['emails_secondary'])) {
$collected = array_merge($collected, (array) $bundle['emails_secondary']);
}
if (! empty($bundle['secondary_parents'])) {
if (!empty($bundle['secondary_parents'])) {
foreach ((array) $bundle['secondary_parents'] as $sp) {
if (! empty($sp['email'])) {
if (!empty($sp['email'])) {
$collected[] = $sp['email'];
}
}
@@ -103,11 +100,11 @@ class WhatsappInviteNotificationService
$parent = $bundle['parent'] ?? [];
foreach (['secondary_email', 'secondparent_email', 'sec_email', 'email_secondary'] as $key) {
if (! empty($parent[$key])) {
if (!empty($parent[$key])) {
$collected[] = $parent[$key];
}
}
if (! empty($parent['secondparent']['email'])) {
if (!empty($parent['secondparent']['email'])) {
$collected[] = $parent['secondparent']['email'];
}
@@ -119,12 +116,12 @@ class WhatsappInviteNotificationService
->where('firstparent_id', $primaryId)
->orderByDesc('updated_at')
->first();
if (! empty($row?->secondparent_email)) {
if (!empty($row?->secondparent_email)) {
$collected[] = $row->secondparent_email;
}
}
} catch (\Throwable $e) {
Log::warning('Secondary email DB lookup failed: '.$e->getMessage());
Log::warning('Secondary email DB lookup failed: ' . $e->getMessage());
}
return $normalize($collected);
@@ -10,7 +10,8 @@ class WhatsappInviteService
private WhatsappContextService $context,
private WhatsappInviteBundleService $bundleService,
private WhatsappInviteNotificationService $notificationService
) {}
) {
}
public function send(array $payload): array
{
@@ -24,7 +25,7 @@ class WhatsappInviteService
$linkBySection = [];
foreach ($links as $link) {
$cid = (int) ($link->class_section_id ?? 0);
if ($cid && ! isset($linkBySection[$cid])) {
if ($cid && !isset($linkBySection[$cid])) {
$linkBySection[$cid] = $link->toArray();
}
}
@@ -37,7 +38,7 @@ class WhatsappInviteService
continue;
}
$bundle = $this->bundleService->bundleForParent($pickedId, $linkBySection, $schoolYear, $semester);
if ($bundle && ! empty($bundle['emails']) && ! empty($bundle['links'])) {
if ($bundle && !empty($bundle['emails']) && !empty($bundle['links'])) {
$bundles[] = $bundle;
}
}
@@ -45,14 +46,14 @@ class WhatsappInviteService
$classSectionId = (int) ($payload['class_section_id'] ?? 0);
$classBundles = $this->bundleService->bundlesForClass($classSectionId, $linkBySection, $schoolYear, $semester);
foreach ($classBundles as $bundle) {
if (! empty($bundle['emails']) && ! empty($bundle['links'])) {
if (!empty($bundle['emails']) && !empty($bundle['links'])) {
$bundles[] = $bundle;
}
}
} elseif ($mode === 'all') {
$allBundles = $this->bundleService->bundlesForAll($linkBySection, $schoolYear, $semester);
foreach ($allBundles as $bundle) {
if (! empty($bundle['emails']) && ! empty($bundle['links'])) {
if (!empty($bundle['emails']) && !empty($bundle['links'])) {
$bundles[] = $bundle;
}
}
@@ -69,7 +70,7 @@ class WhatsappInviteService
$consolidated = $this->bundleService->consolidateBundles($bundles);
$result = $this->notificationService->dispatch($consolidated, $schoolYear, $semester);
if (! ($result['ok'] ?? false)) {
if (!($result['ok'] ?? false)) {
return $result + ['ok' => false];
}
+12 -11
View File
@@ -9,7 +9,9 @@ use Illuminate\Database\Eloquent\ModelNotFoundException;
class WhatsappLinkService
{
public function __construct(private WhatsappContextService $context) {}
public function __construct(private WhatsappContextService $context)
{
}
public function paginate(array $filters): LengthAwarePaginator
{
@@ -27,7 +29,7 @@ class WhatsappLinkService
}
}
if (! empty($filters['class_section_id'])) {
if (!empty($filters['class_section_id'])) {
$query->forSection((int) $filters['class_section_id']);
}
@@ -35,11 +37,11 @@ class WhatsappLinkService
$query->where('active', (int) (bool) $filters['active']);
}
if (! empty($filters['search'])) {
if (!empty($filters['search'])) {
$search = trim((string) $filters['search']);
$query->where(function ($q) use ($search) {
$q->where('class_section_name', 'like', '%'.$search.'%')
->orWhere('invite_link', 'like', '%'.$search.'%')
$q->where('class_section_name', 'like', '%' . $search . '%')
->orWhere('invite_link', 'like', '%' . $search . '%')
->orWhere('class_section_id', $search);
});
}
@@ -48,7 +50,7 @@ class WhatsappLinkService
$sortDir = strtolower((string) ($filters['sort_dir'] ?? 'desc')) === 'asc' ? 'asc' : 'desc';
$allowedSorts = ['class_section_id', 'class_section_name', 'school_year', 'semester', 'active', 'updated_at'];
if (! in_array($sortBy, $allowedSorts, true)) {
if (!in_array($sortBy, $allowedSorts, true)) {
$sortBy = 'updated_at';
}
@@ -61,7 +63,7 @@ class WhatsappLinkService
public function findOrFail(int $id): WhatsappGroupLink
{
$link = WhatsappGroupLink::query()->find($id);
if (! $link) {
if (!$link) {
throw new ModelNotFoundException('WhatsApp group link not found.');
}
@@ -81,7 +83,7 @@ class WhatsappLinkService
$link = WhatsappGroupLink::upsertLinkForSection(
$sectionId,
$sectionName !== '' ? $sectionName : ('Section '.$sectionId),
$sectionName !== '' ? $sectionName : ('Section ' . $sectionId),
$schoolYear,
$semester,
(string) ($payload['invite_link'] ?? ''),
@@ -97,7 +99,7 @@ class WhatsappLinkService
if (array_key_exists('class_section_name', $payload)) {
$name = trim((string) ($payload['class_section_name'] ?? ''));
if ($name === '' && ! empty($payload['class_section_id'])) {
if ($name === '' && !empty($payload['class_section_id'])) {
$name = $this->resolveSectionName((int) $payload['class_section_id']);
}
$payload['class_section_name'] = $name;
@@ -118,7 +120,6 @@ class WhatsappLinkService
private function resolveSectionName(int $sectionId): string
{
$name = ClassSection::getClassSectionNameBySectionId($sectionId);
return $name !== null && $name !== '' ? $name : ('Section '.$sectionId);
return $name !== null && $name !== '' ? $name : ('Section ' . $sectionId);
}
}
@@ -11,9 +11,8 @@ class WhatsappMembershipService
{
public function updateMembership(array $payload, ?int $verifiedBy = null): array
{
if (! Schema::hasTable('whatsapp_group_memberships')) {
if (!Schema::hasTable('whatsapp_group_memberships')) {
Log::error('WhatsApp membership table missing.');
return [
'ok' => false,
'message' => 'Membership table missing. Please run migrations.',
@@ -57,7 +56,7 @@ class WhatsappMembershipService
(bool) $primaryMember,
$verifiedBy
);
$saved[] = 'primary='.$primaryId.':'.($primaryMember ? '1' : '0');
$saved[] = 'primary=' . $primaryId . ':' . ($primaryMember ? '1' : '0');
if ($id <= 0) {
Log::warning('Failed to upsert primary membership.', [
'class_section_id' => $classSectionId,
@@ -76,7 +75,7 @@ class WhatsappMembershipService
(bool) $secondMember,
$verifiedBy
);
$saved[] = 'second='.$secondId.':'.($secondMember ? '1' : '0');
$saved[] = 'second=' . $secondId . ':' . ($secondMember ? '1' : '0');
if ($id <= 0) {
Log::warning('Failed to upsert second membership.', [
'class_section_id' => $classSectionId,