|
|
|
|
@ -74,13 +74,12 @@ public class DocumentApi extends BaseHandler implements DocumentService {
@@ -74,13 +74,12 @@ public class DocumentApi extends BaseHandler implements DocumentService {
|
|
|
|
|
|
|
|
|
|
private final Configuration config; |
|
|
|
|
private final DocumentDb db; |
|
|
|
|
private final ModuleRegistry modules; |
|
|
|
|
|
|
|
|
|
public DocumentApi(ModuleRegistry registry, Configuration config) throws UmbrellaException { |
|
|
|
|
super(registry); |
|
|
|
|
this.config = config; |
|
|
|
|
var dbFile = config.get(CONFIG_DATABASE).orElseThrow(() -> missingFieldException(CONFIG_DATABASE)); |
|
|
|
|
db = new SqliteDb(connect(dbFile)); |
|
|
|
|
modules = registry.add(this); |
|
|
|
|
|
|
|
|
|
Optional<String> templates = config.get(CONFIG_TEMPLATES); |
|
|
|
|
if (templates.isEmpty()) throw missingFieldException(CONFIG_TEMPLATES); |
|
|
|
|
@ -95,7 +94,7 @@ public class DocumentApi extends BaseHandler implements DocumentService {
@@ -95,7 +94,7 @@ public class DocumentApi extends BaseHandler implements DocumentService {
|
|
|
|
|
addCors(ex); |
|
|
|
|
try { |
|
|
|
|
Optional<Token> token = SessionToken.from(ex).map(Token::of); |
|
|
|
|
var user = modules.userService().loadUser(token); |
|
|
|
|
var user = userService().loadUser(token); |
|
|
|
|
if (user.isEmpty()) return unauthorized(ex); |
|
|
|
|
var head = path.pop(); |
|
|
|
|
long docId = Long.parseLong(head); |
|
|
|
|
@ -114,7 +113,7 @@ public class DocumentApi extends BaseHandler implements DocumentService {
@@ -114,7 +113,7 @@ public class DocumentApi extends BaseHandler implements DocumentService {
|
|
|
|
|
private boolean deleteDocument(HttpExchange ex, long docId, UmbrellaUser user) throws IOException, UmbrellaException { |
|
|
|
|
var doc = db.loadDoc(docId); |
|
|
|
|
var companyId = doc.companyId(); |
|
|
|
|
if (!modules.companyService().membership(companyId,user.id())) throw forbidden("You are mot a member of company {0}",doc.companyId()); |
|
|
|
|
if (!companyService().membership(companyId,user.id())) throw forbidden("You are mot a member of company {0}",doc.companyId()); |
|
|
|
|
if (doc.state() != NEW) throw new UmbrellaException(HTTP_BAD_REQUEST,"This document has already been sent"); |
|
|
|
|
return sendContent(ex,db.deleteDoc(docId)); |
|
|
|
|
} |
|
|
|
|
@ -122,7 +121,7 @@ public class DocumentApi extends BaseHandler implements DocumentService {
@@ -122,7 +121,7 @@ public class DocumentApi extends BaseHandler implements DocumentService {
|
|
|
|
|
private boolean deletePosition(HttpExchange ex, long docId, UmbrellaUser user) throws UmbrellaException, IOException { |
|
|
|
|
var doc = db.loadDoc(docId); |
|
|
|
|
var companyId = doc.companyId(); |
|
|
|
|
if (!modules.companyService().membership(companyId,user.id())) throw forbidden("You are mot a member of company {0}",doc.companyId()); |
|
|
|
|
if (!companyService().membership(companyId,user.id())) throw forbidden("You are mot a member of company {0}",doc.companyId()); |
|
|
|
|
if (doc.state() != NEW) throw new UmbrellaException(HTTP_BAD_REQUEST,"This document has already been sent"); |
|
|
|
|
var json = json(ex); |
|
|
|
|
if (!(json.has(POSITION) && json.get(POSITION) instanceof Number number)) throw missingFieldException(POSITION); |
|
|
|
|
@ -135,7 +134,7 @@ public class DocumentApi extends BaseHandler implements DocumentService {
@@ -135,7 +134,7 @@ public class DocumentApi extends BaseHandler implements DocumentService {
|
|
|
|
|
addCors(ex); |
|
|
|
|
try { |
|
|
|
|
Optional<Token> token = SessionToken.from(ex).map(Token::of); |
|
|
|
|
var user = modules.userService().loadUser(token); |
|
|
|
|
var user = userService().loadUser(token); |
|
|
|
|
if (user.isEmpty()) return unauthorized(ex); |
|
|
|
|
var head = path.pop(); |
|
|
|
|
return switch (head){ |
|
|
|
|
@ -166,7 +165,7 @@ public class DocumentApi extends BaseHandler implements DocumentService {
@@ -166,7 +165,7 @@ public class DocumentApi extends BaseHandler implements DocumentService {
|
|
|
|
|
addCors(ex); |
|
|
|
|
try { |
|
|
|
|
Optional<Token> token = SessionToken.from(ex).map(Token::of); |
|
|
|
|
var user = modules.userService().loadUser(token); |
|
|
|
|
var user = userService().loadUser(token); |
|
|
|
|
if (user.isEmpty()) return unauthorized(ex); |
|
|
|
|
var head = path.pop(); |
|
|
|
|
var docId = Long.parseLong(head); |
|
|
|
|
@ -188,7 +187,7 @@ public class DocumentApi extends BaseHandler implements DocumentService {
@@ -188,7 +187,7 @@ public class DocumentApi extends BaseHandler implements DocumentService {
|
|
|
|
|
addCors(ex); |
|
|
|
|
try { |
|
|
|
|
Optional<Token> token = SessionToken.from(ex).map(Token::of); |
|
|
|
|
var user = modules.userService().loadUser(token); |
|
|
|
|
var user = userService().loadUser(token); |
|
|
|
|
if (user.isEmpty()) return unauthorized(ex); |
|
|
|
|
var head = path.pop(); |
|
|
|
|
return switch (head){ |
|
|
|
|
@ -220,7 +219,7 @@ public class DocumentApi extends BaseHandler implements DocumentService {
@@ -220,7 +219,7 @@ public class DocumentApi extends BaseHandler implements DocumentService {
|
|
|
|
|
var attachment = new Attachment(doc.number()+".pdf",rendered.mimeType(),rendered.bytes()); |
|
|
|
|
var message = new Message(user,subject,content,null,List.of(attachment)); |
|
|
|
|
var envelope = new Envelope(message,new User(doc.customer().shortName(),new EmailAddress(email),doc.customer().language())); |
|
|
|
|
modules.postBox().send(envelope); |
|
|
|
|
postBox().send(envelope); |
|
|
|
|
db.save(doc.set(SENT)); |
|
|
|
|
return ok(ex); |
|
|
|
|
} |
|
|
|
|
@ -245,8 +244,8 @@ public class DocumentApi extends BaseHandler implements DocumentService {
@@ -245,8 +244,8 @@ public class DocumentApi extends BaseHandler implements DocumentService {
|
|
|
|
|
private Tuple<Document,Company> getDocument(long docId, UmbrellaUser user) throws UmbrellaException { |
|
|
|
|
var doc = db.loadDoc(docId); |
|
|
|
|
var companyId = doc.companyId(); |
|
|
|
|
var company = modules.companyService().get(companyId); |
|
|
|
|
if (!modules.companyService().membership(companyId,user.id())) throw forbidden("You are mot a member of company {0}",company.name()); |
|
|
|
|
var company = companyService().get(companyId); |
|
|
|
|
if (!companyService().membership(companyId,user.id())) throw forbidden("You are mot a member of company {0}",company.name()); |
|
|
|
|
return Tuple.of(doc,company); |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
@ -365,7 +364,7 @@ public class DocumentApi extends BaseHandler implements DocumentService {
@@ -365,7 +364,7 @@ public class DocumentApi extends BaseHandler implements DocumentService {
|
|
|
|
|
.filter(filter) |
|
|
|
|
.findAny(); |
|
|
|
|
if (optDoc.isEmpty()) throw UmbrellaException.notFound("Cannot render {0} {1}: Missing template \"{2}\"",type,document.number(),template); |
|
|
|
|
Function<String,String> translate = text -> modules.translator().translate(user.language(),text); |
|
|
|
|
Function<String,String> translate = text -> translator().translate(user.language(),text); |
|
|
|
|
var pdfData = new HashMap<String,Object>(); |
|
|
|
|
pdfData.put(FIELD_DOCUMENT,document.renderToMap()); |
|
|
|
|
pdfData.put("translate",translate); |
|
|
|
|
@ -419,8 +418,8 @@ public class DocumentApi extends BaseHandler implements DocumentService {
@@ -419,8 +418,8 @@ public class DocumentApi extends BaseHandler implements DocumentService {
|
|
|
|
|
var json = json(ex); |
|
|
|
|
if (!json.has(COMPANY)) throw missingFieldException(COMPANY); |
|
|
|
|
long companyId = json.getLong(COMPANY); |
|
|
|
|
var company = modules.companyService().get(companyId); |
|
|
|
|
if (!modules.companyService().membership(companyId,user.id())) throw forbidden("You are mot a member of company {0}",company); |
|
|
|
|
var company = companyService().get(companyId); |
|
|
|
|
if (!companyService().membership(companyId,user.id())) throw forbidden("You are mot a member of company {0}",company); |
|
|
|
|
var docs = list(companyId); |
|
|
|
|
var map = new HashMap<Long,Object>(); |
|
|
|
|
for (var entry : docs.entrySet()) map.put(entry.getKey(),entry.getValue().summary()); |
|
|
|
|
@ -466,8 +465,8 @@ public class DocumentApi extends BaseHandler implements DocumentService {
@@ -466,8 +465,8 @@ public class DocumentApi extends BaseHandler implements DocumentService {
|
|
|
|
|
if (!(json.has(SENDER) && json.get(SENDER) instanceof JSONObject senderData)) throw missingFieldException(SENDER); |
|
|
|
|
if (!senderData.has(FIELD_COMPANY) || !(senderData.get(FIELD_COMPANY) instanceof Number companyId)) throw missingFieldException(FIELD_COMPANY); |
|
|
|
|
|
|
|
|
|
var company = modules.companyService().get(companyId.longValue()); |
|
|
|
|
if (!modules.companyService().membership(companyId.longValue(),user.id())) throw forbidden("You are mot a member of company {0}",company); |
|
|
|
|
var company = companyService().get(companyId.longValue()); |
|
|
|
|
if (!companyService().membership(companyId.longValue(),user.id())) throw forbidden("You are mot a member of company {0}",company); |
|
|
|
|
|
|
|
|
|
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); |
|
|
|
|
@ -513,8 +512,8 @@ public class DocumentApi extends BaseHandler implements DocumentService {
@@ -513,8 +512,8 @@ public class DocumentApi extends BaseHandler implements DocumentService {
|
|
|
|
|
private boolean postTemplateList(HttpExchange ex, UmbrellaUser user) throws UmbrellaException, IOException { |
|
|
|
|
var json = json(ex); |
|
|
|
|
if (!(json.has(COMPANY) && json.get(COMPANY) instanceof Number companyId)) throw missingFieldException(COMPANY); |
|
|
|
|
var company = modules.companyService().get(companyId.longValue()); |
|
|
|
|
if (!modules.companyService().membership(companyId.longValue(),user.id())) throw forbidden("You are not a member of {0}",company.name()); |
|
|
|
|
var company = companyService().get(companyId.longValue()); |
|
|
|
|
if (!companyService().membership(companyId.longValue(),user.id())) throw forbidden("You are not a member of {0}",company.name()); |
|
|
|
|
var templates = db.getCompanyTemplates(companyId.longValue()); |
|
|
|
|
return sendContent(ex,templates.stream().map(Template::toMap)); |
|
|
|
|
} |
|
|
|
|
|