update_agent_api_logic.go 1.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950
  1. package whatsapp
  2. import (
  3. "context"
  4. "github.com/suyuan32/simple-admin-common/msg/errormsg"
  5. "wechat-api/ent/predicate"
  6. "wechat-api/ent/whatsapp"
  7. "wechat-api/internal/utils/dberrorhandler"
  8. "wechat-api/internal/svc"
  9. "wechat-api/internal/types"
  10. "github.com/zeromicro/go-zero/core/logx"
  11. )
  12. type UpdateAgentApiLogic struct {
  13. logx.Logger
  14. ctx context.Context
  15. svcCtx *svc.ServiceContext
  16. }
  17. func NewUpdateAgentApiLogic(ctx context.Context, svcCtx *svc.ServiceContext) *UpdateAgentApiLogic {
  18. return &UpdateAgentApiLogic{
  19. Logger: logx.WithContext(ctx),
  20. ctx: ctx,
  21. svcCtx: svcCtx}
  22. }
  23. func (l *UpdateAgentApiLogic) UpdateAgentApi(req *types.UpdateAgentApiReq) (resp *types.BaseMsgResp, err error) {
  24. isAdmin := l.ctx.Value("isAdmin").(bool)
  25. var predicates []predicate.Whatsapp
  26. if !isAdmin {
  27. organizationId := l.ctx.Value("organizationId").(uint64)
  28. predicates = append(predicates, whatsapp.OrganizationID(organizationId))
  29. }
  30. err = l.svcCtx.DB.Whatsapp.UpdateOneID(req.Id).
  31. Where(predicates...).
  32. SetNotNilAPIBase(req.ApiBase).
  33. SetNotNilAPIKey(req.ApiKey).
  34. Exec(l.ctx)
  35. if err != nil {
  36. return nil, dberrorhandler.DefaultEntError(l.Logger, err, req)
  37. }
  38. _ = l.svcCtx.Rds.Del(l.ctx, "wa_info")
  39. return &types.BaseMsgResp{Msg: errormsg.UpdateSuccess}, nil
  40. }