/**
     * Gets the current home for a specific dog.
     * @name getCurrentHome
     * @param {Object} request
     * @param {Object} response
     * @param {string} request.params.dogId
     * @returns {Object}
     */
    function getCurrentHome(request, response) {
        var query = new Parse.Query('Dog'),
            owner;

        query.include('currentOwner');
        query.get(request.params.dogId, {
            success: function (dog) {
                owner = dog.get('currentOwner');
                if (!owner) {
                    response.json({
                        data: {}
                    });
                } else {
                    apiNxt.getConstituent(request, owner.get('constituentId'), function (constituent) {
                        owner.set('constituent', constituent);
                        response.json({
                            data: owner
                        });
                    });
                }
            },
            error: function (dog, error) {
                onParseError(response, error);
            }
        });
    }
    /**
     * Sets the current home of the specified dog.
     * Sets the toDate of the previous currentHome first.
     * @name postCurrentHome
     * @param {Object} request
     * @param {Object} response
     * @param {string} request.params.dogId
     * @param {string} request.body.id
     */
    function postCurrentHome(request, response) {
        var query = new Parse.Query('Dog');

        function createNewCurrentOwner(dog, currentDate) {
            var DogOwnerHistory = new Parse.Object.extend('DogOwnerHistory'),
                newCurrentOwner = new DogOwnerHistory();

            newCurrentOwner.save({
                dog: dog,
                constituentId: request.body.id,
                fromDate: currentDate
            }, {
                success: function (owner) {
                    dog.set('currentOwner', owner);
                    dog.save(null, {
                        success: function (dog) {
                            response.json({
                                data: dog
                            });
                        },
                        error: function (dog, error) {
                            onParseError(response, error);
                        }
                    });
                },
                error: function (owner, error) {
                    onParseError(response, error);
                }
            });
        }

        query.include('currentOwner');
        query.get(request.params.dogId, {
            success: function (dog) {

                var currentOwner = dog.get('currentOwner'),
                    currentDate = new Date();

                if (!currentOwner) {
                    createNewCurrentOwner(dog, currentDate);
                } else {
                    currentOwner.set('toDate', currentDate);
                    currentOwner.save(null, {
                        success: function () {
                            createNewCurrentOwner(dog, currentDate);
                        },
                        error: function (currentOwner, error) {
                            onParseError(response, error);
                        }
                    });
                }
            },
            error: function (dog, error) {
                onParseError(response, error);
            }
        });
    }
