Skip to content

Commit

Permalink
emails
Browse files Browse the repository at this point in the history
  • Loading branch information
hpham096 committed Aug 30, 2023
1 parent eb4be3e commit 62632a1
Show file tree
Hide file tree
Showing 4 changed files with 51 additions and 5 deletions.
6 changes: 3 additions & 3 deletions main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ void displayReportMenu();

// GLOBAL VARIABLES
// replace string with a non-expired access code from google playground
const std::string access = "ya29.a0AfB_byA5bIMJA1Zo7Y2fku5vVcj8RLCNDaUXS5QH5btcrxX273mpgHQGcSIkRFbLiU1tsRzlVScNhNH9Bu_VttcdxzKbJtcspukC6MZn1-t5vONGD04OOmrUs27lkXlNmXuGLvip5t_wXgmNMn7VGrudcHlVybhR92WXc6rlaCgYKAYMSARESFQHsvYls0vGnxHnOxNQDuNehlqTm9A0175";
const std::string access = "ya29.a0AfB_byCJKNyCKv1i0MP9VmuF0AAot69GsQ3CYYZjuops6bjB-Azy9QIvFHlUY01Z8DLrSpAMFygaAR4rfyvnV57ht0nwuJScT-QWGCDg07FHE8w0EJYCjJJ5JxCWj-xgINi3ebq9DKEUmfsUmRNTNQl3Kukz_rKW5RTAzR6LaCgYKAW8SARESFQHsvYlsUOon-hzAPbUOlDt-7C1qfA0175";
vector<User> subscribers;
ReportGenerator reportGen;

Expand Down Expand Up @@ -389,7 +389,7 @@ void displayEmailMenu() {
break;
case 3:
std::cout << "Sending Emails...\n";
newsletter(access, subscribers);
newsletter(access, subscribers, reportGen);
displayEmailMenu();
break;
case 4:
Expand Down Expand Up @@ -418,6 +418,6 @@ void handleEmailOperations() {
}

void displayCheckoutSystem() {
CheckoutSystem();
CheckoutSystem(access, subscribers);
displayMainMenu();
}
Binary file modified main.exe
Binary file not shown.
5 changes: 4 additions & 1 deletion src/CheckoutSystem.h
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,8 @@
#include "product.h"
#include "productDatabase.h"
#include "mainmenuhelpers.h"
#include "email.h"
#include "user.h"

#include <cctype>

Expand Down Expand Up @@ -199,7 +201,7 @@ void displayCart(const vector < pair < Product, int >> & cart) {
cout << "-----------------------------------------------------------\n";
}

void CheckoutSystem() {
void CheckoutSystem(const string& accessToken, const vector<User>& subscribers) {
//productDatabase manage("data/products.json");
productDatabase &manage = productDatabase::getInstance("data/products.json");
string barcode, productId;
Expand Down Expand Up @@ -388,6 +390,7 @@ void CheckoutSystem() {
}
transaction["items"] = itemList;

receipt(accessToken, subscribers, transaction);
saveTransaction(transaction);
} else {
cout << "Transaction Failed! Inventory unchanged!" << endl;
Expand Down
45 changes: 44 additions & 1 deletion src/email.h
Original file line number Diff line number Diff line change
Expand Up @@ -242,7 +242,7 @@ void newsletter(const string& accessToken, const vector<User>& subscribers, Repo
}

content += "</ul>"
"<p>Use coupon code SPECIAL70 to get 70%% off on all soon-to-be expired items!</p>"
"<p>Use coupon code SPECIAL70 to get 70% off on all soon-to-be expired items!</p>"
"</body></html>";

string payload = "{ \"raw\": \"" + base64_encode(reinterpret_cast<const unsigned char*>(content.c_str()), content.length()) + "\" }";
Expand All @@ -261,4 +261,47 @@ void newsletter(const string& accessToken, const vector<User>& subscribers, Repo
}
}

void receipt(const string& accessToken, const vector<User>& subscribers, const json& transaction) {
int count = 0;

for (const auto& user : subscribers){
string content = "To: " + user.getEmail() + "\r\n"
"Subject: Thanks for shopping with us!\r\n"
"Content-Type: text/html\r\n" // Specify HTML content type
"\r\n"
"<html><body>"
"<p><img src=\"https://raw.githubusercontent.com/CS179K-Summer23/cs179-project-marketme/email/data/marketme.png\" alt=\"MarketMe Logo\"></p>"
"<p>Here is your receipt:</p>"
"<p>Date: " + transaction["date"].get<string>() + "</p>"
"<p>Total: $" + to_string(transaction["total"].get<double>()) + "</p>"
"<p>Tax: $" + to_string(transaction["tax"].get<double>()) + "</p>"
"<p>Discount: $" + to_string(transaction["discount"].get<double>()) + "</p>"
"<p>Operator: " + transaction["operator"].get<string>() + "</p>"
"<p>Items:</p>"
"<ul>";

for (const auto& item : transaction["items"]) {
content += "<li>" + item["name"].get<string>() + " - Quantity: " + to_string(item["quantity"].get<int>())
+ ", Price per Item: $" + to_string(item["price_per_item"].get<double>())
+ ", Total Price: $" + to_string(item["total_price"].get<double>()) + "</li>";
}

content += "</ul></body></html>";

string payload = "{ \"raw\": \"" + base64_encode(reinterpret_cast<const unsigned char*>(content.c_str()), content.length()) + "\" }";

sendEmailWithLibcurlCount(accessToken, user.getEmail(), payload, count);
}

if (subscribers.empty()){
cout << "Oops! There are no subscribers in the system yet." << endl;
}
else if (count == 0) {
cout << "Error: Sending emails was unsuccessful." << endl;
}
else {
cout << "Successfully sent " + to_string(count) + " email(s)!" << endl;
}
}

#endif

0 comments on commit 62632a1

Please sign in to comment.