@nestdevx/tenant
Version:
Tenant module for multi-tenant NestJS applications.
68 lines • 3.66 kB
JavaScript
;
var __decorate = (this && this.__decorate) || function (decorators, target, key, desc) {
var c = arguments.length, r = c < 3 ? target : desc === null ? desc = Object.getOwnPropertyDescriptor(target, key) : desc, d;
if (typeof Reflect === "object" && typeof Reflect.decorate === "function") r = Reflect.decorate(decorators, target, key, desc);
else for (var i = decorators.length - 1; i >= 0; i--) if (d = decorators[i]) r = (c < 3 ? d(r) : c > 3 ? d(target, key, r) : d(target, key)) || r;
return c > 3 && r && Object.defineProperty(target, key, r), r;
};
var __metadata = (this && this.__metadata) || function (k, v) {
if (typeof Reflect === "object" && typeof Reflect.metadata === "function") return Reflect.metadata(k, v);
};
var __param = (this && this.__param) || function (paramIndex, decorator) {
return function (target, key) { decorator(target, key, paramIndex); }
};
var TenantService_1;
Object.defineProperty(exports, "__esModule", { value: true });
exports.TenantService = void 0;
const repository_1 = require("../../shared/src/repositories/repository");
const common_1 = require("@nestjs/common");
const tenant_entity_1 = require("./entities/tenant.entity");
const cqrs_1 = require("@nestjs/cqrs");
const mongoose_1 = require("mongoose");
const new_tenant_created_event_1 = require("../../shared/src/events/new-tenant-created-event");
const request_context_1 = require("../../shared/src/hooks/request-context");
const invalid_operation_1 = require("../../shared/src/exceptions/invalid-operation");
const config_1 = require("@nestjs/config");
let TenantService = TenantService_1 = class TenantService extends repository_1.Repository {
constructor(tenantModel, eventBus, cs) {
super(tenantModel, new common_1.Logger(TenantService_1.name));
this.tenantModel = tenantModel;
this.eventBus = eventBus;
this.cs = cs;
}
async getTenantById(tenantId) {
return await this.tenantModel.findById(tenantId).exec();
}
async createNewTenant(createNewTenantDto) {
const { name, host, dbUri, adminEmail, adminPassword, adminConfirmPassword, adminFirstName, adminLastName } = createNewTenantDto;
const defaultHost = this.cs.getOrThrow('DOMAIN_URI').replace("https://", "").replace("http://", "");
const tenant = await this.create({
name,
host: host || defaultHost,
dbUri: dbUri || 'shared',
});
this.logger.log(`Created new tenant: ${tenant.id}`);
(0, request_context_1.runWithContext)({ tenantId: tenant.id }, async () => {
await this.eventBus.publish(new new_tenant_created_event_1.NewTenantCreatedEvent(adminEmail, adminPassword, adminConfirmPassword, adminFirstName, adminLastName, tenant.id));
});
return tenant.id;
}
async findByHostName(hostName) {
this.logger.log(`Finding tenant by hostname: ${hostName}`);
const tenant = await this.tenantModel.findOne({ host: hostName });
if (!tenant?.id) {
this.logger.warn(`No tenant found for hostname: ${hostName}`);
throw new invalid_operation_1.InvalidOperationException(`No tenant found for hostname: ${hostName}`);
}
return tenant;
}
};
exports.TenantService = TenantService;
exports.TenantService = TenantService = TenantService_1 = __decorate([
(0, common_1.Injectable)(),
__param(0, (0, common_1.Inject)(tenant_entity_1.TENANT_MODEL_NAME)),
__metadata("design:paramtypes", [mongoose_1.Model,
cqrs_1.EventBus,
config_1.ConfigService])
], TenantService);
//# sourceMappingURL=tenant.service.js.map