Added the create recurring payment API
This commit is contained in:
parent
3486534c0b
commit
2a53e1c241
@ -2340,93 +2340,118 @@ namespace Marco.Pms.Services.Service
|
|||||||
|
|
||||||
public async Task<ApiResponse<object>> CreateRecurringPaymentAsync(RecurringTemplateDto model, Employee loggedInEmployee, Guid tenantId)
|
public async Task<ApiResponse<object>> CreateRecurringPaymentAsync(RecurringTemplateDto model, Employee loggedInEmployee, Guid tenantId)
|
||||||
{
|
{
|
||||||
using var scope = _serviceScopeFactory.CreateScope();
|
_logger.LogInfo("Start CreateRecurringPaymentAsync called by EmployeeId: {EmployeeId} for TenantId: {TenantId}", loggedInEmployee.Id, tenantId);
|
||||||
var permissionService = scope.ServiceProvider.GetRequiredService<PermissionServices>();
|
|
||||||
var hasPermission = await permissionService.HasPermission(PermissionsMaster.ManageRecurring, loggedInEmployee.Id);
|
|
||||||
|
|
||||||
if (!hasPermission)
|
try
|
||||||
{
|
{
|
||||||
_logger.LogWarning("Employee {EmployeeId} attempted to create recurring template", loggedInEmployee.Id);
|
// Check if user has permission to create recurring payment templates
|
||||||
return ApiResponse<object>.ErrorResponse("You do not have access to create recurring template", "You do not have access to create recurring template", 400);
|
using var scope = _serviceScopeFactory.CreateScope();
|
||||||
|
var permissionService = scope.ServiceProvider.GetRequiredService<PermissionServices>();
|
||||||
|
var hasPermission = await permissionService.HasPermission(PermissionsMaster.ManageRecurring, loggedInEmployee.Id);
|
||||||
|
|
||||||
|
if (!hasPermission)
|
||||||
|
{
|
||||||
|
_logger.LogWarning("Access denied: Employee {EmployeeId} attempted to create recurring payment template without required permission.", loggedInEmployee.Id);
|
||||||
|
return ApiResponse<object>.ErrorResponse("You do not have access to create recurring template.", "Permission denied.", 403);
|
||||||
|
}
|
||||||
|
|
||||||
|
// Concurrently fetch related entities required for validation and response mapping
|
||||||
|
var expenseCategoryTask = Task.Run(async () =>
|
||||||
|
{
|
||||||
|
await using var context = await _dbContextFactory.CreateDbContextAsync();
|
||||||
|
return await context.ExpenseCategoryMasters.FirstOrDefaultAsync(et => et.Id == model.ExpenseCategoryId && et.IsActive);
|
||||||
|
});
|
||||||
|
|
||||||
|
var recurringStatusTask = Task.Run(async () =>
|
||||||
|
{
|
||||||
|
await using var context = await _dbContextFactory.CreateDbContextAsync();
|
||||||
|
return await context.RecurringPaymentStatus.FirstOrDefaultAsync(rs => rs.Id == model.StatusId);
|
||||||
|
});
|
||||||
|
|
||||||
|
var currencyTask = Task.Run(async () =>
|
||||||
|
{
|
||||||
|
await using var context = await _dbContextFactory.CreateDbContextAsync();
|
||||||
|
return await context.CurrencyMaster.FirstOrDefaultAsync(c => c.Id == model.CurrencyId);
|
||||||
|
});
|
||||||
|
|
||||||
|
var projectTask = Task.Run(async () =>
|
||||||
|
{
|
||||||
|
await using var context = await _dbContextFactory.CreateDbContextAsync();
|
||||||
|
return model.ProjectId.HasValue ? await context.Projects.FirstOrDefaultAsync(p => p.Id == model.ProjectId.Value) : null;
|
||||||
|
});
|
||||||
|
|
||||||
|
await Task.WhenAll(expenseCategoryTask, recurringStatusTask, currencyTask, projectTask);
|
||||||
|
|
||||||
|
var expenseCategory = await expenseCategoryTask;
|
||||||
|
if (expenseCategory == null)
|
||||||
|
{
|
||||||
|
_logger.LogWarning("Expense Category not found with Id: {ExpenseCategoryId}", model.ExpenseCategoryId);
|
||||||
|
return ApiResponse<object>.ErrorResponse("Expense Category not found.", "Expense Category not found.", 404);
|
||||||
|
}
|
||||||
|
|
||||||
|
var recurringStatus = await recurringStatusTask;
|
||||||
|
if (recurringStatus == null)
|
||||||
|
{
|
||||||
|
_logger.LogWarning("Recurring Payment Status not found with Id: {StatusId}", model.StatusId);
|
||||||
|
return ApiResponse<object>.ErrorResponse("Recurring Payment Status not found.", "Recurring Payment Status not found.", 404);
|
||||||
|
}
|
||||||
|
|
||||||
|
var currency = await currencyTask;
|
||||||
|
if (currency == null)
|
||||||
|
{
|
||||||
|
_logger.LogWarning("Currency not found with Id: {CurrencyId}", model.CurrencyId);
|
||||||
|
return ApiResponse<object>.ErrorResponse("Currency not found.", "Currency not found.", 404);
|
||||||
|
}
|
||||||
|
|
||||||
|
var project = await projectTask; // Optional, can be null
|
||||||
|
|
||||||
|
// Generate unique UID prefix and postfix per current month/year
|
||||||
|
string uIDPrefix = $"RP/{DateTime.Now:MMyy}";
|
||||||
|
|
||||||
|
var lastRecurringPayment = await _context.RecurringPayments
|
||||||
|
.Where(e => e.UIDPrefix == uIDPrefix)
|
||||||
|
.OrderByDescending(e => e.UIDPostfix)
|
||||||
|
.FirstOrDefaultAsync();
|
||||||
|
|
||||||
|
int uIDPostfix = lastRecurringPayment == null ? 1 : lastRecurringPayment.UIDPostfix + 1;
|
||||||
|
|
||||||
|
// Map input DTO to entity and set additional fields
|
||||||
|
var recurringPayment = _mapper.Map<RecurringPayment>(model);
|
||||||
|
recurringPayment.UIDPrefix = uIDPrefix;
|
||||||
|
recurringPayment.UIDPostfix = uIDPostfix;
|
||||||
|
recurringPayment.IsActive = true;
|
||||||
|
recurringPayment.CreatedAt = DateTime.UtcNow;
|
||||||
|
recurringPayment.CreatedById = loggedInEmployee.Id;
|
||||||
|
recurringPayment.TenantId = tenantId;
|
||||||
|
|
||||||
|
_context.RecurringPayments.Add(recurringPayment);
|
||||||
|
await _context.SaveChangesAsync();
|
||||||
|
|
||||||
|
// Prepare response view model with enriched data
|
||||||
|
var response = _mapper.Map<RecurringPaymentVM>(recurringPayment);
|
||||||
|
response.RecurringPaymentUId = $"{recurringPayment.UIDPrefix}/{recurringPayment.UIDPostfix:D5}";
|
||||||
|
response.CreatedBy = _mapper.Map<BasicEmployeeVM>(loggedInEmployee);
|
||||||
|
response.ExpenseCategory = _mapper.Map<ExpensesCategoryMasterVM>(expenseCategory);
|
||||||
|
response.Status = recurringStatus;
|
||||||
|
response.Currency = currency;
|
||||||
|
response.Project = _mapper.Map<BasicProjectVM>(project);
|
||||||
|
|
||||||
|
_logger.LogInfo("Recurring Payment Template created successfully with UID: {RecurringPaymentUId} by EmployeeId: {EmployeeId}", response.RecurringPaymentUId, loggedInEmployee.Id);
|
||||||
|
|
||||||
|
return ApiResponse<object>.SuccessResponse(response, "Recurring Payment Template created successfully.", 201);
|
||||||
}
|
}
|
||||||
|
catch (Exception ex)
|
||||||
var expenseCategoryTask = Task.Run(async () =>
|
|
||||||
{
|
{
|
||||||
await using var context = await _dbContextFactory.CreateDbContextAsync();
|
_logger.LogError(ex, "Error in CreateRecurringPaymentAsync called by EmployeeId: {EmployeeId}: {Message}", loggedInEmployee.Id, ex.Message);
|
||||||
return await context.ExpenseCategoryMasters.FirstOrDefaultAsync(et => et.Id == model.ExpenseCategoryId && et.IsActive);
|
return ApiResponse<object>.ErrorResponse("An error occurred while creating the recurring payment template.", ex.Message, 500);
|
||||||
});
|
|
||||||
var recurringStatusTask = Task.Run(async () =>
|
|
||||||
{
|
|
||||||
await using var context = await _dbContextFactory.CreateDbContextAsync();
|
|
||||||
return await context.RecurringPaymentStatus.FirstOrDefaultAsync(et => et.Id == model.StatusId);
|
|
||||||
});
|
|
||||||
var currencyTask = Task.Run(async () =>
|
|
||||||
{
|
|
||||||
await using var context = await _dbContextFactory.CreateDbContextAsync();
|
|
||||||
return await context.CurrencyMaster.FirstOrDefaultAsync(c => c.Id == model.CurrencyId);
|
|
||||||
});
|
|
||||||
var projectTask = Task.Run(async () =>
|
|
||||||
{
|
|
||||||
await using var context = await _dbContextFactory.CreateDbContextAsync();
|
|
||||||
return await context.Projects.FirstOrDefaultAsync(P => model.ProjectId.HasValue && P.Id == model.ProjectId.Value);
|
|
||||||
});
|
|
||||||
|
|
||||||
await Task.WhenAll(expenseCategoryTask, currencyTask, projectTask, recurringStatusTask);
|
|
||||||
|
|
||||||
var expenseCategory = await expenseCategoryTask;
|
|
||||||
if (expenseCategory == null)
|
|
||||||
{
|
|
||||||
_logger.LogWarning("Expense Category not found with Id: {ExpenseCategoryId}", model.ExpenseCategoryId);
|
|
||||||
return ApiResponse<object>.ErrorResponse("Expense Category not found.", "Expense Category not found.", 404);
|
|
||||||
}
|
}
|
||||||
|
finally
|
||||||
var recurringStatus = await recurringStatusTask;
|
|
||||||
if (expenseCategory == null)
|
|
||||||
{
|
{
|
||||||
_logger.LogWarning("Recurring Payment Status not found with Id: {StatusId}", model.StatusId);
|
_logger.LogInfo("End CreateRecurringPaymentAsync called by EmployeeId: {EmployeeId}", loggedInEmployee.Id);
|
||||||
return ApiResponse<object>.ErrorResponse("Recurring Payment Status not found.", "Recurring Payment Status not found.", 404);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
var currency = await currencyTask;
|
|
||||||
if (currency == null)
|
|
||||||
{
|
|
||||||
_logger.LogWarning("Currency not found with Id: {CurrencyId}", model.CurrencyId);
|
|
||||||
return ApiResponse<object>.ErrorResponse("Currency not found.", "Currency not found.", 404);
|
|
||||||
}
|
|
||||||
|
|
||||||
var project = await projectTask;
|
|
||||||
|
|
||||||
string uIDPrefix = $"RP/{DateTime.Now:MMyy}";
|
|
||||||
|
|
||||||
var lastExpense = await _context.RecurringPayments
|
|
||||||
.Where(e => e.UIDPrefix == uIDPrefix)
|
|
||||||
.OrderByDescending(e => e.UIDPostfix)
|
|
||||||
.FirstOrDefaultAsync();
|
|
||||||
|
|
||||||
int uIDPostfix = lastExpense == null ? 1 : (lastExpense.UIDPostfix + 1);
|
|
||||||
|
|
||||||
var recurringPayment = _mapper.Map<RecurringPayment>(model);
|
|
||||||
recurringPayment.UIDPrefix = uIDPrefix;
|
|
||||||
recurringPayment.UIDPostfix = uIDPostfix;
|
|
||||||
recurringPayment.IsActive = true;
|
|
||||||
recurringPayment.CreatedAt = DateTime.UtcNow;
|
|
||||||
recurringPayment.CreatedById = loggedInEmployee.Id;
|
|
||||||
recurringPayment.TenantId = tenantId;
|
|
||||||
|
|
||||||
_context.RecurringPayments.Add(recurringPayment);
|
|
||||||
await _context.SaveChangesAsync();
|
|
||||||
|
|
||||||
var response = _mapper.Map<RecurringPaymentVM>(recurringPayment);
|
|
||||||
response.RecurringPaymentUId = $"{recurringPayment.UIDPrefix}/{recurringPayment.UIDPostfix:D5}";
|
|
||||||
response.CreatedBy = _mapper.Map<BasicEmployeeVM>(loggedInEmployee);
|
|
||||||
response.ExpenseCategory = _mapper.Map<ExpensesCategoryMasterVM>(expenseCategory);
|
|
||||||
response.Status = recurringStatus;
|
|
||||||
response.Currency = currency;
|
|
||||||
response.Project = _mapper.Map<BasicProjectVM>(project);
|
|
||||||
|
|
||||||
return ApiResponse<object>.SuccessResponse(response, "Recurring Payment Template created successfully", 201);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
public async Task<ApiResponse<object>> EditRecurringPaymentAsync(Guid id, RecurringTemplateDto model, Employee loggedInEmployee, Guid tenantId)
|
public async Task<ApiResponse<object>> EditRecurringPaymentAsync(Guid id, RecurringTemplateDto model, Employee loggedInEmployee, Guid tenantId)
|
||||||
{
|
{
|
||||||
using var scope = _serviceScopeFactory.CreateScope();
|
using var scope = _serviceScopeFactory.CreateScope();
|
||||||
@ -2435,8 +2460,8 @@ namespace Marco.Pms.Services.Service
|
|||||||
|
|
||||||
if (!hasPermission)
|
if (!hasPermission)
|
||||||
{
|
{
|
||||||
_logger.LogWarning("Employee {EmployeeId} attempted to create recurring template", loggedInEmployee.Id);
|
_logger.LogWarning("Employee {EmployeeId} attempted to update recurring template", loggedInEmployee.Id);
|
||||||
return ApiResponse<object>.ErrorResponse("You do not have access to create recurring template", "You do not have access to create recurring template", 400);
|
return ApiResponse<object>.ErrorResponse("You do not have access to update recurring template", "You do not have access to update recurring template", 400);
|
||||||
}
|
}
|
||||||
|
|
||||||
var expenseCategoryTask = Task.Run(async () =>
|
var expenseCategoryTask = Task.Run(async () =>
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user