Question

I have a reservation model and a user model. In the insert script for my reservation model, I have the following:

function insert(item, user, request) {
    response.send(200, 'test');
    item.organizationid = user.organizationid;
    if (user.hourstoreserve <= (item.endtime - item.starttime)) {
        request.respond(400, 'You do not have the necessary hours available to make this reservation');
    } else if (user.complaints >= user.complaintsallowed) {
        request.respond(400, 'You are over your maximum number of allowed complaints this month.');        
    } else {
        user.hourstoreserve = (user.hourstoreserve - (item.endtime - item.starttime));
        request.execute();   
    };           
};

I need to make sure that item, which should be my new reservation that I am inserting, gets an organizationid from my user. I also then want to make sure the user has it's hourstoreserve validated, and if the reservation is made the user's hourstoreserve should be lowered.

It seems like this script isn't being executed at all. The first response.send(200, 'test'); does not send a response.

I am calling this insert script from my custom api similar to the following:

var reservations = request.service.tables.getTable("reservations");
reservations.insert(newReservation);

The custom API call works and inserts the reservation as it should, it just doesn't seem to execute my insert script.

Any help is appreciated.

Was it helpful?

Solution

When you invoke the CRUD operations of the tables from the service itself (i.e., in the code of a custom API, scheduler or another table), the table scripts are not executed. There's an open feature request to have this feature added to the backend, please upvote it if you think it will help you.

Another problem which I see in your code - the user object which is passed to the insert script isn't an item from your user model; instead, it's the information about the logged in user to the service (i.e., in the client side, after calling one of the login operations, that will be the user information it will have). That object doesn't have any properties about organization id, hourstoreserve, etc. If you need to get that information, you'll need to query your users table and working with it directly.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top