Example #3
0
 }).then(() => {
   let query = new Parse.Query('_User');
   query.include('userProfile');
   return query.get(user.id);
 }).then((user) => {
Example #4
0
 }).then(() => {
   const query = new Parse.Query('AnObject');
   query.include('anotherObject');
   return query.find();
 }).then((res) => {
    /**
     * Posts a note for a specific dog.
     * @name postNotes
     * @param {Object} request
     * @param {Object} response
     * @param {string} request.params.dogId
     * @param {string} request.body.constituentId
     * @param {string} request.body.title
     * @param {string} request.body.description
     * @param {string} request.body.addToOwner
     * @returns {Object}
     */
    function postNotes(request, response) {
        var query = new Parse.Query('Dog'),
            DogNote = new Parse.Object.extend('DogNotes');

        query.include('currentOwner');
        query.get(request.params.dogId, {
            success: function (dog) {
                var currentOwner = dog.get('currentOwner'),
                    dogNote = new DogNote(),
                    dogDate = new Date(),
                    dogBodyParse = {
                        dog: dog,
                        date: dogDate,
                        title: request.body.title,
                        description: request.body.description
                    };

                // Validate current owner if requesting to addConstituentNote
                if (request.body.addConstituentNote && !currentOwner) {
                    onParseError(response, {
                        message: 'Dog does not have a current owner to save the note to.'
                    });
                } else if (!request.body.title || !request.body.description || request.body.title === '' || request.body.description === '') {
                    onParseError(response, {
                        message: 'Title and description are required'
                    });
                } else {

                    dogNote.save(dogBodyParse, {
                        success: function (dogNote) {
                            var dogBodyNxt;

                            if (request.body.addConstituentNote) {
                                dogBodyNxt = {
                                    type: 'Barkbaud',
                                    date: {
                                        y: dogDate.getFullYear(),
                                        m: dogDate.getMonth() + 1,
                                        d: dogDate.getDate()
                                    },
                                    summary: request.body.title,
                                    text: request.body.description
                                };
                                apiNxt.postNotes(request, currentOwner.get('constituentId'), dogBodyNxt, function (apiDogNote) {
                                    response.json({
                                        data: apiDogNote
                                    });
                                });
                            } else {
                                response.json({
                                    data: dogNote
                                });
                            }
                        },
                        error: function (dogNote, error) {
                            onParseError(response, error);
                        }
                    });
                }
            },
            error: function (dog, error) {
                onParseError(response, error);
            }
        });
    }
    /**
     * Gets the previous homes for a specific dog, excluding the current home.
     * @name getPreviousHomes
     * @param {Object} request
     * @param {Object} response
     * @param {string} request.params.dogId
     * @returns {Object}
     */
    function getPreviousHomes(request, response) {
        var queryDog = new Parse.Query('Dog'),
            queryOwnerHistory = new Parse.Query('DogOwnerHistory'),
            currentOwner;

        // Get the requested dog
        queryDog.include('currentOwner');
        queryDog.get(request.params.dogId, {
            success: function (dog) {

                currentOwner = dog.get('currentOwner');
                if (currentOwner) {
                    queryOwnerHistory.notEqualTo('objectId', currentOwner.id);
                }

                // Get the owner history tied to this dog
                queryOwnerHistory.equalTo('dog', dog);
                queryOwnerHistory.descending('fromDate');
                queryOwnerHistory.find({

                    // Successfully found the owner history
                    success: function (history) {
                        var i = 0,
                            j = history.length,
                            responses = [];

                        function buildConstituent(index) {
                            apiNxt.getConstituent(request, history[index].get('constituentId'), function (constituent) {
                                history[index].set('constituent', constituent);
                                if (history.length === ++responses) {
                                    response.json({
                                        data: history
                                    });
                                }
                            });
                        }

                        // Find the constituent information for each of our owner in this dogs owner history
                        if (j === 0) {
                            response.json({
                                data: []
                            });
                        } else {
                            for (i; i < j; i++) {
                                buildConstituent(i);
                            }
                        }

                    },

                    // Error getting the owner history
                    error: function (history, error) {
                        onParseError(response, error);
                    }
                });
            },

            // Error getting the requested dog
            error: function (dog, error) {
                onParseError(response, error);
            }
        });
    }
Example #7
0
  location:'exportLocation_',
  file:'exportMachine_',
}

Parse.initialize(config.appKey, config.jsKey,config.masterKey);
Parse.serverURL = config.serverURL;
Parse.Cloud.useMasterKey();
// ---------------------------------------------------------------------------------- machines
// --------------------------------------------------- MODEL
var MachineModel = Parse.Object.extend({
  className: "Machine"
});

// --------------------------------------------------- QUERRY
var MachineQuery = new Parse.Query(MachineModel);
MachineQuery.include("Location");

// ---------------------------------------------------------------------------------- Location
// --------------------------------------------------- MODEL
var LocationModel = Parse.Object.extend({
  className: "Location"
});
// ---------------------------------------------------------------------------------- Sensor
// --------------------------------------------------- MODEL
var SensorModel = Parse.Object.extend({
  className: "Sensors"
});
// --------------------------------------------------- QUERY
var SensorQuery = new Parse.Query(SensorModel);
SensorQuery.include('SensorType');
SensorQuery.include('Machine');