| @@ -7,6 +7,7 @@ import com.ffii.tsms.modules.data.entity.CustomerRepository | |||||
| import com.ffii.tsms.modules.data.entity.CustomerTypeRepository | import com.ffii.tsms.modules.data.entity.CustomerTypeRepository | ||||
| import com.ffii.tsms.modules.data.web.models.NewCustomerResponse | import com.ffii.tsms.modules.data.web.models.NewCustomerResponse | ||||
| import com.ffii.tsms.modules.project.web.models.SaveCustomerRequest | import com.ffii.tsms.modules.project.web.models.SaveCustomerRequest | ||||
| import org.springframework.beans.BeanUtils | |||||
| import org.springframework.stereotype.Service | import org.springframework.stereotype.Service | ||||
| import java.util.Optional | import java.util.Optional | ||||
| @@ -38,8 +39,8 @@ class CustomerService( | |||||
| val duplicateCustomer = findCustomerByCode(saveCustomer.code) | val duplicateCustomer = findCustomerByCode(saveCustomer.code) | ||||
| //TODO: check duplicate customer | |||||
| if (duplicateCustomer.isPresent()) { | |||||
| //check duplicate customer | |||||
| if (duplicateCustomer.isPresent && duplicateCustomer.orElseThrow().id?.equals(saveCustomer.id) == false) { | |||||
| return NewCustomerResponse( | return NewCustomerResponse( | ||||
| customer = duplicateCustomer.get(), | customer = duplicateCustomer.get(), | ||||
| message = "The customer code has already existed" | message = "The customer code has already existed" | ||||
| @@ -48,18 +49,14 @@ class CustomerService( | |||||
| val customerType = customerTypeRepository.findById(saveCustomer.typeId).orElseThrow() | val customerType = customerTypeRepository.findById(saveCustomer.typeId).orElseThrow() | ||||
| val instance = if (saveCustomer.id != null && saveCustomer.id > 0) customerRepository.findById(saveCustomer.id) | |||||
| .orElseThrow() else Customer(); | |||||
| BeanUtils.copyProperties(saveCustomer, instance) | |||||
| instance.customerType = customerType | |||||
| val customer = | val customer = | ||||
| customerRepository.saveAndFlush( | |||||
| Customer().apply { | |||||
| id = saveCustomer.id | |||||
| code = saveCustomer.code | |||||
| name = saveCustomer.name | |||||
| this.customerType = customerType | |||||
| address = saveCustomer.address | |||||
| district = saveCustomer.district | |||||
| brNo = saveCustomer.brNo | |||||
| } | |||||
| ) | |||||
| customerRepository.saveAndFlush(instance) | |||||
| val customerId = customer.id | val customerId = customer.id | ||||
| @@ -74,7 +71,7 @@ class CustomerService( | |||||
| } | } | ||||
| if (saveCustomer.deleteContactIds.isNotEmpty()) { | if (saveCustomer.deleteContactIds.isNotEmpty()) { | ||||
| customerContactService.deleteContactsByCustomer( saveCustomer.deleteContactIds) | |||||
| customerContactService.deleteContactsByCustomer(saveCustomer.deleteContactIds) | |||||
| } | } | ||||
| if (saveCustomer.addContacts.isNotEmpty()) { | if (saveCustomer.addContacts.isNotEmpty()) { | ||||