//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 })),
]
}
/**
* Create User
* Create a user in the Workspace account. `primary_email` must be on one of the account's verified domains; the password needs 8 to 100 characters. Extra user fields (phones, recovery email, custom schemas…) go in `additional_fields`. The org-unit dropdown needs admin.directory.orgunit or admin.directory.orgunit.readonly on the connection.
*/
export async function main(
auth: RT.Gworkspace,
primary_email: string,
given_name: string,
family_name: string,
password: string,
change_password_at_next_login: boolean = true,
org_unit_path: DynSelect_org_unit_path | undefined,
additional_fields: { [key: string]: any } | undefined,
) {
const body: { [key: string]: any } = {
...additional_fields,
primaryEmail: primary_email,
name: { givenName: given_name, familyName: family_name },
password,
changePasswordAtNextLogin: change_password_at_next_login,
}
if (org_unit_path !== undefined && org_unit_path !== "") {
body.orgUnitPath = org_unit_path
}
const response = await fetch(
"https://admin.googleapis.com/admin/directory/v1/users",
{
method: "POST",
headers: {
Authorization: `Bearer ${auth.token}`,
"Content-Type": "application/json",
Accept: "application/json",
},
body: JSON.stringify(body),
},
)
if (!response.ok) {
throw new Error(`${response.status} ${await response.text()}`)
}
return await response.json()
}
Submitted by hugo989 1 hour ago