framework icon indicating copy to clipboard operation
framework copied to clipboard

How can I validate with schema.define the workflow options?

Open LandyCuadra opened this issue 5 years ago • 8 comments

I am calling workflow from the controller with $WORKFLOW and sending an Object with the body received but can't get it clean, following the schema definition... any Idea of how to achieved that?

LandyCuadra avatar Jul 22 '21 00:07 LandyCuadra

Hi @LandyCuadra, you can clean it via $clean() method:

var data = model.$clean();

BTW: I recommend using Total.js v4 (the model is always clean in v4).

petersirka avatar Jul 22 '21 07:07 petersirka

but I do not have a model var... look at how I do it.

this is my controller function

async function addClient() {
    const self = this,
        {body} = self,
        data = body;

    $WORKFLOW('InsuranceVerification','saveClient', data, (err, response) => {
        if(err){
            self.status = 500;
            self.json(err);
            return;
        }
        self.status = 200;
        self.json(response);
    }, self);
}

and this is in my schema

NEWSCHEMA('InsuranceVerification', (schema) => {
    schema.define("clientId", String, true);
    schema.define("patientId", String);
    schema.define("apptDate", String);
  
    schema.required("patientId", (model, op) => {
      console.log("Operation", op);
      return !!op.update
    });
  
    schema.required("apptDate", (model, op) => {
      console.log("Operation", op);
      return !!op.update
    });

    schema.addWorkflow('saveClient', async function ($) {
            const model = {...$.clean(), ...schema.clean($.options)};
    
           //OPERATION 
    
           $.success("client inserted successfuly");
    });
}

I have tried to call it like this, and getting the model only with $.clean(), so it respect the schema.define but in the schema.required() the op comes empty and loss the controller (because I don't send it)

function addClient() {

    const self = this;
    const body = self.body;
    body.method = self.res.req.method;

    GETSCHEMA("InsuranceVerification").make(body, function (err, model) {
        if (err) {
            self.status = 500;
            self.json(err);
            return;
        }

        model.$workflow("saveClient", function (errSave, res) {
            if (errSave) {
                self.status = 500;
                self.json(err);
                return;
            }
            self.status = 200;
            self.json(res);
        });
    });
}

LandyCuadra avatar Jul 22 '21 14:07 LandyCuadra

Try to add: model.$controler(self) and let me kwow. This can solve a problem with the op comes empty and loss the controller (because I don't send it)

function addClient() {

    const self = this;
    const body = self.body;
    body.method = self.res.req.method;

    GETSCHEMA("InsuranceVerification").make(body, function (err, model) {
        if (err) {
            self.status = 500;
            self.json(err);
            return;
        }
        
        // TRY TO ADD:
        model.$controller(self);

        model.$workflow("saveClient", function (errSave, res) {
            if (errSave) {
                self.status = 500;
                self.json(err);
                return;
            }
            self.status = 200;
            self.json(res);
        });
    });
}

petersirka avatar Jul 23 '21 10:07 petersirka

now I am getting the controller, but op in schema.required() still an empty object

LandyCuadra avatar Jul 24 '21 02:07 LandyCuadra

I understand and this is a bit problem because the op is assigned automatically. In your case you need to assign op manually in the .make() method (but it's not good way). I don't know why don't you use routing to the schemas.

This solves all problems:

ROUTE('POST /api/clients/add/   *InsuranceVerification --> @saveClient');

petersirka avatar Jul 24 '21 07:07 petersirka

yeah, already tried it this way and it works, but the company wants the route poiting to a function.... thank you for your help!

LandyCuadra avatar Jul 24 '21 14:07 LandyCuadra

In your case you need to assign op manually in the .make() method (but it's not good way)

hi again, can show where I can find how to do this?, please, I checked the documentation but the only reference to operations for schema instances was to invoke an addOperation method... with this I could close the issue

LandyCuadra avatar Jul 28 '21 03:07 LandyCuadra

yeah, already tried it this way and it works, but the company wants the route poiting to a function.... thank you for your help!

Total.js creates a function in the background. Routing with the schemas brings clear structure.


// This property is assigned to the `.required()` evaluator.
model.$$workflow = { update: 1 };
model.$controller(self);
model.$workflow("saveClient", function (errSave, res) {

});

Look to the $ACTION() method: https://docs.totaljs.com/latest/en.html#api~global~%24ACTION

petersirka avatar Jul 28 '21 06:07 petersirka