def lambda_handler(event, context): proxyResponse = {} response = {} proxyResponse['headers'] = { "Access-Control-Allow-Methods": "*", "Access-Control-Allow-Headers": "*", "Access-Control-Allow-Origin": "*" } validRequest = validateRequest(event) print(validRequest) if ('error' in validRequest): proxyResponse['statusCode'] = validRequest['errorCode'] proxyResponse['error'] = validRequest['error'] else: ''' BUSINESS LOGIC STARTS HERE ''' questionId = validRequest["questionId"] # data = delete_all("question_master",{"question_id":questionId}) # data = delete_all("question_category",{"question_id":questionId}) # data = delete_all("question_mcq",{"question_id":questionId}) data = db_utility.update_data_one("question_master", {"question_id": questionId}, {"is_active": False}) if (data == "200"): response["responseCode"] = "200" ''' BUSINESS LOGIC ENDS HERE BY RETURNING RESPONSE JSON ''' proxyResponse['statusCode'] = 200 proxyResponse['body'] = json.dumps(response) print(proxyResponse) return proxyResponse
def lambda_handler(event, context): proxyResponse = {} response = {} proxyResponse['headers'] = { "Access-Control-Allow-Methods": "*", "Access-Control-Allow-Headers": "*", "Access-Control-Allow-Origin": "*" } validRequest = validateRequest(event) print(validRequest) if ('error' in validRequest): proxyResponse['statusCode'] = validRequest['errorCode'] proxyResponse['error'] = validRequest['error'] else: ''' BUSINESS LOGIC STARTS HERE ''' questionTitle = validRequest["questionTitle"] questionDescription = validRequest["questionDescription"] questionTypeId = validRequest["questionTypeId"] if not validRequest["imageList"]: containsMedia = False else: containsMedia = True createdTime = datetime.datetime.now().strftime("%Y-%m-%dT%H:%M:%S") duration = validRequest["duration"] createdBy = validRequest["userId"] optionList = validRequest['optionList'] categoryList = validRequest['categoryList'] score = validRequest['score'] if ('questionId' not in validRequest): questionId = db_utility.fetch_next_id("question_master", "question_id") param = { "_id": questionId, "question_id": questionId, "title": questionTitle, "description": questionDescription, "question_type_id": questionTypeId, "contains_media": containsMedia, "score": score, "is_active": True, "last_updated_at": createdTime, "created_at": createdTime, "created_by": createdBy, "duration": duration } if (validRequest['userGroupId'] == 1): param['admin_id'] = validRequest['adminId'] if (validRequest['userGroupId'] == 2): param['organization_id'] = validRequest["organizationId"] if (questionTypeId == 1): if ('answer_count' in validRequest): param['answer_count'] = validRequest["answer_count"] if (containsMedia): param['image_list'] = validRequest['imageList'] data = db_utility.insert_data_one("question_master", param) response["question_id"] = questionId response["responseCode"] = "200" response["message"] = "Question created successfully" else: questionId = validRequest["questionId"] param = { "title": questionTitle, "description": questionDescription, "question_type_id": questionTypeId, "contains_media": containsMedia, "score": score, "last_updated_at": createdTime, "created_by": createdBy, "duration": duration } if (questionTypeId == 1): if ('answer_count' in validRequest): param['answer_count'] = validRequest["answer_count"] if (containsMedia): param['image_list'] = validRequest['imageList'] data = db_utility.update_data_one("question_master", {"question_id": questionId}, param) if (data == "200"): response["question_id"] = questionId response["responseCode"] = "200" response["message"] = "Question updated successfully" data = delete_all("question_category", {"question_id": questionId}) data = delete_all("question_tags", {"question_id": questionId}) data = delete_all("question_mcq", {"question_id": questionId}) questionCategoryId = db_utility.fetch_next_id("question_category", "question_category_id") paramList = [] for category in categoryList: param = {} param["_id"] = questionCategoryId param["question_category_id"] = questionCategoryId param["question_id"] = questionId param["category_id"] = category["categoryId"] questionCategoryId = questionCategoryId + 1 paramList.append(param) print("Inserting categories") data = db_utility.insert_data_many("question_category", paramList) if (questionTypeId == 1): optionId = db_utility.fetch_next_id("question_mcq", "option_id") paramList = [] for option in optionList: param = {} param["_id"] = optionId param["option_id"] = optionId param["question_id"] = questionId param["option"] = option["option"] param["score"] = option["score"] param["is_correct"] = option["isCorrect"] optionId = optionId + 1 paramList.append(param) print("Inserting options") data = db_utility.insert_data_many("question_mcq", paramList) if ('tagList' in validRequest): # tagList = db_utility.fetch_where("question_tags",{"question_id":questionId}) for tag in validRequest['tagList']: tagInfo = db_utility.fetch_where("tag", {"tag_name": tag['tag_name']}) if (len(tagInfo) > 0): tagId = tagInfo[0]['tag_id'] else: tagId = db_utility.fetch_next_id("tag", "tag_id") data = db_utility.insert_data_one( "tag", { "_id": tagId, "tag_id": tagId, "tag_name": tag['tag_name'] }) questionTagId = db_utility.fetch_next_id( "question_tags", "question_tag_id") data = db_utility.insert_data_one( "question_tags", { "_id": questionTagId, "question_tag_id": questionTagId, "tag_id": tagId, "question_id": questionId }) ''' BUSINESS LOGIC ENDS HERE BY RETURNING RESPONSE JSON ''' proxyResponse['statusCode'] = 200 proxyResponse['body'] = json.dumps(response) print(proxyResponse) return proxyResponse
def lambda_handler(event, context): proxyResponse = {} response = {} proxyResponse['headers'] = { "Access-Control-Allow-Methods": "*", "Access-Control-Allow-Headers": "*", "Access-Control-Allow-Origin": "*" } validRequest = validateRequest(event) if ('error' in validRequest): proxyResponse['statusCode'] = validRequest['errorCode'] proxyResponse['error'] = validRequest['error'] else: ''' BUSINESS LOGIC STARTS HERE ''' userName = validRequest['userId'] userDetail = db_utility.fetch_where("user_master", {"user_name": userName})[0] param = {} # for i in requiredParams: # param[i]=validRequest[i] userGroup = userDetail['user_group_id'] # print(userGroup) if userGroup == 2 and validRequest['copy_question_set']: existing_param = db_utility.fetch_where( 'question_set', {'question_set_id': validRequest['question_set_id']})[0] print(existing_param) param = {} param['question_set_id'] = db_utility.fetch_next_id( 'question_set', 'question_set_id') param['_id'] = param['question_set_id'] param['question_set_name'] = existing_param['question_set_name'] param['question_list'] = existing_param['question_list'] param['is_sample_set'] = existing_param['is_sample_set'] param['is_question_bank'] = False if 'expire_at' in existing_param: param['expire_at'] = existing_param['expire_at'] param['duration'] = existing_param['duration'] param['is_active'] = existing_param['is_active'] param['is_partial_complete_allowed'] = existing_param[ 'is_partial_complete_allowed'] param['last_updated_at'] = datetime.datetime.now().strftime( "%Y-%m-%dT%H:%M:%S") param['is_active'] = existing_param['is_active'] param['is_random_order'] = existing_param['is_random_order'] param['is_show'] = True param['created_at'] = datetime.datetime.now().strftime( "%Y-%m-%dT%H:%M:%S") param['created_by'] = userDetail['user_id'] param['organization_id'] = userDetail['organization_id'] data = db_utility.insert_data_one("question_set", param) param['message'] = "Question set copied successfully" if (not validRequest['copy_question_set']): questionSetId = validRequest['question_set_id'] status = False data = db_utility.update_data_one( "question_set", {"question_set_id": questionSetId}, {"is_show": status}) param['message'] = "Question set deleted successfully" else: param["statusCode"] = 404 param['Error'] = UNAUTHENTICATION_ERROR_MSG if data == "200": print("Data Updated") else: param['statusCode'] = 400 param['Error'] = DATABASE_CONNECTION_ERROR response = param ''' BUSINESS LOGIC ENDS HERE BY RETURNING RESPONSE JSON ''' proxyResponse['statusCode'] = 200 # response={} # print(response) proxyResponse['body'] = json.dumps(response) print(proxyResponse) return proxyResponse
def lambda_handler(event, context): proxyResponse = {} response = {} proxyResponse['headers'] = { "Access-Control-Allow-Methods": "*", "Access-Control-Allow-Headers": "*", "Access-Control-Allow-Origin": "*" } validRequest = validateRequest(event) print(validRequest) if ('error' in validRequest): proxyResponse['statusCode'] = validRequest['errorCode'] proxyResponse['error'] = validRequest['error'] else: ''' BUSINESS LOGIC STARTS HERE ''' if ('sampleQuestionSetId' in validRequest): sampleQuestionSetId = validRequest['sampleQuestionSetId'] if ('expireAt' in validRequest): expireAt = validRequest['expireAt'] if ('organizationId' in validRequest): organizationId = validRequest['organizationId'] isActive = validRequest['isActive'] questionSetName = validRequest['questionSetName'] questionList = validRequest['questionList'] isSampleSet = validRequest['isSampleSet'] duration = validRequest['duration'] isPartialCompleteAllowed = validRequest['isPartialCompleteAllowed'] isRandomOrder = validRequest['isRandomOrder'] createdBy = validRequest['userId'] currentTime = datetime.datetime.now().strftime("%Y-%m-%dT%H:%M:%S") param = {} if ((validRequest['userGroupId']) == 1): if ('questionSetId' in validRequest): param['question_set_name'] = questionSetName param['question_list'] = questionList param['is_sample_set'] = isSampleSet param['duration'] = duration param['is_active'] = isActive param['is_partial_complete_allowed'] = isPartialCompleteAllowed param['last_updated_at'] = currentTime param['is_random_order'] = isRandomOrder # param['created_by'] = createdBy print("Updating question bank") questionSetId = validRequest['questionSetId'] if ('expireAt' in validRequest): param['expire_at'] = expireAt if ('sampleQuestionSetId' in validRequest): param['sample_question_set_id'] = sampleQuestionSetId print(param) data = db_utility.update_data_one( "question_set", {"question_set_id": questionSetId}, param) response["questionSetId"] = questionSetId else: questionSetId = db_utility.fetch_next_id( "question_set", "question_set_id") param['_id'] = questionSetId param['question_set_id'] = questionSetId param['question_set_name'] = questionSetName param['question_list'] = questionList param['is_sample_set'] = isSampleSet param['is_question_bank'] = True param['duration'] = duration param['is_active'] = isActive param['is_partial_complete_allowed'] = isPartialCompleteAllowed param['last_updated_at'] = currentTime param['is_random_order'] = isRandomOrder param['created_at'] = currentTime param['created_by'] = createdBy param['is_show'] = True print("Creating question bank") if ('expireAt' in validRequest): param['expire_at'] = expireAt if ('sampleQuestionSetId' in validRequest): param['sample_question_set_id'] = sampleQuestionSetId print(param) data = db_utility.insert_data_one("question_set", param) response["questionSetId"] = questionSetId else: if ('questionSetId' in validRequest): questionSetId = validRequest['questionSetId'] print("Updating question set") param['question_set_name'] = questionSetName param['question_list'] = questionList param['is_sample_set'] = isSampleSet param['duration'] = duration param['is_active'] = isActive param['is_partial_complete_allowed'] = isPartialCompleteAllowed param['last_updated_at'] = currentTime param['is_random_order'] = isRandomOrder # param['created_by'] = createdBy if ('organizationId' in validRequest): param['organization_id'] = organizationId if ('expireAt' in validRequest): param['expire_at'] = expireAt if ('sampleQuestionSetId' in validRequest): param['sample_question_set_id'] = sampleQuestionSetId print(param) data = db_utility.update_data_one( "question_set", {"question_set_id": questionSetId}, param) response["questionSetId"] = questionSetId else: print("Creating question set") questionSetId = db_utility.fetch_next_id( "question_set", "question_set_id") param['_id'] = questionSetId param['question_set_id'] = questionSetId param['question_set_name'] = questionSetName param['question_list'] = questionList param['is_sample_set'] = isSampleSet param['is_question_bank'] = False param['duration'] = duration param['is_active'] = isActive param['is_partial_complete_allowed'] = isPartialCompleteAllowed param['last_updated_at'] = currentTime param['is_random_order'] = isRandomOrder param['created_at'] = currentTime param['created_by'] = createdBy param['is_show'] = True if ('organizationId' in validRequest): param['organization_id'] = organizationId if ('expireAt' in validRequest): param['expire_at'] = expireAt if ('sampleQuestionSetId' in validRequest): param['sample_question_set_id'] = sampleQuestionSetId print(param) data = db_utility.insert_data_one("question_set", param) response["questionSetId"] = questionSetId ''' BUSINESS LOGIC ENDS HERE BY RETURNING RESPONSE JSON ''' proxyResponse['statusCode'] = 200 proxyResponse['body'] = json.dumps(response) print(proxyResponse) return proxyResponse
def lambda_handler(event,context): proxyResponse = {} response = {} proxyResponse['headers'] = {"Access-Control-Allow-Methods":"*", "Access-Control-Allow-Headers":"*", "Access-Control-Allow-Origin":"*"} validRequest = validateRequest(event) if('error' in validRequest): proxyResponse['statusCode'] = validRequest['errorCode'] proxyResponse['error'] = validRequest['error'] else: ''' BUSINESS LOGIC STARTS HERE ''' # print(validRequest) candidateToken=validRequest['candidateToken'] testDetail=db_utility.fetch_where("test",{"token":candidateToken})[0] testId=testDetail['test_id'] # questionSetDetail=db_utility.fetch_where("question_set",{"question_set_id":testDetail['question_set_id']})[0] # sampleQuestionSetDetails=db_utility.fetch_where("question_set",{"question_set_id":questionSetDetail['sample_question_set_id']})[0] # print(sampleQuestionSetDetails['question_list']) questionId=validRequest['questionId'] questionDetail=db_utility.fetch_where('question_master',{'question_id':questionId})[0] answer=validRequest['answer'] print(answer) if questionDetail['question_type_id']==1: param={} param['response_id']=db_utility.fetch_next_id("test_response","response_id") param["_id"]=param['response_id'] param['question_id']=validRequest['questionId'] param['test_id']=testId param['answer_mcq']=answer['mcq'] param['submitted_at']=datetime.datetime.now().strftime("%Y-%m-%dT%H:%M:%S") elif questionDetail['question_type_id']==2: param={} param['response_id']=db_utility.fetch_next_id("test_response","response_id") param["_id"]=param['response_id'] param['question_id']=validRequest['questionId'] param['test_id']=testId if 'videoUrl' in answer: param['video_url']=answer['videoUrl'] if 'textUrl' in answer: param['text_url']=answer['textUrl'] param['submitted_at']=datetime.datetime.now().strftime("%Y-%m-%dT%H:%M:%S") data=db_utility.insert_data_one('test_response',param) # print("1") # print(data) if data=="200": testQuestionTracker=db_utility.fetch_where_and('test_question_tracker',[{'test_id':testId,'question_id':questionId}])[0] param={} currentSortOrder=testQuestionTracker['sort_order'] print(currentSortOrder) param=testQuestionTracker param['submit_status']=True trackerData=db_utility.update_data_one('test_question_tracker',{"question_tracker_id":testQuestionTracker['question_tracker_id']},param) # print(data) if trackerData=="200": param={} param['tracker_id']=db_utility.fetch_next_id("log_tracker","tracker_id") param['_id']=param['tracker_id'] param['test_id']=testId param['activity_type_id']="004" param['activity_time']=datetime.datetime.now().strftime("%Y-%m-%dT%H:%M:%S") # logData=db_utility.insert_data_one('log_tracker',param) logData="200" if logData=="200": print("Data Inserted") # sortOrder=0 sortOrder=currentSortOrder+1 print(sortOrder) while True: nextQuestionId=db_utility.fetch_where('test_question_tracker',{'sort_order':sortOrder,'test_id':testId}) if (len(nextQuestionId)>0 and not nextQuestionId[0]['submit_status']): nextQuestionId=nextQuestionId[0]['question_id'] break elif (len(nextQuestionId)>0 and nextQuestionId[0]['submit_status'] and sortOrder!=currentSortOrder): sortOrder=sortOrder+1 elif (len(nextQuestionId)==0 and sortOrder!=currentSortOrder): sortOrder=1 elif (len(nextQuestionId)>0 and sortOrder==currentSortOrder): nextQuestionId=0 break else: nextQuestionId=0 break if nextQuestionId==0: param={} param['test_ended']=True else: client = boto3.client('lambda',region_name = environ['Region']) # print("Invoking AkriValidateToken micro service to validate token") nextQuestion = client.invoke( FunctionName='WO-'+environ['stage']+'-LookupQuestion', InvocationType='RequestResponse', Payload=json.dumps({"body":{'questionId': nextQuestionId,"candidateToken":candidateToken}}) ) nextQuestion= json.loads(nextQuestion['Payload'].read().decode("utf-8")) param={} param=nextQuestion['body'] param=json.loads(param) param['sort_order']=sortOrder param['test_ended']=False print(param) if len(param)>0: print("Data Fetched") # param=questionDetail['body'] else: param['statusCode']=400 param['Error']=DATABASE_CONNECTION_ERROR response=param ''' BUSINESS LOGIC ENDS HERE BY RETURNING RESPONSE JSON ''' proxyResponse['statusCode'] = 200 # response={} # print(response) proxyResponse['body'] = json.dumps(response) print(proxyResponse) return proxyResponse