Edits history of script submission #23062 for ' Move User to Org Unit (gworkspace)'

  • bunnative
    One script reply has been approved by the moderators
    Ap­pro­ved
    //native
    
    export type DynSelect_org_unit_path = string
    
    // Dropdown of every org unit in the account, plus the root "/".
    export async function org_unit_path(auth: RT.Gworkspace) {
      const response = await fetch(
        "https://admin.googleapis.com/admin/directory/v1/customer/my_customer/orgunits?type=all",
        {
          headers: {
            Authorization: `Bearer ${auth.token}`,
            Accept: "application/json",
          },
        },
      )
      if (!response.ok) {
        throw new Error(`${response.status} ${await response.text()}`)
      }
      const { organizationUnits = [] } = (await response.json()) as {
        organizationUnits?: { orgUnitPath: string }[]
      }
      return [
        { value: "/", label: "/ (root)" },
        ...organizationUnits
          .map((o) => o.orgUnitPath)
          .sort()
          .map((p) => ({ value: p, label: p })),
      ]
    }
    
    /**
     * Move User to Org Unit
     * Move a user to another organizational unit, picked from a dropdown of the account's org units. Listing org units needs admin.directory.orgunit or admin.directory.orgunit.readonly on the connection.
     */
    export async function main(
      auth: RT.Gworkspace,
      user_key: string,
      org_unit_path: DynSelect_org_unit_path,
    ) {
      const response = await fetch(
        `https://admin.googleapis.com/admin/directory/v1/users/${encodeURIComponent(user_key)}`,
        {
          method: "PATCH",
          headers: {
            Authorization: `Bearer ${auth.token}`,
            "Content-Type": "application/json",
            Accept: "application/json",
          },
          body: JSON.stringify({ orgUnitPath: org_unit_path }),
        },
      )
    
      if (!response.ok) {
        throw new Error(`${response.status} ${await response.text()}`)
      }
    
      return await response.json()
    }
    

    Submitted by hugo989 1 hour ago