@@ -59,23 +59,26 @@ export async function POST(request: NextRequest) {
5959
6060 const { action : customerAction } = body
6161
62- const rawIds = accountIds || emails
63- const parsedAccountIds = rawIds
64- ? typeof rawIds === 'string'
65- ? rawIds
66- . split ( ',' )
67- . map ( ( id : string ) => id . trim ( ) )
68- . filter ( ( id : string ) => id )
69- : Array . isArray ( rawIds )
70- ? rawIds
71- : [ ]
72- : [ ]
62+ const parseList = ( raw : unknown ) : string [ ] => {
63+ if ( ! raw ) return [ ]
64+ if ( typeof raw === 'string' ) {
65+ return raw
66+ . split ( ',' )
67+ . map ( ( id : string ) => id . trim ( ) )
68+ . filter ( ( id : string ) => id )
69+ }
70+ return Array . isArray ( raw ) ? raw : [ ]
71+ }
72+
73+ const parsedAccountIds = parseList ( accountIds )
74+ const parsedEmails = parseList ( emails )
7375
7476 const isRemoveOperation = customerAction === 'remove'
75- const isAddOperation = ! isRemoveOperation && parsedAccountIds . length > 0
77+ const isAddOperation =
78+ ! isRemoveOperation && ( parsedAccountIds . length > 0 || parsedEmails . length > 0 )
7679
7780 if ( isRemoveOperation ) {
78- if ( parsedAccountIds . length === 0 ) {
81+ if ( parsedAccountIds . length === 0 && parsedEmails . length === 0 ) {
7982 return NextResponse . json (
8083 { error : 'Account IDs or emails are required for removal' } ,
8184 { status : 400 }
@@ -84,12 +87,16 @@ export async function POST(request: NextRequest) {
8487
8588 const url = `${ baseUrl } /servicedesk/${ serviceDeskId } /customer`
8689
87- logger . info ( 'Removing customers from:' , url , { accountIds : parsedAccountIds } )
90+ const removeBody : Record < string , string [ ] > = { }
91+ if ( parsedAccountIds . length > 0 ) removeBody . accountIds = parsedAccountIds
92+ if ( parsedEmails . length > 0 ) removeBody . usernames = parsedEmails
93+
94+ logger . info ( 'Removing customers from:' , url , removeBody )
8895
8996 const response = await fetch ( url , {
9097 method : 'DELETE' ,
9198 headers : getJsmHeaders ( accessToken ) ,
92- body : JSON . stringify ( { accountIds : parsedAccountIds } ) ,
99+ body : JSON . stringify ( removeBody ) ,
93100 } )
94101
95102 if ( response . status === 204 || response . ok ) {
@@ -119,11 +126,14 @@ export async function POST(request: NextRequest) {
119126 if ( isAddOperation ) {
120127 const url = `${ baseUrl } /servicedesk/${ serviceDeskId } /customer`
121128
122- logger . info ( 'Adding customers to:' , url , { accountIds : parsedAccountIds } )
123-
124- const requestBody : Record < string , unknown > = {
129+ logger . info ( 'Adding customers to:' , url , {
125130 accountIds : parsedAccountIds ,
126- }
131+ usernames : parsedEmails ,
132+ } )
133+
134+ const requestBody : Record < string , unknown > = { }
135+ if ( parsedAccountIds . length > 0 ) requestBody . accountIds = parsedAccountIds
136+ if ( parsedEmails . length > 0 ) requestBody . usernames = parsedEmails
127137
128138 const response = await fetch ( url , {
129139 method : 'POST' ,
0 commit comments