diff --git a/package.json b/package.json index f2e487d..972b030 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "sonor", - "version": "0.5.24", + "version": "0.5.25", "private": true, "dependencies": { "@testing-library/jest-dom": "^4.2.4", diff --git a/src/components/App/App.test.jsx b/src/components/App/App.test.jsx index bcaca60..1ada40c 100644 --- a/src/components/App/App.test.jsx +++ b/src/components/App/App.test.jsx @@ -72,8 +72,9 @@ const updatePreferences = jest.fn((newPrefs, cb) => { DataFormatter.mockImplementation(() => ({ getPreferences: (c) => (c(preferences)), + getAllCampaigns: (c) => (c(mainScreenData)), getQuestionnaireId: (id, c) => (c({ questionnaireId: 'QXT55' })), - getDataForMainScreen: (a, c) => { + getFormattedCampaignsForMainScreen: (a, c) => { if (c) { c(mainScreenData); } return Promise.resolve(mainScreenData); }, diff --git a/src/components/CampaignPortal/CampaignPortal.jsx b/src/components/CampaignPortal/CampaignPortal.jsx index b83cf43..09d3b89 100644 --- a/src/components/CampaignPortal/CampaignPortal.jsx +++ b/src/components/CampaignPortal/CampaignPortal.jsx @@ -15,7 +15,7 @@ import D from '../../i18n'; import './CampaignPortal.css'; function CampaignPortal({ - location, dataRetreiver, + location, dataRetreiver, campaigns }) { const initialData = {}; initialData.interviewers = []; @@ -39,17 +39,14 @@ function CampaignPortal({ useEffect(() => { if (!survey && location.survey) { - dataRetreiver.getDataForMainScreen(null, (campaignsData) => { + dataRetreiver.getFormattedCampaignsForMainScreen(null, (campaignsData) => { const newSurvey = campaignsData.find((s) => s.id === location.survey.id); - newSurvey.allSurveys = campaignsData; - setSurvey(newSurvey); - setSurveyInfo(campaignsData.find((s) => s.id === location.survey.id)); + setSurvey({...newSurvey, allSurveys : campaignsData}); + setSurveyInfo(newSurvey); setRedirect(null); - }); + }, campaigns); } - }, [redirect, dataRetreiver, location, survey]); - useEffect(() => { if (survey) { setIsLoading(true); dataRetreiver.getDataForCampaignPortal(!survey || survey.id, (res) => { @@ -58,7 +55,7 @@ function CampaignPortal({ setIsLoading(false); }); } - }, [redirect, dataRetreiver, location, survey]); + }, [survey]); function handleSort(property, asc) { const [sortedData, newSort] = Utils.handleSort(property, data, sort, 'campaignPortal', asc); diff --git a/src/components/Close/CloseSUTable.jsx b/src/components/Close/CloseSUTable.jsx index a7aaecc..74672e7 100644 --- a/src/components/Close/CloseSUTable.jsx +++ b/src/components/Close/CloseSUTable.jsx @@ -27,7 +27,7 @@ function makeTableForExport(data) { return header.concat(data.map((line) => ([ line.campaign, line.id, - `${line.interviewer.interviewerLastName} ${line.interviewer.interviewerFirstName}`, + `${line.interviewer?.interviewerLastName ?? ""} ${line.interviewer?.interviewerFirstName ?? ""}`, line.ssech, line.location ? line.location.substring(0, 2) : null, line.city, diff --git a/src/components/CollectionTable/CollectionTable.jsx b/src/components/CollectionTable/CollectionTable.jsx index 6ca0a30..7a02379 100644 --- a/src/components/CollectionTable/CollectionTable.jsx +++ b/src/components/CollectionTable/CollectionTable.jsx @@ -54,7 +54,7 @@ class CollectionTable extends React.Component { async refreshData() { this.setState({ loading: true }); - const { dataRetreiver, location } = this.props; + const { dataRetreiver, location, campaigns } = this.props; const { survey, interviewer } = location; const { date } = this.state; const dateToUse = date || new Date().toISOString().slice(0, 10); @@ -64,7 +64,7 @@ class CollectionTable extends React.Component { if (interviewer) { surveyToUse = interviewer; } else if (modeToUse !== C.BY_INTERVIEWER_ONE_SURVEY && modeToUse !== C.BY_SITE) { - surveyToUse = await dataRetreiver.getDataForMainScreen(); + surveyToUse = await dataRetreiver.getFormattedCampaignsForMainScreen(null, null, campaigns); } else { surveyToUse = survey; } diff --git a/src/components/CollectionTable/CollectionTable.test.jsx b/src/components/CollectionTable/CollectionTable.test.jsx index 30b94d4..8e59923 100644 --- a/src/components/CollectionTable/CollectionTable.test.jsx +++ b/src/components/CollectionTable/CollectionTable.test.jsx @@ -58,7 +58,7 @@ const TestingRouter = ({ ComponentWithRedirection }) => ( ); -const mockGetDataForMainScreen = jest.fn(() => (Promise.resolve(mainScreenData))); +const mockGetFormattedCampaignsForMainScreen = jest.fn(() => (mainScreenData)); const mockGetDataForCollectionTable = jest.fn( (survey, date, pagination, mode, cb) => { @@ -82,7 +82,7 @@ const mockGetDataForCollectionTable = jest.fn( const mockDataFormatter = DataFormatter.mockImplementation(() => ({ getDataForCollectionTable: mockGetDataForCollectionTable, - getDataForMainScreen: mockGetDataForMainScreen, + getFormattedCampaignsForMainScreen: mockGetFormattedCampaignsForMainScreen })); const mockDataRetreiver = new DataFormatter(); @@ -314,7 +314,7 @@ it('Reloading the page with no survey set (F5) by survey (Site > Progress)', asy await waitForElement(() => screen.getByTestId('pagination-nav')); // Should call getDataForMainScreen and display the page anyway - expect(mockGetDataForMainScreen).toHaveBeenCalled(); + expect(mockGetFormattedCampaignsForMainScreen).toHaveBeenCalled(); expect(component).toMatchSnapshot(); }); diff --git a/src/components/Header/Header.jsx b/src/components/Header/Header.jsx index 1e03594..a06bb06 100644 --- a/src/components/Header/Header.jsx +++ b/src/components/Header/Header.jsx @@ -17,7 +17,7 @@ import { version } from '../../../package.json'; import './Header.css'; function Header({ - user, showPreferences, dataRetreiver, + user, showPreferences, dataRetreiver, campaigns, preferences }) { const [showFollowDropdown, setShowFollowDropdown] = useState(false); const [showReviewDropdown, setShowReviewDropdown] = useState(false); @@ -160,6 +160,8 @@ function Header({ title={modalTitle} linkTo={modalLink} interviewerMode={modalInterviewerMode} + campaigns={campaigns} + preferences={preferences} /> ); diff --git a/src/components/Header/Header.test.jsx b/src/components/Header/Header.test.jsx index 4b9b38a..50fb8ac 100644 --- a/src/components/Header/Header.test.jsx +++ b/src/components/Header/Header.test.jsx @@ -53,7 +53,7 @@ DataFormatter.mockImplementation(() => ({ getPreferences: (c) => (c(preferences)), getInterviewers: (c) => (c(interviewers)), getQuestionnaireId: (id, c) => (c({ questionnaireId: 'QXT55' })), - getDataForMainScreen: (a, c) => { + getFormattedCampaignsForMainScreen: (a, c, mainScreenData) => { if (c) { c(mainScreenData); } return Promise.resolve(mainScreenData); }, @@ -97,7 +97,7 @@ jest.mock('react-router-dom', () => ({ const TestingRouter = () => ( -
{ mockShowPreferences(); }} dataRetreiver={dataRetreiver} /> +
{ mockShowPreferences(); }} dataRetreiver={dataRetreiver} campaigns={mainScreenData} preferences={preferences}/> { + dataRetreiver.getFormattedCampaignsForMainScreen(null, (data) => { this.setState({ isLoading: false, data: data.filter( (survey) => (preferences[survey.id] && preferences[survey.id].preference), ), }, () => { this.handleSort('label', true); }); - }); + }, campaigns ); } handlePageChange(pagination) { diff --git a/src/components/MainScreen/MainScreen.test.jsx b/src/components/MainScreen/MainScreen.test.jsx index e9ed656..56486d0 100644 --- a/src/components/MainScreen/MainScreen.test.jsx +++ b/src/components/MainScreen/MainScreen.test.jsx @@ -51,7 +51,7 @@ const TestingRouter = ({ ComponentWithRedirection }) => ( ); DataFormatter.mockImplementation(() => ({ - getDataForMainScreen: (a, c) => (c(data)), + getFormattedCampaignsForMainScreen: (a, c) => (c(data)), })); const mockDataRetreiver = new DataFormatter(); diff --git a/src/components/ModalSelection/ModalSelection.jsx b/src/components/ModalSelection/ModalSelection.jsx index 1b1d8aa..3ff4f4f 100644 --- a/src/components/ModalSelection/ModalSelection.jsx +++ b/src/components/ModalSelection/ModalSelection.jsx @@ -6,28 +6,44 @@ import SurveySelector from '../SurveySelector/SurveySelector'; import InterviewerSelector from '../InterviewerSelector/InterviewerSelector'; function ModalSelection({ - linkTo, title, show, setShow, dataRetreiver, interviewerMode, + linkTo, title, show, setShow, dataRetreiver, interviewerMode, campaigns = [], preferences }) { const [redirect, setRedirect] = useState(null); const [surveys, setSurveys] = useState(null); const [interviewers, setInterviewers] = useState(null); + const [campaignsWithUpdatedPreferences, setCampaignsWithUpdatedPreferences] = useState(campaigns); + + const updateCampaignsWithPreferences = () => { + return campaigns.map((campaign) => { + const associatedPreference = Object.entries(preferences).find((preference) => preference[0] === campaign.id) + + if(associatedPreference){ + campaign.preference = associatedPreference[1].preference + } + + return campaign; + }) + } const updateSurveys = useCallback(() => { - dataRetreiver.getDataForMainScreen(null, (data) => { + dataRetreiver.getFormattedCampaignsForMainScreen(null, (data) => { setSurveys({ allSurveys: data.filter( (survey) => survey.preference, ), }); - }); - }, [dataRetreiver]); + }, campaignsWithUpdatedPreferences); + }, [dataRetreiver, campaignsWithUpdatedPreferences]); + useEffect(() => { + setCampaignsWithUpdatedPreferences(updateCampaignsWithPreferences()) + }, [preferences]); + const updateInterviewers = useCallback(() => { dataRetreiver.getInterviewers((data) => { setInterviewers({ allInterviewers: data }); }); - }, [dataRetreiver]); - + }, [dataRetreiver, campaignsWithUpdatedPreferences]); useEffect(() => { if (interviewerMode) { updateInterviewers(); diff --git a/src/components/MonitoringTable/MonitoringTable.jsx b/src/components/MonitoringTable/MonitoringTable.jsx index f327b63..81e1cf9 100644 --- a/src/components/MonitoringTable/MonitoringTable.jsx +++ b/src/components/MonitoringTable/MonitoringTable.jsx @@ -53,7 +53,7 @@ class MonitoringTable extends React.Component { async refreshData() { this.setState({ loading: true }); - const { dataRetreiver, location } = this.props; + const { dataRetreiver, location, campaigns } = this.props; const { survey, interviewer } = location; const { date } = this.state; const dateToUse = date || new Date().toISOString().slice(0, 10); @@ -63,7 +63,7 @@ class MonitoringTable extends React.Component { if (interviewer) { surveyToUse = interviewer; } else if (modeToUse !== C.BY_INTERVIEWER_ONE_SURVEY && modeToUse !== C.BY_SITE) { - surveyToUse = await dataRetreiver.getDataForMainScreen(); + surveyToUse = await dataRetreiver.getFormattedCampaignsForMainScreen(null,null, campaigns); } else { surveyToUse = survey; } @@ -99,7 +99,7 @@ class MonitoringTable extends React.Component { this.handleSort(firstColumnSortAttribute, true); }); } - }, + }, campaigns ); } else { this.setState({ redirect: '/' }); diff --git a/src/components/MonitoringTable/MonitoringTable.test.jsx b/src/components/MonitoringTable/MonitoringTable.test.jsx index b07647b..a995f7e 100644 --- a/src/components/MonitoringTable/MonitoringTable.test.jsx +++ b/src/components/MonitoringTable/MonitoringTable.test.jsx @@ -86,7 +86,7 @@ const mockGetDataForMonitoringTable = jest.fn( const mockDataFormatter = DataFormatter.mockImplementation(() => ({ getDataForMonitoringTable: mockGetDataForMonitoringTable, - getDataForMainScreen: mockGetDataForMainScreen, + getFormattedCampaignsForMainScreen: mockGetDataForMainScreen, })); const mockDataRetreiver = new DataFormatter(); @@ -203,7 +203,7 @@ it('Select another date', async () => { const component = render( - + , ); @@ -216,6 +216,7 @@ it('Select another date', async () => { expect.anything(), expect.anything(), expect.anything(), + mainScreenData ); // And the page should render correctly @@ -228,13 +229,13 @@ it('Component did update', () => { const { container } = render( - + , ); render( - + , { container }, ); @@ -246,6 +247,7 @@ it('Component did update', () => { expect.anything(), C.BY_SITE, expect.anything(), + mainScreenData ); }) diff --git a/src/components/Review/Review.jsx b/src/components/Review/Review.jsx index e31f164..58f1888 100644 --- a/src/components/Review/Review.jsx +++ b/src/components/Review/Review.jsx @@ -13,7 +13,7 @@ import Utils from "../../utils/Utils"; import D from "../../i18n"; import "./Review.css"; -function Review({ location, dataRetreiver, match }) { +function Review({ location, dataRetreiver, match, campaigns }) { const { survey } = location; const { id } = match.params; const [data, setData] = useState([]); @@ -29,7 +29,7 @@ function Review({ location, dataRetreiver, match }) { setData(res); setRedirect(null); setIsLoading(false); - }); + }, campaigns); }, [dataRetreiver, survey]); useEffect(() => { diff --git a/src/components/View/View.jsx b/src/components/View/View.jsx index 6d32d4b..13b6313 100644 --- a/src/components/View/View.jsx +++ b/src/components/View/View.jsx @@ -27,20 +27,29 @@ class View extends React.Component { preferences: {}, redirect: null, loadingPreferences: true, + campaigns: [] }; this.dataRetreiver = new DataFormatter(props.keycloak); } componentDidMount() { this.componentIsMounted = true; - this.loadPreferences(); + this.getAllCampaignsData(); + } + + componentDidUpdate(prevProps, prevState) { + if( !this.state.campaigns.every((element) => prevState.campaigns.includes(element)) + || !prevState.campaigns.every((element) => this.state.campaigns.includes(element)) + ){ + this.loadPreferences(); + } } loadPreferences() { this.setState({ loadingPreferences: true }); this.dataRetreiver.getPreferences((preferences) => { this.setState({ preferences, loadingPreferences: false, redirect: }); - }); + }, this.state.campaigns); } updatePreferences(newPreferences) { @@ -62,9 +71,15 @@ class View extends React.Component { this.setState({ showPreferences: false }); } + getAllCampaignsData() { + this.dataRetreiver.getAllCampaigns((data) => { + this.setState({campaigns: data ?? []}); + }) + } + render() { const { - showPreferences, preferences, redirect, loadingPreferences, + showPreferences, preferences, redirect, loadingPreferences, campaigns } = this.state; const { userData } = this.props; return ( @@ -78,19 +93,21 @@ class View extends React.Component { showPreferences={() => { this.showPreferences(); }} + campaigns={campaigns} + preferences={preferences} /> - } /> + } /> } /> - } /> - } /> + } /> + } /> } /> } /> } /> - } /> + } /> } /> } /> - } /> + } /> diff --git a/src/components/View/View.test.jsx b/src/components/View/View.test.jsx index 78ea620..880477f 100644 --- a/src/components/View/View.test.jsx +++ b/src/components/View/View.test.jsx @@ -71,9 +71,10 @@ const updatePreferences = jest.fn((newPrefs, cb) => { }); DataFormatter.mockImplementation(() => ({ + getAllCampaigns: (c) => (c(mainScreenData)), getPreferences: (c) => (c(preferences)), getQuestionnaireId: (id, c) => (c({ questionnaireId: 'QXT55' })), - getDataForMainScreen: (a, c) => { + getFormattedCampaignsForMainScreen: (a, c) => { if (c) { c(mainScreenData); } return Promise.resolve(mainScreenData); }, diff --git a/src/tests/mocks/formattedReviewData.js b/src/tests/mocks/formattedReviewData.js index 44fb1c2..d073fa6 100644 --- a/src/tests/mocks/formattedReviewData.js +++ b/src/tests/mocks/formattedReviewData.js @@ -95,677 +95,5 @@ const val = [ "idep": "INTW7", "id": "4815" }, - { - "campaignLabel": "Everyday life and health survey 2021", - "interviewer": "Boulanger Jacques", - "idep": "INTW6", - "id": "4819" - }, - { - "campaignLabel": "Everyday life and health survey 2021", - "interviewer": "Boulanger Jacques", - "idep": "INTW6", - "id": "4812" - }, - { - "campaignLabel": "Everyday life and health survey 2021", - "interviewer": "Boulanger Jacques", - "idep": "INTW6", - "id": "4813" - }, - { - "campaignLabel": "Everyday life and health survey 2021", - "interviewer": "Boulanger Jacques", - "idep": "INTW6", - "id": "4811" - }, - { - "campaignLabel": "Everyday life and health survey 2021", - "interviewer": "Boulanger Jacques", - "idep": "INTW6", - "id": "4816" - }, - { - "campaignLabel": "Everyday life and health survey 2021", - "interviewer": "Boulanger Jacques", - "idep": "INTW6", - "id": "4818" - }, - { - "campaignLabel": "Everyday life and health survey 2021", - "interviewer": "Boulanger Jacques", - "idep": "INTW6", - "id": "4817" - }, - { - "campaignLabel": "Everyday life and health survey 2021", - "interviewer": "Dupont Chloé", - "idep": "INTW5", - "id": "1025" - }, - { - "campaignLabel": "Everyday life and health survey 2021", - "interviewer": "Dupont Chloé", - "idep": "INTW5", - "id": "1027" - }, - { - "campaignLabel": "Everyday life and health survey 2021", - "interviewer": "Dupont Chloé", - "idep": "INTW5", - "id": "1024" - }, - { - "campaignLabel": "Everyday life and health survey 2021", - "interviewer": "Dupont Chloé", - "idep": "INTW5", - "id": "1030" - }, - { - "campaignLabel": "Everyday life and health survey 2021", - "interviewer": "Dupont Chloé", - "idep": "INTW5", - "id": "1032" - }, - { - "campaignLabel": "Everyday life and health survey 2021", - "interviewer": "Dupont Chloé", - "idep": "INTW5", - "id": "1023" - }, - { - "campaignLabel": "Everyday life and health survey 2021", - "interviewer": "Dupont Chloé", - "idep": "INTW5", - "id": "1028" - }, - { - "campaignLabel": "Everyday life and health survey 2021", - "interviewer": "Fabres Thierry", - "idep": "INTW7", - "id": "1029" - }, - { - "campaignLabel": "Everyday life and health survey 2021", - "interviewer": "Fabres Thierry", - "idep": "INTW7", - "id": "4815" - }, - { - "campaignLabel": "Survey on something 2020", - "interviewer": "Boulanger Jacques", - "idep": "INTW6", - "id": "4819" - }, - { - "campaignLabel": "Survey on something 2020", - "interviewer": "Boulanger Jacques", - "idep": "INTW6", - "id": "4812" - }, - { - "campaignLabel": "Survey on something 2020", - "interviewer": "Boulanger Jacques", - "idep": "INTW6", - "id": "4813" - }, - { - "campaignLabel": "Survey on something 2020", - "interviewer": "Boulanger Jacques", - "idep": "INTW6", - "id": "4811" - }, - { - "campaignLabel": "Survey on something 2020", - "interviewer": "Boulanger Jacques", - "idep": "INTW6", - "id": "4816" - }, - { - "campaignLabel": "Survey on something 2020", - "interviewer": "Boulanger Jacques", - "idep": "INTW6", - "id": "4818" - }, - { - "campaignLabel": "Survey on something 2020", - "interviewer": "Boulanger Jacques", - "idep": "INTW6", - "id": "4817" - }, - { - "campaignLabel": "Survey on something 2020", - "interviewer": "Dupont Chloé", - "idep": "INTW5", - "id": "1025" - }, - { - "campaignLabel": "Survey on something 2020", - "interviewer": "Dupont Chloé", - "idep": "INTW5", - "id": "1027" - }, - { - "campaignLabel": "Survey on something 2020", - "interviewer": "Dupont Chloé", - "idep": "INTW5", - "id": "1024" - }, - { - "campaignLabel": "Survey on something 2020", - "interviewer": "Dupont Chloé", - "idep": "INTW5", - "id": "1030" - }, - { - "campaignLabel": "Survey on something 2020", - "interviewer": "Dupont Chloé", - "idep": "INTW5", - "id": "1032" - }, - { - "campaignLabel": "Survey on something 2020", - "interviewer": "Dupont Chloé", - "idep": "INTW5", - "id": "1023" - }, - { - "campaignLabel": "Survey on something 2020", - "interviewer": "Dupont Chloé", - "idep": "INTW5", - "id": "1028" - }, - { - "campaignLabel": "Survey on something 2020", - "interviewer": "Fabres Thierry", - "idep": "INTW7", - "id": "1029" - }, - { - "campaignLabel": "Survey on something 2020", - "interviewer": "Fabres Thierry", - "idep": "INTW7", - "id": "4815" - }, - { - "campaignLabel": "Everyday life and health survey 2022", - "interviewer": "Boulanger Jacques", - "idep": "INTW6", - "id": "4819" - }, - { - "campaignLabel": "Everyday life and health survey 2022", - "interviewer": "Boulanger Jacques", - "idep": "INTW6", - "id": "4812" - }, - { - "campaignLabel": "Everyday life and health survey 2022", - "interviewer": "Boulanger Jacques", - "idep": "INTW6", - "id": "4813" - }, - { - "campaignLabel": "Everyday life and health survey 2022", - "interviewer": "Boulanger Jacques", - "idep": "INTW6", - "id": "4811" - }, - { - "campaignLabel": "Everyday life and health survey 2022", - "interviewer": "Boulanger Jacques", - "idep": "INTW6", - "id": "4816" - }, - { - "campaignLabel": "Everyday life and health survey 2022", - "interviewer": "Boulanger Jacques", - "idep": "INTW6", - "id": "4818" - }, - { - "campaignLabel": "Everyday life and health survey 2022", - "interviewer": "Boulanger Jacques", - "idep": "INTW6", - "id": "4817" - }, - { - "campaignLabel": "Everyday life and health survey 2022", - "interviewer": "Dupont Chloé", - "idep": "INTW5", - "id": "1025" - }, - { - "campaignLabel": "Everyday life and health survey 2022", - "interviewer": "Dupont Chloé", - "idep": "INTW5", - "id": "1027" - }, - { - "campaignLabel": "Everyday life and health survey 2022", - "interviewer": "Dupont Chloé", - "idep": "INTW5", - "id": "1024" - }, - { - "campaignLabel": "Everyday life and health survey 2022", - "interviewer": "Dupont Chloé", - "idep": "INTW5", - "id": "1030" - }, - { - "campaignLabel": "Everyday life and health survey 2022", - "interviewer": "Dupont Chloé", - "idep": "INTW5", - "id": "1032" - }, - { - "campaignLabel": "Everyday life and health survey 2022", - "interviewer": "Dupont Chloé", - "idep": "INTW5", - "id": "1023" - }, - { - "campaignLabel": "Everyday life and health survey 2022", - "interviewer": "Dupont Chloé", - "idep": "INTW5", - "id": "1028" - }, - { - "campaignLabel": "Everyday life and health survey 2022", - "interviewer": "Fabres Thierry", - "idep": "INTW7", - "id": "1029" - }, - { - "campaignLabel": "Everyday life and health survey 2022", - "interviewer": "Fabres Thierry", - "idep": "INTW7", - "id": "4815" - }, - { - "campaignLabel": "Survey on something else 2020", - "interviewer": "Boulanger Jacques", - "idep": "INTW6", - "id": "4819" - }, - { - "campaignLabel": "Survey on something else 2020", - "interviewer": "Boulanger Jacques", - "idep": "INTW6", - "id": "4812" - }, - { - "campaignLabel": "Survey on something else 2020", - "interviewer": "Boulanger Jacques", - "idep": "INTW6", - "id": "4813" - }, - { - "campaignLabel": "Survey on something else 2020", - "interviewer": "Boulanger Jacques", - "idep": "INTW6", - "id": "4811" - }, - { - "campaignLabel": "Survey on something else 2020", - "interviewer": "Boulanger Jacques", - "idep": "INTW6", - "id": "4816" - }, - { - "campaignLabel": "Survey on something else 2020", - "interviewer": "Boulanger Jacques", - "idep": "INTW6", - "id": "4818" - }, - { - "campaignLabel": "Survey on something else 2020", - "interviewer": "Boulanger Jacques", - "idep": "INTW6", - "id": "4817" - }, - { - "campaignLabel": "Survey on something else 2020", - "interviewer": "Dupont Chloé", - "idep": "INTW5", - "id": "1025" - }, - { - "campaignLabel": "Survey on something else 2020", - "interviewer": "Dupont Chloé", - "idep": "INTW5", - "id": "1027" - }, - { - "campaignLabel": "Survey on something else 2020", - "interviewer": "Dupont Chloé", - "idep": "INTW5", - "id": "1024" - }, - { - "campaignLabel": "Survey on something else 2020", - "interviewer": "Dupont Chloé", - "idep": "INTW5", - "id": "1030" - }, - { - "campaignLabel": "Survey on something else 2020", - "interviewer": "Dupont Chloé", - "idep": "INTW5", - "id": "1032" - }, - { - "campaignLabel": "Survey on something else 2020", - "interviewer": "Dupont Chloé", - "idep": "INTW5", - "id": "1023" - }, - { - "campaignLabel": "Survey on something else 2020", - "interviewer": "Dupont Chloé", - "idep": "INTW5", - "id": "1028" - }, - { - "campaignLabel": "Survey on something else 2020", - "interviewer": "Fabres Thierry", - "idep": "INTW7", - "id": "1029" - }, - { - "campaignLabel": "Survey on something else 2020", - "interviewer": "Fabres Thierry", - "idep": "INTW7", - "id": "4815" - }, - { - "campaignLabel": "Everyday life and health survey 2026", - "interviewer": "Boulanger Jacques", - "idep": "INTW6", - "id": "4819" - }, - { - "campaignLabel": "Everyday life and health survey 2026", - "interviewer": "Boulanger Jacques", - "idep": "INTW6", - "id": "4812" - }, - { - "campaignLabel": "Everyday life and health survey 2026", - "interviewer": "Boulanger Jacques", - "idep": "INTW6", - "id": "4813" - }, - { - "campaignLabel": "Everyday life and health survey 2026", - "interviewer": "Boulanger Jacques", - "idep": "INTW6", - "id": "4811" - }, - { - "campaignLabel": "Everyday life and health survey 2026", - "interviewer": "Boulanger Jacques", - "idep": "INTW6", - "id": "4816" - }, - { - "campaignLabel": "Everyday life and health survey 2026", - "interviewer": "Boulanger Jacques", - "idep": "INTW6", - "id": "4818" - }, - { - "campaignLabel": "Everyday life and health survey 2026", - "interviewer": "Boulanger Jacques", - "idep": "INTW6", - "id": "4817" - }, - { - "campaignLabel": "Everyday life and health survey 2026", - "interviewer": "Dupont Chloé", - "idep": "INTW5", - "id": "1025" - }, - { - "campaignLabel": "Everyday life and health survey 2026", - "interviewer": "Dupont Chloé", - "idep": "INTW5", - "id": "1027" - }, - { - "campaignLabel": "Everyday life and health survey 2026", - "interviewer": "Dupont Chloé", - "idep": "INTW5", - "id": "1024" - }, - { - "campaignLabel": "Everyday life and health survey 2026", - "interviewer": "Dupont Chloé", - "idep": "INTW5", - "id": "1030" - }, - { - "campaignLabel": "Everyday life and health survey 2026", - "interviewer": "Dupont Chloé", - "idep": "INTW5", - "id": "1032" - }, - { - "campaignLabel": "Everyday life and health survey 2026", - "interviewer": "Dupont Chloé", - "idep": "INTW5", - "id": "1023" - }, - { - "campaignLabel": "Everyday life and health survey 2026", - "interviewer": "Dupont Chloé", - "idep": "INTW5", - "id": "1028" - }, - { - "campaignLabel": "Everyday life and health survey 2026", - "interviewer": "Fabres Thierry", - "idep": "INTW7", - "id": "1029" - }, - { - "campaignLabel": "Everyday life and health survey 2026", - "interviewer": "Fabres Thierry", - "idep": "INTW7", - "id": "4815" - }, - { - "campaignLabel": "Survey on the Simpsons tv show 2021", - "interviewer": "Boulanger Jacques", - "idep": "INTW6", - "id": "4819" - }, - { - "campaignLabel": "Survey on the Simpsons tv show 2021", - "interviewer": "Boulanger Jacques", - "idep": "INTW6", - "id": "4812" - }, - { - "campaignLabel": "Survey on the Simpsons tv show 2021", - "interviewer": "Boulanger Jacques", - "idep": "INTW6", - "id": "4813" - }, - { - "campaignLabel": "Survey on the Simpsons tv show 2021", - "interviewer": "Boulanger Jacques", - "idep": "INTW6", - "id": "4811" - }, - { - "campaignLabel": "Survey on the Simpsons tv show 2021", - "interviewer": "Boulanger Jacques", - "idep": "INTW6", - "id": "4816" - }, - { - "campaignLabel": "Survey on the Simpsons tv show 2021", - "interviewer": "Boulanger Jacques", - "idep": "INTW6", - "id": "4818" - }, - { - "campaignLabel": "Survey on the Simpsons tv show 2021", - "interviewer": "Boulanger Jacques", - "idep": "INTW6", - "id": "4817" - }, - { - "campaignLabel": "Survey on the Simpsons tv show 2021", - "interviewer": "Dupont Chloé", - "idep": "INTW5", - "id": "1025" - }, - { - "campaignLabel": "Survey on the Simpsons tv show 2021", - "interviewer": "Dupont Chloé", - "idep": "INTW5", - "id": "1027" - }, - { - "campaignLabel": "Survey on the Simpsons tv show 2021", - "interviewer": "Dupont Chloé", - "idep": "INTW5", - "id": "1024" - }, - { - "campaignLabel": "Survey on the Simpsons tv show 2021", - "interviewer": "Dupont Chloé", - "idep": "INTW5", - "id": "1030" - }, - { - "campaignLabel": "Survey on the Simpsons tv show 2021", - "interviewer": "Dupont Chloé", - "idep": "INTW5", - "id": "1032" - }, - { - "campaignLabel": "Survey on the Simpsons tv show 2021", - "interviewer": "Dupont Chloé", - "idep": "INTW5", - "id": "1023" - }, - { - "campaignLabel": "Survey on the Simpsons tv show 2021", - "interviewer": "Dupont Chloé", - "idep": "INTW5", - "id": "1028" - }, - { - "campaignLabel": "Survey on the Simpsons tv show 2021", - "interviewer": "Fabres Thierry", - "idep": "INTW7", - "id": "1029" - }, - { - "campaignLabel": "Survey on the Simpsons tv show 2021", - "interviewer": "Fabres Thierry", - "idep": "INTW7", - "id": "4815" - }, - { - "campaignLabel": "Everyday life and health survey 2018", - "interviewer": "Boulanger Jacques", - "idep": "INTW6", - "id": "4819" - }, - { - "campaignLabel": "Everyday life and health survey 2018", - "interviewer": "Boulanger Jacques", - "idep": "INTW6", - "id": "4812" - }, - { - "campaignLabel": "Everyday life and health survey 2018", - "interviewer": "Boulanger Jacques", - "idep": "INTW6", - "id": "4813" - }, - { - "campaignLabel": "Everyday life and health survey 2018", - "interviewer": "Boulanger Jacques", - "idep": "INTW6", - "id": "4811" - }, - { - "campaignLabel": "Everyday life and health survey 2018", - "interviewer": "Boulanger Jacques", - "idep": "INTW6", - "id": "4816" - }, - { - "campaignLabel": "Everyday life and health survey 2018", - "interviewer": "Boulanger Jacques", - "idep": "INTW6", - "id": "4818" - }, - { - "campaignLabel": "Everyday life and health survey 2018", - "interviewer": "Boulanger Jacques", - "idep": "INTW6", - "id": "4817" - }, - { - "campaignLabel": "Everyday life and health survey 2018", - "interviewer": "Dupont Chloé", - "idep": "INTW5", - "id": "1025" - }, - { - "campaignLabel": "Everyday life and health survey 2018", - "interviewer": "Dupont Chloé", - "idep": "INTW5", - "id": "1027" - }, - { - "campaignLabel": "Everyday life and health survey 2018", - "interviewer": "Dupont Chloé", - "idep": "INTW5", - "id": "1024" - }, - { - "campaignLabel": "Everyday life and health survey 2018", - "interviewer": "Dupont Chloé", - "idep": "INTW5", - "id": "1030" - }, - { - "campaignLabel": "Everyday life and health survey 2018", - "interviewer": "Dupont Chloé", - "idep": "INTW5", - "id": "1032" - }, - { - "campaignLabel": "Everyday life and health survey 2018", - "interviewer": "Dupont Chloé", - "idep": "INTW5", - "id": "1023" - }, - { - "campaignLabel": "Everyday life and health survey 2018", - "interviewer": "Dupont Chloé", - "idep": "INTW5", - "id": "1028" - }, - { - "campaignLabel": "Everyday life and health survey 2018", - "interviewer": "Fabres Thierry", - "idep": "INTW7", - "id": "1029" - }, - { - "campaignLabel": "Everyday life and health survey 2018", - "interviewer": "Fabres Thierry", - "idep": "INTW7", - "id": "4815" - } ]; export default val; \ No newline at end of file diff --git a/src/utils/DataFormatter.js b/src/utils/DataFormatter.js index d8c4ad9..14b52cb 100644 --- a/src/utils/DataFormatter.js +++ b/src/utils/DataFormatter.js @@ -25,6 +25,13 @@ class DataFormatter { cb(data); }); } + + getAllCampaigns(cb) { + this.service.getCampaigns((data) => { + cb(data); + }); + + } getPreferences(cb) { return new Promise((resolve) => { @@ -46,6 +53,22 @@ class DataFormatter { }); } + getPreferencesWithCampaignsData(cb, campaigns) { + const formattedData = campaigns + .filter((survey) => Utils.isVisible(survey)) + .reduce((acc, survey) => { + acc[survey.id] = { + label: survey.label, + preference: survey.preference, + }; + return acc; + }, {}); + if (cb) { + cb(formattedData); + } + return formattedData; + } + updatePreferences(preferences, cb) { return new Promise((resolve) => { this.service.putPreferences(preferences, (data) => { @@ -124,6 +147,25 @@ class DataFormatter { }); } + getFormattedCampaignsForMainScreen(date, cb, campaigns){ + const formattedData = campaigns + .filter((survey) => Utils.isVisible(survey, date)) + .map((survey) => { + const formattedSurvey = {}; + Object.assign(formattedSurvey, survey); + formattedSurvey.phase = Utils.getCampaignPhase( + survey.collectionStartDate, + survey.collectionEndDate, + survey.endDate + ); + return formattedSurvey; + }); + if (cb) { + cb(formattedData); + } + return formattedData + } + getDataForClosePage(cb) { this.service.getSurveyUnitsClosable((data) => { cb(data); @@ -158,8 +200,8 @@ class DataFormatter { }); } - getDataForReview(surveyId, cb) { - this.getListSUToReview(surveyId).then((data) => { + getDataForReview(surveyId, cb, campaigns) { + this.getListSUToReview(surveyId, campaigns).then((data) => { cb(data); }); } @@ -195,12 +237,10 @@ class DataFormatter { }); } - getListSUToReview(surveyId) { + getListSUToReview(surveyId, campaigns) { return new Promise((resolve) => { - this.service.getCampaigns((campaigns) => { const promises = campaigns - .filter((campaign) => surveyId === null || campaign.id === surveyId) - + .filter((campaign) => surveyId !== null ? campaign.id === surveyId : campaign.toReview > 0 ) .map( (campaign) => new Promise((resolveCampaign) => { @@ -234,7 +274,6 @@ class DataFormatter { resolve(data.flat()); }); }); - }); } /** @@ -402,7 +441,7 @@ class DataFormatter { }); } - async getDataForMonitoringTable(survey, givenDate, pagination, mode, cb) { + async getDataForMonitoringTable(survey, givenDate, pagination, mode, cb, campaigns) { // Adding 24h to take all states added before the next day into account const date = givenDate + 86400000; const interviewers = []; @@ -424,7 +463,7 @@ class DataFormatter { if (getDataForSingleSurvey) { surveysToGetInterviewersFrom = [survey]; } else { - surveysToGetInterviewersFrom = await this.getDataForMainScreen(date); + surveysToGetInterviewersFrom = await this.getFormattedCampaignsForMainScreen(date, null, campaigns); } site = (await this.service.getUser()).organizationUnit.label; p1 = new Promise((resolve) => { diff --git a/src/utils/DataFormatter.test.js b/src/utils/DataFormatter.test.js index 5960656..ed2c8b5 100644 --- a/src/utils/DataFormatter.test.js +++ b/src/utils/DataFormatter.test.js @@ -154,7 +154,23 @@ it('Test getDataForListSU', async () => { it('Test getDataForReview', async () => { const callBack = jest.fn(); - dataRetreiver.getDataForReview(null, callBack); + dataRetreiver.getDataForReview(null, callBack, [...mainScreenData, { + "id": "simpsons2020x00", + "label": "Survey on the Simpsons tv show 2020", + "managementStartDate": 1576801000000, + "interviewerStartDate": 1575937000000, + "identificationPhaseStartDate": 1577233000000, + "collectionStartDate": 1577837800000, + "collectionEndDate": 1640996200000, + "endDate": 1641514600000, + "allocated": 4, + "toProcessInterviewer": 0, + "toAffect": 0, + "toFollowUp": 0, + "toReview": 16, + "finalized": 0, + "preference": true + },]); await wait(() => expect(callBack).toHaveBeenCalled()); // Should return properly formatted data expect(callBack.mock.calls[0][0].map((a) => a.id).sort()) @@ -188,7 +204,7 @@ it('Test getDataForMonitoringTable (by survey 1 Interviewer)', async () => { it('Test getDataForMonitoringTable (by survey)', async () => { const callBack2 = jest.fn(); const dataRet = new DataFormatter(); - dataRet.getDataForMonitoringTable(mainScreenData1Survey, new Date('2020-08-20T11:01:58.135Z'), null, C.BY_SURVEY, callBack2); + dataRet.getDataForMonitoringTable(mainScreenData1Survey, new Date('2020-08-20T11:01:58.135Z'), null, C.BY_SURVEY, callBack2, mainScreenData); await wait(() => expect(callBack2).toHaveBeenCalled()); // Should return properly formatted data expect(callBack2).toHaveBeenCalledWith(formattedDataMonitoringTableBysurvey); @@ -225,7 +241,7 @@ it('Test finalizeSurveyUnits', async () => { }); it('Test getListSUToReview', async () => { - const res = await dataRetreiver.getListSUToReview('simpsons2020x00'); + const res = await dataRetreiver.getListSUToReview('simpsons2020x00', mainScreenData); // Should return properly formatted data expect(res.map((a) => a.id).sort()) .toEqual(formattedLisSuToReviewSimpsons.map((a) => a.id).sort());