Skip to content

Commit

Permalink
backend : fix production creation service
Browse files Browse the repository at this point in the history
  • Loading branch information
benjaminpochat committed Feb 28, 2024
1 parent 67e9d44 commit e7c9fbe
Show file tree
Hide file tree
Showing 4 changed files with 31 additions and 8 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -2,31 +2,47 @@

import eu.viandeendirect.api.BeefProductionsApiDelegate;
import eu.viandeendirect.model.BeefProduction;
import eu.viandeendirect.model.PackageLot;
import eu.viandeendirect.model.Production;
import eu.viandeendirect.repository.PackageLotRepository;
import eu.viandeendirect.repository.ProductionRepository;
import eu.viandeendirect.service.specs.AuthenticationServiceSpecs;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

@Service
public class BeefProductionService implements BeefProductionsApiDelegate {

@Autowired
ProductionRepository productionRepository;

@Autowired
PackageLotRepository packageLotRepository;

@Autowired
AuthenticationServiceSpecs producerService;

@Override
public ResponseEntity<BeefProduction> getBeefProduction(Integer beefProductionId) {
return new ResponseEntity(productionRepository.findById(beefProductionId).get(), HttpStatus.OK);
Production production = productionRepository.findById(beefProductionId).get();
production.setLots(packageLotRepository.findByProduction(production));
return new ResponseEntity(production, HttpStatus.OK);
}

@Override
public ResponseEntity<BeefProduction> createBeefProduction(BeefProduction beefProduction) {
beefProduction.setProducer(producerService.getAuthenticatedProducer());
BeefProduction productionCreated = productionRepository.save(beefProduction);
beefProduction.getLots().forEach(lot -> lot.setProduction(productionCreated));
Iterable<PackageLot> lotsCreated = packageLotRepository.saveAll(beefProduction.getLots());
List<PackageLot> lotsCreatedAsList = new ArrayList<>();
lotsCreated.forEach(lotsCreatedAsList::add);
productionCreated.setLots(lotsCreatedAsList);
return new ResponseEntity<>(productionCreated, HttpStatus.CREATED);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,10 @@
import com.fasterxml.jackson.annotation.JsonCreator;
import eu.viandeendirect.model.Production;

import jakarta.persistence.GeneratedValue;
import jakarta.persistence.GenerationType;
import jakarta.persistence.ManyToOne;
import jakarta.persistence.SequenceGenerator;
import org.openapitools.jackson.nullable.JsonNullable;
import java.time.OffsetDateTime;
import jakarta.validation.Valid;
Expand All @@ -31,6 +34,8 @@ public class PackageLot {

@JsonProperty("id")
@jakarta.persistence.Id
@GeneratedValue(strategy = GenerationType.SEQUENCE, generator = "package_lot_id_generator")
@SequenceGenerator(name="package_lot_id_generator", sequenceName = "package_lot_id_seq", allocationSize = 1)
private Integer id;

@JsonProperty("production")
Expand Down
10 changes: 5 additions & 5 deletions frontend/app/src/domains/production/views/PackageLotsCreator.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -14,11 +14,11 @@ export default function PackageLotsCreator({
{production.lots?.map(lot => <PackageLotConfigurator
lot={lot}
disabled={disabled}
changeQuantitySoldCallback={changeQuantitySold} />)}
changeQuantitySoldCallback={changeTotalQuantity} />)}
</>

function displayAlerts() {
const totalQuantitySold = production.lots?.map(lot => lot.netWeight * lot.quantity).reduce((total, added) => total + added) || 0
const totalQuantitySold = production.lots?.map(lot => lot.netWeight * lot.quantity).reduce((total, added) => total + added, 0) || 0
if (!isTotalQuantitySoldLowerThanMeatWeight(totalQuantitySold)) {
const meatQuantity = BeefProductionService.getMeatWeight(production.warmCarcassWeight)
return <Alert severity="error">Le poids total des produits préparés ne doit pas dépasser la quantité de viande de l'animal estimée à {meatQuantity} kg.</Alert>
Expand All @@ -29,8 +29,8 @@ export default function PackageLotsCreator({
return totalQuantitySoldUpdated < BeefProductionService.getMeatWeight(production.warmCarcassWeight)
}

function changeQuantitySold() {
const totalQuantitySold = production.lots?.map(lot => lot.netWeight * lot.quantity).reduce((total, added) => total + added) || 0
changeQuantitiesCompliancyCallback(isTotalQuantitySoldLowerThanMeatWeight(totalQuantitySold))
function changeTotalQuantity() {
const totalQuantity = production.lots?.map(lot => lot.netWeight * lot.quantity).reduce((total, added) => total + added) || 0
changeQuantitiesCompliancyCallback(isTotalQuantitySoldLowerThanMeatWeight(totalQuantity))
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ export default function BeefProductionCreator({ callback }) {
const [ activeStep, setActiveStep ] = useState<number>(BREEDING_PROPERTIES_STEP)
const [ beefProduction, setBeefProduction] = useState<BeefProduction>({ productionType: "BeefProduction"})
const [ completedSteps, setCompletedSteps] = useState<Array<number>>([])
const [ saveEnabled, setSaveEnabled] = useState<boolean>(false)
const apiBuilder = new ApiBuilder()

useEffect(() => {
Expand All @@ -41,6 +42,7 @@ export default function BeefProductionCreator({ callback }) {
data.map(template => {
lots.push({
...template,
id: undefined,
quantity: 0,
quantitySold: 0
})
Expand Down Expand Up @@ -118,11 +120,11 @@ export default function BeefProductionCreator({ callback }) {
<StepContent>
<div className="form">
<div>
<PackageLotsCreator production={beefProduction}></PackageLotsCreator>
<PackageLotsCreator production={beefProduction} changeQuantitiesCompliancyCallback={setSaveEnabled}></PackageLotsCreator>
</div>
<div>
<ButtonGroup>
<Button type='submit' variant="contained" size="small" onClick={() => validate() } disabled={!isTotalQuantitySoldLowerThanMeatWeight() || getTotalQuantitySold() === 0}>Valider</Button>
<Button type='submit' variant="contained" size="small" onClick={() => validate() } disabled={!saveEnabled}>Valider</Button>
<Button variant="outlined" size="small" onClick={() => cancel()}>Abandonner</Button>
</ButtonGroup>
</div>
Expand Down

0 comments on commit e7c9fbe

Please sign in to comment.