UNPKG

@c8y/client

Version:

Client application programming interface to access the Cumulocity IoT-Platform REST services.

327 lines • 10.6 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.TenantService = void 0; const index_js_1 = require("../core/index.js"); /** * @description * This service allows for managing tenants. */ class TenantService extends index_js_1.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. */ async detail(entityOrId) { return super.detail(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. */ async create(entity) { return super.create(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. */ async update(entity) { return super.update(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 */ async list(filter = {}) { return super.list(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 */ async delete(entityOrId) { return super.delete(entityOrId); } async current(filter = {}) { const headers = { 'content-type': 'application/json' }; const res = await this.fetch(this.currentTenantUrl, { headers, params: filter }); const data = await 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(); * })(); * ``` */ async enableSupportUser() { const url = 'support-user/enable'; const res = await 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(); * })(); * ``` */ async disableSupportUser() { const url = 'support-user/disable'; const res = await this.fetch(url, this.fetchOptions); return { res, data: null }; } async currentTenantType(tenant) { if (!tenant) { tenant = (await this.current()).data; } if (tenant.customProperties && tenant.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); * })(); * ``` */ async getTfaSettings(tenant) { const entityOrId = this.getIdString(tenant); const url = `tenants/${entityOrId}/tfa`; const res = await this.fetch(url); const tfaSettings = await 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); * })(); * ``` */ async updateTfaStrategy(tenant, tfaStrategy) { 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 = await 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); * })(); * ``` */ async subscribeApplication(tenant, application) { 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 = await 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); * })(); * ``` */ async unsubscribeApplication(tenant, application) { const entityOrId = this.getIdString(tenant); const url = `tenants/${entityOrId}/applications/${application.id}`; const method = 'DELETE'; const res = await this.fetch(url, this.changeFetchOptions({ method }, url)); return { res, data: null }; } getIdString(tenant) { return tenant.id || tenant.name; } onBeforeCreate(obj) { return obj; } } exports.TenantService = TenantService; //# sourceMappingURL=TenantService.js.map