Start a new topic
Answered

Custom Endpoint insert does not create _kmd object with lmt and ect

Hello,


     When I insert a new row into a collection from a custom endpoint, the _kmd object is blank.  Here is my code:


var test =

{

    "elemech_secret": "XXXX",

    "JobNumber": "TEST5678",

    "GlobalCustomerID": 9289790441258,

    "Amount": 10,

    "description": "_test payment 1",

    "type": "N/A",

    "cc_name": "VISA",

    "cc_last_4": 7890,

    "stripe_transaction_id": "ch_1BEIfWLj9GZsdfI987hdU98ydN",

    "status": "succeeded"

}

function onRequest(request, response, modules) {

    var logger = modules.logger;

    try {

        var debug = false;

 

        // 1. get data

        var elemech_secret = request.body.elemech_secret;

        var JobNumber = request.body.JobNumber;

        var GlobalCustomerID = Number(request.body.GlobalCustomerID);

        var Amount = Number(request.body.Amount);

        var description = request.body.description;

        var type = request.body.type;

        var cc_name = request.body.cc_name;

        var cc_last_4 = request.body.cc_last_4;

        var stripe_transaction_id = request.body.stripe_transaction_id;

        var status = request.body.status;

        var kinvey_Payment = modules.collectionAccess.collection("Payment");

        if (debug) console.log("request = " + JSON.stringify(request));

        // 2. validate data

        if (!elemech_secret) {

            logger.fatal("Request did not include elemech_secret");

            response.body = { message: "Request did not include elemech_secret", error: 101 };

            response.complete(400); // Bad Request

        }

        else if (elemech_secret != "XXXX") {

            logger.warn("elemech_secret is incorrect");

            response.body = { message: "elemech_secret is incorrect", error: 102 };

            response.complete(400); // Bad Request

        }

 

        // 3. create data_obj

        var data_obj = {

            "JobNumber": JobNumber,

            "GlobalCustomerID": GlobalCustomerID,

            "Amount": Amount,

            "description": description,

            "type": type,

            "cc_name": cc_name,

            "cc_last_4": cc_last_4,

            "stripe_transaction_id": stripe_transaction_id,

            "status": status,

        };

 

        // 4. insert Payment

        kinvey_Payment.insert(data_obj, function (err, result) {

            if (err) {

                logger.error("Query failed: " + err);

                response.body = { message: "Query failed: " + err, error: 103 };

                response.complete(400); // Bad Request

            }

            else {

                if (debug) logger.info("Payment result = " + JSON.stringify(result));

                response.complete(201); // Created

            }

        });

    }

    catch (e) {

        logger.error(e.toString());

        response.body = { message: e.toString(), error: 100 };

        response.complete(400); // Bad Request

    }

}


Am I doing something wrong?  I tried 'save' without an _id and _kmd is still blank.  I thought that lmt and ect were automatically generated?


I am logging in with the master secret.


Thank you.


Best Answer
John,

This question was answered on portal. Just copying Anton's answer here to complete the loop.

"

To answer your question first. Using the standard Business Logic (as allowed by the embedded code editor) allows to use the collectionAccess module to work with data in your collections. However, to accomplish your case you will need to first create the Kinvey entity attributes with the Kinvey module.  


For example:


var myEntity = {"title": "some title from custom endpoint"};

var myKinveyEntity = modules.kinvey.entity(myEntity);

  

var insertPromise = modules.collectionAccess.collection('books').insertAsync(myKinveyEntity);

  

insertPromise.then(


Here is how the created item with the above code will look like:


[
  {
    "title": "some title from custom endpoint",
    "_id": "5a2ff4d11431b80f00401d95",
    "_acl": {
      "creator": "<<your KID>>"
    },
    "_kmd": {
      "ect": "2017-12-12T15:25:05.079Z",
      "lmt": "2017-12-12T15:25:05.079Z"
    }
  }
]


Can you please try the above approach and let me know if it works to your satisfaction? Note that the creator of the app will be the app key and not a particular user. 


You can also review the other ways of creating custom endpoints and business logic hooks, for example, with a Flex service. Both Business Logic hooks for CRUD operations in Kinvey and Custom Endpoints can be connected to a service from the Kinvey Flex Services runtime. That service is practically a Node.js application which you can built on top of Node.js and deploy to the runtime and which allows for a lot of advanced scenarios.  More about Flex Services can be read here - https://devcenter.kinvey.com/nativescript/guides/flex-services


Please note that by design custom endpoints can be invoked by all users. If you need to restrict the access to a given role or user, ensure that you create your own code for that. For example, you can use the requestContext module as explained here - https://devcenter.kinvey.com/html5/reference/business-logic/reference.html#requestcontext-module to determine the current user making the request and based on that, use another custom logic to ensure that the user is authorized to run that endpoint.


Best regards,

Anton"



 

1 Comment

Answer
John,

This question was answered on portal. Just copying Anton's answer here to complete the loop.

"

To answer your question first. Using the standard Business Logic (as allowed by the embedded code editor) allows to use the collectionAccess module to work with data in your collections. However, to accomplish your case you will need to first create the Kinvey entity attributes with the Kinvey module.  


For example:


var myEntity = {"title": "some title from custom endpoint"};

var myKinveyEntity = modules.kinvey.entity(myEntity);

  

var insertPromise = modules.collectionAccess.collection('books').insertAsync(myKinveyEntity);

  

insertPromise.then(


Here is how the created item with the above code will look like:


[
  {
    "title": "some title from custom endpoint",
    "_id": "5a2ff4d11431b80f00401d95",
    "_acl": {
      "creator": "<<your KID>>"
    },
    "_kmd": {
      "ect": "2017-12-12T15:25:05.079Z",
      "lmt": "2017-12-12T15:25:05.079Z"
    }
  }
]


Can you please try the above approach and let me know if it works to your satisfaction? Note that the creator of the app will be the app key and not a particular user. 


You can also review the other ways of creating custom endpoints and business logic hooks, for example, with a Flex service. Both Business Logic hooks for CRUD operations in Kinvey and Custom Endpoints can be connected to a service from the Kinvey Flex Services runtime. That service is practically a Node.js application which you can built on top of Node.js and deploy to the runtime and which allows for a lot of advanced scenarios.  More about Flex Services can be read here - https://devcenter.kinvey.com/nativescript/guides/flex-services


Please note that by design custom endpoints can be invoked by all users. If you need to restrict the access to a given role or user, ensure that you create your own code for that. For example, you can use the requestContext module as explained here - https://devcenter.kinvey.com/html5/reference/business-logic/reference.html#requestcontext-module to determine the current user making the request and based on that, use another custom logic to ensure that the user is authorized to run that endpoint.


Best regards,

Anton"



 


1 person likes this
Login or Signup to post a comment