Deploy: 2026-05-22 00:12:07
This commit is contained in:
@@ -70,4 +70,47 @@ class GroupController extends BaseController
|
||||
|
||||
$response->json(['status' => 'success', 'message' => 'Contact added to group']);
|
||||
}
|
||||
|
||||
/**
|
||||
* Attach multiple contacts to a group in bulk
|
||||
*/
|
||||
public function bulkAddContacts(Request $request, Response $response)
|
||||
{
|
||||
$errors = $this->validate($request, [
|
||||
'group_id' => 'required',
|
||||
'contact_ids' => 'required'
|
||||
]);
|
||||
if (!empty($errors)) {
|
||||
$response->status(400)->json(['status' => 'error', 'errors' => $errors]);
|
||||
return;
|
||||
}
|
||||
|
||||
$body = $request->getBody();
|
||||
$groupId = (int)$body['group_id'];
|
||||
$contactIds = $body['contact_ids'];
|
||||
|
||||
if (!is_array($contactIds)) {
|
||||
$response->status(400)->json(['status' => 'error', 'message' => 'contact_ids must be an array']);
|
||||
return;
|
||||
}
|
||||
|
||||
$groupModel = new ContactGroup();
|
||||
|
||||
$pdo = \App\Core\Database::getConnection();
|
||||
try {
|
||||
$pdo->beginTransaction();
|
||||
foreach ($contactIds as $contactId) {
|
||||
$groupModel->attachContact($groupId, (int)$contactId);
|
||||
}
|
||||
$pdo->commit();
|
||||
} catch (\Exception $e) {
|
||||
if ($pdo->inTransaction()) {
|
||||
$pdo->rollBack();
|
||||
}
|
||||
$response->status(500)->json(['status' => 'error', 'message' => 'Bulk insert failed: ' . $e->getMessage()]);
|
||||
return;
|
||||
}
|
||||
|
||||
$response->json(['status' => 'success', 'message' => 'Contacts added to group in bulk']);
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user