implemented

- assigning new customer numbers to contacts that don`t have one
- updating customer number counter in company table

Signed-off-by: Stephan Richter <s.richter@srsoftware.de>
This commit is contained in:
2025-10-31 22:09:59 +01:00
parent dc0b381aba
commit 4468f45064
14 changed files with 157 additions and 20 deletions

View File

@@ -15,4 +15,6 @@ public interface CompanyService {
Map<Long,Company> listCompaniesOf(UmbrellaUser user) throws UmbrellaException;
boolean membership(long companyId, long userId) throws UmbrellaException;
void saveNewCustomer(long companyId, String id);
}

View File

@@ -8,4 +8,8 @@ import java.util.Map;
public interface ContactService {
Map<Long, Contact> listContactsOf(UmbrellaUser user) throws UmbrellaException;
Contact load(long userId, long contactId);
Contact save(Contact contact);
}

View File

@@ -3,6 +3,7 @@ package de.srsoftware.umbrella.core.model;
import static de.srsoftware.umbrella.core.Constants.*;
import static de.srsoftware.umbrella.core.exceptions.UmbrellaException.missingFieldException;
import static java.text.MessageFormat.format;
import de.srsoftware.tools.Mappable;
import java.sql.ResultSet;
@@ -10,7 +11,21 @@ import java.sql.SQLException;
import java.util.Map;
import org.json.JSONObject;
public record Contact(long id, String vcard) implements Mappable {
public class Contact implements Mappable{
private final static String X_CUSTOMER_NUMBER = "X-CUSTOMER-NUMBER";
private long id;
private String vcard;
public Contact(long id, String vcard){
this.id = id;
this.vcard = vcard;
}
public long id(){
return id;
}
public static Contact of(ResultSet rs) throws SQLException {
return new Contact(rs.getLong(ID),rs.getString(DATA));
}
@@ -21,8 +36,24 @@ public record Contact(long id, String vcard) implements Mappable {
return new Contact(id,vcard.replace(from, to));
}
public Contact setCustomerNumber(String customerNumber) {
if (vcard.contains(X_CUSTOMER_NUMBER)) {
var pattern = format("^{0}:.*",X_CUSTOMER_NUMBER);
var replacement = format("{0}:{1}",X_CUSTOMER_NUMBER,customerNumber);
vcard = vcard.replaceAll(pattern,replacement);
} else {
var index = vcard.lastIndexOf("\n");
vcard = format("{0}\r\n{1}:{2}{3}",vcard.substring(0,index),X_CUSTOMER_NUMBER,customerNumber,vcard.substring(index));
}
return this;
}
@Override
public Map<String, Object> toMap() {
return Map.of(ID,id,VCARD,vcard);
}
public String vcard(){
return vcard;
}
}