mirror of
https://github.com/warmbly/warmbly.git
synced 2026-09-12 00:05:09 +00:00
98 lines
3.0 KiB
Go
98 lines
3.0 KiB
Go
package main
|
|
|
|
import (
|
|
"context"
|
|
"fmt"
|
|
|
|
"github.com/google/uuid"
|
|
|
|
"github.com/warmbly/warmbly/internal/app/advanced"
|
|
"github.com/warmbly/warmbly/internal/models"
|
|
"github.com/warmbly/warmbly/internal/repository"
|
|
)
|
|
|
|
// nativeActionsAdapter satisfies integration.NativeActions, bridging the
|
|
// integration package's automation executor to the advanced/contact/org
|
|
// services. It converts *errx.Error to error (so a nil error stays nil) and
|
|
// resolves the contact + org owner the native CRM/contact actions need.
|
|
type nativeActionsAdapter struct {
|
|
adv advanced.Service
|
|
contacts repository.ContactRepository
|
|
orgs repository.OrganizationRepository
|
|
}
|
|
|
|
func (a nativeActionsAdapter) ResolveContact(ctx context.Context, orgID uuid.UUID, contactID, email string) (*models.Contact, error) {
|
|
// Both lookups are ORG-SCOPED — never resolve a contact id from another org,
|
|
// even if a stale/crafted id reaches the event data.
|
|
if contactID != "" {
|
|
if id, perr := uuid.Parse(contactID); perr == nil {
|
|
if cs, e := a.contacts.GetByIDsAndOrganization(ctx, orgID, []uuid.UUID{id}); e == nil && len(cs) > 0 {
|
|
return &cs[0], nil
|
|
}
|
|
}
|
|
}
|
|
if email != "" {
|
|
if c, e := a.contacts.GetByEmailAndOrganization(ctx, orgID, email); e == nil && c != nil {
|
|
return c, nil
|
|
}
|
|
}
|
|
return nil, nil
|
|
}
|
|
|
|
func (a nativeActionsAdapter) OrgOwner(ctx context.Context, orgID uuid.UUID) (uuid.UUID, error) {
|
|
org, err := a.orgs.GetByID(ctx, orgID)
|
|
if err != nil {
|
|
return uuid.Nil, err
|
|
}
|
|
if org == nil {
|
|
return uuid.Nil, fmt.Errorf("organization not found")
|
|
}
|
|
return org.OwnerUserID, nil
|
|
}
|
|
|
|
func (a nativeActionsAdapter) AddTag(ctx context.Context, orgID, actorID, contactID, categoryID uuid.UUID) error {
|
|
if _, e := a.contacts.Update(ctx, actorID.String(), contactID.String(), &models.UpdateContact{
|
|
AddCategories: []string{categoryID.String()},
|
|
}); e != nil {
|
|
return e
|
|
}
|
|
return nil
|
|
}
|
|
|
|
func (a nativeActionsAdapter) RemoveTag(ctx context.Context, orgID, actorID, contactID, categoryID uuid.UUID) error {
|
|
if _, e := a.contacts.Update(ctx, actorID.String(), contactID.String(), &models.UpdateContact{
|
|
RemoveCategories: []string{categoryID.String()},
|
|
}); e != nil {
|
|
return e
|
|
}
|
|
return nil
|
|
}
|
|
|
|
func (a nativeActionsAdapter) CreateTask(ctx context.Context, orgID, createdBy uuid.UUID, data *models.CreateCRMTask) error {
|
|
if _, e := a.adv.CreateContactTask(ctx, orgID, createdBy, data); e != nil {
|
|
return e
|
|
}
|
|
return nil
|
|
}
|
|
|
|
func (a nativeActionsAdapter) CreateDeal(ctx context.Context, orgID, createdBy uuid.UUID, data *models.CreateDeal) error {
|
|
if _, e := a.adv.CreateContactDeal(ctx, orgID, createdBy, data); e != nil {
|
|
return e
|
|
}
|
|
return nil
|
|
}
|
|
|
|
func (a nativeActionsAdapter) MoveDealStage(ctx context.Context, orgID, contactID, pipelineID, stageID uuid.UUID) error {
|
|
if _, e := a.adv.MoveContactDealStage(ctx, orgID, contactID, pipelineID, stageID); e != nil {
|
|
return e
|
|
}
|
|
return nil
|
|
}
|
|
|
|
func (a nativeActionsAdapter) Unsubscribe(ctx context.Context, campaignID, contactID uuid.UUID) error {
|
|
if e := a.adv.Unsubscribe(ctx, campaignID, contactID); e != nil {
|
|
return e
|
|
}
|
|
return nil
|
|
}
|