diff --git a/.gitignore b/.gitignore new file mode 100644 index 00000000..f7989456 --- /dev/null +++ b/.gitignore @@ -0,0 +1,6 @@ +.venv +source/RewardsService/rewardsservice/__pycache__/ +source/RewardsService/rewardsservice/handlers/__pycache__/ +source/RewardsService/requirements.txt +source/RewardsService/requirements_bkp.txt +source/RewardsService/data/ \ No newline at end of file diff --git a/source/RewardsService/Dockerfile b/source/RewardsService/Dockerfile index 99c06781..575f7bc8 100644 --- a/source/RewardsService/Dockerfile +++ b/source/RewardsService/Dockerfile @@ -4,5 +4,6 @@ ENV PYTHONPATH=$PYTHONPATH:/code/ RUN mkdir /code WORKDIR /code ADD requirements.txt /code/ +RUN python -m pip install --upgrade pip RUN pip install -r requirements.txt ADD ./ /code/ diff --git a/source/RewardsService/requirements.txt b/source/RewardsService/requirements.txt index c6db96e9..1ca236fd 100644 --- a/source/RewardsService/requirements.txt +++ b/source/RewardsService/requirements.txt @@ -1,3 +1,3 @@ fabric==1.10.2 motor==1.1.0 -tornado==3.2 +tornado==3.2 \ No newline at end of file diff --git a/source/RewardsService/rewardsservice/app.py b/source/RewardsService/rewardsservice/app.py index d2bd28fb..3f667d11 100644 --- a/source/RewardsService/rewardsservice/app.py +++ b/source/RewardsService/rewardsservice/app.py @@ -10,13 +10,13 @@ from settings import settings from url_patterns import url_patterns - class App(tornado.web.Application): def __init__(self, urls): self.logger = logging.getLogger(self.__class__.__name__) tornado.web.Application.__init__(self, urls, **settings) + app = App(url_patterns) @@ -34,5 +34,9 @@ def main(): logger.info("\nStopping server on port {}".format(options.port)) -if __name__ == "__main__": - main() +# if __name__ == "__main__": +# app = tornado.httpserver.HTTPServer(app, xheaders=True) +# port = 7050 +# app.listen(port) +# print('Hello Its running') +# tornado.ioloop.IOLoop.current().start() \ No newline at end of file diff --git a/source/RewardsService/rewardsservice/handlers/rewards_handler.py b/source/RewardsService/rewardsservice/handlers/rewards_handler.py index 4f2aea6e..a231fd23 100644 --- a/source/RewardsService/rewardsservice/handlers/rewards_handler.py +++ b/source/RewardsService/rewardsservice/handlers/rewards_handler.py @@ -1,10 +1,8 @@ import json import tornado.web - from pymongo import MongoClient from tornado.gen import coroutine - class RewardsHandler(tornado.web.RequestHandler): @coroutine @@ -13,3 +11,116 @@ def get(self): db = client["Rewards"] rewards = list(db.rewards.find({}, {"_id": 0})) self.write(json.dumps(rewards)) + + +class AddCustomerDatahandler(tornado.web.RequestHandler): + + @coroutine + def post(self): + # Handle the POST request + + def inner_wrapper(rewards_list,points): + for reward in rewards_list: + if reward['points']==points: + return reward + + # to calculate the rewards for an existing and new customer as well + def calculate_rewards(customer_data): + rewards = list(db.rewards.find({}, {"_id": 0})) + points_list = [i.get('points') for i in rewards ] + reward_points = customer_data.get('reward_points') + if reward_points=points_list[-1]: + customer_data.update({ + "reward_points" : points_list[-1], + "next_reward_tier":"The customer already reached to peak rewards", + "next_reward_tier_progress":"N/A", + "next_reward_name":"N/A" + }) + return customer_data + else: + for i in range(0,len(points_list)-1): + if points_list[i]<=int(reward_points)<=points_list[i+1]: + rewards_data = inner_wrapper(rewards,points_list[0]) + reward_tier,reward_name = rewards_data.get('tier'),rewards_data.get('rewardName') + next_rewards_data = inner_wrapper(rewards,points_list[1]) + next_reward_tier,next_reward_name = next_rewards_data.get('tier'),next_rewards_data.get('rewardName') + next_reward_tier_progress=str(points_list[i+1]-customer_data.get('order'))+"%" + customer_data.update( + { + "reward_tier":reward_tier, + "reward_name":reward_name, + "next_reward_tier":next_reward_tier, + "next_reward_name":next_reward_name, + "next_reward_tier_progress":next_reward_tier_progress + }) + return customer_data + + client = MongoClient("mongodb", 27017) + db = client.get_database("Rewards") + collection = db.get_collection("Customer") + data = tornado.escape.json_decode(self.request.body) + + customer_data ={ + 'customer_email':data.get('customer_email'), + 'order':data.get('order'), + 'reward_points' : round(data.get('order')) + } + + data_exists = list(collection.find({"customer_email":customer_data.get('customer_email')})) + if len(data_exists)>0: + for existed_data in data_exists: + customer_data.update({ + 'customer_email':data.get('customer_email'), + 'order': customer_data.get('order') + int(existed_data.get('order')), + 'reward_points': customer_data.get('reward_points') + int(existed_data.get('reward_points')) + } + ) + result = calculate_rewards(customer_data) + collection.update_one({"customer_email" : customer_data.get('customer_email')},{"$set": result},upsert=True) + self.write("Customer Data has been successful updated") + else: + result = calculate_rewards(customer_data) + collection.insert_one(result) + self.write("Customer Data has been successful registered") + + +class FetchAllCustomerDatahandler(tornado.web.RequestHandler): + + @coroutine + def get(self): + # to fetch all the records in collection + client = MongoClient("mongodb", 27017) + db = client.get_database("Rewards") + collection = db.get_collection("Customer") + rewards = list(collection.find({}, {"_id": 0})) + if rewards: + self.write(json.dumps(rewards)) + else: + self.write("No Customer Records Found") + + @coroutine + def delete(self): + # to delete the collection + client = MongoClient("mongodb", 27017) + db = client.get_database("Rewards") + collection = db.get_collection("Customer") + collection.drop() + self.write("DELETE collection successful") + +class FetchCustomerDatahandler(tornado.web.RequestHandler): + + @coroutine + def get(self,slug): + # to fetch one customer data from collection + client = MongoClient("mongodb", 27017) + db = client.get_database("Rewards") + collection = db.get_collection("Customer") + data_exists = list(collection.find({"customer_email":slug},{"_id": 0})) + if data_exists: + self.write(json.dumps(data_exists)) + else: + self.write("No Customer Records Found") \ No newline at end of file diff --git a/source/RewardsService/rewardsservice/url_patterns.py b/source/RewardsService/rewardsservice/url_patterns.py index 55e471d6..72e95c87 100644 --- a/source/RewardsService/rewardsservice/url_patterns.py +++ b/source/RewardsService/rewardsservice/url_patterns.py @@ -1,5 +1,8 @@ -from handlers.rewards_handler import RewardsHandler +from handlers.rewards_handler import * url_patterns = [ - (r'/rewards', RewardsHandler), + (r"/rewards", RewardsHandler), # default rewards handler + (r"/customer_data/add", AddCustomerDatahandler), # End point to add/update the customer data wrt customer email and order + (r"/customer_data/fetch/([^/]+)", FetchCustomerDatahandler), # End point to fetch one customer data from DB + (r"/customer_data/fetch/all", FetchAllCustomerDatahandler), # End point to fetch all the customer data from DB ]