@c8y/client
Version:
Client application programming interface to access the Cumulocity IoT-Platform REST services.
364 lines • 12.2 kB
JavaScript
import { __awaiter } from "tslib";
import { Service } from '../core';
/**
* @description
* This service allows for managing tenants.
*/
export class TenantService extends Service {
constructor() {
super(...arguments);
this.baseUrl = 'tenant';
this.listUrl = 'tenants';
this.currentTenantUrl = 'currentTenant';
this.propertyName = 'tenants';
this.fetchOptions = {
method: 'PUT',
body: '{}',
headers: { 'content-type': 'application/json', accept: 'application/json' }
};
}
/**
* Get a representation of a tenant.
*
* @param {string|number|IIdentified} entityOrId Tenant's id or tenant object.
*
* @returns Returns promise object that is resolved with the IIdentified wrapped by IResult.
*
* **Example**
* ```typescript
*
* const tenantId: number = 1;
*
* (async () => {
* const {data, res} = await tenantService.detail(tenantId);
* })();
* ```
*
* Required role: ROLE_TENANT_MANAGEMENT_READ<br><br>
* User password is never returned in GET response. Authentication mechanism is provided by another interface.
*/
detail(entityOrId) {
const _super = Object.create(null, {
detail: { get: () => super.detail }
});
return __awaiter(this, void 0, void 0, function* () {
return _super.detail.call(this, entityOrId);
});
}
/**
* Creates a new tenant.
*
* @param {IIdentified} entity Tenant object.
*
* @returns {IResult<IIdentified>} Returns promise object that is resolved with the details of newly created tenant.
*
* **Example**
* ```typescript
*
* const tenantObject = {
* id: "sample_tenant",
* company: "sample_company",
* domain: "sample_domain.com",
* contactName: "Mr. Doe",
* ...
* };
*
* (async () => {
* const {data, res} = await tenantService.create(tenantObject);
* })();
* ```
*
* Required role: ROLE_TENANT_MANAGEMENT_ADMIN or ROLE_TENANT_MANAGEMENT_CREATE<br><br>
* Note that creating a tenant with adminName, adminPass and adminEmail, creates an admin user with these settings.
* For the tenant id SQL keywords (e.g., select, cross, where) are not allowed.
*/
create(entity) {
const _super = Object.create(null, {
create: { get: () => super.create }
});
return __awaiter(this, void 0, void 0, function* () {
return _super.create.call(this, entity);
});
}
/**
* Updates tenant data.
*
* @param {IIdentified} entity Tenant is partially updatable.
*
* @returns {IResult<IIdentified>} Returns promise object that is resolved with the saved tenant object.
*
* **Example**
* ```typescript
*
* const partialUpdateObject: IIdentified = {
* adminName : "newAdmin"
* ...
* }
*
* (async () => {
* const {data, res} = await tenantService.update(partialUpdateObject);
* })();
* ```
*
* Required role: ROLE_TENANT_MANAGEMENT_ADMIN or ROLE_TENANT_MANAGEMENT_UPDATE<br><br>
* Note that updating adminPass and adminEmail updates these settings in the admin user of the tenant.
* Updating adminName has no effect.
*/
update(entity) {
const _super = Object.create(null, {
update: { get: () => super.update }
});
return __awaiter(this, void 0, void 0, function* () {
return _super.update.call(this, entity);
});
}
/**
* Gets the list of tenants filtered by parameters.
*
* @param {object} filter Object containing filters for querying tenants.
*
* @returns Returns promise object that is resolved with the IIdentified wrapped by IResultList.
*
* **Example**
* ```typescript
*
* const filter: object = {
* severity: Severity.MAJOR,
* pageSize: 100,
* withTotalPages: true
* };
*
* (async () => {
* const {data, res, paging} = await tenantService.list(filter);
* })();
* ```
*
* Required role: ROLE_TENANT_MANAGEMENT_READ
*/
list() {
const _super = Object.create(null, {
list: { get: () => super.list }
});
return __awaiter(this, arguments, void 0, function* (filter = {}) {
return _super.list.call(this, filter);
});
}
/**
* Delete a representation of a tenant.
*
* @param {string|number|IIdentified} entityOrId Tenant's id or tenant object.
*
* @returns Returns promise object that is resolved with the IResult.
*
* **Example**
* ```typescript
*
* const tenantId: string = "uniqueTenantId";
*
* (async () => {
* const {data, res} = await tenantService.delete(tenantId);
* })();
* ```
*
* Required role: ROLE_TENANT_MANAGEMENT_ADMIN
*/
delete(entityOrId) {
const _super = Object.create(null, {
delete: { get: () => super.delete }
});
return __awaiter(this, void 0, void 0, function* () {
return _super.delete.call(this, entityOrId);
});
}
current() {
return __awaiter(this, arguments, void 0, function* (filter = {}) {
const headers = { 'content-type': 'application/json' };
const res = yield this.fetch(this.currentTenantUrl, { headers, params: filter });
const data = yield res.json();
return { res, data };
});
}
/**
* enable support user for current tenant.
*
* @returns Returns promise object that is resolved with the IResult.
*
* **Example**
* ```typescript
* (async () => {
* const {res} = await tenantService.enableSupportUser();
* })();
* ```
*/
enableSupportUser() {
return __awaiter(this, void 0, void 0, function* () {
const url = 'support-user/enable';
const res = yield this.fetch(url, this.fetchOptions);
return { res, data: null };
});
}
/**
* disable support user for current tenant.
*
* @returns Returns promise object that is resolved with the IResult.
*
* **Example**
* ```typescript
* (async () => {
* const {res} = await tenantService.disableSupportUser();
* })();
* ```
*/
disableSupportUser() {
return __awaiter(this, void 0, void 0, function* () {
const url = 'support-user/disable';
const res = yield this.fetch(url, this.fetchOptions);
return { res, data: null };
});
}
currentTenantType() {
return __awaiter(this, void 0, void 0, function* () {
const tenantData = yield this.current();
if (tenantData.data.customProperties &&
tenantData.data.customProperties.tenantType === 'TRIAL') {
return 'TRIAL';
}
return 'REGULAR';
});
}
/**
* Returns two factor-authentication settings for given tenant.
*
* @param tenant The tenant object.
*
* @returns Promise which resolves with the object with TFA settings.
*
* **Example**
* ```typescript
* (async () => {
* const currentTenant = (await tenantService.current()).data;
* const currentTenantTfaSettings = await tenantService.getTfaSettings(currentTenant);
*
* const subtenant = (await tenantService.detail('t12345')).data;
* const subtenantTfaSettings = await tenantService.getTfaSettings(subtenant);
* })();
* ```
*/
getTfaSettings(tenant) {
return __awaiter(this, void 0, void 0, function* () {
const entityOrId = this.getIdString(tenant);
const url = `tenants/${entityOrId}/tfa`;
const res = yield this.fetch(url);
const tfaSettings = yield res.json();
return tfaSettings;
});
}
/**
* Update TFA strategy for tenant.
*
* @param tenant The tenant object.
* @param tfaStrategy The TFA strategy.
*
* @returns Returns promise object that is resolved with the IResult.
*
* **Example**
* ```typescript
* (async () => {
* const currentTenant = (await tenantService.current()).data;
*
* const res = await tenantService.updateTfaStrategy(currentTenant, TfaStrategy.TOTP);
* })();
* ```
*/
updateTfaStrategy(tenant, tfaStrategy) {
return __awaiter(this, void 0, void 0, function* () {
const entityOrId = this.getIdString(tenant);
const url = `tenants/${entityOrId}/tfa`;
const method = 'PUT';
const body = JSON.stringify({ strategy: tfaStrategy });
const headers = { 'content-type': 'application/json', accept: 'application/json' };
const res = yield this.fetch(url, { method, body, headers });
return { res, data: null };
});
}
/**
* Subscribes a given application to a given tenant.
*
* @param tenant The tenant object.
* @param application The application object.
*
* @returns Returns promise object that is resolved with the IResult.
*
* **Example**
* ```typescript
* const newApp = {
* name: 'New application',
* type: 'HOSTED',
* key: 'new-app'
* };
*
* const application = (await applicationService.create(newApp)).data;
* const currentTenant = (await tenantService.current()).data;
*
* const {data, res} = await tenantService.subscribeApplication(currentTenant, application);
* })();
* ```
*/
subscribeApplication(tenant, application) {
return __awaiter(this, void 0, void 0, function* () {
const entityOrId = this.getIdString(tenant);
const applicationId = application.id;
const url = `tenants/${entityOrId}/applications`;
const method = 'POST';
const body = JSON.stringify({
application: {
id: applicationId,
self: application.self
}
});
const headers = { 'content-type': this.mimeType('applicationReference') };
const res = yield this.fetch(url, this.changeFetchOptions({ method, body, headers }, url));
return { res, data: null };
});
}
/**
* Unsubscribes a given application from a given tenant.
*
* @param tenant The tenant object.
* @param application The application object.
*
* @returns Returns promise object that is resolved with the IResult.
*
* **Example**
* ```typescript
* const newApp = {
* name: 'New application',
* type: 'HOSTED',
* key: 'new-app'
* };
*
* const application = (await applicationService.create(newApp)).data;
* const currentTenant = (await tenantService.current()).data;
* await tenantService.addApplication(currentTenant, application);
*
* await tenantService.unsubscribeApplication(currentTenant, application);
* })();
* ```
*/
unsubscribeApplication(tenant, application) {
return __awaiter(this, void 0, void 0, function* () {
const entityOrId = this.getIdString(tenant);
const url = `tenants/${entityOrId}/applications/${application.id}`;
const method = 'DELETE';
const res = yield this.fetch(url, this.changeFetchOptions({ method }, url));
return { res, data: null };
});
}
getIdString(tenant) {
return tenant.id || tenant.name;
}
onBeforeCreate(obj) {
return obj;
}
}
//# sourceMappingURL=TenantService.js.map