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 we run approval based on specific conditions, such as if certain model attributes match specific criteria? #46

Open
ehtashamGH opened this issue Oct 28, 2024 · 2 comments

Comments

@ehtashamGH
Copy link

Can we run approval based on specific conditions, such as if certain model attributes match specific criteria? If they do, run approval; otherwise, do not.
@ringunger

@kraziegent
Copy link

Should be able to do that by just overriding the boot method in the Approvable trait.
What I did was create a new trait, extend the ringlesoft approvable trait in my trait then I override the boot method. Something like below
`
protected static function boot(): void
{
parent::boot();

    if (!Auth::user()->isAdmin()) {
        static::created(static function ($model) {
            if ($model->transaction_type && !in_array($model->transaction_type, TransactionType::approvable())) {
                return;
            }

            if (method_exists($model, 'gaurantors') && $model->gaurantors()->where('status', 'pending')->exists()) {
                return;
            }

            $model->approvalStatus()->create([
                'steps' => $model->approvalFlowSteps()->map(function ($item) {
                    return $item->toApprovalStatusArray();
                }),
                'status' => ApprovalStatusEnum::CREATED->value,
                'creator_id' => Auth::id(),
            ]);

            if (property_exists($model, 'autoSubmit') && $model->autoSubmit) {
                $model->submit(Auth::user());
            }
        });
    }
}

`

@ehtashamGH
Copy link
Author

Should be able to do that by just overriding the boot method in the Approvable trait. What I did was create a new trait, extend the ringlesoft approvable trait in my trait then I override the boot method. Something like below ` protected static function boot(): void { parent::boot();

    if (!Auth::user()->isAdmin()) {
        static::created(static function ($model) {
            if ($model->transaction_type && !in_array($model->transaction_type, TransactionType::approvable())) {
                return;
            }

            if (method_exists($model, 'gaurantors') && $model->gaurantors()->where('status', 'pending')->exists()) {
                return;
            }

            $model->approvalStatus()->create([
                'steps' => $model->approvalFlowSteps()->map(function ($item) {
                    return $item->toApprovalStatusArray();
                }),
                'status' => ApprovalStatusEnum::CREATED->value,
                'creator_id' => Auth::id(),
            ]);

            if (property_exists($model, 'autoSubmit') && $model->autoSubmit) {
                $model->submit(Auth::user());
            }
        });
    }
}

`

Okay, thanks! I will definitely give it a try. Is there anything from you on this, @ringunger ?

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