implemented creation of companies
Signed-off-by: Stephan Richter <s.richter@srsoftware.de>
This commit is contained in:
@@ -3,6 +3,7 @@ package de.srsoftware.umbrella.company;
|
||||
|
||||
import static de.srsoftware.umbrella.company.Constants.CONFIG_DATABASE;
|
||||
import static de.srsoftware.umbrella.core.ConnectionProvider.connect;
|
||||
import static de.srsoftware.umbrella.core.Constants.COMPANY;
|
||||
import static de.srsoftware.umbrella.core.Constants.ID;
|
||||
import static de.srsoftware.umbrella.core.Paths.LIST;
|
||||
import static de.srsoftware.umbrella.core.exceptions.UmbrellaException.*;
|
||||
@@ -70,11 +71,23 @@ public class CompanyModule extends BaseHandler implements CompanyService {
|
||||
}
|
||||
}
|
||||
|
||||
private boolean patchProject(long companyId, UmbrellaUser user, HttpExchange ex) throws IOException {
|
||||
var company = get(companyId);
|
||||
if (!membership(companyId,user.id())) throw forbidden("You are mot a member of company {0}",company.name());
|
||||
var json = json(ex);
|
||||
return sendContent(ex,companyDb.save(company.patch(json)));
|
||||
@Override
|
||||
public boolean doPost(Path path, HttpExchange ex) throws IOException {
|
||||
addCors(ex);
|
||||
try {
|
||||
Optional<Token> token = SessionToken.from(ex).map(Token::of);
|
||||
var user = users.loadUser(token);
|
||||
if (user.isEmpty()) return unauthorized(ex);
|
||||
var head = path.pop();
|
||||
return switch (head) {
|
||||
case null -> postNewCompany(user.get(),ex);
|
||||
default -> super.doPost(path,ex);
|
||||
};
|
||||
} catch (NumberFormatException n) {
|
||||
return send(ex,invalidFieldException(ID,"ID (Long)"));
|
||||
} catch (UmbrellaException e) {
|
||||
return send(ex,e);
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
@@ -88,8 +101,6 @@ public class CompanyModule extends BaseHandler implements CompanyService {
|
||||
return sendContent(ex,result);
|
||||
}
|
||||
|
||||
|
||||
|
||||
@Override
|
||||
public Collection<UmbrellaUser> getMembers(long companyId) throws UmbrellaException {
|
||||
var members = new HashSet<UmbrellaUser>();
|
||||
@@ -107,6 +118,21 @@ public class CompanyModule extends BaseHandler implements CompanyService {
|
||||
return companyDb.getMembers(companyId).contains(userId);
|
||||
}
|
||||
|
||||
private boolean patchProject(long companyId, UmbrellaUser user, HttpExchange ex) throws IOException {
|
||||
var company = get(companyId);
|
||||
if (!membership(companyId,user.id())) throw forbidden("You are mot a member of company {0}",company.name());
|
||||
var json = json(ex);
|
||||
return sendContent(ex,companyDb.save(company.patch(json)));
|
||||
}
|
||||
|
||||
private boolean postNewCompany(UmbrellaUser user, HttpExchange ex) throws IOException {
|
||||
var json = json(ex);
|
||||
json.put(ID,0);
|
||||
var company = companyDb.save(Company.of(json));
|
||||
companyDb.addUser(company.id(),user.id());
|
||||
return sendContent(ex,company);
|
||||
}
|
||||
|
||||
@Override
|
||||
public UserService userService() {
|
||||
return users;
|
||||
|
||||
@@ -2,9 +2,9 @@
|
||||
package de.srsoftware.umbrella.company;
|
||||
|
||||
import static de.srsoftware.tools.jdbc.Condition.equal;
|
||||
import static de.srsoftware.tools.jdbc.Query.*;
|
||||
import static de.srsoftware.tools.jdbc.Query.Dialect.SQLITE;
|
||||
import static de.srsoftware.tools.jdbc.Query.SelectQuery.ALL;
|
||||
import static de.srsoftware.tools.jdbc.Query.select;
|
||||
import static de.srsoftware.tools.jdbc.Query.update;
|
||||
import static de.srsoftware.umbrella.company.Constants.TABLE_COMPANIES;
|
||||
import static de.srsoftware.umbrella.company.Constants.TABLE_COMPANIES_USERS;
|
||||
import static de.srsoftware.umbrella.core.Constants.*;
|
||||
@@ -13,6 +13,8 @@ import static de.srsoftware.umbrella.core.exceptions.UmbrellaException.databaseE
|
||||
import de.srsoftware.umbrella.company.api.CompanyDb;
|
||||
import de.srsoftware.umbrella.core.exceptions.UmbrellaException;
|
||||
import de.srsoftware.umbrella.core.model.Company;
|
||||
import org.json.JSONObject;
|
||||
|
||||
import java.sql.Connection;
|
||||
import java.sql.SQLException;
|
||||
import java.util.Collection;
|
||||
@@ -28,6 +30,19 @@ public class SqliteDb implements CompanyDb {
|
||||
db = connection;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void addUser(long company_id, long user_id) {
|
||||
try {
|
||||
insertInto(TABLE_COMPANIES_USERS,COMPANY_ID, USER_ID)
|
||||
.values(company_id, user_id)
|
||||
.ignoreDuplicates(SQLITE)
|
||||
.execute(db)
|
||||
.close();
|
||||
} catch (SQLException e) {
|
||||
throw databaseException("Failed to assign user {0} to company {1}");
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public Collection<Long> getMembers(long companyId) throws UmbrellaException {
|
||||
try {
|
||||
@@ -76,7 +91,19 @@ public class SqliteDb implements CompanyDb {
|
||||
public Company save(Company company) {
|
||||
try {
|
||||
if (company.id() == 0){ // new
|
||||
throw new RuntimeException("Not implemented");
|
||||
long id = 0;
|
||||
var rs = insertInto(TABLE_COMPANIES,NAME, ADDRESS, EMAIL, FIELD_PHONE, FIELD_BANK_ACCOUNT, FIELD_COURT, FIELD_CURRENCY, FIELD_TAX_NUMBER, DECIMALS, DECIMAL_SEPARATOR, THOUSANDS_SEPARATOR, LAST_CUSTOMER_NUMBER, CUSTOMER_NUMBER_PREFIX)
|
||||
.values(company.name(),company.address(),company.email(),company.phone(),company.bankAccount(),company.court(),company.currency(),company.taxId(),company.decimals(),company.decimalSeparator(),company.thousandsSeparator(),0,company.customerNumberPrefix())
|
||||
.execute(db)
|
||||
.getGeneratedKeys();
|
||||
if (rs.next()) id = rs.getLong(1);
|
||||
rs.close();
|
||||
if (id != 0){
|
||||
var json = new JSONObject(company.toMap());
|
||||
json.put(ID,id);
|
||||
return Company.of(json);
|
||||
}
|
||||
return company;
|
||||
} else { // update
|
||||
if (company.isDirty()) {
|
||||
update(TABLE_COMPANIES)
|
||||
|
||||
@@ -7,11 +7,13 @@ import java.util.Collection;
|
||||
import java.util.Map;
|
||||
|
||||
public interface CompanyDb {
|
||||
void addUser(long company_id, long user_id);
|
||||
|
||||
Collection<Long> getMembers(long companyId) throws UmbrellaException;
|
||||
|
||||
Map<Long,Company> listCompaniesOf(long id) throws UmbrellaException;
|
||||
|
||||
Company load(long companyId) throws UmbrellaException;
|
||||
|
||||
Company load(long companyId) throws UmbrellaException;
|
||||
Company save(Company company);
|
||||
}
|
||||
|
||||
@@ -137,7 +137,7 @@ public class Company implements Mappable {
|
||||
switch (key){
|
||||
case NAME: name = json.getString(NAME); break;
|
||||
case ADDRESS: address = json.getString(ADDRESS); break;
|
||||
case FIELD_COURT: court = json.getString(FIELD_COURT);
|
||||
case FIELD_COURT: court = json.getString(FIELD_COURT); break;
|
||||
case FIELD_TAX_NUMBER: taxId = json.getString(FIELD_TAX_NUMBER); break;
|
||||
case FIELD_PHONE: phone = json.getString(FIELD_PHONE); break;
|
||||
case DECIMAL_SEPARATOR: decimalSeparator = json.getString(DECIMAL_SEPARATOR); break;
|
||||
|
||||
@@ -21,8 +21,9 @@
|
||||
let { company } = $props();
|
||||
</script>
|
||||
|
||||
|
||||
<fieldset>
|
||||
{#if company}
|
||||
<legend>{t('edit_object',{object:company.name})}</legend>
|
||||
<fieldset>
|
||||
<legend>{t('name')}</legend>
|
||||
<LineEditor bind:value={company.name} editable={true} onSet={val => patch({name:val})} />
|
||||
@@ -31,5 +32,33 @@
|
||||
<legend>{t('address')}</legend>
|
||||
<Multiline bind:value={company.address} editable={true} onSet={val => patch({address:val})} />
|
||||
</fieldset>
|
||||
|
||||
{/if}
|
||||
<fieldset>
|
||||
<legend>{t('email')}</legend>
|
||||
<LineEditor bind:value={company.email} editable={true} onSet={val => patch({email:val})} />
|
||||
</fieldset>
|
||||
<fieldset>
|
||||
<legend>{t('phone')}</legend>
|
||||
<LineEditor bind:value={company.phone} editable={true} onSet={val => patch({phone:val})} />
|
||||
</fieldset>
|
||||
<fieldset>
|
||||
<legend>{t('bank_account')}</legend>
|
||||
<Multiline bind:value={company.bank_account} editable={true} onSet={val => patch({bank_account:val})} />
|
||||
</fieldset>
|
||||
<fieldset>
|
||||
<legend>{t('local_court')}</legend>
|
||||
<LineEditor bind:value={company.court} editable={true} onSet={val => patch({court:val})} />
|
||||
</fieldset>
|
||||
<fieldset>
|
||||
<legend>{t('tax_id')}</legend>
|
||||
<LineEditor bind:value={company.tax_number} editable={true} onSet={val => patch({tax_number:val})} />
|
||||
</fieldset>
|
||||
<fieldset>
|
||||
<legend>{t('last_customer_number')}</legend>
|
||||
<LineEditor bind:value={company.last_customer_number} editable={true} onSet={val => patch({last_customer_number:val})} />
|
||||
</fieldset>
|
||||
<fieldset>
|
||||
<legend>{t('customer_number_prefix')}</legend>
|
||||
<LineEditor bind:value={company.customer_number_prefix} editable={true} onSet={val => patch({customer_number_prefix:val})} />
|
||||
</fieldset>
|
||||
{/if}
|
||||
</fieldset>
|
||||
@@ -1,20 +1,38 @@
|
||||
<script>
|
||||
import {onMount} from 'svelte';
|
||||
import {api} from '../../urls.svelte.js';
|
||||
import {t} from '../../translations.svelte.js';
|
||||
import {onMount} from 'svelte';
|
||||
import {api} from '../../urls.svelte.js';
|
||||
import {t} from '../../translations.svelte.js';
|
||||
|
||||
import Editor from './Editor.svelte';
|
||||
import Editor from './Editor.svelte';
|
||||
import LineEditor from '../../Components/LineEditor.svelte';
|
||||
|
||||
let companies = $state(null);
|
||||
let error = $state(null);
|
||||
let selected = $state(0);
|
||||
let error = $state(null);
|
||||
let companies = $state(null);
|
||||
let new_company = $state({name:null,email:null});
|
||||
let selected = $state(0);
|
||||
let missing_fields = $derived(!new_company.name || !new_company.email?.includes('@'));
|
||||
|
||||
async function create_company(){
|
||||
const url = api('company');
|
||||
const resp = await fetch(url,{
|
||||
credentials : 'include',
|
||||
method : 'POST',
|
||||
body : JSON.stringify(new_company)
|
||||
});
|
||||
if (resp.ok){
|
||||
const company = await resp.json();
|
||||
companies[company.id] = company;
|
||||
error = null;
|
||||
} else {
|
||||
error = await resp.text();
|
||||
}
|
||||
}
|
||||
|
||||
async function loadCompanies(){
|
||||
const url = api('company/list');
|
||||
const resp = await fetch(url,{credentials:'include'});
|
||||
if (resp.ok){
|
||||
companies = await resp.json();
|
||||
console.log(companies);
|
||||
error = null;
|
||||
} else {
|
||||
error = await resp.text();
|
||||
@@ -29,7 +47,9 @@
|
||||
</script>
|
||||
|
||||
<fieldset>
|
||||
<legend>{t('companies')}</legend>
|
||||
<legend>
|
||||
{t('companies')}
|
||||
</legend>
|
||||
{#if error}
|
||||
<span class="error">{error}</span>
|
||||
{/if}
|
||||
@@ -49,6 +69,17 @@
|
||||
</tr>
|
||||
</thead>
|
||||
<tbody>
|
||||
<tr>
|
||||
<td>
|
||||
<input type="text" bind:value={new_company.name} />
|
||||
</td>
|
||||
<td>
|
||||
<input type="email" bind:value={new_company.email} />
|
||||
</td>
|
||||
<td>
|
||||
<button disabled={missing_fields} onclick={create_company}>{t('create_new_object',{object:t('company')})}</button>
|
||||
</td>
|
||||
</tr>
|
||||
{#each Object.entries(companies) as [cid, company]}
|
||||
<tr onclick={e => showDetail(cid)}>
|
||||
<td>{company.name}</td>
|
||||
|
||||
@@ -2,12 +2,8 @@
|
||||
"abort": "abbrechen",
|
||||
"actions": "Aktionen",
|
||||
"add_login_service": "Login-Service anlegen",
|
||||
"add_member": "Mitarbeiter hinzufügen",
|
||||
"add_position": "hinzufügen",
|
||||
"add_state": "Status hinzufügen",
|
||||
"add_subtask": "Unteraufgabe hinzufügen",
|
||||
"add_task": "Aufgabe hinzufügen",
|
||||
"add_user": "Benutzer hinzufügen",
|
||||
"add_object": "{object} hinzufügen",
|
||||
"address": "Adresse",
|
||||
"advertisement" : "Umbrella ist ein Produkt von {producer}.",
|
||||
"allowed_states": "zulässige Status",
|
||||
"amount": "Menge",
|
||||
@@ -35,10 +31,9 @@
|
||||
"contained_tax": "enthaltene Steuer",
|
||||
"content": "Inhalt",
|
||||
"context": "Kontext",
|
||||
"customer_number_prefix": "Präfix für Kundennummer",
|
||||
"create": "anlegen",
|
||||
"create_new_document": "neues Dokument",
|
||||
"create_new_project": "neues Projekt anlegen",
|
||||
"create_new_user": "Neuen Benutzer anlegen",
|
||||
"create_new_object": "{object} neu anlegen",
|
||||
"CREATE_USERS": "Nutzer anlegen",
|
||||
"create_pdf": "PDF erzeugen",
|
||||
"created_with": "erzeugt mit {tool} von {producer}",
|
||||
@@ -54,6 +49,7 @@
|
||||
"DELETE_USERS": "Nutzer löschen",
|
||||
"delivery_date": "Lieferdatum",
|
||||
"description": "Beschreibung",
|
||||
"detail": "Details",
|
||||
"display_closed_tasks": "abgeschlossene Aufgaben anzeigen",
|
||||
"document": "Dokument",
|
||||
"document_list": "Dokumente",
|
||||
@@ -64,6 +60,7 @@
|
||||
"due_date": "Fälligkeitsdatum",
|
||||
|
||||
"edit": "Bearbeiten",
|
||||
"edit_object" : "{object} bearbeiten",
|
||||
"editing": "Nutzer {0} bearbeiten",
|
||||
"edit_password": "Passwort ändern",
|
||||
"edit_service": "Login-Service \"{0}\" bearbeiten",
|
||||
@@ -100,6 +97,7 @@
|
||||
"key": "Suchbegriff",
|
||||
|
||||
"language": "Sprache",
|
||||
"last_customer_number": "zuletzt vergebene Kundennummer",
|
||||
"list": "Dokumente",
|
||||
"list_of": "Dokumente von {0}",
|
||||
"LIST_USERS": "Nutzer auflisten",
|
||||
@@ -163,6 +161,7 @@
|
||||
"permission_edit": "bearbeiten",
|
||||
"permission_owner": "Besitzer",
|
||||
"permission_read_only": "lesen",
|
||||
"phone": "Telefon",
|
||||
"pos": "Pos",
|
||||
"position": "Position",
|
||||
"positions": "Positionen",
|
||||
|
||||
Reference in New Issue
Block a user