Skip to content

Commit

Permalink
Merge pull request #60 from Serveis-Neby/task/138-payment-works-now
Browse files Browse the repository at this point in the history
Task/138 payment works now
  • Loading branch information
ddbaque authored May 11, 2024
2 parents 13f3bc9 + ef93a59 commit 5073d9e
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 0 deletions.
1 change: 1 addition & 0 deletions include/controllers/notification_controller.h
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
#include <crow.h>
#include <models/notification_model.h>
#include <models/service_model.h>
#include <models/user_model.h>
#include <utils/common.h>
#include <utils/utils.h>
#include <format>
Expand Down
27 changes: 27 additions & 0 deletions src/controllers/notification_controller.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -112,6 +112,33 @@ void NotificationController::handle_notification(pqxx::connection& db, const cro
if (action == NotificationStatus::REFUSED) {
updated_notification = NotificationModel::handle_notification_status(db, NotificationStatus::REFUSED, notification_id, true);
} else {
std::unique_ptr<UserModel> notificationCreator = UserModel::get_user_by_id(db, notification->get_sender_id());
std::unique_ptr<UserModel> serviceCreator = UserModel::get_user_by_id(db, service->get_creator_id());

if (service->get_type() == ServiceType::OFFERED) {
if (notificationCreator->get_balance() < service->get_price()) {
handle_error(res, "notification sender does not have enough coins to pay for the service", 400);
return;
}
else {
int new_sender_balance = notificationCreator->get_balance() - service->get_price();
int new_creator_balance = serviceCreator->get_balance() + service->get_price();
UserModel::update_user_admin(db, notificationCreator->get_id(), notificationCreator->get_username(), new_sender_balance);
UserModel::update_user_admin(db, serviceCreator->get_id(), serviceCreator->get_username(), new_creator_balance);
}
}
else {
if (serviceCreator->get_balance() < service->get_price()) {
handle_error(res, "you don't have the coins to pay for this request", 400);
return;
}
else {
int new_sender_balance = notificationCreator->get_balance() + service->get_price();
int new_creator_balance = serviceCreator->get_balance() - service->get_price();
UserModel::update_user_admin(db, notificationCreator->get_id(), notificationCreator->get_username(), new_sender_balance);
UserModel::update_user_admin(db, serviceCreator->get_id(), serviceCreator->get_username(), new_creator_balance);
}
}
updated_notification = NotificationModel::handle_notification_status(db, NotificationStatus::ACCEPTED, notification_id, true);

bool succes_refused = NotificationModel::refused_notifications(db, updated_notification.get()->get_service_id(), notification_id);
Expand Down

0 comments on commit 5073d9e

Please sign in to comment.