Esempio n. 1
0
        public IActionResult CreateMaterialBatch([FromBody] BatchCreationRequest batchCreationRequest)
        {
            if (batchCreationRequest == null ||
                batchCreationRequest.ExpirationDate == null ||
                batchCreationRequest.StorageSiteId == null ||
                batchCreationRequest.StorageAreaId == null ||
                batchCreationRequest.CustomProps == null)
            {
                return(HandleBadRequest("Batch data missing for batch creation."));
            }

            // Validate expiration date
            if (batchCreationRequest.ExpirationDate.Date <= DateTime.UtcNow.Date)
            {
                return(HandleBadRequest("A batch expiration date must be in the future."));
            }

            // Validate batch number and quantity
            if (batchCreationRequest.BatchNumber <= 0 || batchCreationRequest.Quantity <= 0)
            {
                return(HandleBadRequest("The batch number and quantity need to be greater than 0!"));
            }

            // Batch locking on creation validation
            bool lockBatch = batchCreationRequest.IsLocked;

            if (lockBatch)
            {
                Role role = GetRole();
                if (role != Role.Administrator && role != Role.ScientificAssistant)
                {
                    return(Forbid());
                }
            }

            try
            {
                string userId = GetSubject();

                // Get material
                Material material = MaterialsService.GetMaterial(batchCreationRequest.MaterialId);

                // Get storage location
                StorageSite site = LocationsService.GetStorageSite(batchCreationRequest.StorageSiteId);
                StorageArea area = site.Areas.FirstOrDefault(a => a.Id == batchCreationRequest.StorageAreaId);
                if (area == null)
                {
                    throw new StorageAreaNotFoundException(site.Id, batchCreationRequest.StorageAreaId);
                }
                StorageLocation storageLocation = new StorageLocation(site, area);

                // Proceed with creation and return new batch!
                MaterialBatch batch = InventoryService.CreateMaterialBatch(material,
                                                                           batchCreationRequest.ExpirationDate,
                                                                           storageLocation,
                                                                           batchCreationRequest.BatchNumber,
                                                                           batchCreationRequest.Quantity,
                                                                           batchCreationRequest.CustomProps,
                                                                           lockBatch,
                                                                           userId);
                return(Created(GetNewResourceUri(batch), batch));
            }
            catch (MaterialNotFoundException exception)
            {
                return(HandleResourceNotFoundException(exception));
            }
            catch (StorageSiteNotFoundException exception)
            {
                return(HandleResourceNotFoundException(exception));
            }
            catch (StorageAreaNotFoundException exception)
            {
                return(HandleResourceNotFoundException(exception));
            }
            catch (Exception exception)
            {
                return(HandleUnexpectedException(exception));
            }
        }