get_allow_block_list_logic.go 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899
  1. package whatsapp
  2. import (
  3. "context"
  4. "wechat-api/ent/predicate"
  5. "wechat-api/ent/whatsapp"
  6. "wechat-api/internal/utils/dberrorhandler"
  7. "wechat-api/internal/svc"
  8. "wechat-api/internal/types"
  9. "github.com/zeromicro/go-zero/core/logx"
  10. )
  11. type GetAllowBlockListLogic struct {
  12. logx.Logger
  13. ctx context.Context
  14. svcCtx *svc.ServiceContext
  15. }
  16. func NewGetAllowBlockListLogic(ctx context.Context, svcCtx *svc.ServiceContext) *GetAllowBlockListLogic {
  17. return &GetAllowBlockListLogic{
  18. Logger: logx.WithContext(ctx),
  19. ctx: ctx,
  20. svcCtx: svcCtx}
  21. }
  22. func (l *GetAllowBlockListLogic) GetAllowBlockList(req *types.IDReq) (resp *types.WhatsappAllowBlockListResp, err error) {
  23. isAdmin := l.ctx.Value("isAdmin").(bool)
  24. var predicates []predicate.Whatsapp
  25. predicates = append(predicates, whatsapp.IDEQ(req.Id))
  26. if !isAdmin {
  27. organizationId := l.ctx.Value("organizationId").(uint64)
  28. predicates = append(predicates, whatsapp.OrganizationID(organizationId))
  29. }
  30. whatsappInfo, err := l.svcCtx.DB.Whatsapp.Query().
  31. Where(predicates...).
  32. Only(l.ctx)
  33. if err != nil {
  34. return nil, dberrorhandler.DefaultEntError(l.Logger, err, req)
  35. }
  36. resp = &types.WhatsappAllowBlockListResp{}
  37. resp.Data = types.WhatsappAllowBlockListRespData{}
  38. allowListData := ""
  39. if whatsappInfo.AllowList == nil || len(whatsappInfo.AllowList) == 0 || whatsappInfo.AllowList[0] == "ALL" {
  40. allowListData = "ALL"
  41. } else if len(whatsappInfo.AllowList) > 0 {
  42. for _, av := range whatsappInfo.AllowList {
  43. if av != "" {
  44. allowListData += av + "\n"
  45. }
  46. }
  47. }
  48. resp.Data.AllowList = &allowListData
  49. groupAllowListData := ""
  50. if whatsappInfo.GroupAllowList == nil || len(whatsappInfo.GroupAllowList) == 0 || whatsappInfo.GroupAllowList[0] == "ALL" {
  51. groupAllowListData = "ALL"
  52. } else if len(whatsappInfo.GroupAllowList) > 0 {
  53. for _, gav := range whatsappInfo.GroupAllowList {
  54. if gav != "" {
  55. groupAllowListData += gav + "\n"
  56. }
  57. }
  58. }
  59. resp.Data.GroupAllowList = &groupAllowListData
  60. blockListData := ""
  61. if whatsappInfo.BlockList != nil && len(whatsappInfo.BlockList) > 0 {
  62. if whatsappInfo.BlockList[0] == "ALL" {
  63. blockListData = "ALL"
  64. } else {
  65. for _, bv := range whatsappInfo.BlockList {
  66. if bv != "" {
  67. blockListData += bv + "\n"
  68. }
  69. }
  70. }
  71. }
  72. resp.Data.BlockList = &blockListData
  73. groupBlockListData := ""
  74. if whatsappInfo.GroupBlockList != nil && len(whatsappInfo.GroupBlockList) > 0 {
  75. if whatsappInfo.GroupBlockList[0] == "ALL" {
  76. groupBlockListData = "ALL"
  77. } else {
  78. for _, gbv := range whatsappInfo.GroupBlockList {
  79. if gbv != "" {
  80. groupBlockListData += gbv + "\n"
  81. }
  82. }
  83. }
  84. }
  85. resp.Data.GroupBlockList = &groupBlockListData
  86. return resp, nil
  87. }