Created a new API to delete tenant
This commit is contained in:
parent
dca6414a59
commit
a70c3007f1
@ -20,7 +20,7 @@ namespace Marco.Pms.Services.Controllers
|
|||||||
private readonly ApplicationDbContext _context;
|
private readonly ApplicationDbContext _context;
|
||||||
private readonly UserManager<IdentityUser> _userManager;
|
private readonly UserManager<IdentityUser> _userManager;
|
||||||
private readonly ILoggingService _logger;
|
private readonly ILoggingService _logger;
|
||||||
public TenantController(ApplicationDbContext context,UserManager<IdentityUser> userManager, ILoggingService logger)
|
public TenantController(ApplicationDbContext context, UserManager<IdentityUser> userManager, ILoggingService logger)
|
||||||
{
|
{
|
||||||
_context = context;
|
_context = context;
|
||||||
_userManager = userManager;
|
_userManager = userManager;
|
||||||
@ -55,7 +55,6 @@ namespace Marco.Pms.Services.Controllers
|
|||||||
Description = settings.JobRoleDescription,
|
Description = settings.JobRoleDescription,
|
||||||
TenantId = TenantId
|
TenantId = TenantId
|
||||||
};
|
};
|
||||||
var existingJobRole = await _context.JobRoles.AsNoTracking().FirstOrDefaultAsync(r => r.Name == settings.JobRoleName);
|
|
||||||
|
|
||||||
ApplicationRole role = new ApplicationRole
|
ApplicationRole role = new ApplicationRole
|
||||||
{
|
{
|
||||||
@ -63,27 +62,10 @@ namespace Marco.Pms.Services.Controllers
|
|||||||
Description = settings.RoleDescription,
|
Description = settings.RoleDescription,
|
||||||
TenantId = TenantId
|
TenantId = TenantId
|
||||||
};
|
};
|
||||||
var existingRole = await _context.ApplicationRoles.AsNoTracking().FirstOrDefaultAsync(r => r.Role == settings.RoleName);
|
|
||||||
|
|
||||||
if (existingJobRole == null)
|
_context.JobRoles.Add(jobRole);
|
||||||
{
|
_context.ApplicationRoles.Add(role);
|
||||||
_context.JobRoles.Add(jobRole);
|
await _context.SaveChangesAsync();
|
||||||
await _context.SaveChangesAsync();
|
|
||||||
}
|
|
||||||
else
|
|
||||||
{
|
|
||||||
jobRole = existingJobRole;
|
|
||||||
}
|
|
||||||
|
|
||||||
if (existingRole == null)
|
|
||||||
{
|
|
||||||
_context.ApplicationRoles.Add(role);
|
|
||||||
await _context.SaveChangesAsync();
|
|
||||||
}
|
|
||||||
else
|
|
||||||
{
|
|
||||||
role = existingRole;
|
|
||||||
}
|
|
||||||
|
|
||||||
List<FeaturePermission> permissions = await _context.FeaturePermissions.AsNoTracking().ToListAsync();
|
List<FeaturePermission> permissions = await _context.FeaturePermissions.AsNoTracking().ToListAsync();
|
||||||
List<RolePermissionMappings> rolePermissionMappings = new List<RolePermissionMappings>();
|
List<RolePermissionMappings> rolePermissionMappings = new List<RolePermissionMappings>();
|
||||||
@ -111,7 +93,7 @@ namespace Marco.Pms.Services.Controllers
|
|||||||
var result = await _userManager.CreateAsync(user, createTenantDto.Password);
|
var result = await _userManager.CreateAsync(user, createTenantDto.Password);
|
||||||
if (result.Succeeded)
|
if (result.Succeeded)
|
||||||
{
|
{
|
||||||
Employee newEmployee = CreateTenantDtoToEmployee(createTenantDto, TenantId, user.Id,jobRole.Id);
|
Employee newEmployee = CreateTenantDtoToEmployee(createTenantDto, TenantId, user.Id, jobRole.Id);
|
||||||
_context.Employees.Add(newEmployee);
|
_context.Employees.Add(newEmployee);
|
||||||
await _context.SaveChangesAsync();
|
await _context.SaveChangesAsync();
|
||||||
|
|
||||||
@ -132,7 +114,7 @@ namespace Marco.Pms.Services.Controllers
|
|||||||
foreach (var error in result.Errors)
|
foreach (var error in result.Errors)
|
||||||
{
|
{
|
||||||
// Log error.Description
|
// Log error.Description
|
||||||
_logger.LogError("{Error}",error.Description);
|
_logger.LogError("{Error}", error.Description);
|
||||||
}
|
}
|
||||||
return BadRequest("Failed to create the root user.");
|
return BadRequest("Failed to create the root user.");
|
||||||
}
|
}
|
||||||
@ -140,6 +122,29 @@ namespace Marco.Pms.Services.Controllers
|
|||||||
return BadRequest("Falied to create Tenant");
|
return BadRequest("Falied to create Tenant");
|
||||||
|
|
||||||
}
|
}
|
||||||
|
[HttpDelete("suspend/{tenantId}")]
|
||||||
|
public async Task<IActionResult> SuspendTenant(int tenantId)
|
||||||
|
{
|
||||||
|
if (tenantId <= 0)
|
||||||
|
{
|
||||||
|
return BadRequest("Tenant Id is required and must be greater than zero.");
|
||||||
|
}
|
||||||
|
var tenant = await _context.Tenants.FirstOrDefaultAsync(t => t.Id == tenantId);
|
||||||
|
var user = await _context.ApplicationUsers.FirstOrDefaultAsync(u => u.TenantId == tenantId && u.IsRootUser == true);
|
||||||
|
var employee = await _context.Employees.FirstOrDefaultAsync(e => e.Id == tenantId && e.ApplicationUserId == user.Id);
|
||||||
|
if (tenant == null && user == null)
|
||||||
|
{
|
||||||
|
return NotFound("Tenant Not Found");
|
||||||
|
}
|
||||||
|
|
||||||
|
//tenant.IsActive = false; // Uncomment it after Adding isActive property in tenant
|
||||||
|
//_context.Tenants.Add(tenant);
|
||||||
|
|
||||||
|
employee.IsActive = false;
|
||||||
|
_context.Employees.Add(employee);
|
||||||
|
await _context.SaveChangesAsync();
|
||||||
|
return Ok("Tenant is Suspended");
|
||||||
|
}
|
||||||
private static Employee CreateTenantDtoToEmployee(CreateTenantDto model, int TenantId, string? ApplicationUserId,int jobRoleId)
|
private static Employee CreateTenantDtoToEmployee(CreateTenantDto model, int TenantId, string? ApplicationUserId,int jobRoleId)
|
||||||
{
|
{
|
||||||
return new Employee
|
return new Employee
|
||||||
|
Loading…
x
Reference in New Issue
Block a user