Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Can't find fact values within rule. but i find values within response #63

Closed
muhammadfaizan opened this issue Nov 28, 2017 · 2 comments
Closed

Comments

@muhammadfaizan
Copy link

I can't use my facts values wihtin my rules. can you help me with it?

const allowedActions = ['payment-received', 'checkin'];
  const rules = [
    {
      name: '1- Check actions',
      on: true,
      condition: (F) => {
       // this rule shouldn't execute, because i am providing proper action, but i find it undefined
        F.when(!allowedActions.includes(this.action));
      },
      priority: 100,
      consequence: (F) => {
        debug('1')
        // but its getting executed
        this.result = false;
        this.error = new CustomErrors.InvalidValueError(`please select only allowed action: ${allowedActions.join(', ')}`);
        F.stop();
      },
    },
    {
      name: '2- throw error If appointment is not cancelled or completed',
      on: true,
      priority: 90,
      condition: (F) => {
        debug('2');
        F.when(this.appointment.status >= enums.appointmentStatus.cancelled);
      },
      consequence: (F) => {
        this.result = false;
        this.error = new CustomErrors.PreconditionError('Can\'t edit appointment at this stage');
        F.stop();
      },
    },
    {
      name: '3- Check if action is "checkin" and status is confirmed',
      on: true,
      priority: 80,
      condition: (F) => {
        debug('3');

        F.when(this.action === 'checkin' && this.appointment.status === enums.appointmentStatus.confirmed);
      },
      consequence: (F) => {
        this.result = true;
        this.status = enums.appointmentStatus.checkin;
        F.stop();
      },
    },
    {
      name: '3- Check if payment received and provider exists',
      on: true,
      priority: 70,
      description: 'check if payment received and provider exists',
      condition: (F) => {
        debug('3');
        F.when(this.appointment.provider && this.action === 'payment-received');
      },
      consequence: (F) => {
        this.result = true;
        this.status = enums.appointmentStatus.confirmed;
        F.stop();
      },
    },
    {
      name: '4- check if payment received and provider DOES NOT exists',
      on: true,
      priority: 60,
      description: 'check if payment received and provider exists',
      condition: (F) => {
        debug('4');

        F.when(!this.appointment.provider && this.action === 'payment-received');
      },
      consequence: (F) => {
        // NOTE: this status is confirmed from shafaque (2:23PM, 21-Nov-17 discussion)
        this.result = true;
        this.status = enums.appointmentStatus.new;
        F.stop();
      },
    },
    {
      name: '5- if there is no result assigned, this should throw error',
      on: true,
      priority: 1,
      condition: (F) => {
        F.when(!this.result);
      },
      consequence: (F) => {
        debug('5');
        this.result = false;
        this.error = new CustomErrors.BadRequestError("couldn't identify next status");
        F.stop();
      },
    },
  ];

  const ruleEngine = new RuleEngine();
  ruleEngine.register(rules);
  return new Promise((resolve, reject) => {
    debug(payload);
    ruleEngine.execute({
      action: 'checkin',
      appointment: { status: 7, provider: "ABC XYZ" }
    }, (response) => {
     // here i get all facts, match_paths etc.
      debug('result = ', response);
      if (response.result) {
        resolve({ result: response.status });
      } else {
        reject(response.error);
      }
    });
  });
@nikollatesla
Copy link
Collaborator

muhammadfaizan@ Can you post a completely functional code block with the exact description of the issue? Right now this issue doesn't have a code which is runnable on node.js directly it has custom methods which are not defined. Please share an independent functional code with the problem description, if you need this to be looked into.

@muhammadfaizan
Copy link
Author

@mendaXXXX I guess I found a way to make it work, this issue is closed rightly.
Thank You 👍

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants