-
Notifications
You must be signed in to change notification settings - Fork 2
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Bio functionality #137
Merged
Merged
Bio functionality #137
Changes from all commits
Commits
Show all changes
11 commits
Select commit
Hold shift + click to select a range
97330ac
incomplete Bio functionality
vks333 b139a58
removed Constant from PathwayBase as its not required for Bio
vks333 9856cbc
added pathway classes
vks333 93fe862
resolved merge conflict with main
vks333 532e2dc
Added io functionality
vks333 04a151c
Merge branch 'main' into CFODEV-539
vks333 9bac8cf
Address review comments
carlsixsmith-moj 099e4f1
Add bio when skipping
carlsixsmith-moj 8c98134
Fix issue with missmatched ids
carlsixsmith-moj 489ce3d
removed status from json
vks333 67fd2f2
Merge branch 'main' into CFODEV-539
carlsixsmith-moj File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains 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
This file contains 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
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,69 @@ | ||
using Cfo.Cats.Application.Common.Security; | ||
using Cfo.Cats.Application.Features.Bios.DTOs; | ||
using Cfo.Cats.Application.Features.Bios.DTOs.V1.Pathways.Diversity; | ||
using Cfo.Cats.Application.Features.Bios.DTOs.V1.Pathways.ChildhoodExperiences; | ||
using Cfo.Cats.Application.Features.Bios.DTOs.V1.Pathways.RecentExperiences; | ||
using Cfo.Cats.Application.SecurityConstants; | ||
using Cfo.Cats.Domain.Entities.Bios; | ||
using Newtonsoft.Json; | ||
using Cfo.Cats.Application.Common.Validators; | ||
|
||
|
||
namespace Cfo.Cats.Application.Features.Bios.Commands; | ||
|
||
public static class BeginBio | ||
{ | ||
[RequestAuthorize(Policy = SecurityPolicies.Enrol)] | ||
public class Command : IRequest<Result<Guid>> | ||
{ | ||
public required string ParticipantId { get; set; } | ||
} | ||
|
||
public class Handler : IRequestHandler<Command, Result<Guid>> | ||
{ | ||
private readonly IUnitOfWork _unitOfWork; | ||
private readonly ICurrentUserService _currentUserService; | ||
public Handler(IUnitOfWork unitOfWork, ICurrentUserService currentUserService) | ||
{ | ||
_unitOfWork = unitOfWork; | ||
_currentUserService = currentUserService; | ||
} | ||
|
||
public async Task<Result<Guid>> Handle(Command request, CancellationToken cancellationToken) | ||
{ | ||
Bio bio = new Bio() | ||
{ | ||
Id = Guid.NewGuid(), | ||
ParticipantId = request.ParticipantId, | ||
Pathways = | ||
[ | ||
new ChildhoodExperiencesPathway(), | ||
new DiversityPathway(), | ||
new RecentExperiencesPathway(), | ||
] | ||
}; | ||
|
||
string json = JsonConvert.SerializeObject(bio, new JsonSerializerSettings | ||
{ | ||
TypeNameHandling = TypeNameHandling.Auto | ||
}); | ||
|
||
ParticipantBio bioSurvey = ParticipantBio.Create(bio.Id, request.ParticipantId, bioJson: json, BioStatus.NotStarted); | ||
await _unitOfWork.DbContext.ParticipantBios.AddAsync(bioSurvey); | ||
return Result<Guid>.Success(bio.Id); | ||
} | ||
} | ||
|
||
public class Validator : AbstractValidator<Command> | ||
{ | ||
public Validator() | ||
{ | ||
RuleFor(c => c.ParticipantId) | ||
.MinimumLength(9) | ||
.MaximumLength(9) | ||
.Matches(ValidationConstants.AlphaNumeric) | ||
.WithMessage(string.Format(ValidationConstants.AlphaNumericMessage, nameof(Command.ParticipantId))); | ||
} | ||
} | ||
|
||
} |
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
|
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,75 @@ | ||
using Cfo.Cats.Application.Common.Security; | ||
using Cfo.Cats.Application.Common.Validators; | ||
using Cfo.Cats.Application.Features.Bios.DTOs; | ||
using Cfo.Cats.Application.SecurityConstants; | ||
using Cfo.Cats.Domain.Entities.Bios; | ||
using Newtonsoft.Json; | ||
|
||
namespace Cfo.Cats.Application.Features.Bios.Commands; | ||
|
||
public static class SaveBio | ||
{ | ||
[RequestAuthorize(Policy = SecurityPolicies.Enrol)] | ||
public class Command : IRequest<Result> | ||
{ | ||
public bool Submit { get; set; } = false; | ||
|
||
public required Bio Bio { get; set; } | ||
} | ||
|
||
public class Handler : IRequestHandler<Command, Result> | ||
{ | ||
private readonly IUnitOfWork _unitOfWork; | ||
|
||
public Handler(IUnitOfWork unitOfWork) | ||
{ | ||
_unitOfWork = unitOfWork; | ||
|
||
} | ||
|
||
public async Task<Result> Handle(Command request, CancellationToken cancellationToken) | ||
{ | ||
|
||
ParticipantBio? bio = await _unitOfWork.DbContext.ParticipantBios | ||
.FirstOrDefaultAsync(r => r.Id == request.Bio.Id && r.ParticipantId == request.Bio.ParticipantId, cancellationToken); | ||
|
||
if(bio == null) | ||
{ | ||
return Result.Failure("Bio not found"); | ||
} | ||
|
||
bio.UpdateJson(JsonConvert.SerializeObject(request.Bio, new JsonSerializerSettings | ||
{ | ||
TypeNameHandling = TypeNameHandling.Auto | ||
})); | ||
bio.UpdateStatus(BioStatus.InProgress); | ||
if (request.Submit) | ||
{ | ||
bio.UpdateStatus(BioStatus.Complete); | ||
bio.Submit(); | ||
} | ||
|
||
return Result.Success(); | ||
} | ||
} | ||
|
||
public class Validator : AbstractValidator<Command> | ||
{ | ||
public Validator() | ||
{ | ||
RuleFor(x => x.Bio) | ||
.NotNull(); | ||
|
||
RuleFor(x => x.Bio.ParticipantId) | ||
.MinimumLength(9) | ||
.MaximumLength(9) | ||
.Matches(ValidationConstants.AlphaNumeric) | ||
.WithMessage(string.Format(ValidationConstants.AlphaNumericMessage, nameof(Command.Bio.ParticipantId))); | ||
|
||
RuleFor(x => x.Bio.Id) | ||
.NotEmpty(); | ||
|
||
} | ||
} | ||
|
||
} |
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Caching removed. Change signature to simplify UI |
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,72 @@ | ||
using Cfo.Cats.Application.Common.Security; | ||
using Cfo.Cats.Application.Common.Validators; | ||
using Cfo.Cats.Application.Features.Bios.DTOs; | ||
using Cfo.Cats.Application.Features.Bios.DTOs.V1.Pathways.ChildhoodExperiences; | ||
using Cfo.Cats.Application.Features.Bios.DTOs.V1.Pathways.Diversity; | ||
using Cfo.Cats.Application.Features.Bios.DTOs.V1.Pathways.RecentExperiences; | ||
using Cfo.Cats.Application.SecurityConstants; | ||
using Cfo.Cats.Domain.Entities.Bios; | ||
using DocumentFormat.OpenXml.Office.PowerPoint.Y2021.M06.Main; | ||
using Newtonsoft.Json; | ||
|
||
namespace Cfo.Cats.Application.Features.Bios.Commands; | ||
|
||
public static class SkipBioForNow | ||
{ | ||
[RequestAuthorize(Policy = SecurityPolicies.Enrol)] | ||
public class Command : IRequest<Result> | ||
{ | ||
public string? ParticipantId { get; set;} | ||
} | ||
|
||
public class Handler(IUnitOfWork unitOfWork) : IRequestHandler<Command, Result> | ||
{ | ||
public async Task<Result> Handle(Command request, CancellationToken cancellationToken) | ||
{ | ||
ParticipantBio? bio = await unitOfWork.DbContext.ParticipantBios.FirstOrDefaultAsync(r => r.ParticipantId == request.ParticipantId); | ||
|
||
if (bio == null) | ||
{ | ||
Bio newBio = new Bio() | ||
{ | ||
Id = Guid.NewGuid(), | ||
ParticipantId = request.ParticipantId!, | ||
Pathways = | ||
[ | ||
new ChildhoodExperiencesPathway(), | ||
new DiversityPathway(), | ||
new RecentExperiencesPathway(), | ||
] | ||
}; | ||
|
||
string json = JsonConvert.SerializeObject(newBio, new JsonSerializerSettings | ||
{ | ||
TypeNameHandling = TypeNameHandling.Auto | ||
}); | ||
|
||
bio = ParticipantBio.Create(newBio.Id, request.ParticipantId!, json, BioStatus.NotStarted); | ||
unitOfWork.DbContext.ParticipantBios.Add(bio); | ||
} | ||
|
||
bio.UpdateStatus(BioStatus.SkippedForNow); | ||
return Result.Success(); | ||
} | ||
} | ||
|
||
public class Validator : AbstractValidator<Command> | ||
{ | ||
private IUnitOfWork _unitOfWork; | ||
|
||
public Validator(IUnitOfWork unitOfWork) | ||
{ | ||
_unitOfWork = unitOfWork; | ||
|
||
RuleFor(x => x.ParticipantId) | ||
.MinimumLength(9) | ||
.MaximumLength(9) | ||
.Matches(ValidationConstants.AlphaNumeric) | ||
.WithMessage(string.Format(ValidationConstants.AlphaNumericMessage, nameof(Command.ParticipantId))); | ||
} | ||
} | ||
|
||
} |
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
namespace Cfo.Cats.Application.Features.Bios.DTOs; | ||
|
||
public class Bio | ||
{ | ||
public required Guid Id { get; set; } | ||
public required string ParticipantId { get; set; } | ||
public required PathwayBase[] Pathways { get; set; } | ||
} |
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
namespace Cfo.Cats.Application.Features.Bios.DTOs; | ||
|
||
public class BioPathwayValidator : AbstractValidator<PathwayBase> | ||
{ | ||
public BioPathwayValidator() | ||
{ | ||
RuleForEach(model => model.Questions()) | ||
.Must(q => q.IsValid()) | ||
.WithMessage("You must select a valid option!") | ||
.OverridePropertyName("Questions"); | ||
} | ||
} |
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
namespace Cfo.Cats.Application.Features.Bios.DTOs; | ||
|
||
public class BioValidator : AbstractValidator<Bio> | ||
{ | ||
public BioValidator() | ||
{ | ||
RuleForEach(model => model.Pathways).SetValidator(new BioPathwayValidator()); | ||
} | ||
} |
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
using Cfo.Cats.Domain.Entities.Bios; | ||
|
||
namespace Cfo.Cats.Application.Features.Bios.DTOs; | ||
|
||
public class ParticipantBioDto | ||
{ | ||
public required string ParticipantId { get; set; } | ||
public required DateTime CreatedDate { get; set; } | ||
|
||
private class Mapping : Profile | ||
{ | ||
public Mapping() | ||
{ | ||
CreateMap<ParticipantBio, ParticipantBioDto>() | ||
.ForMember(p => p.CreatedDate, options => options.MapFrom(source => source.Created)); | ||
} | ||
} | ||
} | ||
|
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
using Newtonsoft.Json; | ||
|
||
namespace Cfo.Cats.Application.Features.Bios.DTOs; | ||
|
||
public abstract class PathwayBase | ||
{ | ||
[JsonIgnore] | ||
public abstract string Title { get; } | ||
|
||
[JsonIgnore] | ||
public abstract string Icon { get; } | ||
|
||
public abstract IEnumerable<QuestionBase> Questions(); | ||
|
||
} |
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,53 @@ | ||
using Newtonsoft.Json; | ||
|
||
namespace Cfo.Cats.Application.Features.Bios.DTOs; | ||
|
||
/// <summary> | ||
/// Base class for the all questions | ||
/// </summary> | ||
public abstract partial class QuestionBase | ||
{ | ||
|
||
#pragma warning disable CS8618 // Non-nullable field must contain a non-null value when exiting constructor. Consider declaring as nullable. | ||
protected QuestionBase() | ||
{ | ||
} | ||
#pragma warning restore CS8618 // Non-nullable field must contain a non-null value when exiting constructor. Consider declaring as nullable. | ||
|
||
protected QuestionBase(string question, string[] options) | ||
{ | ||
this.Question = question; | ||
this.Options = options; | ||
} | ||
|
||
protected QuestionBase(string question, string otherInformation, string[] options) | ||
: this(question, options) | ||
{ | ||
this.OtherInformation = otherInformation; | ||
} | ||
|
||
/// <summary> | ||
/// The question we are asking | ||
/// </summary> | ||
[JsonIgnore] | ||
public string Question { get; } | ||
|
||
/// <summary> | ||
/// Any other errata about the question. | ||
/// </summary> | ||
[JsonIgnore] | ||
public string? OtherInformation { get; } | ||
|
||
/// <summary> | ||
/// A collection of options for the answers | ||
/// </summary> | ||
[JsonIgnore] | ||
public string[] Options { get; } | ||
|
||
/// <summary> | ||
/// Is the answer valid | ||
/// </summary> | ||
/// <returns>True if the answer has a valid return value</returns> | ||
public abstract bool IsValid(); | ||
} | ||
|
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.