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

@@ -2,6 +2,7 @@
package de.srsoftware.umbrella.company;
import static de.srsoftware.umbrella.company.Constants.CONFIG_DATABASE;
import static de.srsoftware.umbrella.company.Constants.NEXT_CUSTOMER_NUMBER;
import static de.srsoftware.umbrella.core.ConnectionProvider.connect;
import static de.srsoftware.umbrella.core.Constants.*;
import static de.srsoftware.umbrella.core.ModuleRegistry.*;
@@ -73,7 +74,12 @@ public class CompanyModule extends BaseHandler implements CompanyService {
return switch (head){
case LIST -> getCompanyList(user.get(),ex);
case null,
default -> super.doGet(path,ex);
default -> {
try {
yield getCompany(user.get(), path, Long.parseLong(head), ex);
} catch (NumberFormatException ignored){}
yield super.doGet(path,ex);
}
};
} catch (UmbrellaException e) {
return send(ex,e);
@@ -124,6 +130,14 @@ public class CompanyModule extends BaseHandler implements CompanyService {
return companyDb.load(companyId);
}
private boolean getCompany(UmbrellaUser user, Path path, long companyId, HttpExchange ex) throws IOException {
return switch (path.pop()){
case NEXT_CUSTOMER_NUMBER -> getNextCustomerNumber(user, companyId, ex);
case null, default -> super.doGet(path,ex);
};
}
private boolean getCompanyList(UmbrellaUser user, HttpExchange ex) throws IOException, UmbrellaException {
var result = new HashMap<Long,Map<String,Object>>();
for (var entry : listCompaniesOf(user).entrySet()) result.put(entry.getKey(),entry.getValue().toMap());
@@ -137,6 +151,13 @@ public class CompanyModule extends BaseHandler implements CompanyService {
return members;
}
private boolean getNextCustomerNumber(UmbrellaUser user, long companyId, HttpExchange ex) throws IOException {
var company = companyDb.load(companyId);
if (!membership(companyId,user.id())) throw forbidden("You are mot a member of company {0}",company.name());
var nextCustomerNumber = companyDb.getNextCustomerNumber(companyId);
return sendContent(ex,nextCustomerNumber);
}
@Override
public Map<Long,Company> listCompaniesOf(UmbrellaUser user) throws UmbrellaException {
return loadMembers(companyDb.listCompaniesOf(user.id()));
@@ -205,4 +226,9 @@ public class CompanyModule extends BaseHandler implements CompanyService {
var companies = companyDb.find(user.id(),keys);
return sendContent(ex,mapValues(companies));
}
@Override
public void saveNewCustomer(long companyId, String id) {
companyDb.saveNewCustomer(companyId, id);
}
}

View File

@@ -6,6 +6,7 @@ public class Constants {
public static final String CONFIG_DATABASE = "umbrella.modules.company.database";
public static final String DISTINCT = "DISTINCT *";
public static final String NEXT_CUSTOMER_NUMBER = "next_customer_number";
public static final String TABLE_COMPANIES_USERS = "companies_users";
public static final String TABLE_COMPANIES = "companies";
}

View File

@@ -11,6 +11,7 @@ import static de.srsoftware.umbrella.core.Constants.*;
import static de.srsoftware.umbrella.core.Field.*;
import static de.srsoftware.umbrella.core.Field.COMPANY_ID;
import static de.srsoftware.umbrella.core.exceptions.UmbrellaException.databaseException;
import static de.srsoftware.umbrella.core.exceptions.UmbrellaException.unprocessable;
import static java.lang.System.Logger.Level.ERROR;
import static java.text.MessageFormat.format;
@@ -21,6 +22,7 @@ import de.srsoftware.umbrella.core.model.Company;
import java.sql.Connection;
import java.sql.SQLException;
import java.util.*;
import java.util.regex.Pattern;
import org.json.JSONObject;
public class SqliteDb extends BaseDb implements CompanyDb {
@@ -126,6 +128,24 @@ CREATE TABLE IF NOT EXISTS "companies" (
}
}
@Override
public String getNextCustomerNumber(long companyId) {
try {
var rs = select(LAST_CUSTOMER_NUMBER,CUSTOMER_NUMBER_PREFIX).from(TABLE_COMPANIES).where(ID,equal(companyId)).exec(db);
var last = 0L;
String prefix = null;
if (rs.next()){
last = rs.getLong(LAST_CUSTOMER_NUMBER);
prefix = rs.getString(CUSTOMER_NUMBER_PREFIX);
}
rs.close();
var next = last+1;
return prefix+next; // TODO: currently not taking growing number lengths into account, this should be resolved!
} catch (SQLException e) {
throw databaseException("Failed to load customer number settings for company {0}",companyId);
}
}
@Override
public HashMap<Long, Company> find(long userId, Collection<String> keys) {
try {
@@ -208,4 +228,18 @@ CREATE TABLE IF NOT EXISTS "companies" (
throw UmbrellaException.databaseException("Failed to save {0}…",company.name());
}
}
@Override
public void saveNewCustomer(long companyId, String id) {
var p = Pattern.compile("(?s)(.*?)(\\d+)$");
var m = p.matcher(id);
if (!m.matches()) throw unprocessable("{0} is not a valid customer id: it does not end with a number!");
String prefix = m.group(1); // Prefix before last number
long number = Long.parseLong(m.group(2)); // The last numeric part
try {
update(TABLE_COMPANIES).set(LAST_CUSTOMER_NUMBER,CUSTOMER_NUMBER_PREFIX).where(ID,equal(companyId)).prepare(db).apply(number,prefix).close();
} catch (SQLException e) {
throw databaseException("Failed to update customer number counter for company {0}", companyId);
}
}
}

View File

@@ -18,9 +18,13 @@ public interface CompanyDb {
Collection<Long> getMembers(long companyId) throws UmbrellaException;
String getNextCustomerNumber(long companyId);
Map<Long,Company> listCompaniesOf(long id) throws UmbrellaException;
Company load(long companyId) throws UmbrellaException;
Company save(Company company);
void saveNewCustomer(long companyId, String id);
}