Skip to content

Failed to load resource: the server responded with a status of 400 (Bad Request) #4788

Closed
@byghuutran-zz

Description

@byghuutran-zz

system logs:
`*** Invalid function: "weekChanged" code=141, message=Invalid function: "weekChanged"

*** Error handling request: ParseError { code: 141, message: 'Invalid function: "weekChanged"' } code=141, message=Invalid function: "weekChanged"

Parse.Cloud.define("weekChanged", function(request, response) {
	//add record for the user to NotificationCount Class
	var currentUserId = request.params.currentUserId;									   
	var notificationLeftWeek;	
	var userCount = 0;
   	var NotificationCount = Parse.Object.extend("NotificationCount");
	console.log("NotificationCount Query Find Starting");
	var query = new Parse.Query(NotificationCount);
	query.equalTo("currentUserId", currentUserId);
	
	query.count({
		success: function(count) {
			//alert("Successfully retrieved " + results.length + " scores.");
			// Do something with the returned Parse.Object values

			console.log("NotificationCount Query Count Successful " + count);
			userCount = count;
			
			if (userCount == 0){
				console.log("Inside notificationCount Record Add. Count: " + userCount);
		
				var NotificationCount = Parse.Object.extend("NotificationCount");
				var notificationCount = new NotificationCount();
									
				notificationCount.set("currentUserId", currentUserId);
				notificationCount.set("date", new Date() + "");					
				notificationCount.set("count", notificationAllowedPerWeek);
				notificationCount.set("weekCount",0);
				notificationCount.save(null,{
					success: function(notificationCount){
						response.success(notificationAllowedPerWeek);						   
					},
					error: function(notificationCount, error){
						response.error(error.code + " " + error.message);
					}		   
				});		
			}else{
				var NotificationCount = Parse.Object.extend("NotificationCount");
				console.log("NotificationCount Query Find Starting");
				var query = new Parse.Query(NotificationCount);
				query.equalTo("currentUserId", currentUserId);
				query.find({
					success: function(results){
						for (var i = 0; i < results.length; i++) { 
							var objectNotificationCount = results[i];
							notificationLeftWeek = objectNotificationCount.get('count');
							creationDate = objectNotificationCount.get('date');
							//alert("Date"+object.get('date'));
								  
							weekCount = objectNotificationCount.get('weekCount');
							//$('#notificationCountServer').html('<strong>'+notificationLeft+'</strong>');
							//alert(object.id + ' - ' + object.get('count'));
							//alert("Date 2" +creationDate );
							
							var date1 = new Date(creationDate);
							var date2 = new Date();
							
							//alert(date2);
							var one_day=1000*60*60*24;
									
							// Convert both dates to milliseconds
							var date1_ms = date1.getTime();
							var date2_ms = date2.getTime();
							
							// Calculate the difference in milliseconds
							var difference_ms = date2_ms - date1_ms;
								
							// Convert back to days and return
							var diff = parseInt(Math.round(difference_ms/one_day)/7);							
							
							//alert("diff "+diff);
							console.log("Creation time: " + date1);
							console.log("Current time: " + date2);
							console.log("difference min: " + diff);
							console.log("weekCount: " + weekCount);							
							console.log("notificationLeftForWeek: " + notificationLeftWeek);

							if(diff > weekCount && notificationLeftWeek <= notificationAllowedPerWeek){								
								notificationLeftWeek = notificationAllowedPerWeek;
								//	$('#notificationCountServer').html('<strong>'+notificationLeft+'</strong>');
								objectNotificationCount.set("count", notificationLeftWeek);
								objectNotificationCount.set("weekCount",diff);
								objectNotificationCount.save();
								response.success(notificationAllowedPerWeek);									
							}else{
								response.success(notificationLeftWeek);
							}
						}
					},
					error: function(){
						response.error(error.code + " " + error.message);
					}
				});								
			}
		},
		error: function() {			
			//alert("Error: " + error.code + " " + error.message);
			userCount =0;
		}
	});
});

Parse.Cloud.define("notification", function(request, response) {
	var triggerValue = request.params.triggerValue;
  	var settingValue1 = request.params.settingKey1;
  	var settingValue2 = request.params.settingKey2;
  	var settingValue3 = request.params.settingKey3;
  	var settingValue3 = request.params.settingKey3;
   	var currentUserId = request.params.currentUserId;
   	var notificationLeft;
	var objectNotificationCount;
   
   	/*Parse.Cloud.run('checkNotificationCount', {currentUserId: currentUserId}, {
		success: function(success) {
			console.log("notificationLeft "+notificationLeft);
			notificationLeft = success;
		},
		error: function(error) {
			alert("Error Find Trigger: " + error + " " + error.message);
		}
	});*/
   
   	console.log("Send Notification Begin");
    var NotificationInitialCount = Parse.Object.extend("NotificationCount");
	var queryInitial = new Parse.Query(NotificationInitialCount);
	queryInitial.equalTo("currentUserId", currentUserId);
	console.log("Send Notification UserId: " + currentUserId);
	try{		
		queryInitial.find({
			success: function(results) {
				//alert("Successfully retrieved " + results.length + " scores.");
				// Do something with the returned Parse.Object values
				console.log("success");
				// response.success(results[0].get('count')); 
				for (var i = 0; i < results.length; i++) { 
					objectNotificationCount = results[i];
					notificationLeft = objectNotificationCount.get('count');
				}
				if(notificationLeft > 0){
		  			var query = new Parse.Query(Parse.Installation);
		  			query.equalTo("installationId", request.params.installationId);											
		  			Parse.Push.send({where: query, // Set our Installation query
							data: {					  
								triggerKey:triggerValue, 
								settingKey1:settingValue1,
								settingKey2:settingValue2,
								settingKey3:settingValue3,
								objectType:"android",
								action:"com.huu.library.findmymobile.MESSAGE"
							}
						},{success: function() {
							// Push was successful
							objectNotificationCount.set("count", notificationLeft-1);
							objectNotificationCount.save();
							response.success(notificationLeft-1);								
						},
						error: function(error) {
							// Handle error
							response.error("Error " + error.code + " " + error.message);
						}
					});
				}else{
					response.error("Only " +  notificationAllowedPerWeek + " notifications per week allowed");
				}
			},
			error: function(error) {
				console.log("Error: " + error.code + " " + error.message);
				response.error("Error: " + error.code + " " + error.message);
				alert("Error: " + error.code + " " + error.message);
			}
		});
	}catch(e){
		console.log(e);
	}
   	console.log("notificationLeft "+notificationLeft);
});

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions