Skip to content

Commit

Permalink
message error bug fixes
Browse files Browse the repository at this point in the history
  • Loading branch information
OgunyemiO committed Aug 29, 2024
1 parent 103be99 commit fa6a6f5
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 16 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -222,7 +222,7 @@ describe('ServiceMessagesComponent', () => {
expect(component.filteredMessages.length).toBe(0);
fixture.detectChanges();
expect(component.isBannerError).toBeTruthy();
expect(component.bannerErrorMsgs).toContain({ message: `Invalid start/end date OR The start date is greater than the end date. Message index: ${serviceMessagesFake[0].index}`, index: 2 });
expect(component.bannerErrorMsgs).toContain({ message: `The start date is greater than the end date. Message index: ${serviceMessagesFake[0].index}`, index: 2 });
});

it('should show error message if start date or end date is not well formed', () => {
Expand All @@ -241,6 +241,6 @@ describe('ServiceMessagesComponent', () => {
fixture.autoDetectChanges();
expect(component.isBannerError).toBeTrue();
expect(component.bannerErrorMsgs.length).toBe(1);
expect(component.bannerErrorMsgs).toContain({ message: `Invalid start/end date OR The start date is greater than the end date. Message index: ${serviceMessagesFake[0].index}`, index: 10 });
expect(component.bannerErrorMsgs).toContain({ message: `Invalid start date. Message index: ${serviceMessagesFake[0].index}`, index: 10 });
});
});
Original file line number Diff line number Diff line change
Expand Up @@ -48,30 +48,47 @@ export class ServiceMessagesComponent implements OnInit {

private compareDates(msg: ServiceMessages): boolean {
let currentDateTime = new Date();
let beginDate = null;
let endDate = null;
let beginDate: Date | null = null;
let endDate: Date | null = null;
this.bannerErrorMsgs = [];

if (msg.begin !== undefined && !isNaN(Date.parse(msg.begin))) {
// Parse beginDate if msg.begin is present and valid
if (msg.begin && !isNaN(Date.parse(msg.begin))) {
beginDate = new Date(msg.begin);
}

if (msg.end !== undefined && !isNaN(Date.parse(msg.end))) {
if (msg.end && !isNaN(Date.parse(msg.end))) {
endDate = new Date(msg.end);
}
const findAll = this.originalMessages.filter((msg) => (msg.begin || msg.end) && beginDate > endDate || (msg.begin && isNaN(Date.parse(msg.begin))) || (msg.end && isNaN(Date.parse(msg.end))));

if (findAll.length > 0) {
this.isBannerError = true;
findAll.forEach(msg => {
this.bannerErrorMsgs = [...this.bannerErrorMsgs, { message: `Invalid start/end date OR The start date is greater than the end date. Message index: ${msg.index}`, index: msg.index }];
})
} else {
this.isBannerError = false;
}
this.originalMessages.forEach(msg => {
// Only check for errors if both beginDate and endDate are present and valid
if (beginDate && endDate && beginDate > endDate) {
this.isBannerError = true;
this.bannerErrorMsgs.push({
message: `The start date is greater than the end date. Message index: ${msg.index}`,
index: msg.index
});
}
// Check for invalid beginDate or endDate separately, if they are present
if (msg.begin && isNaN(Date.parse(msg.begin))) {
this.isBannerError = true;
this.bannerErrorMsgs.push({
message: `Invalid start date. Message index: ${msg.index}`,
index: msg.index
});
}
if (msg.end && isNaN(Date.parse(msg.end))) {
this.isBannerError = true;
this.bannerErrorMsgs.push({
message: `Invalid end date. Message index: ${msg.index}`,
index: msg.index
});
}
});

const beginDateOK = !msg.begin || (beginDate && beginDate < currentDateTime);
const endDateOK = !msg.end || (endDate && endDate >= currentDateTime);

return beginDateOK && endDateOK;
}

Expand Down

0 comments on commit fa6a6f5

Please sign in to comment.