BillionMail gives you open-source MailServer, NewsLetter, Email Marketing — fully self-hosted, dev-friendly, and free from monthly fees. Join the discord: https://discord.gg/asfXzBUhZr
Add support for batch tagging with multiple tag IDs and implement endpoint to retrieve all tags
lotk6542ac52e0ca
diff --git a/core/api/tags/tags.go b/core/api/tags/tags.goindex 93a2ebe2..e8b6c821 100644--- a/core/api/tags/tags.go+++ b/core/api/tags/tags.go@@ -16,4 +16,5 @@ type ITagsV1 interface {TagUpdate(ctx context.Context, req *v1.TagUpdateReq) (res *v1.TagUpdateRes, err error)TagDelete(ctx context.Context, req *v1.TagDeleteReq) (res *v1.TagDeleteRes, err error)BatchTagContacts(ctx context.Context, req *v1.BatchTagContactsReq) (res *v1.BatchTagContactsRes, err error)+ TagAll(ctx context.Context, req *v1.TagAllReq) (res *v1.TagAllRes, err error)}diff --git a/core/api/tags/v1/tags.go b/core/api/tags/v1/tags.goindex f89576f5..1565973f 100644--- a/core/api/tags/v1/tags.go+++ b/core/api/tags/v1/tags.go@@ -62,10 +62,22 @@ type BatchTagContactsReq struct {g.Meta `path:"/tags/batch_contacts" method:"post" tags:"Tag" summary:"Tag contacts in bulk"`Authorization string `json:"authorization" dc:"Authorization" in:"header"`GroupId int `json:"group_id" v:"required|min:1" dc:"Group ID"`- TagId int `json:"tag_id" v:"required|min:1" dc:"Tag ID"`+ TagIds []int `json:"tag_ids" v:"required|min-length:1" dc:"Tag IDs"`Data string `json:"data" v:"required" dc:"Contact emails, separated by new lines"`MarkInclude int `json:"mark_include" v:"required|in:0,1" dc:"Mark include (1) or exclude (0)"`}type BatchTagContactsRes struct {api_v1.StandardRes}++type TagAllReq struct {+ g.Meta `path:"/tags/all" method:"get" tags:"Tag" summary:"Get tags all"`+ Authorization string `json:"authorization" dc:"Authorization" in:"header"`+}+type TagAllRes struct {+ api_v1.StandardRes+ Data struct {+ Total int `json:"total" dc:"Total Count"`+ List []*entity.Tag `json:"list" dc:"Tag list"`+ } `json:"data"`+}diff --git a/core/internal/controller/tags/tags_v1_batch_tag_contacts.go b/core/internal/controller/tags/tags_v1_batch_tag_contacts.goindex 085151b4..a1e07786 100644--- a/core/internal/controller/tags/tags_v1_batch_tag_contacts.go+++ b/core/internal/controller/tags/tags_v1_batch_tag_contacts.go@@ -26,45 +26,66 @@ func (c *ControllerV1) BatchTagContacts(ctx context.Context, req *v1.BatchTagConreturn}- var tag struct {+ var tags []struct {Id int `json:"id"`Name string `json:"name"`GroupId int `json:"group_id"`}- err = g.DB().Model("bm_tags").Where("id", req.TagId).Scan(&tag)+ err = g.DB().Model("bm_tags").WhereIn("id", req.TagIds).Scan(&tags)if err != nil {- res.SetError(gerror.New(public.LangCtx(ctx, "Failed to check tag")))+ res.SetError(gerror.New(public.LangCtx(ctx, "Failed to check tags")))return}- if tag.Id == 0 {- res.SetError(gerror.New(public.LangCtx(ctx, "Tag not found")))+ if len(tags) == 0 {+ res.SetError(gerror.New(public.LangCtx(ctx, "No tags found")))return}- if tag.GroupId != req.GroupId {- res.SetError(gerror.New(public.LangCtx(ctx, "Tag does not belong to this group")))+ if len(tags) != len(req.TagIds) {+ res.SetError(gerror.New(public.LangCtx(ctx, "Some tags not found")))return}+ var tagNames []string+ for _, tag := range tags {+ if tag.GroupId != req.GroupId {+ res.SetError(gerror.New(public.LangCtx(ctx, "Tag '{}' does not belong to this group", tag.Name)))+ return+ }+ tagNames = append(tagNames, tag.Name)+ }+emailList := parseEmailList(req.Data)if len(emailList) == 0 {res.SetError(gerror.New(public.LangCtx(ctx, "No valid emails provided")))return}- g.Log().Infof(ctx, "BatchTagContacts: Processing %d emails for tag %s (ID: %d), mode: %d",- len(emailList), tag.Name, req.TagId, req.MarkInclude)+ g.Log().Infof(ctx, "BatchTagContacts: Processing %d emails for tags %v (IDs: %v), mode: %d",+ len(emailList), tagNames, req.TagIds, req.MarkInclude)- var processedCount, skippedCount, errorCount int+ var totalProcessedCount, totalSkippedCount, totalErrorCount interr = g.DB().Transaction(ctx, func(ctx context.Context, tx gdb.TX) error {- if req.MarkInclude == 1 {- processedCount, skippedCount, errorCount, err = c.batchAddTags(ctx, tx, req.GroupId, req.TagId, emailList)- } else {+ for _, tagId := range req.TagIds {+ var processedCount, skippedCount, errorCount int+ var err error++ if req.MarkInclude == 1 {+ processedCount, skippedCount, errorCount, err = c.batchAddTags(ctx, tx, req.GroupId, tagId, emailList)+ } else {+ processedCount, skippedCount, errorCount, err = c.batchAddTagsExclude(ctx, tx, req.GroupId, tagId, emailList)+ }++ if err != nil {+ return err+ }- processedCount, skippedCount, errorCount, err = c.batchAddTagsExclude(ctx, tx, req.GroupId, req.TagId, emailList)+ totalProcessedCount += processedCount+ totalSkippedCount += skippedCount+ totalErrorCount += errorCount}- return err+ return nil})if err != nil {@@ -78,29 +99,29 @@ func (c *ControllerV1) BatchTagContacts(ctx context.Context, req *v1.BatchTagCon}_ = public.WriteLog(ctx, public.LogParams{Type: consts.LOGTYPE.Tag,- Log: "Batch add" + " tag: " + tag.Name + operation + " for " +- g.NewVar(processedCount).String() + " contacts successfully",+ Log: "Batch add" + " tags: " + strings.Join(tagNames, ", ") + operation + " for " ++ g.NewVar(totalProcessedCount).String() + " contact-tag pairs successfully",Data: map[string]interface{}{- "tag_id": req.TagId,- "tag_name": tag.Name,+ "tag_ids": req.TagIds,+ "tag_names": tagNames,"group_id": req.GroupId,"operation": operation,"total_emails": len(emailList),- "processed": processedCount,- "skipped": skippedCount,- "errors": errorCount,+ "processed": totalProcessedCount,+ "skipped": totalSkippedCount,+ "errors": totalErrorCount,},})successMsg := public.LangCtx(ctx, "Batch tag operation completed")if req.MarkInclude == 1 {- successMsg = public.LangCtx(ctx, "Successfully added tag to {} contacts", processedCount)+ successMsg = public.LangCtx(ctx, "Successfully added {} tags to contacts with {} tag assignments", len(req.TagIds), totalProcessedCount)} else {- successMsg = public.LangCtx(ctx, "Successfully removed tag from {} contacts", processedCount)+ successMsg = public.LangCtx(ctx, "Successfully removed {} tags from contacts with {} tag removals", len(req.TagIds), totalProcessedCount)}- if skippedCount > 0 {- successMsg += public.LangCtx(ctx, " ( {} skipped, {} errors)", skippedCount, errorCount)+ if totalSkippedCount > 0 {+ successMsg += public.LangCtx(ctx, " ( {} skipped, {} errors)", totalSkippedCount, totalErrorCount)}res.SetSuccess(successMsg)diff --git a/core/internal/controller/tags/tags_v1_tag_all.go b/core/internal/controller/tags/tags_v1_tag_all.gonew file mode 100644index 00000000..e12f78ad--- /dev/null+++ b/core/internal/controller/tags/tags_v1_tag_all.go@@ -0,0 +1,47 @@+package tags++import (+ "billionmail-core/internal/model/entity"+ "billionmail-core/internal/service/contact"+ "billionmail-core/internal/service/public"+ "context"+ "github.com/gogf/gf/v2/frame/g"++ "billionmail-core/api/tags/v1"+)++func (c *ControllerV1) TagAll(ctx context.Context, req *v1.TagAllReq) (res *v1.TagAllRes, err error) {+ res = &v1.TagAllRes{}++ model := g.DB().Model("bm_tags")+ total, err := model.Count()+ if err != nil {+ return nil, err+ }++ tagList := make([]*entity.Tag, 0)+ err = model.Order("create_time DESC").+ Scan(&tagList)++ if err != nil {+ return nil, err+ }++ for _, tag := range tagList {++ // check if group exists+ GroupInfo, err := contact.GetGroup(ctx, tag.GroupId)++ if err != nil || GroupInfo == nil {+ tag.GroupName = "Unknown"+ } else {+ tag.GroupName = GroupInfo.Name+ }++ }++ res.Data.Total = total+ res.Data.List = tagList+ res.SetSuccess(public.LangCtx(ctx, "Get tag list successfully"))+ return+}