Skip to content

Commit

Permalink
WIP make move from imported
Browse files Browse the repository at this point in the history
  • Loading branch information
darakeon committed Jun 24, 2024
1 parent bb4841a commit b66b4ca
Show file tree
Hide file tree
Showing 11 changed files with 359 additions and 254 deletions.
7 changes: 7 additions & 0 deletions core/BusinessLogic/Repositories/LineRepository.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
using DFM.Entities;

namespace DFM.BusinessLogic.Repositories;

internal class LineRepository : Repo<Line>
{
}
4 changes: 4 additions & 0 deletions core/BusinessLogic/Repositories/Mappings/LineMap.cs
Original file line number Diff line number Diff line change
Expand Up @@ -23,5 +23,9 @@ public void Override(AutoMapping<Line> mapping)

mapping.Map(l => l.Out)
.Column("Out_");

mapping.IgnoreProperty(l => l.HasIn);
mapping.IgnoreProperty(l => l.HasOut);
mapping.IgnoreProperty(l => l.HasCategory);
}
}
2 changes: 2 additions & 0 deletions core/BusinessLogic/Repositories/Repos.cs
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ internal class Repos
internal ContractRepository Contract;
internal ControlRepository Control;
internal DetailRepository Detail;
internal LineRepository Line;
internal MoveRepository Move;
internal WipeRepository Wipe;
internal ScheduleRepository Schedule;
Expand All @@ -38,6 +39,7 @@ IFileService fileService
Contract = new ContractRepository();
Control = new ControlRepository(getUrl);
Detail = new DetailRepository();
Line = new LineRepository();
Move = new MoveRepository(getUrl, valids.Move);
Wipe = new WipeRepository(this, getUrl, fileService);
Schedule = new ScheduleRepository(valids.Schedule);
Expand Down
79 changes: 74 additions & 5 deletions core/BusinessLogic/Services/RobotService.cs
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
using DFM.BusinessLogic.Response;
using DFM.BusinessLogic.Validators;
using DFM.Entities;
using DFM.Entities.Bases;
using DFM.Entities.Enums;
using DFM.Entities.Extensions;
using DFM.Exchange.Importer;
Expand Down Expand Up @@ -503,17 +504,85 @@ private CSVImporter validateArchive(String csv, User user)
return importer;
}

public async Task MakeMoveFromImported()
public async Task<MoveResult> MakeMoveFromImported()
{
if (!parent.Current.IsRobot)
throw Error.Uninvited.Throw();

var line = await queueService.Dequeue();
var dequeued = await queueService.Dequeue();


//var user = parent.Auth.VerifyUser();
if (!dequeued.HasValue)
return null;

//throw new NotImplementedException();
var key = dequeued.Value.Key;
var line = repos.Line.Get(dequeued.Value.Value.ID);
var user = line.Archive.User;

try
{
var move = inTransaction("MakeMoveFromImported", () =>
{
parent.Auth.VerifyUser(user);

var newMove = createMove(line);

var result = parent.BaseMove.SaveMove(
newMove, OperationType.Importing
);

line.Status = ImportStatus.Success;
repos.Line.SaveOrUpdate(line);

return result;
});

parent.BaseMove.FixSummaries(user);

return move;
}
catch (CoreError e)
{
inTransaction("MakeMoveFromImported", () =>
{
line.Status = ImportStatus.Error;
repos.Line.SaveOrUpdate(line);
});

throw;
}
finally
{
queueService.Delete(key);
}
}

private Move createMove(Line line)
{
var move =
new Move
{
Description = line.Description,
Nature = line.GetNature(),
In = line.In == null ? null : repos.Account.GetByName(line.In, line.Archive.User),
Out = line.Out == null ? null : repos.Account.GetByName(line.Out, line.Archive.User),
Category = line.Category == null ? null : repos.Category.GetByName(line.Category, line.Archive.User),
Value = line.DetailList.Any() ? 0 : line.Value ?? 0,
Conversion = line.DetailList.Any() ? null : line.Conversion,
};

foreach (var detail in line.DetailList)
{
var newDetail = detail.Clone();
newDetail.Move = move;
move.DetailList.Add(newDetail);
}

move.SetDate(line.Date);

move.SetPositionInSchedule();

return move;
}

}
}
1 change: 1 addition & 0 deletions core/Entities/Enums/OperationType.cs
Original file line number Diff line number Diff line change
Expand Up @@ -6,5 +6,6 @@ public enum OperationType
Edition = 1,
Deletion = 2,
Scheduling = 3,
Importing = 4,
}
}
19 changes: 19 additions & 0 deletions core/Entities/Line.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
using Keon.Util.DB;
using System;
using System.Collections.Generic;
using System.Linq;

namespace DFM.Entities;

Expand All @@ -26,6 +27,24 @@ public class Line : IEntityLong

public virtual ImportStatus Status { get; set; }

public virtual Boolean HasIn => !String.IsNullOrEmpty(In);
public virtual Boolean HasOut => !String.IsNullOrEmpty(Out);
public virtual Boolean HasCategory => !String.IsNullOrEmpty(Category);

public virtual MoveNature GetNature()
{
if (Nature.HasValue)
return Nature.Value;

if (!HasIn)
return MoveNature.Out;

if (!HasOut)
return MoveNature.In;

return MoveNature.Transfer;
}

public override String ToString()
{
return $"[{ID}] {Position} of {Archive}";
Expand Down
27 changes: 4 additions & 23 deletions core/Exchange/Importer/MoveCsv.cs
Original file line number Diff line number Diff line change
Expand Up @@ -148,19 +148,14 @@ IDictionary<String, Category> categories
{
if (move == null)
{
var hasIn = !String.IsNullOrEmpty(In);
var hasOut = !String.IsNullOrEmpty(Out);

var hasCategory = !String.IsNullOrEmpty(Category);

move = new Move
{
Description = Description,
Nature = getNature(hasIn, hasOut),
Nature = GetNature(),

In = hasIn ? accounts[In] : null,
Out = hasOut ? accounts[Out] : null,
Category = hasCategory ? categories[Category] : null,
In = HasIn ? accounts[In] : null,
Out = HasOut ? accounts[Out] : null,
Category = HasCategory ? categories[Category] : null,

Value = Value ?? 0,
Conversion = Conversion,
Expand All @@ -181,20 +176,6 @@ IDictionary<String, Category> categories
return move;
}

private MoveNature getNature(Boolean hasIn, Boolean hasOut)
{
if (Nature.HasValue)
return Nature.Value;

if (!hasIn)
return MoveNature.Out;

if (!hasOut)
return MoveNature.In;

return MoveNature.Transfer;
}

public Line ToLine(Archive archive)
{
Archive = archive;
Expand Down
Loading

0 comments on commit b66b4ca

Please sign in to comment.