Exemplo n.º 1
0
    def delete(self, affiliation_id):
        """Updates an affiliation."""
        response_object = {}

        affiliation = get_affiliation_by_id(affiliation_id)
        if not affiliation:
            affiliations_namespace.abort(404, f"Affiliation does not exist")

        user = get_user_by_id(affiliation.user_id)
        if not user:
            users_namespace.abort(
                404, f"User {affiliation.user_id} does not exist")

        try:
            msg = "Affiliation number " + str(
                affiliation.id) + " has been deleted from the GoodDriver App."
            send_email("*****@*****.**", "User affiliation deleted.", msg)
            # Req Change 3:
            send_email(user.email,
                       "User affiliation removed from GoodDriver App", msg)
        except:
            pass

        response_object[
            "message"] = f" Affiliation {affiliation_id} was removed!"
        delete_affiliation(affiliation)
        return response_object, 200
Exemplo n.º 2
0
    def post(self):
        """Creates a new user."""
        post_data = request.get_json()
        username = post_data.get("username")
        email = post_data.get("email")
        password = post_data.get("password")
        role = post_data.get("role")
        sponsor_logo = post_data.get("sponsor_logo")
        sponsor_headline = post_data.get("sponsor_headline")
        sponsor_slug = post_data.get("sponsor_slug")
        response_object = {}

        user = get_user_by_email(email)
        if user:
            response_object["message"] = "Sorry. That email already exists."
            return response_object, 400
        new_user = add_user(username, email, password, role)
        response_object["user_id"] = new_user.id
        response_object[
            "message"] = message = f"A new user with email {email} was added!"

        try:
            # print(f"Sending email to {email}")
            msg = "New user account created in GoodDriver App for email: " + email
            send_email("*****@*****.**", "New user created.", msg)
            # Req Change 3:
            send_email(email, "New user account created in GoodDriver App",
                       "Welcome to the GoodDriver App!")
        except:
            pass

        return response_object, 201
Exemplo n.º 3
0
    def post(self):
        """Creates a new user affiliation."""
        post_data = request.get_json()

        user_id = post_data.get("user_id")
        sponsor_name = post_data.get("sponsor_name")
        current_points = post_data.get("current_points")
        status = post_data.get("status")

        response_object = {}

        affiliation = check_dupe_affiliation(user_id, sponsor_name)
        if affiliation:
            response_object[
                "message"] = "Sorry. That driver affiliation already exists."
            return response_object, 400
        new_affiliation = add_affiliation(user_id, sponsor_name,
                                          current_points, status)
        response_object["affiliation_id"] = new_affiliation.id
        response_object[
            "message"] = message = f"A new affiliation for driver id: {user_id} and sponsor {sponsor_name} was added!"

        try:
            # print(f"Sending email to {email}")
            msg = f"A new affiliation for driver id: {user_id} and sponsor {sponsor_name} was added!"
            send_email("*****@*****.**",
                       "New affiliation created in GoodDriver App.", msg)
            # Req Change 3:
            send_email(email, "New user account created in GoodDriver App",
                       "Welcome to the GoodDriver App!")
        except:
            pass

        return response_object, 201
Exemplo n.º 4
0
    def put(self, event_id):
        """Updates an event."""

        event = get_event_by_id(event_id)
        if not event:
            events_namespace.abort(404, f"Event {event_id} does not exist")

        post_data = request.get_json()
        description = post_data.get("description") or event.description
        points = post_data.get("points") or event.points
        current_points = post_data.get(
            "current_points") or event.current_points
        status = post_data.get("status") or event.status
        response_object = {}

        update_event(event, description, points, current_points, status)

        user_record = get_user_by_id(user_id)
        if user_record.get_points_alert and points != event.points:
            try:
                # Req Change 3:
                msg = "You now have " + points + " points in the GoodDriver App."
                send_email(email, "Points updated in GoodDriver App", msg)
            except:
                pass

        response_object["message"] = f"{event.id} was updated!"
        return response_object, 200
Exemplo n.º 5
0
    def post(self):

        """Creates a new message."""
        post_data = request.get_json()
        thread_id = post_data.get("thread_id")
        sender_id = post_data.get("sender_id")
        recipient_id = post_data.get("recipient_id")
        created_date = datetime.datetime.now()
        subject = post_data.get("subject")
        content = post_data.get("content")
        order_id = post_data.get("order_id")

        response_object = {}
        add_message(thread_id, sender_id, recipient_id, subject, content, created_date)
        user_record = get_user_by_id(recipient_id)
        if user_record.get_problem_alert:

            try:
                # Req Change 3:
                msg = f"There was a problem with your order, number {str(order_id)}. Please contact your manager."
                send_email(user_record.email, "Problem with order in GoodDriver App", msg)
            except:
                pass

        response_object["message"] = f"Message was added!"
        return response_object, 201
Exemplo n.º 6
0
    def put(self, affiliation_id):
        """Updates an affiliation."""

        affiliation = get_affiliation_by_id(affiliation_id)
        if not affiliation:
            affiliations_namespace.abort(404, f"Affiliation does not exist")

        post_data = request.get_json()

        user_id = post_data.get("user_id") or affiliation.user_id
        sponsor_name = post_data.get(
            "sponsor_name") or affiliation.sponsor_name
        current_points = post_data.get(
            "current_points") or affiliation.current_points
        status = post_data.get("status") or affiliation.status

        response_object = {}

        user = get_user_by_id(affiliation.user_id)
        if not user:
            users_namespace.abort(404, f"User {user_id} does not exist")

        # Alert if points change
        if user.get_points_alert and current_points != affiliation.current_points:
            print(f"Email sent")
            try:
                # Req Change 3:
                msg = f"\nYou now have {str(current_points)} points in the GoodDriver App."
                send_email(user.email, "GoodDriver App points were updated",
                           msg)
            except:
                pass

        ## TO DO ##
        ## Alert if dropped by sponsor

        ## Alert if added by sponsor

        update_affiliation(affiliation, user_id, sponsor_name, current_points,
                           status)

        response_object[
            "message"] = f"Affiliation {affiliation_id} was updated!"
        return response_object, 200
Exemplo n.º 7
0
    def delete(self, user_id):
        """Updates a user."""
        response_object = {}
        user = get_user_by_id(user_id)
        if not user:
            users_namespace.abort(404, f"User {user_id} does not exist")
        delete_user(user)
        response_object["message"] = f"{user.email} was removed!"

        try:
            msg = "Account number " + str(
                user_id) + " has been deleted from the GoodDriver App."
            send_email("*****@*****.**", "User account deleted.", msg)
            # Req Change 3:
            send_email(
                user.email, "User account removed from GoodDriver App",
                "Your account has been deleted from the GoodDriver App.")
        except:
            pass

        return response_object, 200
Exemplo n.º 8
0
    def post(self):
        """Creates a new event."""
        post_data = request.get_json()
        description = post_data.get("description")
        points = post_data.get("points")
        user_id = post_data.get("user_id")
        sponsor_name = post_data.get("sponsor_name")
        response_object = {}
        add_event(description, points, user_id, sponsor_name)

        # Update points on user record
        user = get_user_by_id(user_id)
        user_affiliations = get_all_affiliations_by_user(user_id)

        user_record = {}
        for item in user_affiliations:
            if item.sponsor_name == sponsor_name:
                user_record = item
                user_id = user_record.user_id
                sponsor_name = user_record.sponsor_name
                updated_points = item.current_points + points
                # current_points = updated_points
                status = user_record.status

                update_affiliation(user_record, user_id, sponsor_name,
                                   updated_points, status)

                if user.get_points_alert:
                    try:
                        # Req Change 3:
                        msg = "You now have " + updated_points + " points in the GoodDriver App."
                        send_email(user.email,
                                   "Points updated in GoodDriver App", msg)
                    except:
                        pass

                response_object["message"] = f"Event was added!"
                return response_object, 201
Exemplo n.º 9
0
    def put(self, order_id):
        """Updates an order."""

        order = get_order_by_id(order_id)
        if not order:
            orders_namespace.abort(404, f"Order {order_id} does not exist")

        post_data = request.get_json()
        status = post_data.get("status") or order.status
        user_id = post_data.get("user_id") or order.user_id
        sponsor_name = post_data.get("sponsor_name") or order.sponsor_name
        response_object = {}
        # print(f"status: {status}")
        if status == "submitted":
            # Process the order
            # Get the user record and order items
            user_record = get_user_by_id(user_id)

            # Req Change 1
            # Reject any order from admin or sponsor_mgr
            if user_record.role != "driver":
                response_object[
                    "message"] = f"{order.id} is a dummy order and will not process."
                return response_object, 412

            user_affiliation = check_dupe_affiliation(user_id, sponsor_name)
            order_items = get_all_order_items_by_order_id(order_id)
            # Verify sufficient points to satisfy order
            # 1. Get points in order and summary of items
            tot_points_in_order = 0
            order_summary = ""
            for item in order_items:
                details = get_catalog_item_by_id(item.catalog_item_id)
                tot_points_in_order += item.points_cost
                order_summary += f"Qty: { str(item.quantity) } - { details.name }, \tPoints cost: { str(item.points_cost * item.quantity) }\n"

            driver_points_after_order = user_affiliation.current_points - tot_points_in_order

            order_summary += "--------------------------------------------------------------------------\n"
            order_summary += f"Total points cost: \t\t\t\t{ str(tot_points_in_order) }\n\n"
            order_summary += "Your order has been placed. After your order, you have " + str(
                driver_points_after_order
            ) + " points remaining in the GoodDriver App.\n"
            # print(f"Order summary: {order_summary}")
            # print(f"Driver points after order: {driver_points_after_order}")
            # print(f"user_record.get_points_alert: {user_record.get_points_alert}")

            if driver_points_after_order >= 0:
                # Process order
                update_order(order, status, user_id, sponsor_name)
                # Update points on user affiliation
                sponsor_name = user_affiliation.sponsor_name
                status = user_affiliation.status
                current_points = driver_points_after_order

                update_affiliation(user_affiliation, user_id, sponsor_name,
                                   current_points, status)

                if user_record.get_points_alert:
                    # try:
                    # Req Change 3:
                    send_email(user_record.email,
                               "Order placed in GoodDriver App", order_summary)
                    # except:
                    #     pass

            else:
                # Driver has insufficient points for order
                if user_record.get_points_alert:
                    try:
                        # Req Change 3:
                        send_email(
                            user_record.email,
                            "Order was not placed in GoodDriver App",
                            "We're sorry, but you have insufficient points to place your order in the GoodDriver App."
                        )
                    except:
                        pass

                response_object[
                    "message"] = f"{order.id} did not update due to insufficient points."
                return response_object, 412

        else:
            # Not an order submission, simply update
            update_order(order, status, user_id, sponsor_name)

        response_object["message"] = f"{order.id} was updated!"
        return response_object, 200