Fixed tag removal issue.

This commit is contained in:
Asaf Gartner 2022-01-31 10:22:25 +02:00
parent 378d6eb836
commit 92d6a31aa9
3 changed files with 39 additions and 38 deletions

View File

@ -798,45 +798,43 @@ func HandleSnippetForInternedMessage(ctx context.Context, dbConn db.ConnOrTx, in
var desiredTags []int var desiredTags []int
var allTags []int var allTags []int
if len(messageTags) > 0 { // Fetch projects so we know what tags the user can apply to their snippet.
// Fetch projects so we know what tags the user can apply to their snippet. projects, err := hmndata.FetchProjects(ctx, tx, interned.HMNUser, hmndata.ProjectsQuery{
projects, err := hmndata.FetchProjects(ctx, tx, interned.HMNUser, hmndata.ProjectsQuery{ OwnerIDs: []int{interned.HMNUser.ID},
OwnerIDs: []int{interned.HMNUser.ID}, })
}) if err != nil {
if err != nil { return oops.New(err, "failed to look up user projects")
return oops.New(err, "failed to look up user projects") }
}
projectIDs := make([]int, len(projects)) projectIDs := make([]int, len(projects))
for i, p := range projects { for i, p := range projects {
projectIDs[i] = p.Project.ID projectIDs[i] = p.Project.ID
} }
type tagsRow struct { type tagsRow struct {
Tag models.Tag `db:"tags"` Tag models.Tag `db:"tags"`
} }
iUserTags, err := db.Query(ctx, tx, tagsRow{}, iUserTags, err := db.Query(ctx, tx, tagsRow{},
` `
SELECT $columns SELECT $columns
FROM FROM
tags tags
JOIN handmade_project AS project ON project.tag = tags.id JOIN handmade_project AS project ON project.tag = tags.id
WHERE WHERE
project.id = ANY ($1) project.id = ANY ($1)
`, `,
projectIDs, projectIDs,
) )
if err != nil { if err != nil {
return oops.New(err, "failed to fetch tags for user projects") return oops.New(err, "failed to fetch tags for user projects")
} }
for _, itag := range iUserTags { for _, itag := range iUserTags {
tag := itag.(*tagsRow).Tag tag := itag.(*tagsRow).Tag
for _, messageTag := range messageTags { allTags = append(allTags, tag.ID)
allTags = append(allTags, tag.ID) for _, messageTag := range messageTags {
if strings.EqualFold(tag.Text, messageTag) { if strings.EqualFold(tag.Text, messageTag) {
desiredTags = append(desiredTags, tag.ID) desiredTags = append(desiredTags, tag.ID)
}
} }
} }
} }

View File

@ -463,7 +463,7 @@ func SetProjectTag(
IncludeHidden: true, IncludeHidden: true,
}) })
if err != nil { if err != nil {
return nil, err return nil, oops.New(err, "Failed to fetch project")
} }
var resultTag *models.Tag var resultTag *models.Tag

View File

@ -760,7 +760,10 @@ func updateProject(ctx context.Context, tx pgx.Tx, user *models.User, payload *P
return oops.New(err, "Failed to update project") return oops.New(err, "Failed to update project")
} }
hmndata.SetProjectTag(ctx, tx, payload.ProjectID, payload.Tag) _, err = hmndata.SetProjectTag(ctx, tx, payload.ProjectID, payload.Tag)
if err != nil {
return err
}
if user.IsStaff { if user.IsStaff {
_, err = tx.Exec(ctx, _, err = tx.Exec(ctx,