using System.Collections.Generic; using System.Linq; using System.Threading; using System.Threading.Tasks; using Microsoft.AspNetCore.SignalR; using xCommons.Configurations; using xCommons.Extensions; using xExceptions.Constants; using xIdentityModels.Extensions; using xIdentityModels.Models; using xPushService.Constants; using xStringService.Interfaces.Dtos; using xStringService.Models.Dtos; using xTermsAndConditionsService.Hubs; using xTermsAndConditionsService.Interfaces; namespace xTermsAndConditionsService.Providers { /// /// an Implementation of Terms and Conditions Management Provided Actions ... /// public class XTermsAndComditionsProvider : IXTermsAndComditionsProvider { // #region Props ... private readonly string termsId; private readonly string[] allowedRoles; private readonly IHubContext hub; private readonly XAppConfiguration appConfiguration; private readonly IXStringServiceProvider stringProvider; #endregion // #region Constructor ... public XTermsAndComditionsProvider( XAppConfiguration appConfiguration, IXStringServiceProvider stringProvider, IHubContext hub = null, string termsId = "terms" ) { // this.hub = hub; this.termsId = termsId; this.stringProvider = stringProvider; this.appConfiguration = appConfiguration; // // Specified roles for Handling Dangerouse Actions ... this.allowedRoles = new string[] { "admin" }; } #endregion // #region Actions ... /// /// Retrieved all Exists Languages Terms and Conditions ... /// /// /// public async Task> TermsLanguages( CancellationToken cancellationToken = default ) { // var result = (await stringProvider .FindManyAsync( includeBuilder: null, ignoreSoftDeleteds: true, cancellationToken: cancellationToken, predicate: x => x.ResourceTitle == termsId, orderBuilder: x => x.OrderBy(y => y.ResourceTitle) )) .Select(x => x.Language); return result; } /// /// Check Has Terms and Conditions based on Specified Language ... /// /// if null, used Default Language ... /// /// public async Task HasTerms( string language = null, CancellationToken cancellationToken = default ) { // // Normalize ... if (language.IsNullOrEmpty()) { language = appConfiguration.DefaultLanguage; } // var result = !(await GetString( language: language, cancellationToken: cancellationToken )) .IsNullOrDefault(); // return result; } /// /// Retrieve Terms and Conditions for Specified Language ... /// /// if null, used Default Language ... /// /// public async Task GetTerms( string language = null, CancellationToken cancellationToken = default ) { // var result = string.Empty; var item = await GetString( language: language, cancellationToken: cancellationToken ); // if (!item.IsNullOrDefault()) { result = item.TranslatedValue; } // return result; } /// /// Add Terms and Conditions for Specified Language ... /// /// /// /// /// /// /// public async Task AddTerms( string language, string terms, XUserClaimsInfoDto userInfo = null, string connectionId = null, CancellationToken cancellationToken = default ) { // // Validate ... var isValid = !language.IsNullOrEmpty() && !terms.IsNullOrEmpty() && !userInfo.IsNullOrDefault() && language != appConfiguration.DefaultLanguage; if (!isValid) { XException.InvalidArgs.Throw(); } // // Check Exists ... isValid = await HasTerms( language: language, cancellationToken: cancellationToken ); if (isValid) { XException.Duplicate.Throw(); } // // Check Permission ... isValid = userInfo.HasAccess(allowedRoles); if (!isValid) { XException.NotAllowed.Throw(); } // // Add Terms ... XStringDto item = null; try { // item = new XStringDto { Language = language, ResourceTitle = termsId, TranslatedValue = terms, }; item = await stringProvider.AddAsync( item: item, saveChanges: true, cancellationToken: cancellationToken ); } catch { } // var result = string.Empty; if (!item.IsNullOrDefault()) { result = item.TranslatedValue; } // if (!result.IsNullOrEmpty()) { // await SendPush( connectionId: connectionId, cancellationToken: cancellationToken, payLoad: item.ToJSON(camelCase: true), action: XBaseEntityHubAction.Add.GetStringValue() ); } // return result; } /// /// Remove Terms and Conditions of Specified Language ... /// /// /// /// /// /// /// public async Task RemoveTerms( string language, XUserClaimsInfoDto userInfo = null, string connectionId = null, CancellationToken cancellationToken = default ) { // var isValid = !language.IsNullOrEmpty() && !userInfo.IsNullOrDefault() && language != appConfiguration.DefaultLanguage; if (!isValid) { XException.InvalidArgs.Throw(); } // // Check Exists ... var item = await GetString( language: language, cancellationToken: cancellationToken ); isValid = !item.IsNullOrDefault(); if (!isValid) { XException.NotFound.Throw(); } // // Check Permission ... isValid = userInfo.HasAccess(allowedRoles); if (!isValid) { XException.NotAllowed.Throw(); } // try { // item = await stringProvider.RemoveAsync( id: item.Id, softDelete: false, saveChanges: true, cancellationToken: cancellationToken ); } catch { item = null; } // var result = !item.IsNullOrDefault(); if (result) { // await SendPush( connectionId: connectionId, cancellationToken: cancellationToken, payLoad: item.ToJSON(camelCase: true), action: XBaseEntityHubAction.Delete.GetStringValue() ); } // return result; } /// /// Update Terms and Conditions of Specified Language ... /// /// /// /// /// /// /// public async Task UpdateTerms( string language, string terms, XUserClaimsInfoDto userInfo = null, string connectionId = null, CancellationToken cancellationToken = default ) { // var isValid = !terms.IsNullOrEmpty() && !language.IsNullOrEmpty() && !userInfo.IsNullOrDefault(); if (!isValid) { XException.InvalidArgs.Throw(); } // // Check Exists ... var item = await GetString( language: language, cancellationToken: cancellationToken ); isValid = !item.IsNullOrDefault(); if (!isValid) { XException.NotFound.Throw(); } // // Check Permission ... isValid = userInfo.HasAccess(allowedRoles); if (!isValid) { XException.NotAllowed.Throw(); } // try { // item.TranslatedValue = terms; item = await stringProvider.UpdateAsync( id: item.Id, item: item, saveChanges: true, cancellationToken: cancellationToken ); } catch { item = null; } // var result = !item.IsNullOrDefault(); if (result) { // await SendPush( connectionId: connectionId, cancellationToken: cancellationToken, payLoad: item.ToJSON(camelCase: true), action: XBaseEntityHubAction.Update.GetStringValue() ); } // return result; } #endregion // #region Hub Actions ... /// /// Send Custom Push Message ... /// /// /// /// /// /// public async Task SendPush( string action, string payLoad, string connectionId = null, CancellationToken cancellationToken = default ) { // var actions = new List { XBaseEntityHubAction.Add.GetStringValue(), XBaseEntityHubAction.Update.GetStringValue(), XBaseEntityHubAction.Delete.GetStringValue(), }; // // Validate ... var isValid = !hub.IsNull() && !action.IsNullOrEmpty() && actions.Contains(action); if (!isValid) { return; } // var clients = hub.Clients.All; if (!connectionId.IsNullOrEmpty()) { clients = hub.Clients.AllExcept(connectionId); } try { // await clients.SendAsync( action, payLoad, connectionId, cancellationToken ); } catch { } } #endregion // #region Private ... /// /// Retrieve Terms and Conditions XString Entity ... /// /// /// /// private async Task GetString( string language = null, CancellationToken cancellationToken = default ) { // // Normalize ... if (language.IsNullOrEmpty()) { language = appConfiguration.DefaultLanguage; } // // Retrieve ... XStringDto result = null; try { // result = await stringProvider.FindOneAsync( includeBuilder: null, ignoreSoftDeleteds: true, predicate: x => x.Language == language && x.ResourceTitle == termsId, cancellationToken: cancellationToken ); } catch { } // return result; } #endregion } }