Skip to content

Commit

Permalink
Fixes and Wizard
Browse files Browse the repository at this point in the history
  • Loading branch information
Ruskyy committed Oct 29, 2023
1 parent d6175a8 commit 143e4cd
Show file tree
Hide file tree
Showing 10 changed files with 153 additions and 61 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -105,6 +105,10 @@ public List<CompanyDto> getAllCompany() {
@Override
public List<CompanyDto> getTopCompanies() {
List<CompanyEntity> companyEntityList = companyRepository.findTop5ByOrderByCountDesc();
return companyEntityList.stream().filter(c -> c.getCount() >= 0).map(this::convertEntityToDto).collect(Collectors.toList());
return companyEntityList
.stream()
.filter(c -> c.getCount() >= 0)
.map(this::convertEntityToDto)
.collect(Collectors.toList());
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -155,10 +155,11 @@ private MaterialDemandFavoriteResponse convertToMaterialDemandResponse(FavoriteE
CompanyEntity sEntity = materialDemand.getSupplierId();
response.setCustomer(cEntity.getId().toString());
response.setSupplier(sEntity.getId().toString());
response.setMaterialDescriptionCustomer(materialDemand.getMaterialDescriptionCustomer());
response.setMaterialNumberCustomer(materialDemand.getMaterialNumberCustomer());
response.setMaterialNumberSupplier(materialDemand.getMaterialNumberSupplier());
response.setChangedAt(materialDemand.getChangedAt().toString());
response.setUnitOfMeasure(materialDemand.getUnitMeasure().getCxSymbol());
response.setUnitOfMeasure(materialDemand.getUnitMeasure().getId().toString());
response.setFavoritedAt(entity.getFavorited_at().toString());

return response;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -119,10 +119,10 @@ private LoggingHistoryEntity convertDtoToEntity(LoggingHistoryRequest loggingHis
String materialDemandID = loggingHistoryRequest.getMaterialDemandId();
String capacityGroupID = loggingHistoryRequest.getCapacityGroupId();

if (null != materialDemandID && !materialDemandID.isEmpty()) {
if (null != materialDemandID && !materialDemandID.isEmpty() && !materialDemandID.equalsIgnoreCase("null")) {
loggingHistoryEntity.setMaterialDemandId(UUID.fromString(loggingHistoryRequest.getMaterialDemandId()));
}
if (null != capacityGroupID && !capacityGroupID.isEmpty()) {
if (null != capacityGroupID && !capacityGroupID.isEmpty() && !capacityGroupID.equalsIgnoreCase("null")) {
loggingHistoryEntity.setCapacityGroupId(UUID.fromString(loggingHistoryRequest.getCapacityGroupId()));
}
loggingHistoryEntity.setUserAccount(loggingHistoryRequest.getUserAccount());
Expand All @@ -140,7 +140,7 @@ private ArchivedLogEntity convertDtoToArchivedEntity(LoggingHistoryRequest loggi
String materialDemandID = loggingHistoryRequest.getMaterialDemandId();
String capacityGroupID = loggingHistoryRequest.getCapacityGroupId();

if (null != materialDemandID && !materialDemandID.isEmpty()) {
if (null != materialDemandID && !materialDemandID.isBlank() && !materialDemandID.equalsIgnoreCase("null")) {
loggingHistoryEntity.setMaterialDemandId(UUID.fromString(loggingHistoryRequest.getMaterialDemandId()));
}
if (null != capacityGroupID && !capacityGroupID.isEmpty() && !capacityGroupID.equalsIgnoreCase("null")) {
Expand Down Expand Up @@ -272,8 +272,12 @@ private LoggingHistoryResponse convertLoggingHistoryResponseDto(LoggingHistoryEn
responseDto.setEventDescription(loggingHistoryEntity.getDescription());
responseDto.setUserAccount(loggingHistoryEntity.getUserAccount());
responseDto.setEventType(String.valueOf(loggingHistoryEntity.getEventType()));
responseDto.setCapacityGroupId(String.valueOf(loggingHistoryEntity.getCapacityGroupId()));
responseDto.setMaterialDemandId(String.valueOf(loggingHistoryEntity.getMaterialDemandId()));
if (loggingHistoryEntity.getCapacityGroupId() == null) {
responseDto.setCapacityGroupId(null);
} else responseDto.setCapacityGroupId(loggingHistoryEntity.getCapacityGroupId().toString());
if (loggingHistoryEntity.getMaterialDemandId() == null) {
responseDto.setMaterialDemandId(null);
} else responseDto.setMaterialDemandId(loggingHistoryEntity.getMaterialDemandId().toString());
responseDto.setObjectType(String.valueOf(loggingHistoryEntity.getObjectType()));
responseDto.setTimeCreated(String.valueOf(loggingHistoryEntity.getTime_created()));

Expand All @@ -290,10 +294,13 @@ private ArchivedLoggingHistoryResponse convertArchivedLoggingHistoryResponseDto(
responseDto.setEventDescription(archivedLogEntity.getDescription());
responseDto.setUserAccount(archivedLogEntity.getUserAccount());
responseDto.setEventType(archivedLogEntity.getEventType().toString());
if (archivedLogEntity.getCapacityGroupId() != null) {
responseDto.setCapacityGroupId(archivedLogEntity.getCapacityGroupId().toString());
}
responseDto.setMaterialDemandId(Objects.requireNonNull(archivedLogEntity.getMaterialDemandId()).toString());
if (archivedLogEntity.getCapacityGroupId() == null) {
responseDto.setCapacityGroupId(null);
} else responseDto.setCapacityGroupId(archivedLogEntity.getCapacityGroupId().toString());

if (archivedLogEntity.getMaterialDemandId() == null) {
responseDto.setMaterialDemandId(null);
} else responseDto.setMaterialDemandId(archivedLogEntity.getMaterialDemandId().toString());
responseDto.setObjectType(archivedLogEntity.getObjectType().toString());
responseDto.setTimeCreated(archivedLogEntity.getTime_created().toString());

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -83,14 +83,14 @@ private EventType processCapacityGroup(String userID, CapacityGroupEntity cgs, b
entity.getMaterialDemandID()
);
if (materialDemand.isPresent()) {
aggregatedAverageDemand += calculateAverageDemand(materialDemand.get().getDemandSeries());
aggregatedAverageDemand += calculateAverageDemand(materialDemand.get().getDemandSeries());
}
}

EventType eventType = determineEventType(cgs, aggregatedAverageDemand);
cgs.setLinkStatus(eventType);
capacityGroupRepository.save(cgs);
logEvent(eventType, userID, postLog,cgs.getId().toString());
logEvent(eventType, userID, postLog, cgs.getId().toString());
return eventType;
}

Expand All @@ -117,14 +117,11 @@ private StatusesEntity getStatus(String userID) {

private double calculateAverageDemand(List<DemandSeries> matchedDemandSeries) {
double totalDemand = matchedDemandSeries
.stream()
.flatMap(demand -> demand.getDemandSeriesValues().stream())
.mapToDouble(DemandSeriesValues::getDemand)
.sum();
long count = matchedDemandSeries
.stream()
.mapToLong(demand -> demand.getDemandSeriesValues().size())
.sum();
.stream()
.flatMap(demand -> demand.getDemandSeriesValues().stream())
.mapToDouble(DemandSeriesValues::getDemand)
.sum();
long count = matchedDemandSeries.stream().mapToLong(demand -> demand.getDemandSeriesValues().size()).sum();

return count == 0 ? 0 : totalDemand / count;
}
Expand All @@ -144,7 +141,7 @@ private EventType determineEventType(CapacityGroupEntity capacityGroup, double t
}
}

private void logEvent(EventType eventType, String userID, boolean postLog,String cgID) {
private void logEvent(EventType eventType, String userID, boolean postLog, String cgID) {
if (postLog) {
LoggingHistoryEntity logEntity = new LoggingHistoryEntity();
logEntity.setObjectType(EventObjectType.CAPACITY_GROUP);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,10 +24,14 @@ import { Alert, Button, Col, Container, Row } from 'react-bootstrap';
import Form from 'react-bootstrap/Form';
import Modal from 'react-bootstrap/Modal';
import { FaSearch } from 'react-icons/fa';
import { LuStar } from 'react-icons/lu';
import Select from 'react-select';
import { CapacityGroupContext } from '../../contexts/CapacityGroupsContextProvider';
import { FavoritesContext } from '../../contexts/FavoritesContextProvider';
import { UnitsofMeasureContext } from '../../contexts/UnitsOfMeasureContextProvider';
import CustomOption from '../../interfaces/customoption_interface';
import { DemandProp } from '../../interfaces/demand_interfaces';
import { FavoriteType } from '../../interfaces/favorite_interface';
import { LoadingCustomMessage } from '../common/LoadingMessages';
import StepBreadcrumbs from './../common/StepsBreadCrumbs';

Expand All @@ -46,10 +50,12 @@ function CapacityGroupWizardModal({ show, onHide, checkedDemands, demands }: Cap
const [isNameInputShaking, setIsNameInputShaking] = useState(false);
const [isActualCapacityInputShaking, setIsActualCapacityInputShaking] = useState(false);
const [isMaximumCapacityShaking, setIsMaximumCapacityShaking] = useState(false);
const [selectedDemands, setSelectedDemands] = useState<DemandProp[]>([]);
const [selectedDemands, setSelectedDemands] = useState<any[]>([]);
const [isLoading, setIsLoading] = useState(false);
const [isSuccess, setIsSuccess] = useState(false);
const context = useContext(CapacityGroupContext);
const { fetchFavoritesByType } = useContext(FavoritesContext)!;

const [createdgroupid, setCreatedgroupid] = useState('');


Expand Down Expand Up @@ -187,6 +193,80 @@ function CapacityGroupWizardModal({ show, onHide, checkedDemands, demands }: Cap
setDefaultActualCapacity(numericValue);
};

const [options, setOptions] = useState<any[]>([]); // State to store options for Creatable component
useEffect(() => {
const fetchData = async () => {
try {
setIsLoading(true);
const favoriteIdsSet = new Set<string>(); // Set to store unique favorite material demand IDs
let materialDemandOptions: any[] = [];

// Fetch material demands from favorites
const favoritesResponse = await fetchFavoritesByType(FavoriteType.MATERIAL_DEMAND);
const favoriteMaterialDemands = favoritesResponse?.materialDemands || [];

// Filter and map favorite material demands to options, ensuring uniqueness based on IDs
const favoriteOptions = favoriteMaterialDemands
.filter((md: any) => {
if (favoriteIdsSet.has(md.id)) {
return false; // Skip duplicate favorites
}
favoriteIdsSet.add(md.id); // Add favorite ID to set
return true; // Include only unique favorites
})
.map((md: any) => {
const label = (
<div>
<LuStar size={12} className="text-warning" /> {md.materialNumberCustomer || 'N/A'} - {md.materialNumberSupplier || 'N/A'} - {md.materialDescriptionCustomer || 'N/A'}
</div>
);
return {
value: md.id,
label: label,
};
});

// Fetch material demands from demands prop
const demandMaterialDemands = demands || [];

// Filter demand material demands to exclude those with IDs present in favorites
const filteredDemandOptions = demandMaterialDemands.filter((md: any) => {
// Exclude demand if its ID is present in favorites
if (favoriteIdsSet.has(md.id)) {
return false;
}
// Include demand if its ID is not in favorites
return true;
});

// Map demand material demands to options
const demandOptions = filteredDemandOptions.map((md: any) => {
const label = (
<div>
{md.materialNumberCustomer || 'N/A'} - {md.materialNumberSupplier || 'N/A'} - {md.materialDescriptionCustomer || 'N/A'}
</div>
);
return {
value: md,
label: label,
};
});

// Combine favorite options and demand options
materialDemandOptions = [...favoriteOptions, ...demandOptions];

setOptions(materialDemandOptions); // Update options state with combined material demands
} catch (error) {
console.error('Error fetching filtered capacity groups:', error);
} finally {
setIsLoading(false);
}
};
fetchData();
console.log(demands);
}, [demands]);


return (
<>
<Modal
Expand Down Expand Up @@ -282,24 +362,18 @@ function CapacityGroupWizardModal({ show, onHide, checkedDemands, demands }: Cap
)}
<Container className="mt-4">
<Select
options={demands?.filter(demand => {
// Filter out demands that are in checkedDemands or selectedDemands
return (
!checkedDemands?.some(checkedDemand => checkedDemand.id === demand.id) &&
!selectedDemands.some(selectedDemand => selectedDemand.id === demand.id)
);
}).map(demand => ({
value: demand,
label: `${demand.id} - ${demand.materialDescriptionCustomer} - ${demand.materialNumberCustomer}`
}))}
value={null} //Just so that we dont get stuck on the data that was selected
options={options}
value={null} //Just so that we don't get stuck on the data that was selected
onChange={selectedOption => {
if (selectedOption) {
setSelectedDemands([...selectedDemands, selectedOption.value]);
}
}}
isSearchable
placeholder={<><FaSearch /> Search for demands...</>}
components={{
Option: CustomOption, // Use the CustomOption component for rendering options
}}
/>
{selectedDemands.length > 0 && (
<Row>
Expand All @@ -316,7 +390,7 @@ function CapacityGroupWizardModal({ show, onHide, checkedDemands, demands }: Cap
<p key={index}>
<strong>Description:</strong> {demand ? demand.materialDescriptionCustomer : 'Not selected'}
<br />
<strong>Customer:</strong> {demand ? demand.customer.companyName : 'Not selected'}
<strong>Customer:</strong> {demand?.customer?.companyName || 'Not selected'}
<br />
<strong>Material Number Customer:</strong> {demand ? demand.materialNumberCustomer : 'Not selected'}
<br />
Expand Down Expand Up @@ -372,7 +446,7 @@ function CapacityGroupWizardModal({ show, onHide, checkedDemands, demands }: Cap
<p>
<strong>Description:</strong> {demand ? demand.materialDescriptionCustomer : 'Not selected'}
<br />
<strong>Customer:</strong> {demand ? demand.customer.companyName : 'Not selected'}
<strong>Customer:</strong> {demand?.customer?.companyName || 'Not selected'}
<br />
<strong>Material Number Customer:</strong> {demand ? demand.materialNumberCustomer : 'Not selected'}
<br />
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -91,7 +91,7 @@ const CompanyOptions: React.FC<CompanyOptionsProps> = ({ selectedCompanyName, on
);

if (loading) {
return <div>Loading options...</div>;
return <div>Loading companies...</div>;
}

return (
Expand Down
41 changes: 29 additions & 12 deletions demand-capacity-mgmt-frontend/src/components/events/EventsTable.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,8 @@ import {
FaArrowUp,
FaCopy,
FaEllipsisV,
FaTrashAlt
FaTrashAlt,
FaUndo
} from 'react-icons/fa';
import { LuStar } from 'react-icons/lu';
import { EventsContext } from '../../contexts/EventsContextProvider';
Expand Down Expand Up @@ -215,13 +216,17 @@ const EventsTable: React.FC<EventsTableProps> = ({ events, isArchive }) => {
{currentEvents.map((event, index) => (
<tr key={index}>
<td>
<span className='inlinefav'>
<LuStar
className={favoriteEvents.includes(event.logID) ? "text-warning" : "text-muted"}
opacity={favoriteEvents.includes(event.logID) ? "1" : "0.2"}
onClick={() => toggleFavorite(event.logID)}
size={25}
/></span>
{!isArchive && (
<span className='inlinefav'>
<LuStar
className={favoriteEvents.includes(event.logID) ? "text-warning" : "text-muted"}
opacity={favoriteEvents.includes(event.logID) ? "1" : "0.2"}
onClick={() => toggleFavorite(event.logID)}
size={25}
/>
</span>
)}

</td>
<td>{new Date(event.timeCreated).toLocaleString()}</td>
<td><OverlayTrigger
Expand Down Expand Up @@ -270,15 +275,27 @@ const EventsTable: React.FC<EventsTableProps> = ({ events, isArchive }) => {
<span><FaEllipsisV /></span>
</Dropdown.Toggle>
<Dropdown.Menu>
<Dropdown.Item onClick={() => handleArchiveClick(event)}><FaArchive
className='text-muted' /> Archive</Dropdown.Item>
{isArchive ? (
<Dropdown.Item onClick={() => handleDeleteButtonClick(event.id.toString())}>
<FaUndo className='text-muted' /> Unarchive
</Dropdown.Item>
) : (
<Dropdown.Item onClick={() => handleArchiveClick(event)}>
<FaArchive className='text-muted' /> Archive
</Dropdown.Item>
)}
<Dropdown.Item onClick={() => {
navigator.clipboard.writeText(event.id.toString())
}}><FaCopy /> Copy ID</Dropdown.Item>
<Dropdown.Item className="red-delete-item"
onClick={() => handleDeleteButtonClick(event.id.toString())}><FaTrashAlt /> Delete</Dropdown.Item>
{isArchive ? null : (
<Dropdown.Item className="red-delete-item"
onClick={() => handleDeleteButtonClick(event.id.toString())}>
<FaTrashAlt /> Delete
</Dropdown.Item>
)}
</Dropdown.Menu>
</Dropdown>

</td>
</tr>
))}
Expand Down
Loading

0 comments on commit 143e4cd

Please sign in to comment.