FIX: Editar datos Broker

This commit is contained in:
Juan Pablo Vial
2025-03-12 18:31:21 -03:00
parent 7c7c8315e2
commit 8b04eb262f
4 changed files with 37 additions and 4 deletions

View File

@ -168,7 +168,20 @@
window.location.reload()
})
},
edit: (broker_rut, data) => {},
edit: data => {
const url = '{{$urls->api}}/proyectos/brokers/edit'
const method = 'post'
const body = new FormData()
body.append('brokers[]', JSON.stringify(data))
return APIClient.fetch(url, {method, body}).then(response => response.json()).then(json => {
if (!json.success) {
console.error(json.errors)
alert('No se pudo editar operador.')
return
}
window.location.reload()
})
},
delete: broker_rut => {
const url = '{{$urls->api}}/proyectos/broker/' + broker_rut
const method = 'delete'

View File

@ -76,12 +76,14 @@
const form = document.getElementById(this.ids.form)
const broker_rut = form.querySelector('[name="broker_rut"]').value
const data = {
rut: form.querySelector('[name="rut"]').value.replace(/\D/g, ''),
name: form.querySelector('[name="name"]').value,
contact: form.querySelector('[name="contact"]').value || '',
legal_name: form.querySelector('[name="legal_name"]').value,
email: form.querySelector('[name="email"]').value || '',
phone: form.querySelector('[name="phone"]').value || ''
}
this.handler.execute().edit(broker_rut, data)
this.handler.execute().edit(data)
}
})
this.modal.modal('hide')

View File

@ -70,9 +70,10 @@ class Brokers
];
foreach ($body['brokers'] as $data) {
try {
$data = json_decode($data, true);
$output['brokers'] []= [
'rut' => $data['rut'],
'broker' => $brokerService->edit(json_decode($data, true)),
'broker' => $brokerService->edit($data),
'success' => true
];
$output['partial'] = true;

View File

@ -76,7 +76,7 @@ class Broker extends Ideal\Service
public function edit(array $data): Model\Proyecto\Broker
{
try {
$broker = $this->brokerRepository->fetchById($data['id']);
$broker = $this->brokerRepository->fetchById($data['rut']);
} catch (EmptyResult $exception) {
throw new ServiceAction\Update(__CLASS__, $exception);
}
@ -180,6 +180,23 @@ class Broker extends Ideal\Service
} catch (EmptyResult $exception) {
throw new ServiceAction\Update(__CLASS__, $exception);
}
if (isset($data['contact'])) {
try {
$representative = $this->contactRepository->fetchByName($data['contact']);
$data['representative_id'] = $representative->id;
} catch (EmptyResult) {
$representativeData = $this->contactRepository->filterData($data);
$representativeData['name'] = $data['contact'];
try {
$representative = $this->contactRepository->create($representativeData);
$representative = $this->contactRepository->save($representative);
$data['representative_id'] = $representative->id;
} catch (PDOException) {
unset($representative);
unset($data['contact']);
}
}
}
try {
$data['broker_rut'] = $broker->rut;
$filteredData = $this->dataRepository->filterData($data);