removed seconds for new api implementation

This commit is contained in:
Arnaud Vergnet 2020-03-22 11:58:24 +01:00
parent a6771f442a
commit c6fb369863
2 changed files with 60 additions and 57 deletions

View file

@ -15,7 +15,7 @@ export type eventObject = {
export default class PlanningEventManager {
// Regex used to check date string validity
static dateRegExp = /^\d{4}-\d{2}-\d{2} \d{2}:\d{2}:\d{2}$/;
static dateRegExp = /^\d{4}-\d{2}-\d{2} \d{2}:\d{2}$/;
/**
* Gets the current day string representation in the format
@ -30,8 +30,8 @@ export default class PlanningEventManager {
/**
* Checks if the given date is before the other.
*
* @param event1Date Event 1 date in format YYYY-MM-DD HH:MM:SS
* @param event2Date Event 2 date in format YYYY-MM-DD HH:MM:SS
* @param event1Date Event 1 date in format YYYY-MM-DD HH:MM
* @param event2Date Event 2 date in format YYYY-MM-DD HH:MM
* @return {boolean}
*/
static isEventBefore(event1Date: string, event2Date: string) {
@ -45,7 +45,7 @@ export default class PlanningEventManager {
/**
* Gets only the date part of the given event date string in the format
* YYYY-MM-DD HH:MM:SS
* YYYY-MM-DD HH:MM
*
* @param dateString The string to get the date from
* @return {string|null} Date in format YYYY:MM:DD or null if given string is invalid
@ -59,7 +59,7 @@ export default class PlanningEventManager {
/**
* Checks if the given date string is in the format
* YYYY-MM-DD HH:MM:SS
* YYYY-MM-DD HH:MM
*
* @param dateString The string to check
* @return {boolean}
@ -72,7 +72,7 @@ export default class PlanningEventManager {
/**
* Converts the given date string to a date object.<br>
* Accepted format: YYYY-MM-DD HH:MM:SS
* Accepted format: YYYY-MM-DD HH:MM
*
* @param dateString The string to convert
* @return {Date|null} The date object or null if the given string is invalid
@ -91,7 +91,7 @@ export default class PlanningEventManager {
date.setHours(
parseInt(timeArray[0]),
parseInt(timeArray[1]),
parseInt(timeArray[2]),
0,
0,
);
} else
@ -113,8 +113,7 @@ export default class PlanningEventManager {
const year = date.getFullYear();
const hours = String(date.getHours()).padStart(2, '0');
const minutes = String(date.getMinutes()).padStart(2, '0');
const seconds = String(date.getSeconds()).padStart(2, '0');
return year + '-' + month + '-' + day + ' ' + hours + ':' + minutes + ':' + seconds;
return year + '-' + month + '-' + day + ' ' + hours + ':' + minutes;
}
/**
@ -172,9 +171,8 @@ export default class PlanningEventManager {
return true;
}
static generateEmptyCalendar(numberOfMonths: number) {
let end = new Date(new Date().setMonth(new Date().getMonth() + numberOfMonths + 1));
const end = new Date(new Date().setMonth(new Date().getMonth() + numberOfMonths + 1));
let daysOfYear = {};
for (let d = new Date(); d <= end; d.setDate(d.getDate() + 1)) {
const dateString = PlanningEventManager.getDateOnlyString(
@ -189,6 +187,7 @@ export default class PlanningEventManager {
let agendaItems = PlanningEventManager.generateEmptyCalendar(numberOfMonths);
for (let i = 0; i < eventList.length; i++) {
const dateString = PlanningEventManager.getDateOnlyString(eventList[i].date_begin);
console.log(dateString);
if (dateString !== null)
this.pushEventInOrder(agendaItems, eventList[i], dateString);
}
@ -196,18 +195,21 @@ export default class PlanningEventManager {
}
static pushEventInOrder(agendaItems: Object, event: eventObject, startDate: string) {
if (agendaItems[startDate].length === 0)
agendaItems[startDate].push(event);
else {
for (let i = 0; i < agendaItems[startDate].length; i++) {
if (PlanningEventManager.isEventBefore(event.date_begin, agendaItems[startDate][i].date_end)) {
agendaItems[startDate].splice(i, 0, event);
break;
} else if (i === agendaItems[startDate].length - 1) {
agendaItems[startDate].push(event);
break;
if (agendaItems[startDate] !== undefined) {
if (agendaItems[startDate].length === 0)
agendaItems[startDate].push(event);
else {
for (let i = 0; i < agendaItems[startDate].length; i++) {
if (PlanningEventManager.isEventBefore(event.date_begin, agendaItems[startDate][i].date_end)) {
agendaItems[startDate].splice(i, 0, event);
break;
} else if (i === agendaItems[startDate].length - 1) {
agendaItems[startDate].push(event);
break;
}
}
}
}
}
}

View file

@ -18,12 +18,13 @@ test('isDescriptionEmpty', () => {
});
test('isEventDateStringFormatValid', () => {
expect(PlanningEventManager.isEventDateStringFormatValid("2020-03-21 09:00:00")).toBeTrue();
expect(PlanningEventManager.isEventDateStringFormatValid("3214-64-12 01:16:65")).toBeTrue();
expect(PlanningEventManager.isEventDateStringFormatValid("2020-03-21 09:00")).toBeTrue();
expect(PlanningEventManager.isEventDateStringFormatValid("3214-64-12 01:16")).toBeTrue();
expect(PlanningEventManager.isEventDateStringFormatValid("3214-64-12 1:16:65")).toBeFalse();
expect(PlanningEventManager.isEventDateStringFormatValid("3214-f4-12 01:16:65")).toBeFalse();
expect(PlanningEventManager.isEventDateStringFormatValid("sqdd 09:00:00")).toBeFalse();
expect(PlanningEventManager.isEventDateStringFormatValid("3214-64-12 01:16:00")).toBeFalse();
expect(PlanningEventManager.isEventDateStringFormatValid("3214-64-12 1:16")).toBeFalse();
expect(PlanningEventManager.isEventDateStringFormatValid("3214-f4-12 01:16")).toBeFalse();
expect(PlanningEventManager.isEventDateStringFormatValid("sqdd 09:00")).toBeFalse();
expect(PlanningEventManager.isEventDateStringFormatValid("2020-03-21")).toBeFalse();
expect(PlanningEventManager.isEventDateStringFormatValid("2020-03-21 truc")).toBeFalse();
expect(PlanningEventManager.isEventDateStringFormatValid("3214-64-12 1:16:65")).toBeFalse();
@ -40,17 +41,17 @@ test('stringToDate', () => {
expect(PlanningEventManager.stringToDate("garbage")).toBeNull();
expect(PlanningEventManager.stringToDate("2020-03-21")).toBeNull();
expect(PlanningEventManager.stringToDate("09:00:00")).toBeNull();
expect(PlanningEventManager.stringToDate("2020-03-21 09:g0:00")).toBeNull();
expect(PlanningEventManager.stringToDate("2020-03-21 09:g0")).toBeNull();
expect(PlanningEventManager.stringToDate("2020-03-21 09:g0:")).toBeNull();
testDate.setFullYear(2020, 2, 21);
testDate.setHours(9, 0, 0, 0);
expect(PlanningEventManager.stringToDate("2020-03-21 09:00:00")).toEqual(testDate);
expect(PlanningEventManager.stringToDate("2020-03-21 09:00")).toEqual(testDate);
testDate.setFullYear(2020, 0, 31);
testDate.setHours(18, 30, 50, 0);
expect(PlanningEventManager.stringToDate("2020-01-31 18:30:50")).toEqual(testDate);
testDate.setHours(18, 30, 0, 0);
expect(PlanningEventManager.stringToDate("2020-01-31 18:30")).toEqual(testDate);
testDate.setFullYear(2020, 50, 50);
testDate.setHours(65, 65, 65, 0);
expect(PlanningEventManager.stringToDate("2020-51-50 65:65:65")).toEqual(testDate);
testDate.setHours(65, 65, 0, 0);
expect(PlanningEventManager.stringToDate("2020-51-50 65:65")).toEqual(testDate);
});
test('getFormattedEventTime', () => {
@ -58,24 +59,24 @@ test('getFormattedEventTime', () => {
.toBe('/ - /');
expect(PlanningEventManager.getFormattedEventTime(undefined, undefined))
.toBe('/ - /');
expect(PlanningEventManager.getFormattedEventTime("20:30:00", "23:00:00"))
expect(PlanningEventManager.getFormattedEventTime("20:30", "23:00"))
.toBe('/ - /');
expect(PlanningEventManager.getFormattedEventTime("2020-03-30", "2020-03-31"))
.toBe('/ - /');
expect(PlanningEventManager.getFormattedEventTime("2020-03-21 09:00:00", "2020-03-21 09:00:00"))
expect(PlanningEventManager.getFormattedEventTime("2020-03-21 09:00", "2020-03-21 09:00"))
.toBe('09:00');
expect(PlanningEventManager.getFormattedEventTime("2020-03-21 09:00:00", "2020-03-22 17:00:00"))
expect(PlanningEventManager.getFormattedEventTime("2020-03-21 09:00", "2020-03-22 17:00"))
.toBe('09:00 - 23:59');
expect(PlanningEventManager.getFormattedEventTime("2020-03-30 20:30:00", "2020-03-30 23:00:00"))
expect(PlanningEventManager.getFormattedEventTime("2020-03-30 20:30", "2020-03-30 23:00"))
.toBe('20:30 - 23:00');
});
test('getDateOnlyString', () => {
expect(PlanningEventManager.getDateOnlyString("2020-03-21 09:00:00")).toBe("2020-03-21");
expect(PlanningEventManager.getDateOnlyString("2021-12-15 09:00:00")).toBe("2021-12-15");
expect(PlanningEventManager.getDateOnlyString("2021-12-o5 09:00:00")).toBeNull();
expect(PlanningEventManager.getDateOnlyString("2020-03-21 09:00")).toBe("2020-03-21");
expect(PlanningEventManager.getDateOnlyString("2021-12-15 09:00")).toBe("2021-12-15");
expect(PlanningEventManager.getDateOnlyString("2021-12-o5 09:00")).toBeNull();
expect(PlanningEventManager.getDateOnlyString("2021-12-15 09:")).toBeNull();
expect(PlanningEventManager.getDateOnlyString("2021-12-15")).toBeNull();
expect(PlanningEventManager.getDateOnlyString("garbage")).toBeNull();
@ -83,33 +84,29 @@ test('getDateOnlyString', () => {
test('isEventBefore', () => {
expect(PlanningEventManager.isEventBefore(
"2020-03-21 09:00:00", "2020-03-21 10:00:00")).toBeTrue();
"2020-03-21 09:00", "2020-03-21 10:00")).toBeTrue();
expect(PlanningEventManager.isEventBefore(
"2020-03-21 10:00:00", "2020-03-21 10:15:00")).toBeTrue();
"2020-03-21 10:00", "2020-03-21 10:15")).toBeTrue();
expect(PlanningEventManager.isEventBefore(
"2020-03-21 10:15:05", "2020-03-21 10:15:54")).toBeTrue();
"2020-03-21 10:15", "2021-03-21 10:15")).toBeTrue();
expect(PlanningEventManager.isEventBefore(
"2020-03-21 10:15:05", "2021-03-21 10:15:05")).toBeTrue();
"2020-03-21 10:15", "2020-05-21 10:15")).toBeTrue();
expect(PlanningEventManager.isEventBefore(
"2020-03-21 10:15:05", "2020-05-21 10:15:05")).toBeTrue();
expect(PlanningEventManager.isEventBefore(
"2020-03-21 10:15:05", "2020-03-30 10:15:05")).toBeTrue();
"2020-03-21 10:15", "2020-03-30 10:15")).toBeTrue();
expect(PlanningEventManager.isEventBefore(
"2020-03-21 10:00:00", "2020-03-21 09:00:00")).toBeFalse();
"2020-03-21 10:00", "2020-03-21 09:00")).toBeFalse();
expect(PlanningEventManager.isEventBefore(
"2020-03-21 10:15:00", "2020-03-21 10:00:00")).toBeFalse();
"2020-03-21 10:15", "2020-03-21 10:00")).toBeFalse();
expect(PlanningEventManager.isEventBefore(
"2020-03-21 10:15:54", "2020-03-21 10:15:05")).toBeFalse();
"2021-03-21 10:15", "2020-03-21 10:15")).toBeFalse();
expect(PlanningEventManager.isEventBefore(
"2021-03-21 10:15:05", "2020-03-21 10:15:05")).toBeFalse();
"2020-05-21 10:15", "2020-03-21 10:15")).toBeFalse();
expect(PlanningEventManager.isEventBefore(
"2020-05-21 10:15:05", "2020-03-21 10:15:05")).toBeFalse();
expect(PlanningEventManager.isEventBefore(
"2020-03-30 10:15:05", "2020-03-21 10:15:05")).toBeFalse();
"2020-03-30 10:15", "2020-03-21 10:15")).toBeFalse();
expect(PlanningEventManager.isEventBefore(
"garbage", "2020-03-21 10:15:05")).toBeFalse();
"garbage", "2020-03-21 10:15")).toBeFalse();
expect(PlanningEventManager.isEventBefore(
undefined, undefined)).toBeFalse();
});
@ -118,12 +115,16 @@ test('dateToString', () => {
let testDate = new Date();
testDate.setFullYear(2020, 2, 21);
testDate.setHours(9, 0, 0, 0);
expect(PlanningEventManager.dateToString(testDate)).toBe("2020-03-21 09:00:00");
expect(PlanningEventManager.dateToString(testDate)).toBe("2020-03-21 09:00");
testDate.setFullYear(2021, 0, 12);
testDate.setHours(9, 10, 0, 0);
expect(PlanningEventManager.dateToString(testDate)).toBe("2021-01-12 09:10:00");
expect(PlanningEventManager.dateToString(testDate)).toBe("2021-01-12 09:10");
testDate.setFullYear(2022, 11, 31);
testDate.setHours(9, 10, 15, 0);
expect(PlanningEventManager.dateToString(testDate)).toBe("2022-12-31 09:10:15");
expect(PlanningEventManager.dateToString(testDate)).toBe("2022-12-31 09:10");
});
test('generateEmptyCalendar', () => {
});