Update Pengajuan Presensi WhatsApp

This commit is contained in:
Ariyutta
2026-09-19 16:05:56 +07:00
parent 8aa3da65ce
commit 9057ab3643
10000 changed files with 1269273 additions and 1062 deletions
+77 -8
View File
@@ -2,32 +2,101 @@
namespace App\Services;
use Carbon\Carbon;
use Google\Client;
use Google\Service\Drive;
use Google\Service\Drive\DriveFile;
use Illuminate\Support\Facades\Log;
use Throwable;
class GoogleDriveService
{
// TODO: isi path credentials service account (JSON) & folder ID tujuan setelah tersedia.
private string $credentialsPath = '';
private string $credentialsPath;
private string $folderId = '';
private string $rootFolderId;
public function uploadPdf(string $content, string $fileName): string|false
public function __construct()
{
if (! $this->credentialsPath || ! $this->folderId) {
Log::error("GoogleDriveService: belum dikonfigurasi, upload '$fileName' dilewati.");
$this->credentialsPath = (string) config('services.google_drive.credentials_path');
$this->rootFolderId = (string) config('services.google_drive.folder_id');
}
public function uploadPdf(string $content, string $nip, string $namaPegawai, string $tanggal, string $namaKategori): string|false
{
if (! $this->credentialsPath || ! file_exists($this->credentialsPath) || ! $this->rootFolderId) {
Log::error('GoogleDriveService: belum dikonfigurasi, upload dilewati.');
return false;
}
try {
// TODO: implementasi upload pakai google/apiclient setelah credentials tersedia.
$client = new Client();
$client->setAuthConfig($this->credentialsPath);
$client->addScope(Drive::DRIVE);
$service = new Drive($client);
$pengajuanPresensiFolderId = $this->findOrCreateFolder($service, 'Pengajuan Presensi', $this->rootFolderId);
$pegawaiFolderId = $this->findOrCreateFolder($service, $this->sanitize("$nip - $namaPegawai"), $pengajuanPresensiFolderId);
$bulanFolderId = $this->findOrCreateFolder($service, Carbon::parse($tanggal)->translatedFormat('F Y'), $pegawaiFolderId);
$fileName = Carbon::parse($tanggal)->format('Y-m-d').'_'.$this->sanitize($namaKategori).'.pdf';
$fileMetadata = new DriveFile([
'name' => $fileName,
'parents' => [$bulanFolderId],
]);
$uploaded = $service->files->create($fileMetadata, [
'data' => $content,
'mimeType' => 'application/pdf',
'uploadType' => 'multipart',
'fields' => 'id, webViewLink',
'supportsAllDrives' => true,
]);
return $uploaded->getWebViewLink() ?: $uploaded->getId();
} catch (Throwable $th) {
Log::error('GoogleDriveService uploadPdf : '.$th->getMessage());
return false;
}
}
return false;
private function findOrCreateFolder(Drive $service, string $name, string $parentId): string
{
$escapedName = str_replace("'", "\\'", $name);
$result = $service->files->listFiles([
'q' => "name='$escapedName' and '$parentId' in parents and mimeType='application/vnd.google-apps.folder' and trashed=false",
'fields' => 'files(id)',
'supportsAllDrives' => true,
'includeItemsFromAllDrives' => true,
'corpora' => 'allDrives',
]);
$existing = $result->getFiles();
if (count($existing) > 0) {
return $existing[0]->getId();
}
$folder = new DriveFile([
'name' => $name,
'mimeType' => 'application/vnd.google-apps.folder',
'parents' => [$parentId],
]);
$created = $service->files->create($folder, [
'fields' => 'id',
'supportsAllDrives' => true,
]);
return $created->getId();
}
private function sanitize(string $value): string
{
return trim(str_replace(['/', '\\'], '-', $value));
}
}
+13 -8
View File
@@ -4,7 +4,7 @@ namespace App\Services;
class VerifikasiAbsenLinkService
{
const TTL_MENIT_DEFAULT = 7200; // 5 hari, selaras dengan batas kadaluarsa pengajuan
const TTL_MENIT_DEFAULT = 7200;
public function generate(int $pengajuanId, string $nip, int $ttlMenit = self::TTL_MENIT_DEFAULT): array
{
@@ -15,17 +15,22 @@ class VerifikasiAbsenLinkService
'nip' => $nip,
'expires' => $expires,
'signature' => $signature,
'url' => route('verifikasi-absen.show', [
'id' => $pengajuanId,
'nip' => $nip,
'expires' => $expires,
'signature' => $signature,
]),
// Suffix untuk parameter tombol URL template WhatsApp (domain sudah baku di template).
'url' => $this->url($pengajuanId, $nip, $expires, $signature),
'button_param' => "{$pengajuanId}?nip={$nip}&expires={$expires}&signature={$signature}",
];
}
public function url(int $pengajuanId, ?string $nip = null, ?int $expires = null, ?string $signature = null): string
{
$base = rtrim(config('services.verifikasi_absen_wa.url'), '/')."/{$pengajuanId}";
if (! $nip || ! $expires || ! $signature) {
return $base;
}
return $base.'?'.http_build_query(compact('nip', 'expires', 'signature'));
}
public function verify(int $pengajuanId, ?string $nip, mixed $expires, ?string $signature): bool
{
if (! $nip || ! $expires || ! $signature) {