Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[SDES-7299] fix: AddToPlanning not checking ContentProfile properly #2036

Merged
merged 1 commit into from
Jul 24, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
23 changes: 14 additions & 9 deletions client/controllers/AddToPlanningController.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -4,13 +4,12 @@ import React from 'react';
import ReactDOM from 'react-dom';
import {Provider} from 'react-redux';
import {ModalsContainer} from '../components';
import {locks} from '../actions';
import {planning} from '../actions';
import {get, isEmpty, isNumber} from 'lodash';
import {registerNotifications, getErrorMessage, isExistingItem} from '../utils';
import {WORKSPACE, MODALS, MAIN} from '../constants';
import {GET_LABEL_MAP} from 'superdesk-core/scripts/apps/workspace/content/constants';
import {planningApi} from '../superdeskApi';
import {planningApi, superdeskApi} from '../superdeskApi';

const DEFAULT_PLANNING_SCHEMA = {
anpa_category: {required: true},
Expand Down Expand Up @@ -193,11 +192,14 @@ export class AddToPlanningController {
}

loadArchiveItem() {
return this.api.find('archive', this.item._id)
.then((newsItem) => {
return Promise.all([
superdeskApi.entities.contentProfile.get(this.item.profile),
this.api.find('archive', this.item._id),
])
.then(([contentProfile, newsItem]) => {
const errMessages = [];
const profile = planningProfile(this.store.getState());
const schema = get(profile, 'schema') || DEFAULT_PLANNING_SCHEMA;
const planningSchema = profile.schema || DEFAULT_PLANNING_SCHEMA;
const requiredError = (field) => this.gettext('[{{ field }}] is a required field')
.replace('{{ field }}', field);
const labels = GET_LABEL_MAP(this.gettext);
Expand All @@ -206,10 +208,13 @@ export class AddToPlanningController {
errMessages.push(this.gettext('Item already linked to a Planning item'));
}

Object.keys(schema)
.filter((field) => get(schema[field], 'required') &&
isEmpty(get(newsItem, field)) &&
!isNumber(get(newsItem, field)))
Object.keys(planningSchema)
.filter((field) => (
contentProfile.schema[field] != null &&
planningSchema[field]?.required === true &&
isEmpty(newsItem[field]) &&
!isNumber(newsItem[field])
))
.forEach((field) => {
errMessages.push(requiredError(labels[field] || field));
});
Expand Down
30 changes: 29 additions & 1 deletion client/utils/testApi.ts
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,35 @@ Object.assign(superdeskApi, {
},
components: {
SelectUser: sinon.stub().returns('<div>Stubbed SelectUser Component</div>'),
}
},
entities: {
contentProfile: {
get: sinon.stub().returns(Promise.resolve({
schema: {
slugline: {maxlength: 24, type: 'string', required: true},
relatedItems: {},
genre: {type: 'list'},
anpa_take_key: {type: 'string'},
place: {type: 'list'},
priority: {type: 'integer'},
urgency: {type: 'integer'},
anpa_category: {type: 'list', required: true},
subject: {type: 'list', required: true},
company_codes: {type: 'list'},
ednote: {type: 'string'},
headline: {maxlength: 42, type: 'string', required: true},
sms: {maxlength: 160, type: 'string'},
abstract: {maxlength: 160, type: 'string'},
body_html: {required: true, type: 'string'},
byline: {type: 'string'},
dateline: {type: 'dict', required: true},
sign_off: {type: 'string'},
footer: {},
body_footer: {type: 'string'},
},
})),
},
},
});

Object.assign(planningApi, {
Expand Down
Loading