Created
January 25, 2021 05:13
-
-
Save mzekrallah/4ce7d3991b0aee41634f6af4763c6e2f to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public static ConsultationUserExtendInfo GetUserExtendInfo(ConsultationSessionStateDto consultation, List<ConsultationPreviousStatesDto> previousStates) | |
{ | |
ConsultationUserExtendInfo userExtendInfo = new ConsultationUserExtendInfo() | |
{ | |
IsAllowed = true, | |
//ExtendDuration = 5 // Minutes Duration By Default | |
}; | |
try | |
{ | |
// if the user has extended once before or the doctor has extended once before, then reject extend | |
var consultType = consultation.Type.ToEnumOrDefault<ConsultationType>(ConsultationType.Unknown); | |
var consultLastState = consultation.LastState.ToEnum<ConsultationState>(); | |
if (consultType == ConsultationType.CuraAnyTimeConsult || | |
consultType == ConsultationType.BundleConsult || | |
consultType == ConsultationType.AdhocSupportConsult || | |
consultType == ConsultationType.AdhocConsult || | |
consultType == ConsultationType.SupportClinicConsult || | |
consultType == ConsultationType.TelesalesConsult || | |
consultType == ConsultationType.Unknown) | |
{ | |
userExtendInfo.IsAllowed = false; | |
userExtendInfo.Reason = Resources.SessionExtensionErrorInvalidType; | |
return userExtendInfo; | |
} | |
// if in an invalid state, can't extend | |
if (consultLastState != ConsultationState.SessionEndedByUser && | |
consultLastState != ConsultationState.SessionEndedByDoctor && | |
consultLastState != ConsultationState.SessionTimerEnded) | |
{ | |
// only allow followup if it hasn't been consumed before (no additional paid followups anymore) | |
userExtendInfo.IsAllowed = false; | |
userExtendInfo.Reason = Resources.SessionExtensionErrorInvalidState; | |
return userExtendInfo; | |
} | |
//if already extended by the user once before, then reject | |
if (previousStates?.Count(q => q.LastState == ConsultationState.SessionExtendedForTokenByUserRequest.ToFriendlyString()) > 0) | |
{ | |
userExtendInfo.IsAllowed = false; | |
userExtendInfo.Reason = Resources.SessionExtensionErrorAlreadyExtended; | |
return userExtendInfo; | |
} | |
//if already extended by the doctor once before, then reject | |
if (previousStates?.Count(q => q.LastState == ConsultationState.SessionExtendedForFreeByDoctorRequest.ToFriendlyString()) | |
// || q.LastState == ConsultationState.SessionExtendedForTokenByDoctorRequest.ToFriendlyString()) | |
> 0) | |
{ | |
userExtendInfo.IsAllowed = false; | |
userExtendInfo.Reason = Resources.SessionExtensionErrorAlreadyExtendedBydoctor; | |
return userExtendInfo; | |
} | |
// if already had a free followup, then reject extension | |
if (previousStates?.Count(q => q.LastState == ConsultationState.SessionExtendedByUserFollowUpRequest.ToFriendlyString()) | |
> 0) | |
{ | |
userExtendInfo.IsAllowed = false; | |
userExtendInfo.Reason = Resources.SessionExtensionErrorAlreadyExtended; | |
return userExtendInfo; | |
} | |
// Prevent session extend on the client unless the extension was requested within 3 days of starting the consultation | |
// Ensuring the entity is filled, fixing an issue where the timer ends before the doctor accepts the consultation | |
var consultAcceptedEntity = consultation.PreviousStates.SafeWhere(q => q.LastState == "SessionAccepted").FirstOrDefault(); | |
if (consultAcceptedEntity == null) | |
consultAcceptedEntity = consultation.PreviousStates.LastOrDefault(); | |
//var consultation = Repository.GetConsultationSessionState(new ConsultationSessionState() { Id = request.Id }); | |
if (consultAcceptedEntity.Created <= DateTime.UtcNow.AddDays(-3)) | |
{ | |
// TODO :: must revamp status code .. this should be 500 server error since the client has no problem | |
// making the request.. its the server that couldn't fullfill the client request so it should be 5xx error not 4xx | |
// but if you return 500, then cura apps show generic 'service is down' error message so this needs fixing in clients | |
userExtendInfo.IsAllowed = false; | |
userExtendInfo.Reason = Resources.SessionExtensionErrorPastThreeDays; | |
return userExtendInfo; | |
// TODO :: must send typed custom exceptions instead. Clients still sees them as ServiceStack.WebServiceExceptions | |
// throw new CuraBusinessException((int)HttpStatusCode.BadRequest, Resources.SessionExtensionNotAllowed); | |
} | |
} | |
catch (Exception Ex) | |
{ | |
ExceptionLogger.Log(Ex); | |
return userExtendInfo; | |
} | |
return userExtendInfo; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment