Skip to content

Commit 6333c68

Browse files
committed
fix(drafts): include channel id when updating drafts
1 parent 03293ec commit 6333c68

3 files changed

Lines changed: 55 additions & 0 deletions

File tree

‎README.md‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -319,6 +319,7 @@ frontcli drafts get dra_xxx
319319

320320
# Update draft (optimistic locking with version)
321321
frontcli drafts update dra_xxx --body "Updated draft" --draft-version draft-ver-123
322+
frontcli drafts update dra_xxx --channel cha_xxx --body-file ./reply.html --draft-version draft-ver-123
322323

323324
# Delete draft
324325
frontcli drafts delete dra_xxx --draft-version draft-ver-123

‎internal/cmd/drafts.go‎

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -257,6 +257,7 @@ func (c *DraftGetCmd) Run(flags *RootFlags) error {
257257

258258
type DraftUpdateCmd struct {
259259
ID string `arg:"" help:"Draft ID"`
260+
Channel string `help:"Channel ID required by Front for some draft updates"`
260261
Body string `help:"New body"`
261262
BodyFile string `help:"Read body from file" type:"existingfile"`
262263
Subject string `help:"New subject"`
@@ -294,6 +295,10 @@ func (c *DraftUpdateCmd) Run(flags *RootFlags) error {
294295
req["body"] = body
295296
}
296297

298+
if c.Channel != "" {
299+
req["channel_id"] = c.Channel
300+
}
301+
297302
if c.Subject != "" {
298303
req["subject"] = c.Subject
299304
}

‎internal/cmd/drafts_test.go‎

Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -683,6 +683,55 @@ func TestDraftUpdateSendsStringVersion(t *testing.T) {
683683
}
684684
}
685685

686+
func TestDraftUpdateSendsChannelIDWhenProvided(t *testing.T) {
687+
t.Setenv("HOME", t.TempDir())
688+
689+
var gotBody map[string]any
690+
691+
srv := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
692+
if err := json.NewDecoder(r.Body).Decode(&gotBody); err != nil {
693+
t.Fatalf("decode request body: %v", err)
694+
}
695+
696+
w.Header().Set("Content-Type", "application/json")
697+
_, _ = io.WriteString(w, `{
698+
"id":"msg_draft_123",
699+
"type":"email",
700+
"is_inbound":false,
701+
"draft_mode":"shared",
702+
"version":"draft-ver-456",
703+
"created_at":1710000000,
704+
"body":"updated body",
705+
"text":"updated body",
706+
"subject":"Updated draft"
707+
}`)
708+
}))
709+
defer srv.Close()
710+
711+
old := newClientFromAuth
712+
newClientFromAuth = func(_, _ string) (*api.Client, error) {
713+
return api.NewClientWithBaseURL(oauth2.StaticTokenSource(&oauth2.Token{AccessToken: "token"}), srv.URL), nil
714+
}
715+
t.Cleanup(func() { newClientFromAuth = old })
716+
717+
cmd := DraftUpdateCmd{
718+
ID: "msg_draft_123",
719+
Channel: "cha_v4x",
720+
DraftVersion: "draft-ver-123",
721+
Body: "updated body",
722+
}
723+
flags := &RootFlags{JSON: true, Account: "test@example.com"}
724+
725+
if err := cmd.Run(flags); err != nil {
726+
t.Fatalf("Run: %v", err)
727+
}
728+
729+
channelID, ok := gotBody["channel_id"].(string)
730+
if !ok || channelID != "cha_v4x" {
731+
t.Fatalf("expected channel_id in request body, got %#v", gotBody["channel_id"])
732+
}
733+
}
734+
686735
func TestDraftUpdateHumanOutputUsesStringVersion(t *testing.T) {
687736
t.Setenv("HOME", t.TempDir())
688737

0 commit comments

Comments
 (0)