Skip to content

Commit

Permalink
devonfw-forge#41: add functionality to calculate and store voting res…
Browse files Browse the repository at this point in the history
…ult to backend
  • Loading branch information
sarahffm committed Dec 2, 2022
1 parent d5c2816 commit 0cbf2d3
Show file tree
Hide file tree
Showing 10 changed files with 72 additions and 10 deletions.
1 change: 1 addition & 0 deletions client/app/Types/Type.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,4 +3,5 @@ export enum Type {
TaskStatusModified = 1,
TaskDeleted = 2,
EstimationAdded = 3,
TaskResultAdded = 4
}
6 changes: 6 additions & 0 deletions client/pages/session/[id].tsx
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,12 @@ export default function Session({ id, data }: any) {

break;
}
case Type.TaskResultAdded: {
// let { payload } = parsed as IMessage<ITaskResultDto>;

console.log("TaskResultAdded received.");
// console.log(payload);
}
default: {
break;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
using Task = System.Threading.Tasks.Task;
using System.Net.WebSockets;
using LiteDB;
using Devon4Net.Application.WebAPI.Implementation.Domain.Entities;

namespace Devon4Net.Application.WebAPI.Implementation.Business.SessionManagement.Controllers
{
Expand Down Expand Up @@ -230,6 +231,20 @@ public async Task<IActionResult> ChangeTaskStatus(long sessionId, [FromBody] Tas
{
await _webSocketHandler.Send(new Message<List<TaskStatusChangeDto>> { Type = MessageType.TaskStatusModified, Payload = modifiedTasks }, sessionId);

// If the status changed to evaluated, another message is sent that contains the voting result
if (statusChange.Status == Status.Evaluated)
{
var evaluatedTask = modifiedTasks.Find(item => item.Id == statusChange.Id);

var taskResult = new TaskResultDto()
{
Id = statusChange.Id,
Result = evaluatedTask.Result
};

await _webSocketHandler.Send(new Message<TaskResultDto> { Type = MessageType.TaskResultAdded, Payload = taskResult }, sessionId);
}

return Ok(modifiedTasks);
}
else
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
using Devon4Net.Application.WebAPI.Implementation.Domain.Entities;

namespace Devon4Net.Application.WebAPI.Implementation.Business.SessionManagement.Dtos
{
public class TaskResultDto
{
public string Id { get; set; }

public Result Result { get; set; }
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,8 @@ public class TaskStatusChangeDto

public Status Status { get; set; }

public Result? Result { get; set; }

public void Deconstruct(out string id, out Status status)
{
id = Id;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,5 +12,6 @@ public enum MessageType
TaskStatusModified,
TaskDeleted,
EstimationAdded,
TaskResultAdded
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -118,19 +118,20 @@ public async Task<Estimation> AddNewEstimation(long sessionId, string taskId, st
}

var task = session.Tasks.First(item => item.Id == taskId);

var estimations = task.Estimations;

var newEstimation = new Estimation { VoteBy = voteBy, Complexity = complexity };

// if estimation by user already exists, delete previous estimation before adding new
if (estimations != null && estimations.Any()) {
if (estimations != null && estimations.Any())
{
var alreadyContainsEstimation = estimations.Where(item => item.VoteBy == voteBy).Any();

if (alreadyContainsEstimation)
{
var oldEstimation = estimations.First(est => est.VoteBy == voteBy);

estimations.Remove(oldEstimation);
}
}
Expand Down Expand Up @@ -274,21 +275,34 @@ private string generateInviteToken()
return (false, new List<TaskStatusChangeDto>());
}

// and it contains the task requested to be chnaged
// and it contains the task requested to be changed
var (modified, taskChanges) = session.ChangeStatusOfTask(id, status);

if (!modified)
{
return (false, new List<TaskStatusChangeDto>());
}

// calculate the result if the task is evaluated
Result result = new Result();
if (status == Status.Evaluated)
{
result = session.Tasks.ToList().Find(item => item.Id == id).calculateResult();
}

var finished = _sessionRepository.Update(session);

// and we could properly update the database
if (finished)
{
var converted = taskChanges.Select<(String id, Status status), TaskStatusChangeDto>(item => new TaskStatusChangeDto { Id = item.id, Status = item.status }).ToList();

// add the result to the DTO if task is evaluated
if (status == Status.Evaluated)
{
converted.Find(item => item.Id == id).Result = result;
}

return (true, converted);
}
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
namespace Devon4Net.Application.WebAPI.Implementation.Domain.Entities
{
public partial class Result
public partial class Result
{
public int AmountOfVotes { get; set; }

public int ComplexityAverage { get; set; }
public double ComplexityAverage { get; set; }
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -68,13 +68,13 @@ suspended tasks are untouched
{
case Status.Open:
{
modifiedTasks = SuspendPendingTasksOtherThan(taskId);
break;
modifiedTasks = SuspendPendingTasksOtherThan(taskId);
break;
}
case Status.Suspended: break;
case Status.Ended:
{
if(task.Status != Status.Evaluated)
if (task.Status != Status.Evaluated)
{
return (false, modifiedTasks);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,5 +17,17 @@ public partial class Task
public IList<Estimation> Estimations { get; set; }

public Result? Result { get; set; }

public Result calculateResult()
{
var taskResult = new Result()
{
AmountOfVotes = Estimations.Count,
ComplexityAverage = Estimations.Select(est => est.Complexity).Average()
};

Result = taskResult;
return Result;
}
}
}

0 comments on commit 0cbf2d3

Please sign in to comment.