@ -21,13 +21,13 @@ import de.srsoftware.umbrella.core.BaseHandler;
import de.srsoftware.umbrella.core.api.CompanyService ;
import de.srsoftware.umbrella.core.api.CompanyService ;
import de.srsoftware.umbrella.core.api.UserService ;
import de.srsoftware.umbrella.core.api.UserService ;
import de.srsoftware.umbrella.core.exceptions.UmbrellaException ;
import de.srsoftware.umbrella.core.exceptions.UmbrellaException ;
import de.srsoftware.umbrella.core.model.Company ;
import de.srsoftware.umbrella.core.model.Token ;
import de.srsoftware.umbrella.core.model.Token ;
import de.srsoftware.umbrella.core.model.UmbrellaUser ;
import de.srsoftware.umbrella.core.model.UmbrellaUser ;
import de.srsoftware.umbrella.documents.model.* ;
import de.srsoftware.umbrella.documents.model.* ;
import java.io.IOException ;
import java.io.IOException ;
import java.time.LocalDate ;
import java.time.LocalDate ;
import java.util.HashMap ;
import java.util.HashMap ;
import java.util.Map ;
import java.util.Optional ;
import java.util.Optional ;
import java.util.stream.Collectors ;
import java.util.stream.Collectors ;
import org.json.JSONArray ;
import org.json.JSONArray ;
@ -92,7 +92,7 @@ public class DocumentApi extends BaseHandler {
}
}
private boolean getCompanies ( HttpExchange ex , UmbrellaUser user , Token token ) throws IOException , UmbrellaException {
private boolean getCompanies ( HttpExchange ex , UmbrellaUser user , Token token ) throws IOException , UmbrellaException {
return sendContent ( ex , getLegacyCompanies ( ex , user , token ) ) ;
return sendContent ( ex , companies . listCompaniesOf ( user ) . stream ( ) . map ( Company : : toMap ) ) ;
}
}
private boolean getContacts ( HttpExchange ex , UmbrellaUser user , Token token ) throws IOException , UmbrellaException {
private boolean getContacts ( HttpExchange ex , UmbrellaUser user , Token token ) throws IOException , UmbrellaException {
@ -120,14 +120,6 @@ public class DocumentApi extends BaseHandler {
return sendContent ( ex , doc . renderToMap ( ) ) ;
return sendContent ( ex , doc . renderToMap ( ) ) ;
}
}
private HashMap < Long , Map < String , Object > > getLegacyCompanies ( HttpExchange ex , UmbrellaUser umbrellaUser , Token token ) throws IOException , UmbrellaException {
var location = config . get ( "umbrella.modules.company.baseUrl" ) . map ( s - > s + "/json" ) . orElseThrow ( ( ) - > new UmbrellaException ( 500 , "umbrella.modules.company.baseUrl not configured!" ) ) ;
var resp = request ( location , token . asMap ( ) , MIME_FORM_URL , null ) ;
if ( ! ( resp instanceof JSONObject json ) ) throw new UmbrellaException ( 500 , "{0} did not return JSON!" , location ) ;
var result = new HashMap < Long , Map < String , Object > > ( ) ;
for ( var key : json . keySet ( ) ) result . put ( Long . parseLong ( key ) , json . getJSONObject ( key ) . toMap ( ) ) ;
return result ;
}
private JSONArray getLegacyContacts ( HttpExchange ex , UmbrellaUser umbrellaUser , Token token ) throws IOException , UmbrellaException {
private JSONArray getLegacyContacts ( HttpExchange ex , UmbrellaUser umbrellaUser , Token token ) throws IOException , UmbrellaException {
var location = config . get ( "umbrella.modules.contact.baseUrl" ) . map ( s - > s + "/json" ) . orElseThrow ( ( ) - > new UmbrellaException ( 500 , "umbrella.modules.contact.baseUrl not configured!" ) ) ;
var location = config . get ( "umbrella.modules.contact.baseUrl" ) . map ( s - > s + "/json" ) . orElseThrow ( ( ) - > new UmbrellaException ( 500 , "umbrella.modules.contact.baseUrl not configured!" ) ) ;
var resp = request ( location , token . asMap ( ) , MIME_FORM_URL , null ) ;
var resp = request ( location , token . asMap ( ) , MIME_FORM_URL , null ) ;
@ -140,9 +132,8 @@ public class DocumentApi extends BaseHandler {
var json = json ( ex ) ;
var json = json ( ex ) ;
if ( ! json . has ( COMPANY ) ) throw missingFieldException ( COMPANY ) ;
if ( ! json . has ( COMPANY ) ) throw missingFieldException ( COMPANY ) ;
long companyId = json . getLong ( COMPANY ) ;
long companyId = json . getLong ( COMPANY ) ;
var companies = getLegacyCompanies ( ex , user , token ) ;
var company = companies . get ( companyId ) ;
var company = companies . get ( companyId ) ;
if ( company = = null ) return forbidden ( ex ) ;
if ( ! companies . membership ( companyId , user . id ( ) ) ) throw new UmbrellaException ( HTTP_FORBIDDEN , "You are mot a member of company {0}" , company ) ;
var docs = db . listDocs ( companyId ) ;
var docs = db . listDocs ( companyId ) ;
var map = new HashMap < Long , Object > ( ) ;
var map = new HashMap < Long , Object > ( ) ;
for ( var entry : docs . entrySet ( ) ) map . put ( entry . getKey ( ) , entry . getValue ( ) . summary ( ) ) ;
for ( var entry : docs . entrySet ( ) ) map . put ( entry . getKey ( ) , entry . getValue ( ) . summary ( ) ) ;
@ -159,10 +150,7 @@ public class DocumentApi extends BaseHandler {
if ( ! senderData . has ( FIELD_COMPANY ) | | ! ( senderData . get ( FIELD_COMPANY ) instanceof Number companyId ) ) throw missingFieldException ( FIELD_COMPANY ) ;
if ( ! senderData . has ( FIELD_COMPANY ) | | ! ( senderData . get ( FIELD_COMPANY ) instanceof Number companyId ) ) throw missingFieldException ( FIELD_COMPANY ) ;
var company = companies . get ( companyId . longValue ( ) ) ;
var company = companies . get ( companyId . longValue ( ) ) ;
var members = companies . getMembers ( companyId . longValue ( ) ) ;
if ( ! companies . membership ( companyId . longValue ( ) , user . id ( ) ) ) throw new UmbrellaException ( HTTP_FORBIDDEN , "You are mot a member of company {0}" , company ) ;
var isMember = false ;
for ( var member : members ) isMember | = user . equals ( member ) ;
if ( ! isMember ) return sendContent ( ex , HTTP_FORBIDDEN , "You are mot a member of company " + companyId ) ;
if ( ! json . has ( FIELD_CUSTOMER ) | | ! ( json . get ( FIELD_CUSTOMER ) instanceof JSONObject customerData ) ) throw missingFieldException ( FIELD_CUSTOMER ) ;
if ( ! json . has ( FIELD_CUSTOMER ) | | ! ( json . get ( FIELD_CUSTOMER ) instanceof JSONObject customerData ) ) throw missingFieldException ( FIELD_CUSTOMER ) ;
if ( ! json . has ( FIELD_TYPE ) | | ! ( json . get ( FIELD_TYPE ) instanceof Number docTypeId ) ) throw missingFieldException ( FIELD_TYPE ) ;
if ( ! json . has ( FIELD_TYPE ) | | ! ( json . get ( FIELD_TYPE ) instanceof Number docTypeId ) ) throw missingFieldException ( FIELD_TYPE ) ;