Skip to content

Commit 13826df

Browse files
authored
Merge pull request #3606 from codeeu/dev
Require Changes sheet only for bulk user upload
2 parents 42f9a27 + 4f285db commit 13826df

4 files changed

Lines changed: 30 additions & 6 deletions

File tree

app/Http/Controllers/BulkUserChangesController.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -35,8 +35,8 @@ function ($attribute, $value, $fail) {
3535
return;
3636
}
3737
$ext = strtolower($value->getClientOriginalExtension());
38-
if (! in_array($ext, ['csv', 'xlsx', 'xls'], true)) {
39-
$fail('The file must be a CSV or Excel file (.csv, .xlsx, or .xls).');
38+
if (! in_array($ext, ['xlsx', 'xls'], true)) {
39+
$fail('The file must be an Excel workbook (.xlsx or .xls) with a Changes sheet.');
4040
}
4141
},
4242
],

app/Services/BulkUserChanges/BulkUserChangesSheetReader.php

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -90,7 +90,12 @@ private function resolveChangesSheet(array $sheetNames, \PhpOffice\PhpSpreadshee
9090
}
9191
}
9292

93-
return $spreadsheet->getSheet(0);
93+
$available = implode(', ', array_map(fn (string $n) => '"'.$n.'"', $sheetNames));
94+
95+
throw new \InvalidArgumentException(
96+
'No sheet named "Changes" found. Only the Changes tab is read; other sheets are ignored. '
97+
.'Sheets in this file: '.($available !== '' ? $available : '(none)').'.'
98+
);
9499
}
95100

96101
private function detectHeaderRow(Worksheet $sheet): int

resources/views/admin/bulk-user-changes/index.blade.php

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88
</section>
99

1010
<section class="codeweek-content-wrapper">
11-
<p class="mb-4">Upload the client <strong>Changes</strong> sheet (Excel). The tool finds the header row automatically — rows above current data (e.g. old history) are ignored if they have no email/action. <strong>Missing users are never created.</strong></p>
11+
<p class="mb-4">Upload the client Excel workbook. <strong>Only the sheet named <code>Changes</code> is read</strong> — all other tabs are ignored. The tool finds the header row on that sheet automatically; rows with no email/action are skipped. <strong>Missing users are never created.</strong></p>
1212

1313
@if ($errors->any())
1414
<div class="mb-4 p-4 rounded bg-red-50 border border-red-200">
@@ -25,9 +25,9 @@
2525
<div class="codeweek-form-inner-container">
2626
<div class="mb-4">
2727
<label for="bulk-user-changes-file" class="font-medium">Excel file (.xlsx) <span class="text-red-600">*</span></label>
28-
<input type="file" name="file" id="bulk-user-changes-file" accept=".xlsx,.xls,.csv" required
28+
<input type="file" name="file" id="bulk-user-changes-file" accept=".xlsx,.xls" required
2929
class="mt-2 text-sm file:mr-2 file:py-2 file:px-4 file:rounded-full file:border-0 file:font-semibold file:bg-primary file:text-white hover:file:opacity-90 file:cursor-pointer cursor-pointer">
30-
<p class="text-sm text-gray-600 mt-2">Uses the sheet named <code>Changes</code>. Required columns: Country, Full name, Email address, ACTION, Role.</p>
30+
<p class="text-sm text-gray-600 mt-2">Must include a tab named <code>Changes</code> (other tabs are not processed). Required columns on that tab: Country, Full name, Email address, ACTION, Role.</p>
3131
</div>
3232
<button type="submit" class="bg-primary cursor-pointer px-6 py-3 rounded-full font-semibold text-[#20262C] hover:bg-hover-orange duration-300">Upload &amp; preview</button>
3333
</div>

tests/Unit/BulkUserChanges/BulkUserChangesSheetReaderTest.php

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,4 +26,23 @@ public function test_reads_changes_sheet_and_finds_anita_row(): void
2626
$this->assertSame('role_add', $anita['operation']);
2727
$this->assertSame('leading teacher', $anita['role_name']);
2828
}
29+
30+
public function test_rejects_workbook_without_changes_sheet(): void
31+
{
32+
$path = sys_get_temp_dir().'/bulk_user_changes_no_changes_'.uniqid().'.xlsx';
33+
$spreadsheet = new \PhpOffice\PhpSpreadsheet\Spreadsheet;
34+
$spreadsheet->getActiveSheet()->setTitle('Contacts');
35+
$spreadsheet->getActiveSheet()->setCellValue('A1', 'Country');
36+
$writer = new \PhpOffice\PhpSpreadsheet\Writer\Xlsx($spreadsheet);
37+
$writer->save($path);
38+
39+
try {
40+
$this->expectException(\InvalidArgumentException::class);
41+
$this->expectExceptionMessage('No sheet named "Changes" found');
42+
43+
app(BulkUserChangesSheetReader::class)->read($path);
44+
} finally {
45+
@unlink($path);
46+
}
47+
}
2948
}

0 commit comments

Comments
 (0)