@@ -4,6 +4,7 @@ import { basename } from "node:path";
44import FormData from "form-data" ;
55import { z } from "zod" ;
66import { config } from "../../config/env" ;
7+ import { formatErrorMessage } from "../../utils/error-utils" ;
78import { MicrosoftAuthService } from "../auth/microsoft-auth" ;
89
910/**
@@ -80,20 +81,24 @@ export const WorkItemDtoSchema = z.object({
8081} ) ;
8182export type WorkItemDto = z . infer < typeof WorkItemDtoSchema > ;
8283
84+ const PostTaskRecordResponseSchema = z . object ( {
85+ workItemId : z . string ( ) . uuid ( ) ,
86+ } ) ;
87+
8388/**
8489 * Sends work item details to the portal API.
8590 * Requires the user to be authenticated with Microsoft.
8691 */
8792export async function SendWorkItemDetailsToPortal (
8893 payload : WorkItemDto ,
89- ) : Promise < { success : true } | { success : false ; error : string } > {
94+ ) : Promise < { success : true ; workItemId : string } | { success : false ; error : string } > {
9095 const ms = MicrosoftAuthService . getInstance ( ) ;
9196 const result = await ms . getToken ( ) ;
9297
9398 const body = JSON . stringify ( payload ) ;
9499
95100 try {
96- await makePortalRequest (
101+ const responseData = await makePortalRequest (
97102 "/desktopapp/post-task-record" ,
98103 {
99104 headers : {
@@ -104,6 +109,34 @@ export async function SendWorkItemDetailsToPortal(
104109 result . accessToken ,
105110 ) ;
106111
112+ const parsed = JSON . parse ( responseData ) ;
113+ const validated = PostTaskRecordResponseSchema . parse ( parsed ) ;
114+ return { success : true , workItemId : validated . workItemId } as const ;
115+ } catch ( error ) {
116+ const message = formatErrorMessage ( error ) ;
117+ return { success : false , error : message } as const ;
118+ }
119+ }
120+
121+ export async function CancelWorkItemInPortal (
122+ workItemId : string ,
123+ ) : Promise < { success : true } | { success : false ; error : string } > {
124+ const ms = MicrosoftAuthService . getInstance ( ) ;
125+ if ( ! ( await ms . isAuthenticated ( ) ) ) {
126+ return { success : false , error : "User is not authenticated with Microsoft" } ;
127+ }
128+ const result = await ms . getToken ( ) ;
129+
130+ try {
131+ await makePortalRequest (
132+ `/desktopapp/work-items/${ workItemId } :cancel` ,
133+ {
134+ headers : {
135+ "Content-Type" : "application/json" ,
136+ } ,
137+ } ,
138+ result . accessToken ,
139+ ) ;
107140 return { success : true } as const ;
108141 } catch ( error ) {
109142 const message = error instanceof Error ? error . message : String ( error ) ;
@@ -169,7 +202,7 @@ export async function UploadScreenshotToPortal(
169202
170203 return { success : true , url : validatedResponse . url } ;
171204 } catch ( error ) {
172- const message = error instanceof Error ? error . message : String ( error ) ;
205+ const message = formatErrorMessage ( error ) ;
173206 return { success : false , error : message } ;
174207 }
175208}
0 commit comments