From 63b5805658833bb3fa0745a37b1b67bf23cf6ad5 Mon Sep 17 00:00:00 2001 From: Tina_Azure <-> Date: Mon, 10 Apr 2023 19:28:40 +0200 Subject: [PATCH] Spec marked Handler for the creation of a new request --- spec/spec.md | 2 +- src/main.cpp | 88 ++++++++++++++++++++++++++++++++++------------------ 2 files changed, 59 insertions(+), 31 deletions(-) diff --git a/spec/spec.md b/spec/spec.md index 01ccddd..73a5263 100644 --- a/spec/spec.md +++ b/spec/spec.md @@ -107,7 +107,7 @@ See the freelancer section under concepts for more information of what this shou Accessing this page will provide the customer with a rendered HTML document of a freelancer's job template. This rendering will take data that the freelancer provides for the job, ideally stored in the database, and fills it in for delivery to the customer. The job template should use a html template that the system administrator provides for filling in. -### "/customer/$freelancer/template/$templateName/request" ++++ "/customer/$freelancer/template/$templateName/request" request data partially provided by the template, gets filled out by the customer on this page. diff --git a/src/main.cpp b/src/main.cpp index 4738e4e..fa1dd0d 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -9,6 +9,9 @@ using namespace std; int main() { + Utilities::config configuration; + configuration.readConfigFile(); + crow::SimpleApp app; // CROW_ROUTE(app, "/test.json") @@ -145,21 +148,24 @@ int main() { return page.render(ctx); }); - - - - + /* + * executes the creation of a new request. + */ CROW_ROUTE(app, "/customer//template//request/fulfilment").methods("POST"_method) ([databaseURI, configuration](const crow::request& postRequest, string freelancerName, string templateName ) { + pqxx::connection databaseConnection(databaseURI); + string templateHTML = "customer_Freelancer_Template_Request_Fulfilment_ERROR.html"; Database::requestsItem newRequest; + crow::mustache::context ctx; - - + ctx["freelancername"] = freelancerName; + ctx["templatename"] = templateName; string postRequestBody = postRequest.body.c_str(); Utilities::decodeString(postRequestBody); vector splitPostRequestBody = Utilities::splitStringIntoVector(postRequestBody, '&'); + bool requestFillCompletion = false; for (string item : splitPostRequestBody) { vector splitItem = Utilities::splitStringIntoVector(item, '='); if (splitItem.at(0) == "customername") @@ -173,33 +179,55 @@ int main() { if (splitItem.at(0) == "templateID") newRequest.templateID = stoi(splitItem.at(1)); } + if (newRequest.customerName != "" && (newRequest.customerEmailAddress != "" || newRequest.customerContactDetails != "") && newRequest.requestDescription != "" ) + requestFillCompletion = true; + + + + ctx["templateid"] = newRequest.templateID; - pqxx::connection databaseConnection(databaseURI); pqxx::result resultTemplate = Database::executePreparedStatement_SELECT_TEMPLATE_FLAT(databaseConnection, newRequest.templateID); + if (resultTemplate.size() > 0 && requestFillCompletion) { + for (int i = 0; i < resultTemplate.columns(); ++i) { + //freelancerid + newRequest.freelancerID = stoi(resultTemplate.at(0).at(0).c_str()); + //currencypreference + newRequest.currencyPreference = resultTemplate.at(0).at(1).c_str(); + //priceupfront + newRequest.priceUpFront = resultTemplate.at(0).at(2).c_str(); + //priceondeliver + newRequest.priceOnDeliver = resultTemplate.at(0).at(3).c_str(); + } - for (int i = 0; i < resultTemplate.columns(); ++i) { - //freelancerid - newRequest.freelancerID = stoi(resultTemplate.at(0).at(0).c_str()); - //currencypreference - newRequest.currencyPreference = resultTemplate.at(0).at(1).c_str(); - //priceupfront - newRequest.priceUpFront = resultTemplate.at(0).at(2).c_str(); - //priceondeliver - newRequest.priceOnDeliver = resultTemplate.at(0).at(3).c_str(); + pqxx::result resultCommissionState = Database::executePreparedStatement_SELECT_FREELANCER_COMMISSION_STATE(databaseConnection, newRequest.freelancerID); + + //the commissionstate + if (resultCommissionState.size() == 0 || stoi(resultTemplate.at(0).at(0).c_str()) == 1) { + ctx["ERROR_COMMISSIONS_CLOSED"] = true; + } + else { + int resultInsertOperation = Database::executePreparedStatement_INSERT_ITEM_IN_REQUESTS(databaseConnection, newRequest); + if (resultInsertOperation == 0) { + templateHTML = "customer_Freelancer_Template_Request_Fulfilment.html"; + pqxx::result resultEmailAddress = Database::executePreparedStatement_SELECT_FREELANCER_EMAIL(databaseConnection, newRequest.freelancerID); + if (resultEmailAddress.size() > 0) + Utilities::sendEmail(configuration, resultEmailAddress.at(0).at(0).c_str(), "NEW REQUEST", newRequest.toJSONString()); + } + else { + ctx["ERROR_UNABLE_TO_CREATE_REQUEST"] = true; + } + } } - newRequest.outputItem(); + else { + if (requestFillCompletion) + ctx["ERROR_TEMPLATE_NOT_FOUND"] = true; + else + ctx["REQUEST_NOT_FILLED"] = true; - - - - - - Utilities::sendEmail(configuration, "testfaewfaw@tempr.email", "NEW REQUEST", "NEW REQUEST"); - - string templateHTML = "customer_Freelancer_Template_Request_Fulfilment.html"; + } auto page = crow::mustache::load(templateHTML); - return page.render(); + return page.render(ctx); }); @@ -221,7 +249,7 @@ int main() { - CROW_ROUTE(app, "/getEntry/") + /*CROW_ROUTE(app, "/getEntry/") ([databaseURI](int entry) { pqxx::connection databaseConnection(databaseURI); pqxx::result result = Database::executePreparedStatement_SELECT_ITEM_BY_ID(databaseConnection, entry); @@ -269,11 +297,11 @@ int main() { if (!jsonBody) return crow::response(crow::status::BAD_REQUEST); - /* + * Example CURL to insert into DB: * Price must be delivered as a string to avoid unnecessary conversion to double which would lead to the addition to a database of the value 0.359999999999999999999999999 instead of 0.36. * curl -H "Content-Type: application/json" -H "Accept: application/json" -d '{"customerEmailAddress":"2@testmail.com","freelancer":"nick","templateName":"coding","currencyPreference":"XMR","priceUpFront":"0.36","priceOnDeliver":"0.36","requestDescription":"This is a test","accepted":false,"upFrontInvoiceID":"12424242424","onDeliverInvoiceID":"329532532532","upFrontPaid":false,"onDeliverPaid":false}' http://0.0.0.0:18080/newRequest - */ + Database::requestsItem item; item.insertJsonIntoItem(jsonBody); @@ -282,7 +310,7 @@ int main() { return crow::response(200); }); - + */ //set the port, set the app to run on multiple threads, and run the app app.port(18080).multithreaded().run();