Skip to content

Commit

Permalink
M2-5060 Change logic of monthly event calculation (#404)
Browse files Browse the repository at this point in the history
* Change logic of monthly event calculation

* Test file for monthly events calculations

* Fix all warnings in ScheduledDateCalculator.monthly.test

* Fix all warnings in ScheduledDateCalculator

* Fix all warnings in AvailableGroupBuilder.test and ScheduledGroupEvaluator.test

* Fix all warnings in AvailableGroupEvaluator

* Fix all warnings in GroupUtility

---------

Co-authored-by: Viktor Riabkov <[email protected]>
  • Loading branch information
moiskillnadne and Viktor Riabkov authored Mar 5, 2024
1 parent 6e1a23e commit a46e851
Show file tree
Hide file tree
Showing 6 changed files with 477 additions and 49 deletions.
9 changes: 7 additions & 2 deletions src/abstract/lib/GroupBuilder/AvailableGroupBuilder.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -73,8 +73,13 @@ const getScheduledEventEntity = (settings: {
const section = getScheduledSection();

const scheduledAt = new Date(scheduledAtDay);
scheduledAt.setHours(section.timeFrom!.hours);
scheduledAt.setMinutes(section.timeFrom!.minutes);

if (section.timeFrom === null) {
throw new Error('[getScheduledEventEntity]: timeFrom is null');
}

scheduledAt.setHours(section.timeFrom.hours);
scheduledAt.setMinutes(section.timeFrom.minutes);

const result: EventEntity = {
entity: getActivity(),
Expand Down
18 changes: 15 additions & 3 deletions src/abstract/lib/GroupBuilder/AvailableGroupEvaluator.ts
Original file line number Diff line number Diff line change
Expand Up @@ -31,10 +31,22 @@ export class AvailableGroupEvaluator implements IEvaluator<EventEntity> {

const now = this.utility.getNow();

if (event.availability.timeFrom === null) {
throw new Error('[isValidWhenNoSpreadAndNoAccessBeforeStartTime] timeFrom is null');
}

if (event.availability.timeTo === null) {
throw new Error('[isValidWhenNoSpreadAndNoAccessBeforeStartTime] timeTo is null');
}

if (event.scheduledAt === null) {
throw new Error('[isValidWhenNoSpreadAndNoAccessBeforeStartTime] scheduledAt is null');
}

const isCurrentTimeInTimeWindow = isTimeInInterval({
timeToCheck: getHourMinute(now),
intervalFrom: event.availability.timeFrom!,
intervalTo: event.availability.timeTo!,
intervalFrom: event.availability.timeFrom,
intervalTo: event.availability.timeTo,
including: 'from',
});

Expand All @@ -45,7 +57,7 @@ export class AvailableGroupEvaluator implements IEvaluator<EventEntity> {
const isCompletedToday = !!endAt && this.utility.isToday(new Date(endAt));

return (
isScheduledToday && now > event.scheduledAt! && isCurrentTimeInTimeWindow && !isCompletedToday
isScheduledToday && now > event.scheduledAt && isCurrentTimeInTimeWindow && !isCompletedToday
);
}

Expand Down
60 changes: 48 additions & 12 deletions src/abstract/lib/GroupBuilder/GroupUtility.ts
Original file line number Diff line number Diff line change
Expand Up @@ -31,9 +31,17 @@ export class GroupUtility {
}

private getStartedAt(eventActivity: EventEntity): Date {
const record = this.getProgressRecord(eventActivity)!;
const record = this.getProgressRecord(eventActivity);

if (record === null) {
throw new Error('[getStartedAt] Progress record is null');
}

if (record.startAt === null) {
throw new Error('[getStartedAt] Progress record startAt is null');
}

return new Date(record.startAt!);
return new Date(record.startAt);
}

private getAllowedTimeInterval(
Expand All @@ -43,8 +51,16 @@ export class GroupUtility {
): DatesFromTo {
const { event } = eventActivity;

const { hours: hoursFrom, minutes: minutesFrom } = event.availability.timeFrom!;
const { hours: hoursTo, minutes: minutesTo } = event.availability.timeTo!;
if (event.availability.timeFrom === null) {
throw new Error('[getAllowedTimeInterval] timeFrom is null');
}

if (event.availability.timeTo === null) {
throw new Error('[getAllowedTimeInterval] timeTo is null');
}

const { hours: hoursFrom, minutes: minutesFrom } = event.availability.timeFrom;
const { hours: hoursTo, minutes: minutesTo } = event.availability.timeTo;

if (scheduledWhen === 'today') {
const allowedFrom = this.getToday();
Expand Down Expand Up @@ -154,25 +170,41 @@ export class GroupUtility {

let from = this.getToday();

if (timeFrom === null) {
throw new Error('[getVoidInterval] timeFrom is null');
}

if (timeTo === null) {
throw new Error('[getVoidInterval] timeTo is null');
}

if (buildFrom) {
from = this.getToday();
from.setHours(timeTo!.hours);
from.setMinutes(timeTo!.minutes);
from.setHours(timeTo.hours);
from.setMinutes(timeTo.minutes);
}

const to = this.getToday();
to.setHours(timeFrom!.hours);
to.setMinutes(timeFrom!.minutes);
to.setHours(timeFrom.hours);
to.setMinutes(timeFrom.minutes);

return { from, to };
}

public isSpreadToNextDay(event: ScheduleEvent): boolean {
if (event.availability.timeFrom === null) {
throw new Error('[isSpreadToNextDay] timeFrom is null');
}

if (event.availability.timeTo === null) {
throw new Error('[isSpreadToNextDay] timeTo is null');
}

return (
event.availability.availabilityType === AvailabilityLabelType.ScheduledAccess &&
isSourceLess({
timeSource: event.availability.timeTo!,
timeTarget: event.availability.timeFrom!,
timeSource: event.availability.timeTo,
timeTarget: event.availability.timeFrom,
})
);
}
Expand All @@ -188,7 +220,7 @@ export class GroupUtility {
isAccessBeforeStartTime,
);

const completedAt = this.getCompletedAt(eventActivity)!;
const completedAt = this.getCompletedAt(eventActivity);

if (!completedAt) {
return false;
Expand Down Expand Up @@ -272,7 +304,11 @@ export class GroupUtility {

public getTimeToComplete(eventActivity: EventEntity): HourMinute | null {
const { event } = eventActivity;
const timer = event.timers.timer!;
const timer = event.timers.timer;

if (timer === null) {
throw new Error('[getTimeToComplete] Timer is null');
}

const startedTime = this.getStartedAt(eventActivity);

Expand Down
9 changes: 7 additions & 2 deletions src/abstract/lib/GroupBuilder/ScheduledGroupEvaluator.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -72,8 +72,13 @@ const getScheduledEventEntity = (settings: {
const section = getScheduledSection();

const scheduledAt = new Date(scheduledAtDay);
scheduledAt.setHours(section.timeFrom!.hours);
scheduledAt.setMinutes(section.timeFrom!.minutes);

if (section.timeFrom === null) {
throw new Error('[getScheduledEventEntity]: timeFrom is null');
}

scheduledAt.setHours(section.timeFrom.hours);
scheduledAt.setMinutes(section.timeFrom.minutes);

const result: EventEntity = {
entity: getActivity(),
Expand Down
Loading

0 comments on commit a46e851

Please sign in to comment.