Skip to content

Commit

Permalink
make getCoverageDueDate configurable
Browse files Browse the repository at this point in the history
SDBELGA-782
  • Loading branch information
petrjasek committed May 6, 2024
1 parent bc73e1e commit f58eb5c
Show file tree
Hide file tree
Showing 4 changed files with 60 additions and 27 deletions.
6 changes: 6 additions & 0 deletions client/config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -78,6 +78,10 @@ if (appConfig.planning == null) {
appConfig.planning = {};
}

if (appConfig.coverage == null) {
appConfig.coverage = {};
}

export function updateConfigAfterLoad() {
if (appConfig?.planning?.dateformat == null) {
appConfig.planning.dateformat = appConfig.view.dateformat;
Expand All @@ -95,3 +99,5 @@ export function updateConfigAfterLoad() {
appConfig.planning.autosave_timeout = 1500;
}
}

export const planningConfig = appConfig as IPlanningConfig;
6 changes: 6 additions & 0 deletions client/configure.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
import {planningConfig} from './config';
import {IPlanningConfig} from './interfaces';

export const setCoverageDueDateCallback = (callback: IPlanningConfig['coverage']['getDueDate']) => {
planningConfig.coverage.getDueDate = callback;
};
4 changes: 4 additions & 0 deletions client/interfaces.ts
Original file line number Diff line number Diff line change
Expand Up @@ -296,6 +296,10 @@ export interface IPlanningConfig extends ISuperdeskGlobalConfig {
default_create_planning_series_with_event_series?: boolean;
event_related_item_search_provider_name?: string;
};

coverage?: {
getDueDate?(planningItem: IPlanningItem, eventItem?: IEventItem): moment.Moment | null;
};
}

export interface ISession {
Expand Down
71 changes: 44 additions & 27 deletions client/utils/planning.ts
Original file line number Diff line number Diff line change
Expand Up @@ -61,9 +61,9 @@ import {
sanitizeItemFields,
stringUtils,
} from './index';
import {getUsersDefaultLanguage} from './users';
import * as selectors from '../selectors';
import {IMenuItem} from 'superdesk-ui-framework/react/components/Menu';
import {planningConfig} from '../config';

const isCoverageAssigned = (coverage) => !!get(coverage, 'assigned_to.desk');

Expand Down Expand Up @@ -1361,34 +1361,10 @@ function defaultCoverageValues(
}

if (planningItem) {
let coverageTime: moment.Moment = null;

if (planningItem?.event_item == null) {
coverageTime = moment(planningItem?.planning_date || moment());
} else if (eventItem) {
coverageTime = moment(eventItem?.dates?.end || moment());
}
const getCoverageDueDateCallback = planningConfig.coverage?.getDueDate || getDefaultCoverageDueDate;
const coverageTime = getCoverageDueDateCallback(planningItem as IPlanningItem, eventItem);

if (coverageTime) {
coverageTime.add(1, 'hour');

// Only round up to the hour if we didn't derive coverage time from an Event
if (!eventItem) {
coverageTime.minute() ?
coverageTime
.add(1, 'hour')
.startOf('hour') :
coverageTime.startOf('hour');
}

if (moment().isAfter(coverageTime)) {
coverageTime = moment();
coverageTime.minute() ?
coverageTime
.add(1, 'hour')
.startOf('hour') :
coverageTime.startOf('hour');
}
newCoverage.planning.scheduled = coverageTime;
}

Expand All @@ -1407,13 +1383,54 @@ function defaultCoverageValues(
}
}
}

newCoverage.planning._scheduledTime = newCoverage.planning.scheduled;
}

self.setDefaultAssignment(newCoverage, preferredCoverageDesks, g2contentType, defaultDesk);
return newCoverage;
}

function getDefaultCoverageDueDate(
planningItem: IPlanningItem,
eventItem?: IEventItem,
): moment.Moment | null {
let coverageTime: moment.Moment = null;

if (eventItem) {
coverageTime = moment(eventItem?.dates?.end);
} else if (planningItem.planning_date) {
coverageTime = moment('' + planningItem.planning_date);
}

if (!coverageTime) {
return coverageTime;
}

coverageTime.add(1, 'hour');

// Only round up to the hour if we didn't derive coverage time from an Event
if (!eventItem) {
coverageTime.minute() ?
coverageTime
.add(1, 'hour')
.startOf('hour') :
coverageTime.startOf('hour');
}

// If the coverage time is in the past, set it to the current time
if (moment().isAfter(coverageTime)) {
coverageTime = moment();
coverageTime.minute() ?
coverageTime
.add(1, 'hour')
.startOf('hour') :
coverageTime.startOf('hour');
}

return coverageTime;
}

function setDefaultAssignment(
coverage: DeepPartial<IPlanningCoverageItem>,
preferredCoverageDesks: {[key: string]: IDesk['_id']},
Expand Down

0 comments on commit f58eb5c

Please sign in to comment.