- JWT claims extended with tenantId; login enforces strict tenant verification - AuthorityMiddleware: tenant scope check + Casbin path permission + anti-spoofing - CRM relation API (upstream/downstream one-hop, create/update/history, full graph) - CrmRepo backed by PostgreSQL with $N placeholders - gRPC tenant propagation via UnaryClientInterceptor (x-tenant-id metadata) - All legacy tables (12) gain tenant_id column with indexes - All model queries inject WHERE tenant_id filter - Casbin gorm-adapter downgraded to v3.28.0 for v2 compatibility - GraphSyncWorker (Kafka -> Neo4j) with idempotent MERGE - Full graph API restricted to admin role only - Database migrations for MySQL (CRM tables + tenant columns) and PostgreSQL (CRM init) - Docker Compose: added postgres service to main stack, graph stack with Kafka/Debezium/Neo4j Made-with: Cursor
56 lines
1.4 KiB
Go
56 lines
1.4 KiB
Go
package relation
|
|
|
|
import (
|
|
"context"
|
|
"errors"
|
|
|
|
"muyu-apiserver/gateway/internal/repo"
|
|
"muyu-apiserver/gateway/internal/svc"
|
|
"muyu-apiserver/gateway/internal/types"
|
|
"muyu-apiserver/pkg/ctxdata"
|
|
|
|
"github.com/zeromicro/go-zero/core/logx"
|
|
)
|
|
|
|
type CreateRelationLogic struct {
|
|
logx.Logger
|
|
ctx context.Context
|
|
svcCtx *svc.ServiceContext
|
|
}
|
|
|
|
func NewCreateRelationLogic(ctx context.Context, svcCtx *svc.ServiceContext) *CreateRelationLogic {
|
|
return &CreateRelationLogic{
|
|
Logger: logx.WithContext(ctx),
|
|
ctx: ctx,
|
|
svcCtx: svcCtx,
|
|
}
|
|
}
|
|
|
|
func (l *CreateRelationLogic) CreateRelation(req *types.CrmCreateRelationReq) (*types.IdResp, error) {
|
|
if req.FromTenantId == "" || req.ToTenantId == "" || req.RelationType == "" {
|
|
return nil, errors.New("fromTenantId/toTenantId/relationType are required")
|
|
}
|
|
if req.FromTenantId == req.ToTenantId {
|
|
return nil, errors.New("fromTenantId and toTenantId cannot be equal")
|
|
}
|
|
|
|
tenantId := ctxdata.GetTenantId(l.ctx)
|
|
userId := ctxdata.GetUserId(l.ctx)
|
|
relID, err := l.svcCtx.CrmRepo.CreateRelation(l.ctx, repo.CreateRelationInput{
|
|
OwnerTenantId: tenantId,
|
|
FromTenantId: req.FromTenantId,
|
|
ToTenantId: req.ToTenantId,
|
|
RelationType: req.RelationType,
|
|
Status: 1,
|
|
ValidFrom: parseTimeOrNow(req.ValidFrom),
|
|
ValidTo: parseNullTime(req.ValidTo),
|
|
Source: "manual",
|
|
CreatedBy: userId,
|
|
})
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
return &types.IdResp{Id: relID}, nil
|
|
}
|
|
|