1- using Resgrid . Model ;
1+ using System ;
2+ using Resgrid . Model ;
23using Resgrid . Model . Repositories ;
34using Resgrid . Model . Services ;
45using System . Collections . Generic ;
56using System . Linq ;
67using System . Threading ;
78using System . Threading . Tasks ;
9+ using Resgrid . Framework ;
10+ using Resgrid . Model . Events ;
11+ using Resgrid . Model . Providers ;
812
913namespace Resgrid . Services
1014{
@@ -15,20 +19,23 @@ public class ContactsService : IContactsService
1519 private readonly IContactNotesRepository _contactNotesRepository ;
1620 private readonly IContactNoteTypesRepository _contactNoteTypesRepository ;
1721 private readonly IContactAssociationsRepository _contactAssociationsRepository ;
22+ private readonly IEventAggregator _eventAggregator ;
1823
1924 public ContactsService ( IContactsRepository contactsRepository , IContactNotesRepository contactNotesRepository ,
20- IContactCategoryRepository contactCategoryRepository ,
21- IContactNoteTypesRepository contactNoteTypesRepository , IContactAssociationsRepository contactAssociationsRepository )
25+ IContactCategoryRepository contactCategoryRepository , IContactNoteTypesRepository contactNoteTypesRepository ,
26+ IContactAssociationsRepository contactAssociationsRepository , IEventAggregator eventAggregator )
2227 {
2328 _contactsRepository = contactsRepository ;
2429 _contactCategoryRepository = contactCategoryRepository ;
2530 _contactNotesRepository = contactNotesRepository ;
2631 _contactNoteTypesRepository = contactNoteTypesRepository ;
2732 _contactAssociationsRepository = contactAssociationsRepository ;
33+ _eventAggregator = eventAggregator ;
2834 }
2935
3036 public async Task < List < Contact > > GetAllContactsForDepartmentAsync ( int departmentId )
3137 {
38+ var contactsResult = new List < Contact > ( ) ;
3239 var contacts = await _contactsRepository . GetAllByDepartmentIdAsync ( departmentId ) ;
3340 var categories = await _contactCategoryRepository . GetAllByDepartmentIdAsync ( departmentId ) ;
3441
@@ -37,11 +44,16 @@ public async Task<List<Contact>> GetAllContactsForDepartmentAsync(int department
3744
3845 foreach ( var contact in contacts )
3946 {
47+ if ( contact . IsDeleted )
48+ continue ;
49+
4050 if ( categories != null && categories . Any ( ) )
4151 contact . Category = categories . FirstOrDefault ( x => x . ContactCategoryId == contact . ContactCategoryId ) ;
52+
53+ contactsResult . Add ( contact ) ;
4254 }
4355
44- return contacts . ToList ( ) ;
56+ return contactsResult ;
4557 }
4658
4759 public async Task < List < ContactCategory > > GetContactCategoriesForDepartmentAsync ( int departmentId )
@@ -156,5 +168,30 @@ public async Task<bool> DoesContactNoteTypeAlreadyExistAsync(int departmentId, s
156168 return await _contactNotesRepository . SaveOrUpdateAsync ( note , cancellationToken ) ;
157169 }
158170
171+ public async Task < bool > DeleteContactAsync ( string contactId , string userId , int departmentId , string ipAddress , string userAgent , CancellationToken cancellationToken = default ( CancellationToken ) )
172+ {
173+ var auditEvent = new AuditEvent ( ) ;
174+ auditEvent . DepartmentId = departmentId ;
175+ auditEvent . UserId = userId ;
176+ auditEvent . Type = AuditLogTypes . ContactRemoved ;
177+ auditEvent . Successful = true ;
178+ auditEvent . IpAddress = ipAddress ;
179+ auditEvent . ServerName = Environment . MachineName ;
180+ auditEvent . UserAgent = userAgent ;
181+
182+ var contact = await _contactsRepository . GetByIdAsync ( contactId ) ;
183+ auditEvent . Before = contact . CloneJsonToString ( ) ;
184+
185+ contact . IsDeleted = true ;
186+ contact . EditedByUserId = userId ;
187+ contact . EditedOn = DateTime . UtcNow ;
188+
189+ await SaveContactAsync ( contact , cancellationToken ) ;
190+
191+ auditEvent . After = contact . CloneJsonToString ( ) ;
192+ _eventAggregator . SendMessage < AuditEvent > ( auditEvent ) ;
193+
194+ return true ;
195+ }
159196 }
160197}
0 commit comments