@twin.org/api-tenant-processor
Version:
API Tenant Processor for converting and api key to a tenant id.
98 lines • 3.22 kB
JavaScript
// Copyright 2024 IOTA Stiftung.
// SPDX-License-Identifier: Apache-2.0.
import { Guards } from "@twin.org/core";
import { EntityStorageConnectorFactory } from "@twin.org/entity-storage-models";
import { Tenant } from "./entities/tenant.js";
/**
* Service for performing email messaging operations to a connector.
*/
export class TenantAdminService {
/**
* Runtime name for the class.
*/
static CLASS_NAME = "TenantAdminService";
/**
* Entity storage connector used by the service.
* @internal
*/
_entityStorageConnector;
/**
* Create a new instance of TenantAdminService.
* @param options The options for the connector.
*/
constructor(options) {
this._entityStorageConnector = EntityStorageConnectorFactory.get(options?.tenantEntityStorageType ?? "tenant");
}
/**
* Returns the class name of the component.
* @returns The class name of the component.
*/
className() {
return TenantAdminService.CLASS_NAME;
}
/**
* Get a tenant by its id.
* @param tenantId The id of the tenant.
* @returns The tenant or undefined if not found.
*/
async get(tenantId) {
Guards.stringValue(TenantAdminService.CLASS_NAME, "tenantId", tenantId);
let tenant;
try {
tenant = await this._entityStorageConnector.get(tenantId);
}
catch { }
return tenant;
}
/**
* Get a tenant by its api key.
* @param apiKey The api key of the tenant.
* @returns The tenant or undefined if not found.
*/
async getByApiKey(apiKey) {
Guards.stringValue(TenantAdminService.CLASS_NAME, "apiKey", apiKey);
let tenant;
try {
tenant = await this._entityStorageConnector.get(apiKey, "apiKey");
}
catch { }
return tenant;
}
/**
* Set a tenant.
* @param tenant The tenant to store.
* @returns Nothing.
*/
async set(tenant) {
Guards.objectValue(TenantAdminService.CLASS_NAME, "tenant", tenant);
const tenantEntity = new Tenant();
tenantEntity.id = tenant.id;
tenantEntity.apiKey = tenant.apiKey;
tenantEntity.dateCreated = new Date(Date.now()).toISOString();
tenantEntity.label = tenant.label;
await this._entityStorageConnector.set(tenantEntity);
}
/**
* Remove a tenant by its id.
* @param tenantId The id of the tenant.
* @returns Nothing.
*/
async remove(tenantId) {
Guards.stringValue(TenantAdminService.CLASS_NAME, "tenantId", tenantId);
return this._entityStorageConnector.remove(tenantId);
}
/**
* Query tenants with pagination.
* @param cursor The cursor to start from.
* @param limit The maximum number of tenants to return.
* @returns The tenants and the next cursor if more tenants are available.
*/
async query(cursor, limit) {
const result = await this._entityStorageConnector.query(undefined, undefined, undefined, cursor, limit);
return {
tenants: result.entities,
cursor: result.cursor
};
}
}
//# sourceMappingURL=tenantAdminService.js.map