Skip to content

Commit

Permalink
Merge pull request #19 from vishnuvinay89/all-saas-0.2-dev
Browse files Browse the repository at this point in the history
TaskId #234213 - Map user with tenant admin role on creation of tenant
  • Loading branch information
sudeeppr1998 authored Jan 30, 2025
2 parents 57a6555 + 7dfcf2e commit 5f8ac45
Showing 1 changed file with 21 additions and 2 deletions.
23 changes: 21 additions & 2 deletions src/tenant/tenant.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -147,6 +147,9 @@ export class TenantService {

public async createTenants(request, tenantCreateDto, response) {
let apiId = APIID.TENANT_CREATE;
const decoded = jwt_decode(request.headers["authorization"]);
const userId = decoded["sub"];
tenantCreateDto.createdBy = userId;
// tenantCreateDto.status=tenantCreateDto.status || 'active'
try {
let checkExitTenants = await this.tenantRepository.find({
Expand All @@ -166,8 +169,23 @@ export class TenantService {
}
let result = await this.tenantRepository.save(tenantCreateDto);
if(result) {
await this.createRolesAndAssignPrivileges(result.tenantId)

let rolesDataofTenant = await this.createRolesAndAssignPrivileges(
result.tenantId
);
const tenantAdminRole = rolesDataofTenant.roles.find(
(role) => role.code === "tenant_admin"
);
let tenantRoleMappingData = {
userId: userId,
tenantRoleMapping: {
tenantId: result.tenantId,
roleId: tenantAdminRole.roleId, //get role id of tenant_admin from roles list
},
};
await this.userService.assignUserToTenant(
tenantRoleMappingData,
request
);
}
return APIResponse.success(
response,
Expand Down Expand Up @@ -339,6 +357,7 @@ export class TenantService {
const createPrivilegeRoleDto = new CreatePrivilegeRoleDto(privilegeMappingRequest)
// Call to map privileges to the role
await this.rolePrivilegeService.createPrivilegeRole(request as any, createPrivilegeRoleDto, response as any);
return rolesResult;
} else {
console.warn(`No privileges found for role "${roleCode}".`);
}
Expand Down

0 comments on commit 5f8ac45

Please sign in to comment.