Skip to content

Commit

Permalink
rework scheduled updates to workflow so logic is contained inside cov…
Browse files Browse the repository at this point in the history
…erages field
  • Loading branch information
tomaskikutis committed Dec 13, 2024
1 parent 273d0e4 commit 8bd4c20
Show file tree
Hide file tree
Showing 12 changed files with 73 additions and 104 deletions.
17 changes: 0 additions & 17 deletions client/actions/planning/ui.ts
Original file line number Diff line number Diff line change
Expand Up @@ -514,22 +514,6 @@ const saveFromAuthoring = (original, updates) => (
}
);

const addScheduledUpdateToWorkflow = (original, coverage, coverageIndex, scheduledUpdate, index) => (
(dispatch, getState, {notify}) => {
let updates = {coverages: cloneDeep(original.coverages)};
let coverage = updates.coverages[coverageIndex];

coverage.scheduled_updates[index] = planningUtils.addToWorkflow(scheduledUpdate,
selectors.general.newsCoverageStatus(getState()));

return dispatch(planningApis.save(original, updates))
.then((savedItem) => {
notify.success(gettext('Scheduled update added to workflow.'));
return dispatch(self.updateItemOnSave(savedItem));
});
}
);

/**
* Action to update the values of a single Coverage so the Assignment is placed in the workflow
* @param {object} original - Original Planning item
Expand Down Expand Up @@ -597,7 +581,6 @@ const self = {
openFeaturedPlanningModal,
updateItemOnSave,
addNewCoverageToPlanning,
addScheduledUpdateToWorkflow,
};

export default self;
22 changes: 20 additions & 2 deletions client/api/planning.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import {
ISearchSpikeState,
IPlanningRelatedEventLink,
IPlanningNewsCoverageStatus,
ICoverageScheduledUpdate,
} from '../interfaces';
import {appConfig} from 'appConfig';

Expand Down Expand Up @@ -218,7 +219,7 @@ function bulkAddCoverageToWorkflow(planningItems: Array<IPlanningItem>): Promise
const updates = {coverages: cloneDeep(plan.coverages)};

updates.coverages = plan.coverages
.map((coverage) => planningUtils.addToWorkflow(coverage, coverageStatuses));
.map((coverage) => planningUtils.addCoverageToWorkflow(coverage, coverageStatuses));

return planning.update(plan, updates)
.then((updatedPlan) => {
Expand Down Expand Up @@ -253,13 +254,29 @@ function addCoverageToWorkflow(

return coverages.map((coverage) => {
if (coverage.coverage_id === coverageToAddToWorkflow.coverage_id) {
return planningUtils.addToWorkflow(coverageToAddToWorkflow, coverageStatuses);
return planningUtils.addCoverageToWorkflow(coverageToAddToWorkflow, coverageStatuses);
} else {
return coverage;
}
});
}

function addScheduledUpdateToWorkflow(
updates: Array<ICoverageScheduledUpdate>,
updateToAddToWorkflow: ICoverageScheduledUpdate,
): Array<ICoverageScheduledUpdate> {
const {vocabulary} = superdeskApi.entities;
const coverageStatuses = vocabulary.getAll().get('newscoveragestatus').items as Array<IPlanningNewsCoverageStatus>;

return updates.map((update) => {
if (update.scheduled_update_id === updateToAddToWorkflow.scheduled_update_id) {
return planningUtils.addScheduledUpdateToWorkflow(updateToAddToWorkflow, coverageStatuses);
} else {
return update;
}
});
}

export const planning: IPlanningAPI['planning'] = {
search: searchPlanning,
searchGetAll: searchPlanningGetAll,
Expand All @@ -274,6 +291,7 @@ export const planning: IPlanningAPI['planning'] = {
coverages: {
setDefaultValues: setDefaultValues,
addCoverageToWorkflow: addCoverageToWorkflow,
addScheduledUpdateToWorkflow: addScheduledUpdateToWorkflow,
bulkAddCoverageToWorkflow: bulkAddCoverageToWorkflow,
},
};
6 changes: 0 additions & 6 deletions client/components/Coverages/CoverageArrayInput.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -66,12 +66,6 @@ interface IProps {
setCoverageDefaultDesk(coverage: IPlanningCoverageItem): void;
setCoverageAddAdvancedMode(enabled: boolean): Promise<void>;
createUploadLink(file: IFile): void;
onAddScheduledUpdateToWorkflow(
coverage: IPlanningCoverageItem,
index: number,
scheduledUpdate?: ICoverageScheduledUpdate,
scheduledUpdateIndex?: number
): void;
onRemoveAssignment(assignemnt: IAssignmentItem): Promise<void>;
uploadFiles(files: Array<Array<File>>): Promise<Array<IFile>>;
notifyValidationErrors(errors: Array<string>): void;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -441,6 +441,7 @@ export class CoverageFormComponent extends React.Component<IProps, IState> {
this.props.includeScheduledUpdates &&
this.props.value.planning?.g2_content_type === 'text'
),
scheduledUpdates: this.props.value.scheduled_updates ?? [],
},
priority: {field: 'planning.priority'},
};
Expand Down
31 changes: 12 additions & 19 deletions client/components/Coverages/ScheduledUpdate/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -23,11 +23,13 @@ import {CoverageFormHeader} from '../CoverageEditor/CoverageFormHeader';
import {CoveragePreviewTopBar} from '../CoveragePreview/CoveragePreviewTopBar';

import {planningUtils, stringUtils, assignmentUtils} from '../../../utils';
import {PLANNING, COVERAGES} from '../../../constants';
import {PLANNING} from '../../../constants';
import {planningApis} from '../../../api';

interface IProps {
diff: IPlanningCoverageItem;
planning: IPlanningItem;
scheduledUpdates: Array<ICoverageScheduledUpdate>;
index: number;
field: string;
value: ICoverageScheduledUpdate;
Expand Down Expand Up @@ -64,12 +66,6 @@ interface IProps {
onOpen?(coverage: ICoverageScheduledUpdate): void;
onClose?(coverage: ICoverageScheduledUpdate): void;
onCancelScheduledUpdate?(): void;
onAddScheduledUpdateToWorkflow(
coverage: IPlanningCoverageItem,
coverageIndex: number,
scheduledUpdate: ICoverageScheduledUpdate,
scheduledIndex: number
): void;
}

export class ScheduledUpdate extends React.PureComponent<IProps> {
Expand All @@ -80,20 +76,10 @@ export class ScheduledUpdate extends React.PureComponent<IProps> {
constructor(props: IProps) {
super(props);

this.addScheduledUpdateToWorkflow = this.addScheduledUpdateToWorkflow.bind(this);
this.onOpen = this.onOpen.bind(this);
this.onClose = this.onClose.bind(this);
}

addScheduledUpdateToWorkflow() {
this.props.onAddScheduledUpdateToWorkflow(
this.props.diff,
this.props.coverageIndex,
this.props.value,
this.props.index
);
}

onOpen() {
this.props.onOpen(this.props.value);
}
Expand Down Expand Up @@ -135,7 +121,6 @@ export class ScheduledUpdate extends React.PureComponent<IProps> {
onOpen,
onClose,
message,
onAddScheduledUpdateToWorkflow,
testId,
...props
} = this.props;
Expand All @@ -160,7 +145,15 @@ export class ScheduledUpdate extends React.PureComponent<IProps> {
id: 'addToWorkflow',
label: gettext('Add to workflow'),
icon: 'icon-assign',
callback: this.addScheduledUpdateToWorkflow,
callback: () => {
this.props.onChange(
'scheduled_updates',
planningApis.planning.coverages.addScheduledUpdateToWorkflow(
this.props.scheduledUpdates,
this.props.value,
),
);
},
});
}

Expand Down
7 changes: 0 additions & 7 deletions client/components/Main/ItemEditor/ItemManager.ts
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,6 @@ export class ItemManager {
this.addCoverage = this.addCoverage.bind(this);
this.startPartialSave = this.startPartialSave.bind(this);
this.openInModal = this.openInModal.bind(this);
this.addScheduledUpdateToWorkflow = this.addScheduledUpdateToWorkflow.bind(this);
this.removeAssignment = this.removeAssignment.bind(this);
this.setStateForPartialSave = this.setStateForPartialSave.bind(this);

Expand Down Expand Up @@ -880,12 +879,6 @@ export class ItemManager {
);
}

addScheduledUpdateToWorkflow(planning, coverage, covergeIndex, scheduledUpdate, index) {
return this.dispatch<any>(actions.planning.ui.addScheduledUpdateToWorkflow(planning, coverage, covergeIndex,
scheduledUpdate, index))
.then((updates) => this.finalisePartialSave(this.getCoverageAfterPartialSave(updates, index)));
}

removeAssignment(planning, coverage, index) {
return this.dispatch<any>(actions.planning.ui.removeAssignment(planning, coverage, index))
.then((updates) => this.finalisePartialSave(this.getCoverageAfterPartialSave(updates, index)));
Expand Down
19 changes: 0 additions & 19 deletions client/components/Planning/PlanningEditor/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -118,7 +118,6 @@ class PlanningEditorComponent extends React.Component<IProps, IState> {
this.onCoverageChange = this.onCoverageChange.bind(this);
this.onPlanningDateChange = this.onPlanningDateChange.bind(this);
this.onTimeToBeConfirmed = this.onTimeToBeConfirmed.bind(this);
this.onAddScheduledUpdateToWorkflow = this.onAddScheduledUpdateToWorkflow.bind(this);
this.onRemoveAssignment = this.onRemoveAssignment.bind(this);
}

Expand Down Expand Up @@ -334,21 +333,6 @@ class PlanningEditorComponent extends React.Component<IProps, IState> {
}
}

onAddScheduledUpdateToWorkflow(
coverage: IPlanningCoverageItem,
coverageIndex: number,
scheduledUpdate: ICoverageScheduledUpdate,
index: number
) {
this.onPartialSave(
coverage,
coverageIndex,
COVERAGES.PARTIAL_SAVE.SCHEDULED_UPDATES_ADD_TO_WORKFLOW,
scheduledUpdate,
index
);
}

onRemoveAssignment(
coverage: IPlanningCoverageItem,
index: number,
Expand Down Expand Up @@ -392,8 +376,6 @@ class PlanningEditorComponent extends React.Component<IProps, IState> {

if (action === COVERAGES.PARTIAL_SAVE.REMOVE_ASSIGNMENT) {
partialSaveAction = this.props.itemManager.removeAssignment;
} else if (action == COVERAGES.PARTIAL_SAVE.SCHEDULED_UPDATES_ADD_TO_WORKFLOW) {
partialSaveAction = this.props.itemManager.addScheduledUpdateToWorkflow;
}

partialSaveAction(this.props.item, coverage, index, scheduledUpdate, scheduledUpdateIndex);
Expand Down Expand Up @@ -482,7 +464,6 @@ class PlanningEditorComponent extends React.Component<IProps, IState> {
preferredCoverageDesks: this.props.preferredCoverageDesks,
setCoverageDefaultDesk: this.props.setCoverageDefaultDesk,
setCoverageAddAdvancedMode: this.props.setCoverageAddAdvancedMode,
onAddScheduledUpdateToWorkflow: this.onAddScheduledUpdateToWorkflow,
onRemoveAssignment: this.onRemoveAssignment,
defaultValue: [],
files: this.props.files,
Expand Down
6 changes: 0 additions & 6 deletions client/components/fields/editor/coverages.interface.ts
Original file line number Diff line number Diff line change
Expand Up @@ -29,12 +29,6 @@ export interface IPropsEditorFieldCoverages extends IEditorFieldProps {
onPopupClose?(): void;
setCoverageDefaultDesk(coverage: IPlanningCoverageItem): void;
setCoverageAddAdvancedMode(enabled: boolean): Promise<void>;
onAddScheduledUpdateToWorkflow(
coverage: IPlanningCoverageItem,
index: number,
scheduledUpdate?: ICoverageScheduledUpdate,
scheduledUpdateIndex?: number
): void;
onRemoveAssignment(
coverage: IPlanningCoverageItem,
index: number,
Expand Down
2 changes: 0 additions & 2 deletions client/constants/coverages.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,7 @@ import {superdeskApi} from '../superdeskApi';
export const COVERAGES = {
WORKFLOW_STATE: {ACTIVE: 'active'},
PARTIAL_SAVE: {
ADD_TO_WORKFLOW: 'ADD_TO_WORKFLOW',
REMOVE_ASSIGNMENT: 'REMOVE_ASSIGNMENT',
SCHEDULED_UPDATES_ADD_TO_WORKFLOW: 'SCHEDULED_UPDATES_ADD_TO_WORKFLOW',
SCHEDULED_UPDATES_REMOVE_ASSIGNMENT: 'SCHEDULED_UPDATES_REMOVE_ASSIGNMENT',
},
HISTORY_OPERATIONS: {
Expand Down
11 changes: 4 additions & 7 deletions client/interfaces.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1343,13 +1343,6 @@ export interface IFormItemManager {
coverage: IPlanningCoverageItem,
index: number
): Promise<IPlanningItem>;
addScheduledUpdateToWorkflow(
planning: IPlanningItem,
coverage: IPlanningCoverageItem,
coverageIndex: number,
scheduledUpdate: ICoverageScheduledUpdate,
index: number
): Promise<IPlanningItem>;
finalisePartialSave(diff: DeepPartial<IEventOrPlanningItem>, updateDirtyFlag: boolean): Promise<void>;
setState(newState: Partial<IEditorState>): Promise<IEditorState>;
getState(): IEditorState;
Expand Down Expand Up @@ -2221,6 +2214,10 @@ export interface IPlanningAPI {
coverages: Array<IPlanningCoverageItem>,
coverageToAddToWorkflow: IPlanningCoverageItem,
): Array<IPlanningCoverageItem>;
addScheduledUpdateToWorkflow(
updates: Array<ICoverageScheduledUpdate>,
updateToAddToWorkflow: ICoverageScheduledUpdate,
): Array<ICoverageScheduledUpdate>;
bulkAddCoverageToWorkflow(planingItems: Array<IPlanningItem>): Promise<Array<IPlanningItem>>;
}
create(updates: Partial<IPlanningItem>): Promise<IPlanningItem>;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,6 @@ export class Editor extends React.PureComponent<IProps> {
setCoverageAddAdvancedMode={() => Promise.resolve()}
preferredCoverageDesks={{}}
uploadFiles={() => Promise.resolve([])}
onAddScheduledUpdateToWorkflow={noop}
message={{}}
notifyValidationErrors={noop}
onChange={(fieldPath: any, value: any) => {
Expand Down
54 changes: 36 additions & 18 deletions client/utils/planning.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -1764,30 +1764,47 @@ function canAddScheduledUpdateToWorkflow(
);
}

function setCoverageActiveValues(
coverage: IPlanningCoverageItem | ICoverageScheduledUpdate,
function addToWorkflowCommon<T extends IPlanningCoverageItem | ICoverageScheduledUpdate>(
item: T,
newsCoverageStatus: Array<IPlanningNewsCoverageStatus>
): void {
set(coverage, 'news_coverage_status', newsCoverageStatus.find((s) => s.qcode === 'ncostat:int'));
set(coverage, 'workflow_status', COVERAGES.WORKFLOW_STATE.ACTIVE);
set(coverage, 'assigned_to.state', ASSIGNMENTS.WORKFLOW_STATE.ASSIGNED);
): T {
const next: T = cloneDeep(item);

next.news_coverage_status = newsCoverageStatus.find((s) => s.qcode === 'ncostat:int');
next.workflow_status = COVERAGES.WORKFLOW_STATE.ACTIVE;

if (next.assigned_to != null) {
next.assigned_to.state = ASSIGNMENTS.WORKFLOW_STATE.ASSIGNED;
}

return next;
}

function addToWorkflow(
updatedCoverage: IPlanningCoverageItem,
function addCoverageToWorkflow(
coverage: IPlanningCoverageItem,
newsCoverageStatus: Array<IPlanningNewsCoverageStatus>
): IPlanningCoverageItem {
const coverage = cloneDeep(updatedCoverage);
const coverageNext = addToWorkflowCommon(coverage, newsCoverageStatus);

setCoverageActiveValues(coverage, newsCoverageStatus);
(get(coverage, 'scheduled_updates') || []).forEach((s) => {
// Add the scheduled_update to workflow if they have an assignment
if (get(s, 'assigned_to')) {
setCoverageActiveValues(s, newsCoverageStatus);
}
});
if (coverageNext.scheduled_updates != null) {
coverageNext.scheduled_updates = coverageNext.scheduled_updates.map((update) => {
// Add the scheduled_update to workflow if they have an assignment
if (update.assigned_to != null) {
return addToWorkflowCommon(update, newsCoverageStatus);
} else {
return update;
}
});
}

return coverage;
return coverageNext;
}

function addScheduledUpdateToWorkflow(
update: ICoverageScheduledUpdate,
newsCoverageStatus: Array<IPlanningNewsCoverageStatus>
): ICoverageScheduledUpdate {
return addToWorkflowCommon(update, newsCoverageStatus);
}

function getPlanningFiles(planning: IPlanningItem): IPlanningItem['files'] {
Expand Down Expand Up @@ -1950,7 +1967,8 @@ const self = {
getDateStringForPlanning,
setDefaultAssignment,
getCoverageDateText,
addToWorkflow: addToWorkflow,
addCoverageToWorkflow,
addScheduledUpdateToWorkflow,
canAddScheduledUpdateToWorkflow,
getDefaultCoverageStatus,
getPlanningFiles,
Expand Down

0 comments on commit 8bd4c20

Please sign in to comment.