Edits history of script submission #24140 for ' List devices (ericsson_ep5g)'

  • bunnative
    One script reply has been approved by the moderators
    Ap­pro­ved
    //native
    
    /**
     * List devices
     * List the site's devices (IMSIs) one page at a time, with their status (Online, Disconnected, SIM not written, Disabled). Returns the page and the total count of matching IMSIs; fetch the next page with page + 1.
     */
    export async function main(
      auth: RT.EricssonEp5g,
      page: number | undefined,
      perpage: number | undefined,
      sorton:
        | "DeviceName"
        | "SegmentName"
        | "Status"
        | "Imsi"
        | "NumberOfSegments"
        | undefined,
      order: "ASC" | "DESC" | undefined,
      search: string | undefined,
      watchdog: "true" | "false" | undefined,
      filters:
        | (
            | "Gateway"
            | "Watchdog"
            | "Online"
            | "Disconnected"
            | "Disabled"
            | "SIM not written"
            | "eSIM not installed"
            | "eSIM"
            | "VisitingSIM"
            | "LocalSIM"
            | "IsLockedToDevice"
            | "IsReversedLockedToDevice"
            | "NonUniqueLockedImei"
          )[]
        | undefined,
      segment_filter: string[] | undefined
    ) {
      const base = auth.base_url.replace(/\/+$/, "")
      const url = new URL(
        `${base}/v2/organization/${encodeURIComponent(auth.organization)}/site/${encodeURIComponent(auth.site)}/nc/imsi`
      )
      const params: { [key: string]: string | number | undefined } = {
        page: page ?? undefined,
        perpage: perpage ?? undefined,
        sorton: sorton || undefined,
        order: order || undefined,
        search: search || undefined,
        watchdog: watchdog || undefined,
        filters: filters?.length ? filters.join(",") : undefined,
        segmentFilter: segment_filter?.length
          ? segment_filter.join(",")
          : undefined,
      }
      for (const [k, v] of Object.entries(params)) {
        if (v != null && v !== "") {
          url.searchParams.append(k, String(v))
        }
      }
    
      const response = await fetch(url, {
        headers: {
          "X-Api-Key": auth.api_key,
          ...(auth.user_role ? { "X-User-Role": auth.user_role } : {}),
          Accept: "application/json",
        },
      })
    
      if (!response.ok) {
        throw new Error(`${response.status} ${await response.text()}`)
      }
    
      const totalCount = response.headers.get("Total-Count")
      return {
        items: await response.json(),
        total_count: totalCount != null ? Number(totalCount) : null,
        page: page ?? null,
        perpage: perpage ?? null,
      }
    }
    

    Submitted by hugo989 6 hours ago