saostad / freshdesk-client

1 stars 1 forks source link

CreateTicket - Cannot read properties of undefined (reading 'id') #1

Open legrandjeremy opened 1 year ago

legrandjeremy commented 1 year ago

Hi,

I just discovered your Freshdesk library and thanks for this ! I have an error with the following code

import { TicketPriority, TicketStatus, createTicket } from 'freshdesk-client'
try {
    const token = "XXXX";
    const baseUri = "https://XXXX.freshdesk.com";
    const ticketFreshdesk = await createTicket({
      baseUri,
      token,
      ticket: {
        description: "Test ticket description",
        subject: "Test ticket subject",
        type: null,
        priority: TicketPriority.Medium,
        status: TicketStatus.Open,
        source: 106,
        email: "test@test.com",
        group_id: 36000012486
      }
    });

    console.log(ticketFreshdesk.id);
  } catch (err) {
    console.log(err)
  }

The ticket is created inside Freshdesk but I received the error "TypeError: Cannot read properties of undefined (reading 'id')"

Any idea ?

Thanks for your help

legrandjeremy commented 1 year ago

@saostad : I forked the repo and found the issue.

The current code :

const data = await postData<
    z.infer<typeof NewTicketInputSchema>,
    { ticket: z.infer<typeof TicketSchema> }
  >({
    uri,
    token,
    data: ticket,
  });

But the reponse is just a ticketSchema not ticket: ticketSchema

So I tried this, worked perfectly, I have no write access to create a pull request to fix createTicket.ts:

import { writeLog } from "fast-node-logger";
import { TicketSchema, NewTicketInputSchema } from "../typings/ticket";
import { z } from "zod";
import { postData } from "../helpers/postData";
import { BaseCreateInput } from "../typings/general";

/** 
Source Type Value
Email   1
Portal  2
Phone   3
Chat    4
Feedback widget 5
Yammer  6
AWS Cloudwatch  7
Pagerduty   8
Walkup  9
Slack   10
*/
export enum TicketSourceType {
  Email = 1,
  Portal = 2,
  Phone = 3,
  Chat = 4,
  FeedbackWidget = 5,
  Yammer = 6,
  AWSCloudwatch = 7,
  Pagerduty = 8,
  Walkup = 9,
  Slack = 10,
}

/**
 * Status   Value
Open    2
Pending 3
Resolved    4
Closed  5
 */
export enum TicketStatus {
  Open = 2,
  Pending = 3,
  Resolved = 4,
  Closed = 5,
}

/**
 * Priorities   Value
Low 1
Medium  2
High    3
Urgent  4
 */
export enum TicketPriority {
  Low = 1,
  Medium = 2,
  High = 3,
  Urgent = 4,
}

type CreateTicket = BaseCreateInput & {
  ticket: z.infer<typeof NewTicketInputSchema>;
};

/** Create a Ticket
This API helps you to create a new ticket in your service desk.
@ref https://api.freshservice.com/v2/#create_ticket
*/
export async function createTicket({ baseUri, token, ticket }: CreateTicket) {
  writeLog(`createTicket()`, { level: "debug" });

  const uri = `${baseUri}/api/v2/tickets`;

  const data = await postData<
    z.infer<typeof NewTicketInputSchema>,
    z.infer<typeof TicketSchema>
  >({
    uri,
    token,
    data: ticket,
  });

  writeLog(`ticket created with id: ${data.id}`, {
    stdout: true,
    level: "info",
  });

  return data;
}