Compare commits
No commits in common. "87ebee8005623549a1b1c0c966f1a01a34964229" and "5717bb6dacf491405842fd114e55d399a183f346" have entirely different histories.
87ebee8005
...
5717bb6dac
@ -1,5 +1,4 @@
|
|||||||
using AutoMapper;
|
using AutoMapper;
|
||||||
using FirebaseAdmin.Messaging;
|
|
||||||
using Marco.Pms.DataAccess.Data;
|
using Marco.Pms.DataAccess.Data;
|
||||||
using Marco.Pms.Helpers.Utility;
|
using Marco.Pms.Helpers.Utility;
|
||||||
using Marco.Pms.Model.DocumentManager;
|
using Marco.Pms.Model.DocumentManager;
|
||||||
@ -11,7 +10,6 @@ using Marco.Pms.Model.Utilities;
|
|||||||
using Marco.Pms.Model.ViewModels.Activities;
|
using Marco.Pms.Model.ViewModels.Activities;
|
||||||
using Marco.Pms.Model.ViewModels.DocumentManager;
|
using Marco.Pms.Model.ViewModels.DocumentManager;
|
||||||
using Marco.Pms.Services.Service;
|
using Marco.Pms.Services.Service;
|
||||||
using Marco.Pms.Services.Service.ServiceInterfaces;
|
|
||||||
using MarcoBMS.Services.Helpers;
|
using MarcoBMS.Services.Helpers;
|
||||||
using MarcoBMS.Services.Service;
|
using MarcoBMS.Services.Service;
|
||||||
using Microsoft.AspNetCore.Authorization;
|
using Microsoft.AspNetCore.Authorization;
|
||||||
@ -843,38 +841,6 @@ namespace Marco.Pms.Services.Controllers
|
|||||||
response.ParentAttachmentId = versionMapping.ParentAttachmentId;
|
response.ParentAttachmentId = versionMapping.ParentAttachmentId;
|
||||||
response.Version = versionMapping.Version;
|
response.Version = versionMapping.Version;
|
||||||
|
|
||||||
_ = Task.Run(async () =>
|
|
||||||
{
|
|
||||||
// --- Push Notification Section ---
|
|
||||||
// This section attempts to send a test push notification to the user's device.
|
|
||||||
// It's designed to fail gracefully and handle invalid Firebase Cloud Messaging (FCM) tokens.
|
|
||||||
var _firebase = scope.ServiceProvider.GetRequiredService<IFirebaseService>();
|
|
||||||
|
|
||||||
var name = $"{loggedInEmployee.FirstName} {loggedInEmployee.LastName}";
|
|
||||||
|
|
||||||
if (EmployeeEntity == entityType && model.EntityId != loggedInEmployee.Id)
|
|
||||||
{
|
|
||||||
var notification = new Notification
|
|
||||||
{
|
|
||||||
Title = $"Document added to Employee.",
|
|
||||||
Body = $"Document added to your profile of type \"{documentType.Name}\" by {name}"
|
|
||||||
};
|
|
||||||
|
|
||||||
await _firebase.SendEmployeeDocumentMessageAsync(model.EntityId, notification, tenantId);
|
|
||||||
}
|
|
||||||
if (ProjectEntity == entityType)
|
|
||||||
{
|
|
||||||
var notification = new Notification
|
|
||||||
{
|
|
||||||
Title = $"Document added to Project.",
|
|
||||||
Body = $"Document added to your Project of type \"{documentType.Name}\" by {name}"
|
|
||||||
};
|
|
||||||
|
|
||||||
await _firebase.SendProjectDocumentMessageAsync(model.EntityId, notification, tenantId);
|
|
||||||
}
|
|
||||||
|
|
||||||
});
|
|
||||||
|
|
||||||
return Ok(ApiResponse<object>.SuccessResponse(response, "Document added successfully", 200));
|
return Ok(ApiResponse<object>.SuccessResponse(response, "Document added successfully", 200));
|
||||||
}
|
}
|
||||||
catch (Exception ex)
|
catch (Exception ex)
|
||||||
@ -966,40 +932,6 @@ namespace Marco.Pms.Services.Controllers
|
|||||||
}
|
}
|
||||||
|
|
||||||
_logger.LogInfo("Document verified successfully. DocumentId: {DocumentId}, VerifiedBy: {EmployeeId}", documentAttachment.Id, loggedInEmployee.Id);
|
_logger.LogInfo("Document verified successfully. DocumentId: {DocumentId}, VerifiedBy: {EmployeeId}", documentAttachment.Id, loggedInEmployee.Id);
|
||||||
_ = Task.Run(async () =>
|
|
||||||
{
|
|
||||||
// --- Push Notification Section ---
|
|
||||||
// This section attempts to send a test push notification to the user's device.
|
|
||||||
// It's designed to fail gracefully and handle invalid Firebase Cloud Messaging (FCM) tokens.
|
|
||||||
var _firebase = scope.ServiceProvider.GetRequiredService<IFirebaseService>();
|
|
||||||
|
|
||||||
var name = $"{loggedInEmployee.FirstName} {loggedInEmployee.LastName}";
|
|
||||||
|
|
||||||
var entityType = documentAttachment.DocumentType?.DocumentCategory?.EntityTypeId;
|
|
||||||
var documentType = documentAttachment.DocumentType;
|
|
||||||
|
|
||||||
if (EmployeeEntity == entityType && documentAttachment.EntityId != loggedInEmployee.Id)
|
|
||||||
{
|
|
||||||
var notification = new Notification
|
|
||||||
{
|
|
||||||
Title = $"Your Document is Verified.",
|
|
||||||
Body = $"Your Document of type \"{documentType?.Name}\" is Verified by {name}"
|
|
||||||
};
|
|
||||||
|
|
||||||
await _firebase.SendEmployeeDocumentMessageAsync(documentAttachment.EntityId, notification, tenantId);
|
|
||||||
}
|
|
||||||
if (ProjectEntity == entityType)
|
|
||||||
{
|
|
||||||
var notification = new Notification
|
|
||||||
{
|
|
||||||
Title = $"Document for your project is verified",
|
|
||||||
Body = $"Document for your Project of type \"{documentType?.Name}\" is Verified by {name}"
|
|
||||||
};
|
|
||||||
|
|
||||||
await _firebase.SendProjectDocumentMessageAsync(documentAttachment.EntityId, notification, tenantId);
|
|
||||||
}
|
|
||||||
|
|
||||||
});
|
|
||||||
return Ok(ApiResponse<object>.SuccessResponse(new { }, "Document is verified successfully", 200));
|
return Ok(ApiResponse<object>.SuccessResponse(new { }, "Document is verified successfully", 200));
|
||||||
}
|
}
|
||||||
catch (Exception ex)
|
catch (Exception ex)
|
||||||
@ -1244,7 +1176,7 @@ namespace Marco.Pms.Services.Controllers
|
|||||||
oldAttachment.DocumentId = model.DocumentId;
|
oldAttachment.DocumentId = model.DocumentId;
|
||||||
oldAttachment.Description = model.Description;
|
oldAttachment.Description = model.Description;
|
||||||
oldAttachment.DocumentDataId = document.Id;
|
oldAttachment.DocumentDataId = document.Id;
|
||||||
if (oldAttachment.IsVerified != null)
|
if (oldAttachment.IsVerified == true)
|
||||||
{
|
{
|
||||||
oldAttachment.IsVerified = null;
|
oldAttachment.IsVerified = null;
|
||||||
_logger.LogInfo("Reset verification flag for AttachmentId: {AttachmentId}", oldAttachment.Id);
|
_logger.LogInfo("Reset verification flag for AttachmentId: {AttachmentId}", oldAttachment.Id);
|
||||||
@ -1264,7 +1196,7 @@ namespace Marco.Pms.Services.Controllers
|
|||||||
oldAttachment.Name = model.Name;
|
oldAttachment.Name = model.Name;
|
||||||
oldAttachment.DocumentId = model.DocumentId;
|
oldAttachment.DocumentId = model.DocumentId;
|
||||||
oldAttachment.Description = model.Description;
|
oldAttachment.Description = model.Description;
|
||||||
if (oldAttachment.IsVerified != null)
|
if (oldAttachment.IsVerified == true)
|
||||||
{
|
{
|
||||||
oldAttachment.IsVerified = null;
|
oldAttachment.IsVerified = null;
|
||||||
_logger.LogInfo("Reset verification flag for AttachmentId: {AttachmentId}", oldAttachment.Id);
|
_logger.LogInfo("Reset verification flag for AttachmentId: {AttachmentId}", oldAttachment.Id);
|
||||||
@ -1356,38 +1288,6 @@ namespace Marco.Pms.Services.Controllers
|
|||||||
response.Version = newVersionMapping.Version;
|
response.Version = newVersionMapping.Version;
|
||||||
_logger.LogInfo("API completed successfully for AttachmentId: {AttachmentId}", newAttachment.Id);
|
_logger.LogInfo("API completed successfully for AttachmentId: {AttachmentId}", newAttachment.Id);
|
||||||
|
|
||||||
_ = Task.Run(async () =>
|
|
||||||
{
|
|
||||||
// --- Push Notification Section ---
|
|
||||||
// This section attempts to send a test push notification to the user's device.
|
|
||||||
// It's designed to fail gracefully and handle invalid Firebase Cloud Messaging (FCM) tokens.
|
|
||||||
var _firebase = scope.ServiceProvider.GetRequiredService<IFirebaseService>();
|
|
||||||
|
|
||||||
var name = $"{loggedInEmployee.FirstName} {loggedInEmployee.LastName}";
|
|
||||||
|
|
||||||
if (EmployeeEntity == entityType && newAttachment.EntityId != loggedInEmployee.Id)
|
|
||||||
{
|
|
||||||
var notification = new Notification
|
|
||||||
{
|
|
||||||
Title = $"Your Document is updated.",
|
|
||||||
Body = $"Your Document of type \"{documentType?.Name}\" is updated by {name}"
|
|
||||||
};
|
|
||||||
|
|
||||||
await _firebase.SendEmployeeDocumentMessageAsync(newAttachment.EntityId, notification, tenantId);
|
|
||||||
}
|
|
||||||
if (ProjectEntity == entityType)
|
|
||||||
{
|
|
||||||
var notification = new Notification
|
|
||||||
{
|
|
||||||
Title = "Your Project Document is updated.",
|
|
||||||
Body = $"Your Project Document of type \"{documentType?.Name}\" is updated by {name}"
|
|
||||||
};
|
|
||||||
|
|
||||||
await _firebase.SendProjectDocumentMessageAsync(newAttachment.EntityId, notification, tenantId);
|
|
||||||
}
|
|
||||||
|
|
||||||
});
|
|
||||||
|
|
||||||
return Ok(ApiResponse<object>.SuccessResponse(response, "Document Updated successfully", 200));
|
return Ok(ApiResponse<object>.SuccessResponse(response, "Document Updated successfully", 200));
|
||||||
}
|
}
|
||||||
catch (Exception ex)
|
catch (Exception ex)
|
||||||
@ -1471,41 +1371,6 @@ namespace Marco.Pms.Services.Controllers
|
|||||||
// Log the successful completion of the operation
|
// Log the successful completion of the operation
|
||||||
_logger.LogInfo("DocumentAttachment ID: {DocumentId} has been {Message} by employee ID: {EmployeeId}", id, message, loggedInEmployee.Id);
|
_logger.LogInfo("DocumentAttachment ID: {DocumentId} has been {Message} by employee ID: {EmployeeId}", id, message, loggedInEmployee.Id);
|
||||||
|
|
||||||
_ = Task.Run(async () =>
|
|
||||||
{
|
|
||||||
// --- Push Notification Section ---
|
|
||||||
// This section attempts to send a test push notification to the user's device.
|
|
||||||
// It's designed to fail gracefully and handle invalid Firebase Cloud Messaging (FCM) tokens.
|
|
||||||
var _firebase = scope.ServiceProvider.GetRequiredService<IFirebaseService>();
|
|
||||||
|
|
||||||
var entityType = documentAttachment.DocumentType?.DocumentCategory?.EntityTypeId;
|
|
||||||
var documentType = documentAttachment.DocumentType;
|
|
||||||
|
|
||||||
var name = $"{loggedInEmployee.FirstName} {loggedInEmployee.LastName}";
|
|
||||||
|
|
||||||
if (EmployeeEntity == entityType && documentAttachment.EntityId != loggedInEmployee.Id)
|
|
||||||
{
|
|
||||||
var notification = new Notification
|
|
||||||
{
|
|
||||||
Title = $"Your Document is {message}.",
|
|
||||||
Body = $"Your Document of type \"{documentType?.Name}\" is {message} by {name}"
|
|
||||||
};
|
|
||||||
|
|
||||||
await _firebase.SendEmployeeDocumentMessageAsync(documentAttachment.EntityId, notification, tenantId);
|
|
||||||
}
|
|
||||||
if (ProjectEntity == entityType)
|
|
||||||
{
|
|
||||||
var notification = new Notification
|
|
||||||
{
|
|
||||||
Title = "Your Project Document is {message}.",
|
|
||||||
Body = $"Your Project Document of type \"{documentType?.Name}\" is {message} by {name}"
|
|
||||||
};
|
|
||||||
|
|
||||||
await _firebase.SendProjectDocumentMessageAsync(documentAttachment.EntityId, notification, tenantId);
|
|
||||||
}
|
|
||||||
|
|
||||||
});
|
|
||||||
|
|
||||||
// Return success response
|
// Return success response
|
||||||
return Ok(ApiResponse<object>.SuccessResponse(new { }, $"Document attachment is {message}", 200));
|
return Ok(ApiResponse<object>.SuccessResponse(new { }, $"Document attachment is {message}", 200));
|
||||||
}
|
}
|
||||||
|
|||||||
@ -1,5 +1,4 @@
|
|||||||
using AutoMapper;
|
using AutoMapper;
|
||||||
using FirebaseAdmin.Messaging;
|
|
||||||
using Marco.Pms.DataAccess.Data;
|
using Marco.Pms.DataAccess.Data;
|
||||||
using Marco.Pms.Helpers.Utility;
|
using Marco.Pms.Helpers.Utility;
|
||||||
using Marco.Pms.Model.Directory;
|
using Marco.Pms.Model.Directory;
|
||||||
@ -120,19 +119,16 @@ namespace Marco.Pms.Services.Service
|
|||||||
|
|
||||||
// --- Advanced Filtering from 'filter' parameter ---
|
// --- Advanced Filtering from 'filter' parameter ---
|
||||||
ContactFilterDto? contactFilter = TryDeserializeContactFilter(filter);
|
ContactFilterDto? contactFilter = TryDeserializeContactFilter(filter);
|
||||||
if (contactFilter != null)
|
if (contactFilter?.BucketIds?.Any() ?? false)
|
||||||
{
|
|
||||||
if (contactFilter.BucketIds?.Any() ?? false)
|
|
||||||
{
|
{
|
||||||
// Note: Permission filtering is already applied. Here we further restrict by the user's filter choice.
|
// Note: Permission filtering is already applied. Here we further restrict by the user's filter choice.
|
||||||
contactQuery = contactQuery.Where(c => dbContext.ContactBucketMappings.Any(cbm =>
|
contactQuery = contactQuery.Where(c => dbContext.ContactBucketMappings.Any(cbm =>
|
||||||
cbm.ContactId == c.Id && contactFilter.BucketIds.Contains(cbm.BucketId)));
|
cbm.ContactId == c.Id && contactFilter.BucketIds.Contains(cbm.BucketId)));
|
||||||
}
|
}
|
||||||
if (contactFilter.CategoryIds?.Any() ?? false)
|
if (contactFilter?.CategoryIds?.Any() ?? false)
|
||||||
{
|
{
|
||||||
contactQuery = contactQuery.Where(c => c.ContactCategoryId.HasValue && contactFilter.CategoryIds.Contains(c.ContactCategoryId.Value));
|
contactQuery = contactQuery.Where(c => c.ContactCategoryId.HasValue && contactFilter.CategoryIds.Contains(c.ContactCategoryId.Value));
|
||||||
}
|
}
|
||||||
}
|
|
||||||
|
|
||||||
// --- Standard Filtering ---
|
// --- Standard Filtering ---
|
||||||
if (projectId != null)
|
if (projectId != null)
|
||||||
@ -939,7 +935,6 @@ namespace Marco.Pms.Services.Service
|
|||||||
/// <returns>An ApiResponse containing the newly created contact's view model or an error.</returns>
|
/// <returns>An ApiResponse containing the newly created contact's view model or an error.</returns>
|
||||||
public async Task<ApiResponse<object>> CreateContactAsync(CreateContactDto createContact, Guid tenantId, Employee loggedInEmployee)
|
public async Task<ApiResponse<object>> CreateContactAsync(CreateContactDto createContact, Guid tenantId, Employee loggedInEmployee)
|
||||||
{
|
{
|
||||||
using var scope = _serviceScopeFactory.CreateScope();
|
|
||||||
Guid loggedInEmployeeId = loggedInEmployee.Id;
|
Guid loggedInEmployeeId = loggedInEmployee.Id;
|
||||||
|
|
||||||
if (string.IsNullOrWhiteSpace(createContact.Name) ||
|
if (string.IsNullOrWhiteSpace(createContact.Name) ||
|
||||||
@ -1012,25 +1007,6 @@ namespace Marco.Pms.Services.Service
|
|||||||
contactVM.BucketIds = contactBucketMappings.Select(cb => cb.BucketId).ToList();
|
contactVM.BucketIds = contactBucketMappings.Select(cb => cb.BucketId).ToList();
|
||||||
contactVM.ProjectIds = projectMappings.Select(cp => cp.ProjectId).ToList();
|
contactVM.ProjectIds = projectMappings.Select(cp => cp.ProjectId).ToList();
|
||||||
|
|
||||||
_ = Task.Run(async () =>
|
|
||||||
{
|
|
||||||
// --- Push Notification Section ---
|
|
||||||
// This section attempts to send a test push notification to the user's device.
|
|
||||||
// It's designed to fail gracefully and handle invalid Firebase Cloud Messaging (FCM) tokens.
|
|
||||||
var _firebase = scope.ServiceProvider.GetRequiredService<IFirebaseService>();
|
|
||||||
|
|
||||||
var name = $"{loggedInEmployee.FirstName} {loggedInEmployee.LastName}";
|
|
||||||
|
|
||||||
var notification = new Notification
|
|
||||||
{
|
|
||||||
Title = "New Contact Created",
|
|
||||||
Body = $"New Contact \"{contact.Name}\" is created by {name} in your bucket"
|
|
||||||
};
|
|
||||||
|
|
||||||
await _firebase.SendContactAsync(contactVM.BucketIds, notification, tenantId);
|
|
||||||
|
|
||||||
});
|
|
||||||
|
|
||||||
return ApiResponse<object>.SuccessResponse(contactVM, "Contact created successfully.", 201);
|
return ApiResponse<object>.SuccessResponse(contactVM, "Contact created successfully.", 201);
|
||||||
}
|
}
|
||||||
catch (Exception ex)
|
catch (Exception ex)
|
||||||
@ -1046,7 +1022,6 @@ namespace Marco.Pms.Services.Service
|
|||||||
|
|
||||||
public async Task<ApiResponse<object>> UpdateContactAsync(Guid id, UpdateContactDto updateContact, Guid tenantId, Employee loggedInEmployee)
|
public async Task<ApiResponse<object>> UpdateContactAsync(Guid id, UpdateContactDto updateContact, Guid tenantId, Employee loggedInEmployee)
|
||||||
{
|
{
|
||||||
using var scope = _serviceScopeFactory.CreateScope();
|
|
||||||
if (updateContact == null)
|
if (updateContact == null)
|
||||||
{
|
{
|
||||||
_logger.LogWarning("Employee {EmployeeId} sent empty payload for updating contact.", loggedInEmployee.Id);
|
_logger.LogWarning("Employee {EmployeeId} sent empty payload for updating contact.", loggedInEmployee.Id);
|
||||||
@ -1480,28 +1455,6 @@ namespace Marco.Pms.Services.Service
|
|||||||
|
|
||||||
_logger.LogInfo("Contact {ContactId} successfully updated by employee {EmployeeId}.", contact.Id, loggedInEmployee.Id);
|
_logger.LogInfo("Contact {ContactId} successfully updated by employee {EmployeeId}.", contact.Id, loggedInEmployee.Id);
|
||||||
|
|
||||||
_ = Task.Run(async () =>
|
|
||||||
{
|
|
||||||
// --- Push Notification Section ---
|
|
||||||
// This section attempts to send a test push notification to the user's device.
|
|
||||||
// It's designed to fail gracefully and handle invalid Firebase Cloud Messaging (FCM) tokens.
|
|
||||||
var _firebase = scope.ServiceProvider.GetRequiredService<IFirebaseService>();
|
|
||||||
|
|
||||||
if (contactVM.BucketIds?.Any() ?? false)
|
|
||||||
{
|
|
||||||
var name = $"{loggedInEmployee.FirstName} {loggedInEmployee.LastName}";
|
|
||||||
|
|
||||||
var notification = new Notification
|
|
||||||
{
|
|
||||||
Title = $"Contact Updated - \"{contact.Name}\"",
|
|
||||||
Body = $"Contact \"{contact.Name}\" is updated by {name} in your bucket"
|
|
||||||
};
|
|
||||||
|
|
||||||
await _firebase.SendContactAsync(contactVM.BucketIds, notification, tenantId);
|
|
||||||
}
|
|
||||||
|
|
||||||
});
|
|
||||||
|
|
||||||
return ApiResponse<object>.SuccessResponse(contactVM, "Contact Updated Successfully", 200);
|
return ApiResponse<object>.SuccessResponse(contactVM, "Contact Updated Successfully", 200);
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -1518,15 +1471,6 @@ namespace Marco.Pms.Services.Service
|
|||||||
return ApiResponse<object>.ErrorResponse("Contact ID is empty", "Contact ID is empty", 400);
|
return ApiResponse<object>.ErrorResponse("Contact ID is empty", "Contact ID is empty", 400);
|
||||||
}
|
}
|
||||||
|
|
||||||
var bucketIds = await _context.ContactBucketMappings.Where(cb => cb.ContactId == id).Select(cb => cb.BucketId).ToListAsync();
|
|
||||||
var hasContactAccess = await _context.EmployeeBucketMappings.AnyAsync(eb => bucketIds.Contains(eb.BucketId) && eb.EmployeeId == loggedInEmployee.Id);
|
|
||||||
if (hasContactAccess)
|
|
||||||
{
|
|
||||||
_logger.LogWarning("Employee {EmployeeId} does not have permission to delete contact {ContactId}",
|
|
||||||
loggedInEmployee.Id, id);
|
|
||||||
return ApiResponse<object>.ErrorResponse("Unauthorized", "You do not have permission", 403);
|
|
||||||
}
|
|
||||||
|
|
||||||
// Try to find the contact for the given tenant
|
// Try to find the contact for the given tenant
|
||||||
Contact? contact = await _context.Contacts.FirstOrDefaultAsync(c => c.Id == id && c.TenantId == tenantId);
|
Contact? contact = await _context.Contacts.FirstOrDefaultAsync(c => c.Id == id && c.TenantId == tenantId);
|
||||||
|
|
||||||
@ -1561,45 +1505,10 @@ namespace Marco.Pms.Services.Service
|
|||||||
|
|
||||||
_logger.LogInfo("Contact ID {ContactId} has been {(DeletedOrActivated)} by Employee ID {EmployeeId}.", id, active ? "activated" : "deleted", loggedInEmployee.Id);
|
_logger.LogInfo("Contact ID {ContactId} has been {(DeletedOrActivated)} by Employee ID {EmployeeId}.", id, active ? "activated" : "deleted", loggedInEmployee.Id);
|
||||||
|
|
||||||
using var scope = _serviceScopeFactory.CreateScope();
|
|
||||||
|
|
||||||
_ = Task.Run(async () =>
|
|
||||||
{
|
|
||||||
// --- Push Notification Section ---
|
|
||||||
// This section attempts to send a test push notification to the user's device.
|
|
||||||
// It's designed to fail gracefully and handle invalid Firebase Cloud Messaging (FCM) tokens.
|
|
||||||
var _firebase = scope.ServiceProvider.GetRequiredService<IFirebaseService>();
|
|
||||||
|
|
||||||
if (bucketIds.Any())
|
|
||||||
{
|
|
||||||
var name = $"{loggedInEmployee.FirstName} {loggedInEmployee.LastName}";
|
|
||||||
|
|
||||||
Notification notification;
|
|
||||||
if (active)
|
|
||||||
{
|
|
||||||
notification = new Notification
|
|
||||||
{
|
|
||||||
Title = $"Contact restored - \"{contact.Name}\"",
|
|
||||||
Body = $"Contact \"{contact.Name}\" is restored by {name} in your bucket"
|
|
||||||
};
|
|
||||||
}
|
|
||||||
else
|
|
||||||
{
|
|
||||||
notification = new Notification
|
|
||||||
{
|
|
||||||
Title = $"Contact Deleted - \"{contact.Name}\"",
|
|
||||||
Body = $"Contact \"{contact.Name}\" is deleted by {name} in your bucket"
|
|
||||||
};
|
|
||||||
}
|
|
||||||
|
|
||||||
await _firebase.SendContactAsync(bucketIds, notification, tenantId);
|
|
||||||
}
|
|
||||||
|
|
||||||
});
|
|
||||||
|
|
||||||
return ApiResponse<object>.SuccessResponse(new { }, active ? "Contact is activated successfully" : "Contact is deleted successfully", 200);
|
return ApiResponse<object>.SuccessResponse(new { }, active ? "Contact is activated successfully" : "Contact is deleted successfully", 200);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
#endregion
|
#endregion
|
||||||
|
|
||||||
#endregion
|
#endregion
|
||||||
@ -2020,14 +1929,6 @@ namespace Marco.Pms.Services.Service
|
|||||||
|
|
||||||
try
|
try
|
||||||
{
|
{
|
||||||
var bucketIds = await _context.ContactBucketMappings.Where(cb => cb.ContactId == noteDto.ContactId).Select(cb => cb.BucketId).ToListAsync();
|
|
||||||
var hasContactAccess = await _context.EmployeeBucketMappings.AnyAsync(eb => bucketIds.Contains(eb.BucketId) && eb.EmployeeId == loggedInEmployee.Id);
|
|
||||||
if (hasContactAccess)
|
|
||||||
{
|
|
||||||
_logger.LogWarning("Employee {EmployeeId} does not have permission to delete contact {ContactId}",
|
|
||||||
loggedInEmployee.Id, noteDto.ContactId);
|
|
||||||
return ApiResponse<object>.ErrorResponse("Unauthorized", "You do not have permission", 403);
|
|
||||||
}
|
|
||||||
// Check if the contact exists and is active for this tenant
|
// Check if the contact exists and is active for this tenant
|
||||||
Contact? contact = await _context.Contacts
|
Contact? contact = await _context.Contacts
|
||||||
.AsNoTracking() // optimization for read-only query
|
.AsNoTracking() // optimization for read-only query
|
||||||
@ -2058,27 +1959,6 @@ namespace Marco.Pms.Services.Service
|
|||||||
"Employee {EmployeeId} successfully added a note (NoteId: {NoteId}) to Contact {ContactId} for Tenant {TenantId}.",
|
"Employee {EmployeeId} successfully added a note (NoteId: {NoteId}) to Contact {ContactId} for Tenant {TenantId}.",
|
||||||
loggedInEmployee.Id, note.Id, contact.Id, tenantId);
|
loggedInEmployee.Id, note.Id, contact.Id, tenantId);
|
||||||
|
|
||||||
using var scope = _serviceScopeFactory.CreateScope();
|
|
||||||
|
|
||||||
_ = Task.Run(async () =>
|
|
||||||
{
|
|
||||||
// --- Push Notification Section ---
|
|
||||||
// This section attempts to send a test push notification to the user's device.
|
|
||||||
// It's designed to fail gracefully and handle invalid Firebase Cloud Messaging (FCM) tokens.
|
|
||||||
var _firebase = scope.ServiceProvider.GetRequiredService<IFirebaseService>();
|
|
||||||
|
|
||||||
var name = $"{loggedInEmployee.FirstName} {loggedInEmployee.LastName}";
|
|
||||||
|
|
||||||
var notification = new Notification
|
|
||||||
{
|
|
||||||
Title = $"New note added at Contact - \"{contact.Name}\"",
|
|
||||||
Body = $"New note added at Contact \"{contact.Name}\" by {name} in your bucket"
|
|
||||||
};
|
|
||||||
|
|
||||||
await _firebase.SendContactNoteAsync(bucketIds, notification, tenantId);
|
|
||||||
|
|
||||||
});
|
|
||||||
|
|
||||||
return ApiResponse<object>.SuccessResponse(noteVM, "Note added successfully.", 200);
|
return ApiResponse<object>.SuccessResponse(noteVM, "Note added successfully.", 200);
|
||||||
}
|
}
|
||||||
catch (Exception ex)
|
catch (Exception ex)
|
||||||
@ -2111,15 +1991,6 @@ namespace Marco.Pms.Services.Service
|
|||||||
return ApiResponse<object>.ErrorResponse("Invalid or empty payload", "Invalid or empty payload", 400);
|
return ApiResponse<object>.ErrorResponse("Invalid or empty payload", "Invalid or empty payload", 400);
|
||||||
}
|
}
|
||||||
|
|
||||||
var bucketIds = await _context.ContactBucketMappings.Where(cb => cb.ContactId == noteDto.ContactId).Select(cb => cb.BucketId).ToListAsync();
|
|
||||||
var hasContactAccess = await _context.EmployeeBucketMappings.AnyAsync(eb => bucketIds.Contains(eb.BucketId) && eb.EmployeeId == loggedInEmployee.Id);
|
|
||||||
if (hasContactAccess)
|
|
||||||
{
|
|
||||||
_logger.LogWarning("Employee {EmployeeId} does not have permission to delete contact {ContactId}",
|
|
||||||
loggedInEmployee.Id, noteDto.ContactId);
|
|
||||||
return ApiResponse<object>.ErrorResponse("Unauthorized", "You do not have permission", 403);
|
|
||||||
}
|
|
||||||
|
|
||||||
// Check if the contact belongs to this tenant
|
// Check if the contact belongs to this tenant
|
||||||
Contact? contact = await _context.Contacts
|
Contact? contact = await _context.Contacts
|
||||||
.AsNoTracking()
|
.AsNoTracking()
|
||||||
@ -2190,27 +2061,6 @@ namespace Marco.Pms.Services.Service
|
|||||||
_logger.LogInfo("Employee {EmployeeId} successfully updated Note {NoteId} for Contact {ContactId} at {UpdatedAt}",
|
_logger.LogInfo("Employee {EmployeeId} successfully updated Note {NoteId} for Contact {ContactId} at {UpdatedAt}",
|
||||||
loggedInEmployee.Id, noteVM.Id, contact.Id, noteVM.UpdatedAt);
|
loggedInEmployee.Id, noteVM.Id, contact.Id, noteVM.UpdatedAt);
|
||||||
|
|
||||||
using var scope = _serviceScopeFactory.CreateScope();
|
|
||||||
|
|
||||||
_ = Task.Run(async () =>
|
|
||||||
{
|
|
||||||
// --- Push Notification Section ---
|
|
||||||
// This section attempts to send a test push notification to the user's device.
|
|
||||||
// It's designed to fail gracefully and handle invalid Firebase Cloud Messaging (FCM) tokens.
|
|
||||||
var _firebase = scope.ServiceProvider.GetRequiredService<IFirebaseService>();
|
|
||||||
|
|
||||||
var name = $"{loggedInEmployee.FirstName} {loggedInEmployee.LastName}";
|
|
||||||
|
|
||||||
var notification = new Notification
|
|
||||||
{
|
|
||||||
Title = $"Note updated at Contact - \"{contact.Name}\"",
|
|
||||||
Body = $"Note updated at Contact \"{contact.Name}\" by {name} in your bucket"
|
|
||||||
};
|
|
||||||
|
|
||||||
await _firebase.SendContactNoteAsync(bucketIds, notification, tenantId);
|
|
||||||
|
|
||||||
});
|
|
||||||
|
|
||||||
return ApiResponse<object>.SuccessResponse(noteVM, "Note updated successfully", 200);
|
return ApiResponse<object>.SuccessResponse(noteVM, "Note updated successfully", 200);
|
||||||
}
|
}
|
||||||
catch (DbUpdateException ex)
|
catch (DbUpdateException ex)
|
||||||
@ -2253,28 +2103,6 @@ namespace Marco.Pms.Services.Service
|
|||||||
return ApiResponse<object>.ErrorResponse("Note not found", "Note not found", 404);
|
return ApiResponse<object>.ErrorResponse("Note not found", "Note not found", 404);
|
||||||
}
|
}
|
||||||
|
|
||||||
var bucketIds = await _context.ContactBucketMappings.Where(cb => cb.ContactId == note.ContactId).Select(cb => cb.BucketId).ToListAsync();
|
|
||||||
var hasContactAccess = await _context.EmployeeBucketMappings.AnyAsync(eb => bucketIds.Contains(eb.BucketId) && eb.EmployeeId == loggedInEmployee.Id);
|
|
||||||
if (hasContactAccess)
|
|
||||||
{
|
|
||||||
_logger.LogWarning("Employee {EmployeeId} does not have permission to delete contact {ContactId}",
|
|
||||||
loggedInEmployee.Id, note.ContactId);
|
|
||||||
return ApiResponse<object>.ErrorResponse("Unauthorized", "You do not have permission", 403);
|
|
||||||
}
|
|
||||||
|
|
||||||
// Check if the contact belongs to this tenant
|
|
||||||
Contact? contact = await _context.Contacts
|
|
||||||
.AsNoTracking()
|
|
||||||
.FirstOrDefaultAsync(c => c.Id == note.ContactId && c.TenantId == tenantId);
|
|
||||||
|
|
||||||
if (contact == null)
|
|
||||||
{
|
|
||||||
_logger.LogWarning("Employee {EmployeeId} attempted to update note {NoteId} for Contact {ContactId}, but the contact was not found in Tenant {TenantId}.",
|
|
||||||
loggedInEmployee.Id, note.Id, note.ContactId, tenantId);
|
|
||||||
|
|
||||||
return ApiResponse<object>.ErrorResponse("Contact not found", "Contact not found", 404);
|
|
||||||
}
|
|
||||||
|
|
||||||
// Capture old state for audit logging
|
// Capture old state for audit logging
|
||||||
var oldObject = _updateLogsHelper.EntityToBsonDocument(note);
|
var oldObject = _updateLogsHelper.EntityToBsonDocument(note);
|
||||||
|
|
||||||
@ -2318,38 +2146,6 @@ namespace Marco.Pms.Services.Service
|
|||||||
loggedInEmployee.Id, id, currentTime);
|
loggedInEmployee.Id, id, currentTime);
|
||||||
}
|
}
|
||||||
|
|
||||||
using var scope = _serviceScopeFactory.CreateScope();
|
|
||||||
|
|
||||||
_ = Task.Run(async () =>
|
|
||||||
{
|
|
||||||
// --- Push Notification Section ---
|
|
||||||
// This section attempts to send a test push notification to the user's device.
|
|
||||||
// It's designed to fail gracefully and handle invalid Firebase Cloud Messaging (FCM) tokens.
|
|
||||||
var _firebase = scope.ServiceProvider.GetRequiredService<IFirebaseService>();
|
|
||||||
|
|
||||||
var name = $"{loggedInEmployee.FirstName} {loggedInEmployee.LastName}";
|
|
||||||
|
|
||||||
Notification notification;
|
|
||||||
if (active)
|
|
||||||
{
|
|
||||||
notification = new Notification
|
|
||||||
{
|
|
||||||
Title = $"Note restored at Contact - \"{contact.Name}\"",
|
|
||||||
Body = $"Note restored at Contact \"{contact.Name}\" by {name} in your bucket"
|
|
||||||
};
|
|
||||||
}
|
|
||||||
else
|
|
||||||
{
|
|
||||||
notification = new Notification
|
|
||||||
{
|
|
||||||
Title = $"Note deleted at Contact - \"{contact.Name}\"",
|
|
||||||
Body = $"Note deleted at Contact \"{contact.Name}\" by {name} in your bucket"
|
|
||||||
};
|
|
||||||
}
|
|
||||||
await _firebase.SendContactNoteAsync(bucketIds, notification, tenantId);
|
|
||||||
|
|
||||||
});
|
|
||||||
|
|
||||||
return ApiResponse<object>.SuccessResponse(new { },
|
return ApiResponse<object>.SuccessResponse(new { },
|
||||||
active ? "Note restored successfully" : "Note deleted successfully",
|
active ? "Note restored successfully" : "Note deleted successfully",
|
||||||
200);
|
200);
|
||||||
@ -2468,7 +2264,6 @@ namespace Marco.Pms.Services.Service
|
|||||||
|
|
||||||
_logger.LogInfo("Fetched {BucketCount} buckets for Employee {EmployeeId} successfully", bucketVMs.Count, loggedInEmployee.Id);
|
_logger.LogInfo("Fetched {BucketCount} buckets for Employee {EmployeeId} successfully", bucketVMs.Count, loggedInEmployee.Id);
|
||||||
|
|
||||||
|
|
||||||
return ApiResponse<object>.SuccessResponse(bucketVMs, $"{bucketVMs.Count} buckets fetched successfully", 200);
|
return ApiResponse<object>.SuccessResponse(bucketVMs, $"{bucketVMs.Count} buckets fetched successfully", 200);
|
||||||
}
|
}
|
||||||
public async Task<ApiResponse<object>> CreateBucketAsync(CreateBucketDto bucketDto, Guid tenantId, Employee loggedInEmployee)
|
public async Task<ApiResponse<object>> CreateBucketAsync(CreateBucketDto bucketDto, Guid tenantId, Employee loggedInEmployee)
|
||||||
@ -2542,27 +2337,6 @@ namespace Marco.Pms.Services.Service
|
|||||||
|
|
||||||
_logger.LogInfo("Employee {EmployeeId} successfully created bucket {BucketId}", loggedInEmployee.Id, newBucket.Id);
|
_logger.LogInfo("Employee {EmployeeId} successfully created bucket {BucketId}", loggedInEmployee.Id, newBucket.Id);
|
||||||
|
|
||||||
using var scope = _serviceScopeFactory.CreateScope();
|
|
||||||
|
|
||||||
_ = Task.Run(async () =>
|
|
||||||
{
|
|
||||||
// --- Push Notification Section ---
|
|
||||||
// This section attempts to send a test push notification to the user's device.
|
|
||||||
// It's designed to fail gracefully and handle invalid Firebase Cloud Messaging (FCM) tokens.
|
|
||||||
var _firebase = scope.ServiceProvider.GetRequiredService<IFirebaseService>();
|
|
||||||
|
|
||||||
var name = $"{loggedInEmployee.FirstName} {loggedInEmployee.LastName}";
|
|
||||||
|
|
||||||
var notification = new Notification
|
|
||||||
{
|
|
||||||
Title = "New Bucket created",
|
|
||||||
Body = $"New Bucket created \"{newBucket.Name}\" by {name}"
|
|
||||||
};
|
|
||||||
|
|
||||||
await _firebase.SendBucketAsync(newBucket.Id, notification, tenantId);
|
|
||||||
|
|
||||||
});
|
|
||||||
|
|
||||||
return ApiResponse<object>.SuccessResponse(bucketVM, "Bucket created successfully", 200);
|
return ApiResponse<object>.SuccessResponse(bucketVM, "Bucket created successfully", 200);
|
||||||
}
|
}
|
||||||
catch (Exception ex)
|
catch (Exception ex)
|
||||||
@ -2675,27 +2449,6 @@ namespace Marco.Pms.Services.Service
|
|||||||
|
|
||||||
_logger.LogInfo("Employee ID {LoggedInEmployeeId} successfully updated bucket ID {BucketId}.", loggedInEmployee.Id, bucket.Id);
|
_logger.LogInfo("Employee ID {LoggedInEmployeeId} successfully updated bucket ID {BucketId}.", loggedInEmployee.Id, bucket.Id);
|
||||||
|
|
||||||
using var scope = _serviceScopeFactory.CreateScope();
|
|
||||||
|
|
||||||
_ = Task.Run(async () =>
|
|
||||||
{
|
|
||||||
// --- Push Notification Section ---
|
|
||||||
// This section attempts to send a test push notification to the user's device.
|
|
||||||
// It's designed to fail gracefully and handle invalid Firebase Cloud Messaging (FCM) tokens.
|
|
||||||
var _firebase = scope.ServiceProvider.GetRequiredService<IFirebaseService>();
|
|
||||||
|
|
||||||
var name = $"{loggedInEmployee.FirstName} {loggedInEmployee.LastName}";
|
|
||||||
|
|
||||||
var notification = new Notification
|
|
||||||
{
|
|
||||||
Title = $"Bucket updated - \"{bucket.Name}\"",
|
|
||||||
Body = $"Bucket updated \"{bucket.Name}\" by {name}"
|
|
||||||
};
|
|
||||||
|
|
||||||
await _firebase.SendBucketAsync(bucket.Id, notification, tenantId);
|
|
||||||
|
|
||||||
});
|
|
||||||
|
|
||||||
return ApiResponse<object>.SuccessResponse(bucketVM, "Bucket updated successfully", 200);
|
return ApiResponse<object>.SuccessResponse(bucketVM, "Bucket updated successfully", 200);
|
||||||
}
|
}
|
||||||
catch (Exception ex)
|
catch (Exception ex)
|
||||||
@ -2766,9 +2519,6 @@ namespace Marco.Pms.Services.Service
|
|||||||
int assignedEmployeesCount = 0;
|
int assignedEmployeesCount = 0;
|
||||||
int removedEmployeesCount = 0;
|
int removedEmployeesCount = 0;
|
||||||
|
|
||||||
List<Guid> assignedEmployeeIds = new List<Guid>();
|
|
||||||
List<Guid> removedEmployeeIds = new List<Guid>();
|
|
||||||
|
|
||||||
// Process each assignment request
|
// Process each assignment request
|
||||||
foreach (var assignBucket in assignBuckets)
|
foreach (var assignBucket in assignBuckets)
|
||||||
{
|
{
|
||||||
@ -2791,7 +2541,6 @@ namespace Marco.Pms.Services.Service
|
|||||||
};
|
};
|
||||||
_context.EmployeeBucketMappings.Add(newMapping);
|
_context.EmployeeBucketMappings.Add(newMapping);
|
||||||
assignedEmployeesCount++;
|
assignedEmployeesCount++;
|
||||||
assignedEmployeeIds.Add(assignBucket.EmployeeId);
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
else
|
else
|
||||||
@ -2800,7 +2549,6 @@ namespace Marco.Pms.Services.Service
|
|||||||
var existingMapping = employeeBuckets.FirstOrDefault(eb => eb.EmployeeId == assignBucket.EmployeeId);
|
var existingMapping = employeeBuckets.FirstOrDefault(eb => eb.EmployeeId == assignBucket.EmployeeId);
|
||||||
if (existingMapping != null && bucket.CreatedByID != assignBucket.EmployeeId)
|
if (existingMapping != null && bucket.CreatedByID != assignBucket.EmployeeId)
|
||||||
{
|
{
|
||||||
removedEmployeeIds.Add(existingMapping.EmployeeId);
|
|
||||||
_context.EmployeeBucketMappings.Remove(existingMapping);
|
_context.EmployeeBucketMappings.Remove(existingMapping);
|
||||||
removedEmployeesCount++;
|
removedEmployeesCount++;
|
||||||
}
|
}
|
||||||
@ -2859,41 +2607,7 @@ namespace Marco.Pms.Services.Service
|
|||||||
{
|
{
|
||||||
_logger.LogInfo("Employee {EmployeeId} removed {Count} employees from bucket {BucketId}.", loggedInEmployee.Id, removedEmployeesCount, bucketId);
|
_logger.LogInfo("Employee {EmployeeId} removed {Count} employees from bucket {BucketId}.", loggedInEmployee.Id, removedEmployeesCount, bucketId);
|
||||||
}
|
}
|
||||||
using var scope = _serviceScopeFactory.CreateScope();
|
|
||||||
|
|
||||||
_ = Task.Run(async () =>
|
|
||||||
{
|
|
||||||
// --- Push Notification Section ---
|
|
||||||
// This section attempts to send a test push notification to the user's device.
|
|
||||||
// It's designed to fail gracefully and handle invalid Firebase Cloud Messaging (FCM) tokens.
|
|
||||||
var _firebase = scope.ServiceProvider.GetRequiredService<IFirebaseService>();
|
|
||||||
|
|
||||||
var name = $"{loggedInEmployee.FirstName} {loggedInEmployee.LastName}";
|
|
||||||
|
|
||||||
if (assignedEmployeeIds.Any())
|
|
||||||
{
|
|
||||||
|
|
||||||
var notification = new Notification
|
|
||||||
{
|
|
||||||
Title = "You have assigned to Bucket",
|
|
||||||
Body = $"You have assigned to bucket \"{bucket.Name}\" by {name}"
|
|
||||||
};
|
|
||||||
|
|
||||||
await _firebase.SendAssignBucketAsync(assignedEmployeeIds, notification, tenantId);
|
|
||||||
}
|
|
||||||
if (removedEmployeeIds.Any())
|
|
||||||
{
|
|
||||||
|
|
||||||
var notification = new Notification
|
|
||||||
{
|
|
||||||
Title = "You have removed from Bucket",
|
|
||||||
Body = $"You have removed from bucket \"{bucket.Name}\" by {name}"
|
|
||||||
};
|
|
||||||
|
|
||||||
await _firebase.SendAssignBucketAsync(removedEmployeeIds, notification, tenantId);
|
|
||||||
}
|
|
||||||
|
|
||||||
});
|
|
||||||
return ApiResponse<object>.SuccessResponse(bucketVm, "Bucket details updated successfully", 200);
|
return ApiResponse<object>.SuccessResponse(bucketVm, "Bucket details updated successfully", 200);
|
||||||
}
|
}
|
||||||
public async Task<ApiResponse<object>> DeleteBucketAsync(Guid id, Guid tenantId, Employee loggedInEmployee)
|
public async Task<ApiResponse<object>> DeleteBucketAsync(Guid id, Guid tenantId, Employee loggedInEmployee)
|
||||||
@ -3006,27 +2720,6 @@ namespace Marco.Pms.Services.Service
|
|||||||
UpdatedAt = DateTime.UtcNow
|
UpdatedAt = DateTime.UtcNow
|
||||||
}, bucketCollection);
|
}, bucketCollection);
|
||||||
|
|
||||||
using var scope = _serviceScopeFactory.CreateScope();
|
|
||||||
|
|
||||||
_ = Task.Run(async () =>
|
|
||||||
{
|
|
||||||
// --- Push Notification Section ---
|
|
||||||
// This section attempts to send a test push notification to the user's device.
|
|
||||||
// It's designed to fail gracefully and handle invalid Firebase Cloud Messaging (FCM) tokens.
|
|
||||||
var _firebase = scope.ServiceProvider.GetRequiredService<IFirebaseService>();
|
|
||||||
|
|
||||||
var name = $"{loggedInEmployee.FirstName} {loggedInEmployee.LastName}";
|
|
||||||
|
|
||||||
var notification = new Notification
|
|
||||||
{
|
|
||||||
Title = $"Bucket deleted - \"{bucket.Name}\"",
|
|
||||||
Body = $"Bucket deleted \"{bucket.Name}\" by {name}"
|
|
||||||
};
|
|
||||||
|
|
||||||
await _firebase.SendBucketAsync(bucket.Id, notification, tenantId);
|
|
||||||
|
|
||||||
});
|
|
||||||
|
|
||||||
return ApiResponse<object>.SuccessResponse(new { }, "Bucket deleted successfully", 200);
|
return ApiResponse<object>.SuccessResponse(new { }, "Bucket deleted successfully", 200);
|
||||||
}
|
}
|
||||||
catch (Exception ex)
|
catch (Exception ex)
|
||||||
|
|||||||
@ -31,7 +31,6 @@ namespace Marco.Pms.Services.Service
|
|||||||
_serviceScopeFactory = serviceScopeFactory ?? throw new ArgumentNullException(nameof(serviceScopeFactory));
|
_serviceScopeFactory = serviceScopeFactory ?? throw new ArgumentNullException(nameof(serviceScopeFactory));
|
||||||
}
|
}
|
||||||
|
|
||||||
// Auth Controller
|
|
||||||
public async Task SendLoginOnAnotherDeviceMessageAsync(Guid employeeId, string fcmToken, Guid tenentId)
|
public async Task SendLoginOnAnotherDeviceMessageAsync(Guid employeeId, string fcmToken, Guid tenentId)
|
||||||
{
|
{
|
||||||
await using var _context = await _dbContextFactory.CreateDbContextAsync();
|
await using var _context = await _dbContextFactory.CreateDbContextAsync();
|
||||||
@ -1623,224 +1622,6 @@ namespace Marco.Pms.Services.Service
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// Directory Controller
|
|
||||||
public async Task SendContactAsync(List<Guid> bucketIds, Notification notification, Guid tenantId)
|
|
||||||
{
|
|
||||||
await using var _context = await _dbContextFactory.CreateDbContextAsync();
|
|
||||||
using var scope = _serviceScopeFactory.CreateScope();
|
|
||||||
var _logger = scope.ServiceProvider.GetRequiredService<ILoggingService>();
|
|
||||||
|
|
||||||
var assignedEmployeeIdsTask = Task.Run(async () =>
|
|
||||||
{
|
|
||||||
await using var _context = await _dbContextFactory.CreateDbContextAsync();
|
|
||||||
return await _context.EmployeeBucketMappings.Where(eb => bucketIds.Contains(eb.BucketId)).Select(eb => eb.EmployeeId).ToListAsync();
|
|
||||||
});
|
|
||||||
var directoryAdminEmployeeIdsTask = Task.Run(async () =>
|
|
||||||
{
|
|
||||||
await using var dbContext = await _dbContextFactory.CreateDbContextAsync();
|
|
||||||
var roleIds = await dbContext.RolePermissionMappings
|
|
||||||
.Where(rp => rp.FeaturePermissionId == PermissionsMaster.DirectoryAdmin)
|
|
||||||
.Select(rp => rp.ApplicationRoleId).ToListAsync();
|
|
||||||
var employeeIds = await dbContext.EmployeeRoleMappings
|
|
||||||
.Where(er =>
|
|
||||||
roleIds.Contains(er.RoleId))
|
|
||||||
.Select(er => er.EmployeeId)
|
|
||||||
.ToListAsync();
|
|
||||||
return employeeIds;
|
|
||||||
});
|
|
||||||
|
|
||||||
await Task.WhenAll(assignedEmployeeIdsTask, directoryAdminEmployeeIdsTask);
|
|
||||||
|
|
||||||
var assignedEmployeeIds = assignedEmployeeIdsTask.Result;
|
|
||||||
var directoryAdminEmployeeIds = directoryAdminEmployeeIdsTask.Result;
|
|
||||||
|
|
||||||
assignedEmployeeIds.AddRange(directoryAdminEmployeeIds);
|
|
||||||
|
|
||||||
assignedEmployeeIds = assignedEmployeeIds.Distinct().ToList();
|
|
||||||
|
|
||||||
var data = new Dictionary<string, string>()
|
|
||||||
{
|
|
||||||
{ "Keyword", "Contact_Modefied" }
|
|
||||||
};
|
|
||||||
|
|
||||||
var registrationTokensForNotification = await _context.FCMTokenMappings.Where(ft => assignedEmployeeIds.Contains(ft.EmployeeId) && ft.ExpiredAt >= DateTime.UtcNow).Select(ft => ft.FcmToken).ToListAsync();
|
|
||||||
|
|
||||||
await SendMessageToMultipleDevicesWithDataAsync(registrationTokensForNotification, notification, data);
|
|
||||||
}
|
|
||||||
public async Task SendContactNoteAsync(List<Guid> bucketIds, Notification notification, Guid tenantId)
|
|
||||||
{
|
|
||||||
await using var _context = await _dbContextFactory.CreateDbContextAsync();
|
|
||||||
using var scope = _serviceScopeFactory.CreateScope();
|
|
||||||
var _logger = scope.ServiceProvider.GetRequiredService<ILoggingService>();
|
|
||||||
|
|
||||||
var assignedEmployeeIdsTask = Task.Run(async () =>
|
|
||||||
{
|
|
||||||
await using var _context = await _dbContextFactory.CreateDbContextAsync();
|
|
||||||
return await _context.EmployeeBucketMappings.Where(eb => bucketIds.Contains(eb.BucketId)).Select(eb => eb.EmployeeId).ToListAsync();
|
|
||||||
});
|
|
||||||
var directoryAdminEmployeeIdsTask = Task.Run(async () =>
|
|
||||||
{
|
|
||||||
await using var dbContext = await _dbContextFactory.CreateDbContextAsync();
|
|
||||||
var roleIds = await dbContext.RolePermissionMappings
|
|
||||||
.Where(rp => rp.FeaturePermissionId == PermissionsMaster.DirectoryAdmin)
|
|
||||||
.Select(rp => rp.ApplicationRoleId).ToListAsync();
|
|
||||||
var employeeIds = await dbContext.EmployeeRoleMappings
|
|
||||||
.Where(er =>
|
|
||||||
roleIds.Contains(er.RoleId))
|
|
||||||
.Select(er => er.EmployeeId)
|
|
||||||
.ToListAsync();
|
|
||||||
return employeeIds;
|
|
||||||
});
|
|
||||||
|
|
||||||
await Task.WhenAll(assignedEmployeeIdsTask, directoryAdminEmployeeIdsTask);
|
|
||||||
|
|
||||||
var assignedEmployeeIds = assignedEmployeeIdsTask.Result;
|
|
||||||
var directoryAdminEmployeeIds = directoryAdminEmployeeIdsTask.Result;
|
|
||||||
|
|
||||||
assignedEmployeeIds.AddRange(directoryAdminEmployeeIds);
|
|
||||||
|
|
||||||
assignedEmployeeIds = assignedEmployeeIds.Distinct().ToList();
|
|
||||||
|
|
||||||
var data = new Dictionary<string, string>()
|
|
||||||
{
|
|
||||||
{ "Keyword", "Contact_Note_Modefied" }
|
|
||||||
};
|
|
||||||
|
|
||||||
var registrationTokensForNotification = await _context.FCMTokenMappings.Where(ft => assignedEmployeeIds.Contains(ft.EmployeeId) && ft.ExpiredAt >= DateTime.UtcNow).Select(ft => ft.FcmToken).ToListAsync();
|
|
||||||
|
|
||||||
await SendMessageToMultipleDevicesWithDataAsync(registrationTokensForNotification, notification, data);
|
|
||||||
}
|
|
||||||
public async Task SendBucketAsync(Guid bucketId, Notification notification, Guid tenantId)
|
|
||||||
{
|
|
||||||
await using var _context = await _dbContextFactory.CreateDbContextAsync();
|
|
||||||
using var scope = _serviceScopeFactory.CreateScope();
|
|
||||||
var _logger = scope.ServiceProvider.GetRequiredService<ILoggingService>();
|
|
||||||
|
|
||||||
var assignedEmployeeIdsTask = Task.Run(async () =>
|
|
||||||
{
|
|
||||||
await using var _context = await _dbContextFactory.CreateDbContextAsync();
|
|
||||||
return await _context.EmployeeBucketMappings.Where(eb => bucketId == eb.BucketId).Select(eb => eb.EmployeeId).ToListAsync();
|
|
||||||
});
|
|
||||||
var directoryAdminEmployeeIdsTask = Task.Run(async () =>
|
|
||||||
{
|
|
||||||
await using var dbContext = await _dbContextFactory.CreateDbContextAsync();
|
|
||||||
var roleIds = await dbContext.RolePermissionMappings
|
|
||||||
.Where(rp => rp.FeaturePermissionId == PermissionsMaster.DirectoryAdmin)
|
|
||||||
.Select(rp => rp.ApplicationRoleId).ToListAsync();
|
|
||||||
var employeeIds = await dbContext.EmployeeRoleMappings
|
|
||||||
.Where(er =>
|
|
||||||
roleIds.Contains(er.RoleId))
|
|
||||||
.Select(er => er.EmployeeId)
|
|
||||||
.ToListAsync();
|
|
||||||
return employeeIds;
|
|
||||||
});
|
|
||||||
|
|
||||||
await Task.WhenAll(assignedEmployeeIdsTask, directoryAdminEmployeeIdsTask);
|
|
||||||
|
|
||||||
var assignedEmployeeIds = assignedEmployeeIdsTask.Result;
|
|
||||||
var directoryAdminEmployeeIds = directoryAdminEmployeeIdsTask.Result;
|
|
||||||
|
|
||||||
assignedEmployeeIds.AddRange(directoryAdminEmployeeIds);
|
|
||||||
|
|
||||||
assignedEmployeeIds = assignedEmployeeIds.Distinct().ToList();
|
|
||||||
|
|
||||||
var data = new Dictionary<string, string>()
|
|
||||||
{
|
|
||||||
{ "Keyword", "Bucket_Modefied" }
|
|
||||||
};
|
|
||||||
|
|
||||||
var registrationTokensForNotification = await _context.FCMTokenMappings.Where(ft => assignedEmployeeIds.Contains(ft.EmployeeId) && ft.ExpiredAt >= DateTime.UtcNow).Select(ft => ft.FcmToken).ToListAsync();
|
|
||||||
|
|
||||||
await SendMessageToMultipleDevicesWithDataAsync(registrationTokensForNotification, notification, data);
|
|
||||||
}
|
|
||||||
public async Task SendAssignBucketAsync(List<Guid> employeeIds, Notification notification, Guid tenantId)
|
|
||||||
{
|
|
||||||
await using var _context = await _dbContextFactory.CreateDbContextAsync();
|
|
||||||
using var scope = _serviceScopeFactory.CreateScope();
|
|
||||||
var _logger = scope.ServiceProvider.GetRequiredService<ILoggingService>();
|
|
||||||
|
|
||||||
var data = new Dictionary<string, string>()
|
|
||||||
{
|
|
||||||
{ "Keyword", "Bucket_Assigned" }
|
|
||||||
};
|
|
||||||
|
|
||||||
var registrationTokensForNotification = await _context.FCMTokenMappings.Where(ft => employeeIds.Contains(ft.EmployeeId) && ft.ExpiredAt >= DateTime.UtcNow).Select(ft => ft.FcmToken).ToListAsync();
|
|
||||||
|
|
||||||
await SendMessageToMultipleDevicesWithDataAsync(registrationTokensForNotification, notification, data);
|
|
||||||
}
|
|
||||||
|
|
||||||
//Document Controller
|
|
||||||
public async Task SendEmployeeDocumentMessageAsync(Guid employeeId, Notification notification, Guid tenantId)
|
|
||||||
{
|
|
||||||
await using var _context = await _dbContextFactory.CreateDbContextAsync();
|
|
||||||
using var scope = _serviceScopeFactory.CreateScope();
|
|
||||||
|
|
||||||
var data = new Dictionary<string, string>()
|
|
||||||
{
|
|
||||||
{ "Keyword", "Employee_Document_Modefied" }
|
|
||||||
};
|
|
||||||
|
|
||||||
var registrationTokensForNotification = await _context.FCMTokenMappings.Where(ft => ft.EmployeeId == employeeId && ft.ExpiredAt >= DateTime.UtcNow).Select(ft => ft.FcmToken).ToListAsync();
|
|
||||||
|
|
||||||
await SendMessageToMultipleDevicesWithDataAsync(registrationTokensForNotification, notification, data);
|
|
||||||
}
|
|
||||||
public async Task SendProjectDocumentMessageAsync(Guid projectId, Notification notification, Guid tenantId)
|
|
||||||
{
|
|
||||||
await using var _context = await _dbContextFactory.CreateDbContextAsync();
|
|
||||||
using var scope = _serviceScopeFactory.CreateScope();
|
|
||||||
|
|
||||||
var assignedEmployeeIdsTask = Task.Run(async () =>
|
|
||||||
{
|
|
||||||
await using var _context = await _dbContextFactory.CreateDbContextAsync();
|
|
||||||
return await _context.ProjectAllocations.Where(pa => projectId == pa.ProjectId).Select(pa => pa.EmployeeId).ToListAsync();
|
|
||||||
});
|
|
||||||
var manageProjectEmployeeIdsTask = Task.Run(async () =>
|
|
||||||
{
|
|
||||||
await using var dbContext = await _dbContextFactory.CreateDbContextAsync();
|
|
||||||
var roleIds = await dbContext.RolePermissionMappings
|
|
||||||
.Where(rp => rp.FeaturePermissionId == PermissionsMaster.ManageProject)
|
|
||||||
.Select(rp => rp.ApplicationRoleId).ToListAsync();
|
|
||||||
var employeeIds = await dbContext.EmployeeRoleMappings
|
|
||||||
.Where(er =>
|
|
||||||
roleIds.Contains(er.RoleId))
|
|
||||||
.Select(er => er.EmployeeId)
|
|
||||||
.ToListAsync();
|
|
||||||
return employeeIds;
|
|
||||||
});
|
|
||||||
var viewDocumentEmployeeIdsTask = Task.Run(async () =>
|
|
||||||
{
|
|
||||||
await using var dbContext = await _dbContextFactory.CreateDbContextAsync();
|
|
||||||
var roleIds = await dbContext.RolePermissionMappings
|
|
||||||
.Where(rp => rp.FeaturePermissionId == PermissionsMaster.ViewDocument)
|
|
||||||
.Select(rp => rp.ApplicationRoleId).ToListAsync();
|
|
||||||
var employeeIds = await dbContext.EmployeeRoleMappings
|
|
||||||
.Where(er =>
|
|
||||||
roleIds.Contains(er.RoleId))
|
|
||||||
.Select(er => er.EmployeeId)
|
|
||||||
.ToListAsync();
|
|
||||||
return employeeIds;
|
|
||||||
});
|
|
||||||
|
|
||||||
await Task.WhenAll(assignedEmployeeIdsTask, manageProjectEmployeeIdsTask, viewDocumentEmployeeIdsTask);
|
|
||||||
|
|
||||||
var assignedEmployeeIds = assignedEmployeeIdsTask.Result;
|
|
||||||
var manageProjectEmployeeIds = manageProjectEmployeeIdsTask.Result;
|
|
||||||
var viewDocumentEmployeeIds = viewDocumentEmployeeIdsTask.Result;
|
|
||||||
|
|
||||||
assignedEmployeeIds.AddRange(manageProjectEmployeeIds);
|
|
||||||
|
|
||||||
var finalEmployeeIds = assignedEmployeeIds.Intersect(viewDocumentEmployeeIds).ToList();
|
|
||||||
|
|
||||||
var data = new Dictionary<string, string>()
|
|
||||||
{
|
|
||||||
{ "Keyword", "Project_Document_Modefied" }
|
|
||||||
};
|
|
||||||
|
|
||||||
var registrationTokensForNotification = await _context.FCMTokenMappings.Where(ft => finalEmployeeIds.Contains(ft.EmployeeId) && ft.ExpiredAt >= DateTime.UtcNow).Select(ft => ft.FcmToken).ToListAsync();
|
|
||||||
|
|
||||||
await SendMessageToMultipleDevicesWithDataAsync(registrationTokensForNotification, notification, data);
|
|
||||||
}
|
|
||||||
|
|
||||||
#region =================================================================== Helper Functions ===================================================================
|
#region =================================================================== Helper Functions ===================================================================
|
||||||
|
|
||||||
public async Task SendMessageToMultipleDevicesWithDataAsync(List<string> registrationTokens, Notification notificationFirebase, Dictionary<string, string> data)
|
public async Task SendMessageToMultipleDevicesWithDataAsync(List<string> registrationTokens, Notification notificationFirebase, Dictionary<string, string> data)
|
||||||
|
|||||||
@ -1,5 +1,4 @@
|
|||||||
using FirebaseAdmin.Messaging;
|
using Marco.Pms.Model.Dtos.Attendance;
|
||||||
using Marco.Pms.Model.Dtos.Attendance;
|
|
||||||
using Marco.Pms.Model.Expenses;
|
using Marco.Pms.Model.Expenses;
|
||||||
using Marco.Pms.Model.Projects;
|
using Marco.Pms.Model.Projects;
|
||||||
|
|
||||||
@ -23,13 +22,5 @@ namespace Marco.Pms.Services.Service.ServiceInterfaces
|
|||||||
Task SendProjectAllocationMessageAsync(List<ProjectAllocation> projectAllocations, string name, Guid tenantId);
|
Task SendProjectAllocationMessageAsync(List<ProjectAllocation> projectAllocations, string name, Guid tenantId);
|
||||||
Task SendModifyProjectMessageAsync(Project project, string name, bool IsExist, Guid tenantId);
|
Task SendModifyProjectMessageAsync(Project project, string name, bool IsExist, Guid tenantId);
|
||||||
Task SendExpenseMessageAsync(Expenses expenses, string name, Guid tenantId);
|
Task SendExpenseMessageAsync(Expenses expenses, string name, Guid tenantId);
|
||||||
|
|
||||||
Task SendContactAsync(List<Guid> bucketIds, Notification notification, Guid tenantId);
|
|
||||||
Task SendContactNoteAsync(List<Guid> bucketIds, Notification notification, Guid tenantId);
|
|
||||||
Task SendBucketAsync(Guid bucketId, Notification notification, Guid tenantId);
|
|
||||||
Task SendAssignBucketAsync(List<Guid> employeeIds, Notification notification, Guid tenantId);
|
|
||||||
|
|
||||||
Task SendEmployeeDocumentMessageAsync(Guid employeeId, Notification notification, Guid tenantId);
|
|
||||||
Task SendProjectDocumentMessageAsync(Guid projectId, Notification notification, Guid tenantId);
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user