Skip to content

Commit

Permalink
fix: (invoiceninja) change json structure for get all tickets by status
Browse files Browse the repository at this point in the history
  • Loading branch information
buttonsbond authored Mar 19, 2024
1 parent 4bba8bb commit cc1dfc7
Showing 1 changed file with 39 additions and 43 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -6,34 +6,33 @@ export const getAllTicketsByStatus = createAction({
auth: freshdeskAuth,
name: 'get_all_tickets_by_status',
displayName: 'Get All Tickets By Status',
description:
'Get All Tickets by selected status from Freshdesk.',
description: 'Get All Tickets by selected status from Freshdesk.',

props: {
status_filter: Property.StaticMultiSelectDropdown({
displayName: 'Choose Status(es)',
description: 'Select one or status values',
required: true,
options: {
options: [
{
label: 'Open',
value: 'status:2'
},
{
label: 'Pending',
value: 'status:3'
},
{
label: 'Resolved',
value: 'status:4'
},
{
label: 'Closed',
value: 'status:5'
}
]
}
displayName: 'Choose Status(es)',
description: 'Select one or status values',
required: true,
options: {
options: [
{
label: 'Open',
value: 'status:2'
},
{
label: 'Pending',
value: 'status:3'
},
{
label: 'Resolved',
value: 'status:4'
},
{
label: 'Closed',
value: 'status:5'
}
]
}
}),
},

Expand All @@ -48,14 +47,13 @@ export const getAllTicketsByStatus = createAction({
// Remove trailing slash from base_url
const baseUrl = context.auth.base_url.replace(/\/$/, '');
const queryParams = new URLSearchParams();
// Assuming context.propsValue.status_filter is an array of strings
const replacedArray = context.propsValue.status_filter.map(str => str.replace(/,/g, ' OR '));

// Join the array elements into a single string with ' OR ' between them
const replacedString = '"' + replacedArray.join(' OR ') + '"';
// Adjusted to accept number or string
const replacedArray = context.propsValue.status_filter.map(str => str.replace(/,/g, ' OR '));
const replacedString = '"' + replacedArray.join(' OR ') + '"';
queryParams.append('query', replacedString || '');

const url = `${baseUrl}/api/v2/search/tickets/?${queryParams.toString()}`;
// console.log("INVOICENINJA: (" + replacedString + ") ");

const httprequestdata = {
method: HttpMethod.GET,
Expand All @@ -64,9 +62,7 @@ const replacedString = '"' + replacedArray.join(' OR ') + '"';
};
const response = await httpClient.sendRequest(httprequestdata);

if (response.status == 200) { // something was returned


if (response.status == 200) {
// Define an interface for the ticket structure
interface Ticket {
id: number;
Expand All @@ -84,13 +80,15 @@ const replacedString = '"' + replacedArray.join(' OR ') + '"';

// response.body.results is an array of ticket objects
const ticketResults: Ticket[] = response.body.results;

// Sort the ticketResults array by requester_id
ticketResults.sort((a, b) => a.requester_id - b.requester_id);
// Initialize an empty object to store tickets
const tickets: { [key: string]: Ticket } = {};

// Iterate through each ticket result
ticketResults.forEach((ticketResult: Ticket, index: number) => {
// Initialize an empty array to store tickets
const tickets: Ticket[] = [];

// Iterate through each ticket result and push it to the tickets array
ticketResults.forEach((ticketResult: Ticket) => {
// Map status number to corresponding string
let statusString: string;
switch (ticketResult.status) {
Expand All @@ -110,8 +108,8 @@ const replacedString = '"' + replacedArray.join(' OR ') + '"';
statusString = 'Unknown';
}

// Extract only the selected properties and store them as an instance of ticket
tickets[`ticket ${index + 1}`] = {
// Push the ticket object with modified status string to the tickets array
tickets.push({
id: ticketResult.id,
requester_id: ticketResult.requester_id,
responder_id: ticketResult.responder_id,
Expand All @@ -123,12 +121,10 @@ const replacedString = '"' + replacedArray.join(' OR ') + '"';
description_text: ticketResult.description_text,
description: ticketResult.description,
// Add more properties if necessary
};
});
});

// Now you have an object with keys like 'ticket 1', 'ticket 2', etc., containing each ticket instance with the specified properties
//console.log(tickets);

// Return the tickets array
return tickets;
} else {
return response.status;
Expand Down

0 comments on commit cc1dfc7

Please sign in to comment.