import { AuthService } from "./auth.js";
const BASE_URL = "https://usermanagement-pp.monotype.com/api/v3";
export class MonotypeApiClient {
constructor(token = null) {
this.auth = new AuthService(token);
}
/**
* Invite user for customer (equivalent to create user for customer)
* @param {string} email - Email address of the user to invite
* @param {string} roleId - Role ID to assign to the user
* @param {string[]} teamIds - Array of team IDs to assign (optional)
* @param {number} status - Invitation status (default: 1)
* @returns {Promise<Object>} API response
*/
async inviteUserForCustomer(email, roleId, teamIds = [], status = 1) {
const customerId = this.auth.getCustomerId();
const url = `${BASE_URL}/customers/${customerId}/invitations`;
const payload = {
data: [
{
emails: [email],
status,
roleId,
teamIds: teamIds.length > 0 ? teamIds.join(",") : "",
},
],
};
const headers = this.auth.getAuthHeaders();
try {
const response = await fetch(url, {
method: "POST",
headers,
body: JSON.stringify(payload),
});
if (!response.ok) {
const errorText = await response.text();
throw new Error(
`API request failed: ${response.status} ${response.statusText}. ${errorText}`
);
}
const data = await response.json();
return {
success: true,
data,
};
} catch (error) {
return {
success: false,
error: error.message,
};
}
}
/**
* Get all teams for a specific customer
* @param {string} customerId - Customer ID (optional, uses auth service if not provided)
* @returns {Promise<Object>} API response with teams
*/
async getTeamsForCustomer(customerId = null) {
const customerIdToUse = customerId || this.auth.getCustomerId();
const url = `${BASE_URL}/customers/${customerIdToUse}/teams`;
const headers = {
...this.auth.getAuthHeaders(),
"X-HTTP-Method-Override": "GET",
};
// Query parameters for projection
const projection = "name,description,createdBy,customerId,profile.id,profilesCount,adminProfile.id";
const urlWithParams = `${url}?projection=${encodeURIComponent(projection)}`;
// Request body for filter (as per API example)
const body = {
projection: "idpUserId, activity.lastActivityTimestamp, role.id, team.id",
filter: {
"#and": {
"profile.id": {
in: [this.auth.getProfileId()],
},
},
},
};
try {
const response = await fetch(urlWithParams, {
method: "POST", // Using POST with X-HTTP-Method-Override header
headers,
body: JSON.stringify(body),
});
if (!response.ok) {
const errorText = await response.text();
throw new Error(
`API request failed: ${response.status} ${response.statusText}. ${errorText}`
);
}
const data = await response.json();
return {
success: true,
data,
};
} catch (error) {
return {
success: false,
error: error.message,
};
}
}
/**
* Get all roles for a specific customer
* @param {string} customerId - Customer ID (optional, uses auth service if not provided)
* @returns {Promise<Object>} API response with roles
*/
async getRolesForCustomer(customerId = null) {
const customerIdToUse = customerId || this.auth.getCustomerId();
const url = `${BASE_URL}/customers/${customerIdToUse}/roles`;
const headers = {
...this.auth.getAuthHeaders(),
"X-HTTP-Method-Override": "GET",
};
// Query parameters for projection
const projection = "id,displayName,typeId,description,permissions";
const urlWithParams = `${url}?projection=${encodeURIComponent(projection)}`;
try {
const response = await fetch(urlWithParams, {
method: "GET",
headers,
});
if (!response.ok) {
const errorText = await response.text();
throw new Error(
`API request failed: ${response.status} ${response.statusText}. ${errorText}`
);
}
const data = await response.json();
return {
success: true,
data,
};
} catch (error) {
return {
success: false,
error: error.message,
};
}
}
}