mirror of
https://github.com/absmach/magistrala.git
synced 2026-08-07 07:14:46 +00:00
16ba29cf4a
Property Based Tests / api-test (push) Has been cancelled
Continuous Delivery / lint-and-build (push) Has been cancelled
Deploy GitHub Pages / swagger-ui (push) Has been cancelled
CI Pipeline / Lint Proto (push) Has been cancelled
CI Pipeline / Detect Changes (push) Has been cancelled
Continuous Delivery / Build and Push Docker Images (push) Has been cancelled
CI Pipeline / lint-and-build (push) Has been cancelled
CI Pipeline / Test ${{ matrix.module }} (push) Has been cancelled
CI Pipeline / Upload Coverage (push) Has been cancelled
Signed-off-by: Arvindh <arvindh91@gmail.com> Signed-off-by: dusan <borovcanindusan1@gmail.com> Signed-off-by: Rodney Osodo <socials@rodneyosodo.com> Co-authored-by: Dušan Borovčanin <dusan.borovcanin@absmach.eu> Co-authored-by: Rodney Osodo <socials@rodneyosodo.com> Co-authored-by: dusan <borovcanindusan1@gmail.com>
97 lines
2.7 KiB
Go
97 lines
2.7 KiB
Go
// Copyright (c) Abstract Machines
|
|
// SPDX-License-Identifier: Apache-2.0
|
|
|
|
package re
|
|
|
|
import (
|
|
"context"
|
|
|
|
"github.com/absmach/magistrala/internal/atom"
|
|
"github.com/absmach/magistrala/pkg/authn"
|
|
)
|
|
|
|
type atomService struct {
|
|
Service
|
|
projector atom.Projector
|
|
}
|
|
|
|
func WithAtom(svc Service, projector atom.Projector) Service {
|
|
if projector == nil {
|
|
return svc
|
|
}
|
|
return atomService{Service: svc, projector: projector}
|
|
}
|
|
|
|
func (svc atomService) AddRule(ctx context.Context, session authn.Session, r Rule) (Rule, error) {
|
|
rule, err := svc.Service.AddRule(ctx, session, r)
|
|
if err != nil {
|
|
return rule, err
|
|
}
|
|
if err := svc.projector.UpsertResource(ctx, ruleProjection(rule)); err != nil {
|
|
return rule, nil
|
|
}
|
|
return rule, nil
|
|
}
|
|
|
|
func (svc atomService) UpdateRule(ctx context.Context, session authn.Session, r Rule) (Rule, error) {
|
|
rule, err := svc.Service.UpdateRule(ctx, session, r)
|
|
return svc.upsertAfterRuleChange(ctx, rule, err)
|
|
}
|
|
|
|
func (svc atomService) UpdateRuleTags(ctx context.Context, session authn.Session, r Rule) (Rule, error) {
|
|
rule, err := svc.Service.UpdateRuleTags(ctx, session, r)
|
|
return svc.upsertAfterRuleChange(ctx, rule, err)
|
|
}
|
|
|
|
func (svc atomService) UpdateRuleSchedule(ctx context.Context, session authn.Session, r Rule) (Rule, error) {
|
|
rule, err := svc.Service.UpdateRuleSchedule(ctx, session, r)
|
|
return svc.upsertAfterRuleChange(ctx, rule, err)
|
|
}
|
|
|
|
func (svc atomService) EnableRule(ctx context.Context, session authn.Session, id string) (Rule, error) {
|
|
rule, err := svc.Service.EnableRule(ctx, session, id)
|
|
return svc.upsertAfterRuleChange(ctx, rule, err)
|
|
}
|
|
|
|
func (svc atomService) DisableRule(ctx context.Context, session authn.Session, id string) (Rule, error) {
|
|
rule, err := svc.Service.DisableRule(ctx, session, id)
|
|
return svc.upsertAfterRuleChange(ctx, rule, err)
|
|
}
|
|
|
|
func (svc atomService) RemoveRule(ctx context.Context, session authn.Session, id string) error {
|
|
if err := svc.Service.RemoveRule(ctx, session, id); err != nil {
|
|
return err
|
|
}
|
|
_ = svc.projector.DeleteResource(ctx, id)
|
|
return nil
|
|
}
|
|
|
|
func (svc atomService) upsertAfterRuleChange(ctx context.Context, rule Rule, err error) (Rule, error) {
|
|
if err != nil {
|
|
return rule, err
|
|
}
|
|
if err := svc.projector.UpsertResource(ctx, ruleProjection(rule)); err != nil {
|
|
return rule, nil
|
|
}
|
|
return rule, nil
|
|
}
|
|
|
|
func ruleProjection(r Rule) atom.Resource {
|
|
res := atom.ResourceFromFields(atom.ObjectFields{
|
|
ID: r.ID,
|
|
Kind: atom.KindRule,
|
|
Name: r.Name,
|
|
TenantID: r.DomainID,
|
|
OwnerID: r.CreatedBy,
|
|
Status: r.Status.String(),
|
|
Tags: r.Tags,
|
|
CreatedBy: r.CreatedBy,
|
|
UpdatedBy: r.UpdatedBy,
|
|
CreatedAt: r.CreatedAt,
|
|
UpdatedAt: r.UpdatedAt,
|
|
})
|
|
res.Attributes["input_channel"] = r.InputChannel
|
|
res.Attributes["input_topic"] = r.InputTopic
|
|
return res
|
|
}
|