Added the create recurring payment API
This commit is contained in:
parent
3486534c0b
commit
2a53e1c241
@ -2340,38 +2340,47 @@ namespace Marco.Pms.Services.Service
|
||||
|
||||
public async Task<ApiResponse<object>> CreateRecurringPaymentAsync(RecurringTemplateDto model, Employee loggedInEmployee, Guid tenantId)
|
||||
{
|
||||
_logger.LogInfo("Start CreateRecurringPaymentAsync called by EmployeeId: {EmployeeId} for TenantId: {TenantId}", loggedInEmployee.Id, tenantId);
|
||||
|
||||
try
|
||||
{
|
||||
// Check if user has permission to create recurring payment templates
|
||||
using var scope = _serviceScopeFactory.CreateScope();
|
||||
var permissionService = scope.ServiceProvider.GetRequiredService<PermissionServices>();
|
||||
var hasPermission = await permissionService.HasPermission(PermissionsMaster.ManageRecurring, loggedInEmployee.Id);
|
||||
|
||||
if (!hasPermission)
|
||||
{
|
||||
_logger.LogWarning("Employee {EmployeeId} attempted to create 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);
|
||||
_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(et => et.Id == model.StatusId);
|
||||
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 await context.Projects.FirstOrDefaultAsync(P => model.ProjectId.HasValue && P.Id == model.ProjectId.Value);
|
||||
return model.ProjectId.HasValue ? await context.Projects.FirstOrDefaultAsync(p => p.Id == model.ProjectId.Value) : null;
|
||||
});
|
||||
|
||||
await Task.WhenAll(expenseCategoryTask, currencyTask, projectTask, recurringStatusTask);
|
||||
await Task.WhenAll(expenseCategoryTask, recurringStatusTask, currencyTask, projectTask);
|
||||
|
||||
var expenseCategory = await expenseCategoryTask;
|
||||
if (expenseCategory == null)
|
||||
@ -2381,7 +2390,7 @@ namespace Marco.Pms.Services.Service
|
||||
}
|
||||
|
||||
var recurringStatus = await recurringStatusTask;
|
||||
if (expenseCategory == null)
|
||||
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);
|
||||
@ -2394,17 +2403,19 @@ namespace Marco.Pms.Services.Service
|
||||
return ApiResponse<object>.ErrorResponse("Currency not found.", "Currency not found.", 404);
|
||||
}
|
||||
|
||||
var project = await projectTask;
|
||||
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 lastExpense = await _context.RecurringPayments
|
||||
var lastRecurringPayment = await _context.RecurringPayments
|
||||
.Where(e => e.UIDPrefix == uIDPrefix)
|
||||
.OrderByDescending(e => e.UIDPostfix)
|
||||
.FirstOrDefaultAsync();
|
||||
|
||||
int uIDPostfix = lastExpense == null ? 1 : (lastExpense.UIDPostfix + 1);
|
||||
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;
|
||||
@ -2416,6 +2427,7 @@ namespace Marco.Pms.Services.Service
|
||||
_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);
|
||||
@ -2424,8 +2436,21 @@ namespace Marco.Pms.Services.Service
|
||||
response.Currency = currency;
|
||||
response.Project = _mapper.Map<BasicProjectVM>(project);
|
||||
|
||||
return ApiResponse<object>.SuccessResponse(response, "Recurring Payment Template created successfully", 201);
|
||||
_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)
|
||||
{
|
||||
_logger.LogError(ex, "Error in CreateRecurringPaymentAsync called by EmployeeId: {EmployeeId}: {Message}", loggedInEmployee.Id, ex.Message);
|
||||
return ApiResponse<object>.ErrorResponse("An error occurred while creating the recurring payment template.", ex.Message, 500);
|
||||
}
|
||||
finally
|
||||
{
|
||||
_logger.LogInfo("End CreateRecurringPaymentAsync called by EmployeeId: {EmployeeId}", loggedInEmployee.Id);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
public async Task<ApiResponse<object>> EditRecurringPaymentAsync(Guid id, RecurringTemplateDto model, Employee loggedInEmployee, Guid tenantId)
|
||||
{
|
||||
@ -2435,8 +2460,8 @@ namespace Marco.Pms.Services.Service
|
||||
|
||||
if (!hasPermission)
|
||||
{
|
||||
_logger.LogWarning("Employee {EmployeeId} attempted to create 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);
|
||||
_logger.LogWarning("Employee {EmployeeId} attempted to update recurring template", loggedInEmployee.Id);
|
||||
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 () =>
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user