Skip to content

Commit 45d6a3e

Browse files
committed
fix outlook drafting
1 parent 7e9c01f commit 45d6a3e

File tree

2 files changed

+32
-21
lines changed

2 files changed

+32
-21
lines changed

apps/web/__tests__/outlook-operations.test.ts

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -429,8 +429,14 @@ describe.skipIf(!TEST_OUTLOOK_EMAIL)("Outlook Webhook Payload", () => {
429429
const draft = await provider.getDraft(draftAction.draftId);
430430

431431
expect(draft).toBeDefined();
432+
433+
// Verify draft is actually a reply, not a fresh draft
434+
expect(draft?.threadId).toBeTruthy();
435+
expect(draft?.threadId).not.toBe("");
436+
432437
console.log(" ✅ Draft created successfully");
433438
console.log(` Draft ID: ${draftAction.draftId}`);
439+
console.log(` Thread ID: ${draft?.threadId}`);
434440
console.log(` Subject: ${draft?.subject || "(no subject)"}`);
435441
console.log(" Content:");
436442
console.log(

apps/web/utils/outlook/mail.ts

Lines changed: 26 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -186,29 +186,34 @@ export async function draftEmail(
186186
emailAddress: { address: addr },
187187
}));
188188

189-
const draft = {
190-
subject: args.subject || originalEmail.headers.subject,
191-
body: {
192-
contentType: "html",
193-
content: html,
194-
},
195-
toRecipients: [
196-
{
197-
emailAddress: {
198-
address: recipients.to,
199-
},
200-
},
201-
],
202-
...(ccRecipients.length > 0 ? { ccRecipients } : {}),
203-
conversationId: originalEmail.threadId,
204-
isDraft: true,
205-
};
189+
// Use createReply endpoint to create a proper reply draft
190+
// This ensures the draft is linked to the original message as a reply
191+
const replyDraft: Message = await client
192+
.getClient()
193+
.api(`/me/messages/${originalEmail.id}/createReply`)
194+
.post({});
206195

207-
const result: Message = await client
196+
// Update the draft with our content
197+
const updatedDraft: Message = await client
208198
.getClient()
209-
.api("/me/messages")
210-
.post(draft);
211-
return result;
199+
.api(`/me/messages/${replyDraft.id}`)
200+
.patch({
201+
subject: args.subject || originalEmail.headers.subject,
202+
body: {
203+
contentType: "html",
204+
content: html,
205+
},
206+
toRecipients: [
207+
{
208+
emailAddress: {
209+
address: recipients.to,
210+
},
211+
},
212+
],
213+
...(ccRecipients.length > 0 ? { ccRecipients } : {}),
214+
});
215+
216+
return updatedDraft;
212217
}
213218

214219
function convertTextToHtmlParagraphs(text?: string | null): string {

0 commit comments

Comments
 (0)