Skip to content
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

fix: validate name when editing chemical #197

Merged
merged 1 commit into from
Jan 2, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions src/ChemDec.Api/ChemDec.Api.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,9 @@

<PropertyGroup>
<TargetFramework>net8.0</TargetFramework>
<UserSecretsId>4da654b3-752c-4bfe-9a96-f05b76086cfa</UserSecretsId>
<UserSecretsId>42a37c7c-77f0-4a47-a24c-5104015b5f06</UserSecretsId>
</PropertyGroup>

<ItemGroup>
<PackageReference Include="AutoMapper" Version="13.0.1" />
<PackageReference Include="Azure.Extensions.AspNetCore.Configuration.Secrets" Version="1.3.1" />
Expand Down
138 changes: 84 additions & 54 deletions src/ChemDec.Api/Controllers/Handlers/ChemicalHandler.cs
Original file line number Diff line number Diff line change
Expand Up @@ -43,8 +43,34 @@ public ChemicalHandler(Db.ChemContext db, IMapper mapper, UserResolver userResol

public async Task<(Model.Chemical, IEnumerable<string>)> SaveOrUpdate(Model.Chemical chemical)
{
var validationErrors = new List<string>();
var validationErrors = ValidateChemical(chemical);
if (validationErrors.Any()) return (null, validationErrors);

var user = await _userService.GetCurrentUser();
var dbObject = await GetExistingChemical(chemical);

if (dbObject != null)
{
await UpdateChemical(chemical, dbObject);
}
else
{
await AddNewChemical(chemical, user);
}

var savedChemical = await db.Chemicals
.ProjectTo<Model.Chemical>(mapper.ConfigurationProvider)
.FirstOrDefaultAsync(ps => ps.Id == chemical.Id);

return (savedChemical, null);
}

private List<string> ValidateChemical(Model.Chemical chemical)
{
var validationErrors = new List<string>();

chemical.Name = chemical.Name.Trim();
chemical.Description = chemical.Description.Trim();

if (string.IsNullOrEmpty(chemical.Name))
{
Expand All @@ -58,78 +84,82 @@ public ChemicalHandler(Db.ChemContext db, IMapper mapper, UserResolver userResol
{
validationErrors.Add("Chemical description cannot contain semicolons.");
}

if (string.IsNullOrEmpty(chemical.Description))
{
validationErrors.Add("Chemical description must be set");
}

var existingChemical = db.Chemicals
.FirstOrDefaultAsync(ps => ps.Name.Trim() == chemical.Name && ps.Id != chemical.Id).Result;

Db.Chemical dbObject = null;
if (chemical.Id != Guid.Empty)
{
dbObject = await db.Chemicals
.FirstOrDefaultAsync(ps => ps.Id == chemical.Id);
}
else
if (existingChemical != null)
{
dbObject = await db.Chemicals
.FirstOrDefaultAsync(ps => ps.Name == chemical.Name);
validationErrors.Add("Chemical with name " + chemical.Name + " already exists!");
}

if (chemical.Id == Guid.Empty && dbObject != null)
return validationErrors;
}

private async Task<Db.Chemical> GetExistingChemical(Model.Chemical chemical)
{
if (chemical.Id != Guid.Empty)
{
validationErrors.Add("Chemical with name " + chemical.Name + " already exist");
return await db.Chemicals
.FirstOrDefaultAsync(ps => ps.Id == chemical.Id);
}
return null;
}

if (validationErrors.Any()) return (null, validationErrors);
private async Task UpdateChemical(Model.Chemical chemical, Db.Chemical dbObject)
{
var tentative = dbObject.Tentative;
var tocWeight = dbObject.TocWeight;
var nWeight = dbObject.NitrogenWeight;
var density = dbObject.Density;

mapper.Map(chemical, dbObject);
dbObject.Tentative = tentative; // can only be approved through Approve-method
dbObject = HandleRelations(chemical, dbObject);

if (dbObject != null)
if (dbObject.TocWeight != tocWeight || dbObject.NitrogenWeight != nWeight || dbObject.Density != density)
{
// User check to see if user can update chemicals

chemical.Id = dbObject.Id;
var tentative = dbObject.Tentative;
var tocWeight = dbObject.TocWeight;
var nWeight = dbObject.NitrogenWeight;
var density = dbObject.Density;

mapper.Map(chemical, dbObject);
dbObject.Tentative = tentative; // can only be approved through Approve-method
dbObject = HandleRelations(chemical, dbObject);
if (dbObject.TocWeight != tocWeight || dbObject.NitrogenWeight != nWeight || dbObject.Density != density)
{
// Recalculate shipments
var shipmentsToUpdate = await db.ShipmentChemicals.Include(i => i.Shipment).Where(w => w.ChemicalId == dbObject.Id).ToListAsync();
foreach (var shipment in shipmentsToUpdate)
{
ShipmentHandler.CalculateChemicals(shipment.Shipment.RinsingOffshorePercent, shipment, dbObject);
}
}

await db.SaveChangesAsync();
await RecalculateShipments(dbObject);
}
else

await db.SaveChangesAsync();
}

private async Task RecalculateShipments(Db.Chemical dbObject)
{
var shipmentsToUpdate = await db.ShipmentChemicals
.Include(i => i.Shipment)
.Where(w => w.ChemicalId == dbObject.Id)
.ToListAsync();

foreach (var shipment in shipmentsToUpdate)
{
// User check. Only chem administrator can add non-tentative chemicals
var newDbObject = mapper.Map<Db.Chemical>(chemical);

if (newDbObject.Id == Guid.Empty)
newDbObject.Id = Guid.NewGuid();
newDbObject = HandleRelations(chemical, newDbObject);
newDbObject.ProposedBy = user.Email;
newDbObject.ProposedByEmail = user.Email;
newDbObject.ProposedByName = user.Name;
newDbObject.Proposed = DateTime.Now;
db.Chemicals.Add(newDbObject);
chemical.Id = newDbObject.Id;

await db.SaveChangesAsync();
await SendNewChemicalEmail(user, newDbObject);
ShipmentHandler.CalculateChemicals(shipment.Shipment.RinsingOffshorePercent, shipment, dbObject);
}
}

return (await db.Chemicals.ProjectTo<Model.Chemical>(mapper.ConfigurationProvider).FirstOrDefaultAsync(ps => ps.Id == chemical.Id), null);
private async Task AddNewChemical(Model.Chemical chemical, User user)
{
var newDbObject = mapper.Map<Db.Chemical>(chemical);

if (newDbObject.Id == Guid.Empty)
newDbObject.Id = Guid.NewGuid();

newDbObject = HandleRelations(chemical, newDbObject);
newDbObject.ProposedBy = user.Email;
newDbObject.ProposedByEmail = user.Email;
newDbObject.ProposedByName = user.Name;
newDbObject.Proposed = DateTime.Now;

db.Chemicals.Add(newDbObject);
chemical.Id = newDbObject.Id;

await db.SaveChangesAsync();
await SendNewChemicalEmail(user, newDbObject);
}

public async Task<(bool, IEnumerable<string>)> Approve(Guid chemicalId)
Expand Down
Loading