diff --git a/src/actions.js b/src/actions.js index bd6f324..dfeb0ec 100644 --- a/src/actions.js +++ b/src/actions.js @@ -1,342 +1,608 @@ import { - graphql, formatPageQuery, formatPageQueryWithCount, decodeId, formatMutation, formatGQLString + graphql, + formatPageQuery, + formatPageQueryWithCount, + decodeId, + formatMutation, + formatGQLString, + graphqlWithVariables, } from "@openimis/fe-core"; import { APPLY_DEFAULT_VALIDITY_FILTER } from "./constants"; -const regexExp = /^[0-9a-fA-F]{8}\b-[0-9a-fA-F]{4}\b-[0-9a-fA-F]{4}\b-[0-9a-fA-F]{4}\b-[0-9a-fA-F]{12}$/gi; +const regexExp = + /^[0-9a-fA-F]{8}\b-[0-9a-fA-F]{4}\b-[0-9a-fA-F]{4}\b-[0-9a-fA-F]{4}\b-[0-9a-fA-F]{12}$/gi; -const CONTRACT_FULL_PROJECTION = modulesManager => [ - "id", "code", "amount", "amountNotified", "amountRectified", "amountDue", "dateApproved", "datePaymentDue", - "state", "paymentReference", "amendment", "dateValidFrom", "dateValidTo", "isDeleted", - "policyHolder" + modulesManager.getProjection("policyHolder.PolicyHolderPicker.projection") +const CONTRACT_FULL_PROJECTION = (modulesManager) => [ + "id", + "code", + "amount", + "amountNotified", + "amountRectified", + "amountDue", + "dateApproved", + "datePaymentDue", + "state", + "paymentReference", + "amendment", + "dateValidFrom", + "dateValidTo", + "isDeleted", + "policyHolder" + + modulesManager.getProjection("policyHolder.PolicyHolderPicker.projection"), ]; export const CONTRACT_PICKER_PROJECTION = ["id", "code"]; -const CONTRACTDETAILS_FULL_PROJECTION = modulesManager => [ - "id", "jsonExt", "contract{id}", - "insuree" + modulesManager.getProjection("insuree.InsureePicker.projection"), - "contributionPlanBundle" + modulesManager.getProjection("contributionPlan.ContributionPlanBundlePicker.projection") +const CONTRACTDETAILS_FULL_PROJECTION = (modulesManager) => [ + "id", + "jsonExt", + "contract{id}", + "insuree" + modulesManager.getProjection("insuree.InsureePicker.projection"), + "contributionPlanBundle" + + modulesManager.getProjection( + "contributionPlan.ContributionPlanBundlePicker.projection" + ), ]; -const CONTRACTCONTRIBUTIONDETAILS_FULL_PROJECTION = modulesManager => [ - "jsonExt", - "contractDetails" + `{${CONTRACTDETAILS_FULL_PROJECTION(modulesManager)}}`, - "contributionPlan" + modulesManager.getProjection("contributionPlan.ContributionPlanPicker.projection") +const CONTRACTCONTRIBUTIONDETAILS_FULL_PROJECTION = (modulesManager) => [ + "jsonExt", + "contractDetails" + `{${CONTRACTDETAILS_FULL_PROJECTION(modulesManager)}}`, + "contributionPlan" + + modulesManager.getProjection( + "contributionPlan.ContributionPlanPicker.projection" + ), ]; -const INSUREEPOLICY_FULL_PROJECTION = modulesManager => [ - "insuree" + modulesManager.getProjection("insuree.InsureePicker.projection") +const INSUREEPOLICY_FULL_PROJECTION = (modulesManager) => [ + "insuree" + modulesManager.getProjection("insuree.InsureePicker.projection"), ]; -const extendedFilters = query => - `"{${query - .filter(filter => filter.split(":")[0] !== APPLY_DEFAULT_VALIDITY_FILTER) - .map(filter => `\\"${filter.split(":")[0]}\\": \\"${filter.split(":")[1].replaceAll('"', '').trim()}\\"`) - .join(",") - .replaceAll("false", "False") - .replaceAll("true", "True")}}"`; +const extendedFilters = (query) => + `"{${query + .filter((filter) => filter.split(":")[0] !== APPLY_DEFAULT_VALIDITY_FILTER) + .map( + (filter) => + `\\"${filter.split(":")[0]}\\": \\"${filter + .split(":")[1] + .replaceAll('"', "") + .trim()}\\"` + ) + .join(",") + .replaceAll("false", "False") + .replaceAll("true", "True")}}"`; function dateTimeToDate(date) { - return date.split('T')[0]; + return date.split("T")[0]; } export function fetchContracts(modulesManager, params) { - const payload = formatPageQueryWithCount( - "contract", - params, - CONTRACT_FULL_PROJECTION(modulesManager) - ); - return graphql(payload, "CONTRACT_CONTRACTS"); + const payload = formatPageQueryWithCount( + "contract", + params, + CONTRACT_FULL_PROJECTION(modulesManager) + ); + return graphql(payload, "CONTRACT_CONTRACTS"); } export function fetchPickerContracts(params) { - const payload = formatPageQuery( - "contract", - params, - CONTRACT_PICKER_PROJECTION - ); - return graphql(payload, "CONTRACT_CONTRACTS"); + const payload = formatPageQuery( + "contract", + params, + CONTRACT_PICKER_PROJECTION + ); + return graphql(payload, "CONTRACT_CONTRACTS"); } export function fetchContract(modulesManager, params) { - const payload = formatPageQuery( - "contract", - params, - CONTRACT_FULL_PROJECTION(modulesManager) - ); - return graphql(payload, "CONTRACT_CONTRACT"); + const payload = formatPageQuery( + "contract", + params, + CONTRACT_FULL_PROJECTION(modulesManager) + ); + return graphql(payload, "CONTRACT_CONTRACT"); +} + +export function clearContract() { + return (dispatch) => { + dispatch({ type: "CONTRACT_CONTRACT_CLEAR" }); + }; } export function fetchContractDetails(modulesManager, params) { - const payload = formatPageQueryWithCount( - "contractDetails", - params, - CONTRACTDETAILS_FULL_PROJECTION(modulesManager) - ); - return graphql(payload, "CONTRACT_CONTRACTDETAILS"); + const payload = formatPageQueryWithCount( + "contractDetails", + params, + CONTRACTDETAILS_FULL_PROJECTION(modulesManager) + ); + return graphql(payload, "CONTRACT_CONTRACTDETAILS"); } export function fetchContractContributionDetails(modulesManager, params) { - const payload = formatPageQueryWithCount( - "contractContributionPlanDetails", - params, - CONTRACTCONTRIBUTIONDETAILS_FULL_PROJECTION(modulesManager) - ); - return graphql(payload, "CONTRACT_CONTRACTCONTRIBUTIONDETAILS"); + const payload = formatPageQueryWithCount( + "contractContributionPlanDetails", + params, + CONTRACTCONTRIBUTIONDETAILS_FULL_PROJECTION(modulesManager) + ); + return graphql(payload, "CONTRACT_CONTRACTCONTRIBUTIONDETAILS"); } export function fetchInsureePolicies(modulesManager, params) { - const payload = formatPageQueryWithCount( - "insureePolicy", - params, - INSUREEPOLICY_FULL_PROJECTION(modulesManager) - ); - return graphql(payload, "CONTRACT_INSUREEPOLICIES"); + const payload = formatPageQueryWithCount( + "insureePolicy", + params, + INSUREEPOLICY_FULL_PROJECTION(modulesManager) + ); + return graphql(payload, "CONTRACT_INSUREEPOLICIES"); } function formatContractGQL(contract, readOnlyFields = []) { - // check if uuid decoded correctly - var policyHolderId = null; - if (contract.policyHolder && !readOnlyFields.includes('policyHolder')){ - policyHolderId = contract.policyHolder.id - policyHolderId = (regexExp.test(policyHolderId))? policyHolderId : decodeId(policyHolderId); - } - return ` - ${!!contract.id ? `id: "${decodeId(contract.id)}"` : ''} - ${!!contract.code && !readOnlyFields.includes('code') ? `code: "${formatGQLString(contract.code)}"` : ""} - ${!!contract.policyHolder && !readOnlyFields.includes('policyHolder') ? `policyHolderId: "${policyHolderId}"` : ""} - ${!!contract.dateApproved && !readOnlyFields.includes('dateApproved') ? `dateApproved: "${contract.dateApproved}"` : ""} - ${!!contract.datePaymentDue && !readOnlyFields.includes('datePaymentDue') ? `datePaymentDue: "${contract.datePaymentDue}"` : ""} - ${!!contract.paymentReference && !readOnlyFields.includes('paymentReference') ? `paymentReference: "${formatGQLString(contract.paymentReference)}"` : ""} - ${!!contract.dateValidFrom && !readOnlyFields.includes('dateValidFrom') ? `dateValidFrom: "${dateTimeToDate(contract.dateValidFrom)}"` : ""} - ${!!contract.dateValidTo && !readOnlyFields.includes('dateValidTo') ? `dateValidTo: "${dateTimeToDate(contract.dateValidTo)}"` : ""} + // check if uuid decoded correctly + var policyHolderId = null; + if (contract.policyHolder && !readOnlyFields.includes("policyHolder")) { + policyHolderId = contract.policyHolder.id; + policyHolderId = regexExp.test(policyHolderId) + ? policyHolderId + : decodeId(policyHolderId); + } + return ` + ${!!contract.id ? `id: "${decodeId(contract.id)}"` : ""} + ${ + !!contract.code && !readOnlyFields.includes("code") + ? `code: "${formatGQLString(contract.code)}"` + : "" + } + ${ + !!contract.policyHolder && !readOnlyFields.includes("policyHolder") + ? `policyHolderId: "${policyHolderId}"` + : "" + } + ${ + !!contract.dateApproved && !readOnlyFields.includes("dateApproved") + ? `dateApproved: "${contract.dateApproved}"` + : "" + } + ${ + !!contract.datePaymentDue && + !readOnlyFields.includes("datePaymentDue") + ? `datePaymentDue: "${contract.datePaymentDue}"` + : "" + } + ${ + !!contract.paymentReference && + !readOnlyFields.includes("paymentReference") + ? `paymentReference: "${formatGQLString( + contract.paymentReference + )}"` + : "" + } + ${ + !!contract.dateValidFrom && !readOnlyFields.includes("dateValidFrom") + ? `dateValidFrom: "${dateTimeToDate(contract.dateValidFrom)}"` + : "" + } + ${ + !!contract.dateValidTo && !readOnlyFields.includes("dateValidTo") + ? `dateValidTo: "${dateTimeToDate(contract.dateValidTo)}"` + : "" + } `; } function formatContractDetailsGQL(contractDetails) { - return ` - ${!!contractDetails.id ? `id: "${decodeId(contractDetails.id)}"` : ''} - ${!!contractDetails.contract ? `contractId: "${decodeId(contractDetails.contract.id)}"` : ''} - ${!!contractDetails.insuree ? `insureeId: ${decodeId(contractDetails.insuree.id)}` : ''} - ${!!contractDetails.contributionPlanBundle ? `contributionPlanBundleId: "${decodeId(contractDetails.contributionPlanBundle.id)}"` : ''} - ${!!contractDetails.jsonExt ? `jsonExt: ${JSON.stringify(contractDetails.jsonExt)}` : ''} + return ` + ${!!contractDetails.id ? `id: "${decodeId(contractDetails.id)}"` : ""} + ${ + !!contractDetails.contract + ? `contractId: "${decodeId(contractDetails.contract.id)}"` + : "" + } + ${ + !!contractDetails.insuree + ? `insureeId: ${decodeId(contractDetails.insuree.id)}` + : "" + } + ${ + !!contractDetails.contributionPlanBundle + ? `contributionPlanBundleId: "${decodeId( + contractDetails.contributionPlanBundle.id + )}"` + : "" + } + ${ + !!contractDetails.jsonExt + ? `jsonExt: ${JSON.stringify(contractDetails.jsonExt)}` + : "" + } `; } export function createContract(contract, clientMutationLabel) { - let mutation = formatMutation("createContract", formatContractGQL(contract), clientMutationLabel); - var requestedDateTime = new Date(); - return graphql( - mutation.payload, - ["CONTRACT_MUTATION_REQ", "CONTRACT_CREATE_CONTRACT_RESP", "CONTRACT_MUTATION_ERR"], - { - clientMutationId: mutation.clientMutationId, - clientMutationLabel, - requestedDateTime - } - ); + let mutation = formatMutation( + "createContract", + formatContractGQL(contract), + clientMutationLabel + ); + var requestedDateTime = new Date(); + return graphql( + mutation.payload, + [ + "CONTRACT_MUTATION_REQ", + "CONTRACT_CREATE_CONTRACT_RESP", + "CONTRACT_MUTATION_ERR", + ], + { + clientMutationId: mutation.clientMutationId, + clientMutationLabel, + requestedDateTime, + } + ); } -export function updateContract(contract, clientMutationLabel, readOnlyFields = []) { - let mutation = formatMutation("updateContract", formatContractGQL(contract, readOnlyFields), clientMutationLabel); - var requestedDateTime = new Date(); - return graphql( - mutation.payload, - ["CONTRACT_MUTATION_REQ", "CONTRACT_UPDATE_CONTRACT_RESP", "CONTRACT_MUTATION_ERR"], - { - clientMutationId: mutation.clientMutationId, - clientMutationLabel, - requestedDateTime - } - ); +export function updateContract( + contract, + clientMutationLabel, + readOnlyFields = [] +) { + let mutation = formatMutation( + "updateContract", + formatContractGQL(contract, readOnlyFields), + clientMutationLabel + ); + var requestedDateTime = new Date(); + return graphql( + mutation.payload, + [ + "CONTRACT_MUTATION_REQ", + "CONTRACT_UPDATE_CONTRACT_RESP", + "CONTRACT_MUTATION_ERR", + ], + { + clientMutationId: mutation.clientMutationId, + clientMutationLabel, + requestedDateTime, + } + ); } -export function deleteContract(contract, clientMutationLabel, clientMutationDetails = null) { - let contractUuids = `uuids: ["${decodeId(contract.id)}"]`; - let mutation = formatMutation("deleteContract", contractUuids, clientMutationLabel, clientMutationDetails); - var requestedDateTime = new Date(); - return graphql( - mutation.payload, - ["CONTRACT_MUTATION_REQ", "CONTRACT_DELETE_CONTRACT_RESP", "CONTRACT_MUTATION_ERR"], - { - clientMutationId: mutation.clientMutationId, - clientMutationLabel, - requestedDateTime - } - ); +export function deleteContract( + contract, + clientMutationLabel, + clientMutationDetails = null +) { + let contractUuids = `uuids: ["${decodeId(contract.id)}"]`; + let mutation = formatMutation( + "deleteContract", + contractUuids, + clientMutationLabel, + clientMutationDetails + ); + var requestedDateTime = new Date(); + return graphql( + mutation.payload, + [ + "CONTRACT_MUTATION_REQ", + "CONTRACT_DELETE_CONTRACT_RESP", + "CONTRACT_MUTATION_ERR", + ], + { + clientMutationId: mutation.clientMutationId, + clientMutationLabel, + requestedDateTime, + } + ); } export function submitContract(contract, clientMutationLabel) { - let contractId = `id: "${decodeId(contract.id)}"`; - let mutation = formatMutation("submitContract", contractId, clientMutationLabel); - var requestedDateTime = new Date(); - return graphql( - mutation.payload, - ["CONTRACT_MUTATION_REQ", "CONTRACT_SUBMIT_CONTRACT_RESP", "CONTRACT_MUTATION_ERR"], - { - clientMutationId: mutation.clientMutationId, - clientMutationLabel, - requestedDateTime - } - ); + let contractId = `id: "${decodeId(contract.id)}"`; + let mutation = formatMutation( + "submitContract", + contractId, + clientMutationLabel + ); + var requestedDateTime = new Date(); + return graphql( + mutation.payload, + [ + "CONTRACT_MUTATION_REQ", + "CONTRACT_SUBMIT_CONTRACT_RESP", + "CONTRACT_MUTATION_ERR", + ], + { + clientMutationId: mutation.clientMutationId, + clientMutationLabel, + requestedDateTime, + } + ); } export function approveContract(contract, clientMutationLabel) { - let contractId = `id: "${decodeId(contract.id)}"`; - let mutation = formatMutation("approveContract", contractId, clientMutationLabel); - var requestedDateTime = new Date(); - return graphql( - mutation.payload, - ["CONTRACT_MUTATION_REQ", "CONTRACT_APPROVE_CONTRACT_RESP", "CONTRACT_MUTATION_ERR"], - { - clientMutationId: mutation.clientMutationId, - clientMutationLabel, - requestedDateTime - } - ); + let contractId = `id: "${decodeId(contract.id)}"`; + let mutation = formatMutation( + "approveContract", + contractId, + clientMutationLabel + ); + var requestedDateTime = new Date(); + return graphql( + mutation.payload, + [ + "CONTRACT_MUTATION_REQ", + "CONTRACT_APPROVE_CONTRACT_RESP", + "CONTRACT_MUTATION_ERR", + ], + { + clientMutationId: mutation.clientMutationId, + clientMutationLabel, + requestedDateTime, + } + ); } -export function approveContractBulk(contracts, clientMutationLabel, clientMutationDetails = null) { - let contractUuids = `contractUuids: ["${contracts.map(contract => decodeId(contract.id)).join("\",\"")}"]`; - let mutation = formatMutation("approveBulkContract", contractUuids, clientMutationLabel, clientMutationDetails); - var requestedDateTime = new Date(); - contracts.forEach(contract => contract.clientMutationId = mutation.clientMutationId); - return graphql( - mutation.payload, - ["CONTRACT_MUTATION_REQ", "CONTRACT_APPROVE_CONTRACT_BULK_RESP", "CONTRACT_MUTATION_ERR"], - { - clientMutationId: mutation.clientMutationId, - clientMutationLabel, - clientMutationDetails: !!clientMutationDetails ? JSON.stringify(clientMutationDetails) : null, - requestedDateTime - } - ); +export function approveContractBulk( + contracts, + clientMutationLabel, + clientMutationDetails = null +) { + let contractUuids = `contractUuids: ["${contracts + .map((contract) => decodeId(contract.id)) + .join('","')}"]`; + let mutation = formatMutation( + "approveBulkContract", + contractUuids, + clientMutationLabel, + clientMutationDetails + ); + var requestedDateTime = new Date(); + contracts.forEach( + (contract) => (contract.clientMutationId = mutation.clientMutationId) + ); + return graphql( + mutation.payload, + [ + "CONTRACT_MUTATION_REQ", + "CONTRACT_APPROVE_CONTRACT_BULK_RESP", + "CONTRACT_MUTATION_ERR", + ], + { + clientMutationId: mutation.clientMutationId, + clientMutationLabel, + clientMutationDetails: !!clientMutationDetails + ? JSON.stringify(clientMutationDetails) + : null, + requestedDateTime, + } + ); } export function approveContractAll(query, clientMutationLabel) { - const mutationInput = ` + const mutationInput = ` contractUuids: [] extendedFilters: ${extendedFilters(query)} `; - const mutation = formatMutation("approveBulkContract", mutationInput, clientMutationLabel); - var requestedDateTime = new Date(); - return graphql( - mutation.payload, - ["CONTRACT_MUTATION_REQ", "CONTRACT_APPROVE_CONTRACT_BULK_RESP", "CONTRACT_MUTATION_ERR"], - { - clientMutationId: mutation.clientMutationId, - clientMutationLabel, - requestedDateTime - } - ); + const mutation = formatMutation( + "approveBulkContract", + mutationInput, + clientMutationLabel + ); + var requestedDateTime = new Date(); + return graphql( + mutation.payload, + [ + "CONTRACT_MUTATION_REQ", + "CONTRACT_APPROVE_CONTRACT_BULK_RESP", + "CONTRACT_MUTATION_ERR", + ], + { + clientMutationId: mutation.clientMutationId, + clientMutationLabel, + requestedDateTime, + } + ); } - export function counterContract(contract, clientMutationLabel) { - let contractId = `id: "${decodeId(contract.id)}"`; - let mutation = formatMutation("counterContract", contractId, clientMutationLabel); - var requestedDateTime = new Date(); - return graphql( - mutation.payload, - ["CONTRACT_MUTATION_REQ", "CONTRACT_COUNTER_CONTRACT_RESP", "CONTRACT_MUTATION_ERR"], - { - clientMutationId: mutation.clientMutationId, - clientMutationLabel, - requestedDateTime - } - ); + let contractId = `id: "${decodeId(contract.id)}"`; + let mutation = formatMutation( + "counterContract", + contractId, + clientMutationLabel + ); + var requestedDateTime = new Date(); + return graphql( + mutation.payload, + [ + "CONTRACT_MUTATION_REQ", + "CONTRACT_COUNTER_CONTRACT_RESP", + "CONTRACT_MUTATION_ERR", + ], + { + clientMutationId: mutation.clientMutationId, + clientMutationLabel, + requestedDateTime, + } + ); } -export function counterContractBulk(contracts, clientMutationLabel, clientMutationDetails = null) { - let contractUuids = `contractUuids: ["${contracts.map(contract => decodeId(contract.id)).join("\",\"")}"]`; - let mutation = formatMutation("counterBulkContract", contractUuids, clientMutationLabel, clientMutationDetails); - var requestedDateTime = new Date(); - contracts.forEach(contract => contract.clientMutationId = mutation.clientMutationId); - return graphql( - mutation.payload, - ["CONTRACT_MUTATION_REQ", "CONTRACT_COUNTER_CONTRACT_BULK_RESP", "CONTRACT_MUTATION_ERR"], - { - clientMutationId: mutation.clientMutationId, - clientMutationLabel, - clientMutationDetails: !!clientMutationDetails ? JSON.stringify(clientMutationDetails) : null, - requestedDateTime - } - ); +export function counterContractBulk( + contracts, + clientMutationLabel, + clientMutationDetails = null +) { + let contractUuids = `contractUuids: ["${contracts + .map((contract) => decodeId(contract.id)) + .join('","')}"]`; + let mutation = formatMutation( + "counterBulkContract", + contractUuids, + clientMutationLabel, + clientMutationDetails + ); + var requestedDateTime = new Date(); + contracts.forEach( + (contract) => (contract.clientMutationId = mutation.clientMutationId) + ); + return graphql( + mutation.payload, + [ + "CONTRACT_MUTATION_REQ", + "CONTRACT_COUNTER_CONTRACT_BULK_RESP", + "CONTRACT_MUTATION_ERR", + ], + { + clientMutationId: mutation.clientMutationId, + clientMutationLabel, + clientMutationDetails: !!clientMutationDetails + ? JSON.stringify(clientMutationDetails) + : null, + requestedDateTime, + } + ); } export function counterContractAll(query, clientMutationLabel) { - const mutationInput = ` + const mutationInput = ` contractUuids: [] extendedFilters: ${extendedFilters(query)} `; - const mutation = formatMutation("counterBulkContract", mutationInput, clientMutationLabel); - var requestedDateTime = new Date(); - return graphql( - mutation.payload, - ["CONTRACT_MUTATION_REQ", "CONTRACT_COUNTER_CONTRACT_BULK_RESP", "CONTRACT_MUTATION_ERR"], - { - clientMutationId: mutation.clientMutationId, - clientMutationLabel, - requestedDateTime - } - ); + const mutation = formatMutation( + "counterBulkContract", + mutationInput, + clientMutationLabel + ); + var requestedDateTime = new Date(); + return graphql( + mutation.payload, + [ + "CONTRACT_MUTATION_REQ", + "CONTRACT_COUNTER_CONTRACT_BULK_RESP", + "CONTRACT_MUTATION_ERR", + ], + { + clientMutationId: mutation.clientMutationId, + clientMutationLabel, + requestedDateTime, + } + ); } export function amendContract(contract, clientMutationLabel) { - let contractId = `id: "${decodeId(contract.id)}"`; - let mutation = formatMutation("amendContract", contractId, clientMutationLabel); - var requestedDateTime = new Date(); - return graphql( - mutation.payload, - ["CONTRACT_MUTATION_REQ", "CONTRACT_AMEND_CONTRACT_RESP", "CONTRACT_MUTATION_ERR"], - { - clientMutationId: mutation.clientMutationId, - clientMutationLabel, - requestedDateTime - } - ); + let contractId = `id: "${decodeId(contract.id)}"`; + let mutation = formatMutation( + "amendContract", + contractId, + clientMutationLabel + ); + var requestedDateTime = new Date(); + return graphql( + mutation.payload, + [ + "CONTRACT_MUTATION_REQ", + "CONTRACT_AMEND_CONTRACT_RESP", + "CONTRACT_MUTATION_ERR", + ], + { + clientMutationId: mutation.clientMutationId, + clientMutationLabel, + requestedDateTime, + } + ); } export function createContractDetails(contractDetails, clientMutationLabel) { - let mutation = formatMutation("createContractDetails", formatContractDetailsGQL(contractDetails), clientMutationLabel); - var requestedDateTime = new Date(); - return graphql( - mutation.payload, - ["CONTRACT_MUTATION_REQ", "CONTRACT_CREATE_CONTRACTDETAILS_RESP", "CONTRACT_MUTATION_ERR"], - { - clientMutationId: mutation.clientMutationId, - clientMutationLabel, - requestedDateTime - } - ); + let mutation = formatMutation( + "createContractDetails", + formatContractDetailsGQL(contractDetails), + clientMutationLabel + ); + var requestedDateTime = new Date(); + return graphql( + mutation.payload, + [ + "CONTRACT_MUTATION_REQ", + "CONTRACT_CREATE_CONTRACTDETAILS_RESP", + "CONTRACT_MUTATION_ERR", + ], + { + clientMutationId: mutation.clientMutationId, + clientMutationLabel, + requestedDateTime, + } + ); } export function updateContractDetails(contractDetails, clientMutationLabel) { - let mutation = formatMutation("updateContractDetails", formatContractDetailsGQL(contractDetails), clientMutationLabel); - var requestedDateTime = new Date(); - return graphql( - mutation.payload, - ["CONTRACT_MUTATION_REQ", "CONTRACT_UPDATE_CONTRACTDETAILS_RESP", "CONTRACT_MUTATION_ERR"], - { - clientMutationId: mutation.clientMutationId, - clientMutationLabel, - requestedDateTime - } - ); + let mutation = formatMutation( + "updateContractDetails", + formatContractDetailsGQL(contractDetails), + clientMutationLabel + ); + var requestedDateTime = new Date(); + return graphql( + mutation.payload, + [ + "CONTRACT_MUTATION_REQ", + "CONTRACT_UPDATE_CONTRACTDETAILS_RESP", + "CONTRACT_MUTATION_ERR", + ], + { + clientMutationId: mutation.clientMutationId, + clientMutationLabel, + requestedDateTime, + } + ); } -export function deleteContractDetails(contractDetails, clientMutationLabel, clientMutationDetails = null) { - let contractDetailsUuids = `uuids: ["${decodeId(contractDetails.id)}"]`; - let mutation = formatMutation("deleteContractDetails", contractDetailsUuids, clientMutationLabel, clientMutationDetails); - var requestedDateTime = new Date(); - return graphql( - mutation.payload, - ["CONTRACT_MUTATION_REQ", "CONTRACT_DELETE_CONTRACTDETAILS_RESP", "CONTRACT_MUTATION_ERR"], - { - clientMutationId: mutation.clientMutationId, - clientMutationLabel, - requestedDateTime - } - ); +export function deleteContractDetails( + contractDetails, + clientMutationLabel, + clientMutationDetails = null +) { + let contractDetailsUuids = `uuids: ["${decodeId(contractDetails.id)}"]`; + let mutation = formatMutation( + "deleteContractDetails", + contractDetailsUuids, + clientMutationLabel, + clientMutationDetails + ); + var requestedDateTime = new Date(); + return graphql( + mutation.payload, + [ + "CONTRACT_MUTATION_REQ", + "CONTRACT_DELETE_CONTRACTDETAILS_RESP", + "CONTRACT_MUTATION_ERR", + ], + { + clientMutationId: mutation.clientMutationId, + clientMutationLabel, + requestedDateTime, + } + ); } + +export const contractCodeValidation = (mm, variables) => { + return graphqlWithVariables( + ` + query ($contractCode: String!) { + validateContractCode(contractCode: $contractCode) + { + isValid + } + } + `, + variables, + "CONTRACT_CODE_FIELDS_VALIDATION" + ); +}; + +export const contractCodeSetValid = () => { + return (dispatch) => { + dispatch({ type: "CONTRACT_CODE_FIELDS_VALIDATION_SET_VALID" }); + }; +}; + +export const contractCodeClear = () => { + return (dispatch) => { + dispatch({ type: "CONTRACT_CODE_FIELDS_VALIDATION_CLEAR" }); + }; +}; diff --git a/src/components/ContractForm.js b/src/components/ContractForm.js index 40d4695..560a6fb 100644 --- a/src/components/ContractForm.js +++ b/src/components/ContractForm.js @@ -1,4 +1,15 @@ import React, { Component, Fragment } from "react"; +import { bindActionCreators } from "redux"; +import { connect } from "react-redux"; +import { injectIntl } from "react-intl"; + +import { Fab, Tooltip } from "@material-ui/core"; +import { withTheme, withStyles } from "@material-ui/core/styles"; +import CheckIcon from "@material-ui/icons/Check"; +import CloseIcon from "@material-ui/icons/Close"; +import NoteAddIcon from "@material-ui/icons/NoteAdd"; +import OpenInBrowserIcon from "@material-ui/icons/OpenInBrowser"; + import { Form, withModulesManager, @@ -8,17 +19,7 @@ import { decodeId, Helmet, } from "@openimis/fe-core"; -import { Fab, Tooltip } from "@material-ui/core"; -import { injectIntl } from "react-intl"; -import { bindActionCreators } from "redux"; -import { connect } from "react-redux"; -import { withTheme, withStyles } from "@material-ui/core/styles"; -import { fetchContract } from "../actions"; -import OpenInBrowserIcon from "@material-ui/icons/OpenInBrowser"; -import CheckIcon from "@material-ui/icons/Check"; -import CloseIcon from "@material-ui/icons/Close"; -import NoteAddIcon from "@material-ui/icons/NoteAdd"; -import ContractHeadPanel from "./ContractHeadPanel"; +import { fetchContract, clearContract} from "../actions"; import { UPDATABLE_STATES, APPROVABLE_STATES, @@ -30,6 +31,7 @@ import { RIGHT_PORTALPOLICYHOLDERCONTRACT_SUBMIT, RIGHT_PORTALPOLICYHOLDERCONTRACT_AMEND } from "../constants"; +import ContractHeadPanel from "./ContractHeadPanel"; import ContractTabPanel from "./ContractTabPanel"; const styles = theme => ({ @@ -98,6 +100,10 @@ class ContractForm extends Component { } } + componentWillUnmount() { + this.props.clearContract(); + } + isMandatoryFieldsEmpty = () => { const { contract } = this.state; if (!!contract.code && !!contract.dateValidFrom && !!contract.dateValidTo) { @@ -106,7 +112,7 @@ class ContractForm extends Component { return true; } - canSave = () => !this.isMandatoryFieldsEmpty(); + canSave = () => !this.isMandatoryFieldsEmpty() && this.props.isCodeValid; save = contract => this.props.save(contract, this.state.readOnlyFields); @@ -175,7 +181,7 @@ class ContractForm extends Component { } render() { - const { intl, rights, classes, contract, back, setConfirmedAction, counter, predefinedPolicyHolderId } = this.props; + const { intl, rights, classes, contract, back, setConfirmedAction, counter, save, predefinedPolicyHolderId } = this.props; return ( @@ -205,6 +211,7 @@ class ContractForm extends Component { setConfirmedAction={setConfirmedAction} isAmendment={this.isAmendment()} isPolicyHolderPredefined={!!predefinedPolicyHolderId} + openDirty={save} /> {rights.includes(RIGHT_POLICYHOLDERCONTRACT_APPROVE) && this.isApprovable() && !this.state.isDirty && ( @@ -227,11 +234,12 @@ const mapStateToProps = state => ({ errorContract: state.contract.errorContract, policyHolders: state.policyHolder.policyHolders, submittingMutation: state.contract.submittingMutation, - mutation: state.contract.mutation + mutation: state.contract.mutation, + isCodeValid: state.contract.validationFields?.contractCode?.isValid, }); const mapDispatchToProps = dispatch => { - return bindActionCreators({ fetchContract, journalize }, dispatch); + return bindActionCreators({ clearContract, fetchContract, journalize }, dispatch); }; export default withModulesManager(injectIntl(withTheme(withStyles(styles)(connect(mapStateToProps, mapDispatchToProps)(ContractForm))))); diff --git a/src/components/ContractHeadPanel.js b/src/components/ContractHeadPanel.js index 45ee3ea..cff61f6 100644 --- a/src/components/ContractHeadPanel.js +++ b/src/components/ContractHeadPanel.js @@ -1,233 +1,322 @@ import React, { Fragment } from "react"; +import { connect } from "react-redux"; +import { injectIntl } from "react-intl"; + import { Grid, Divider, Typography } from "@material-ui/core"; +import { withTheme, withStyles } from "@material-ui/core/styles"; + import { - withModulesManager, - formatMessage, - FormPanel, - TextInput, - FormattedMessage, - PublishedComponent, - NumberInput + withModulesManager, + formatMessage, + FormPanel, + TextInput, + FormattedMessage, + PublishedComponent, + NumberInput, + ValidatedTextInput, } from "@openimis/fe-core"; -import { injectIntl } from "react-intl"; -import { withTheme, withStyles } from "@material-ui/core/styles"; import { - MAX_CODE_LENGTH, - MAX_PAYMENT_REFERENCE_LENGTH, - MIN_AMOUNT_VALUE, - MIN_AMENDMENT_VALUE, - DEFAULT_STATE_VALUE, - DEFAULT_AMENDMENT_VALUE, - RIGHT_POLICYHOLDERCONTRACT_APPROVE, - RIGHT_POLICYHOLDERCONTRACT_UPDATE, - RIGHT_PORTALPOLICYHOLDERCONTRACT_UPDATE + contractCodeValidation, + contractCodeSetValid, + contractCodeClear, +} from "../actions"; +import { + MAX_CODE_LENGTH, + MAX_PAYMENT_REFERENCE_LENGTH, + MIN_AMOUNT_VALUE, + MIN_AMENDMENT_VALUE, + DEFAULT_STATE_VALUE, + DEFAULT_AMENDMENT_VALUE, + RIGHT_POLICYHOLDERCONTRACT_APPROVE, + RIGHT_POLICYHOLDERCONTRACT_UPDATE, + RIGHT_PORTALPOLICYHOLDERCONTRACT_UPDATE, } from "../constants"; import ContractStatePicker from "../pickers/ContractStatePicker"; -const styles = theme => ({ - tableTitle: theme.table.title, - item: theme.paper.item, - fullHeight: { - height: "100%" - } +const styles = (theme) => ({ + tableTitle: theme.table.title, + item: theme.paper.item, + fullHeight: { + height: "100%", + }, }); class ContractHeadPanel extends FormPanel { - constructor(props) { - super(props); - this.state = { - isPolicyHolderSet: false - }; - this.updatableFields = ['code', 'policyHolder', 'paymentReference', 'dateValidFrom', 'dateValidTo']; - } + constructor(props) { + super(props); + this.state = { + isPolicyHolderSet: false, + }; + this.updatableFields = [ + "code", + "policyHolder", + "paymentReference", + "dateValidFrom", + "dateValidTo", + ]; + } - componentDidUpdate(prevProps, prevState, snapshot) { - this._componentDidUpdate(prevProps, prevState, snapshot); - if (prevProps.savedContract !== this.props.savedContract && !!this.props.savedContract) { - this.setState( - (_, props) => ({ isPolicyHolderSet: !!props.savedContract.policyHolder }), - () => this.setReadOnlyFields() - ); - } + componentDidUpdate(prevProps, prevState, snapshot) { + this._componentDidUpdate(prevProps, prevState, snapshot); + if ( + prevProps.savedContract !== this.props.savedContract && + !!this.props.savedContract + ) { + this.setState( + (_, props) => ({ + isPolicyHolderSet: !!props.savedContract.policyHolder, + }), + () => this.setReadOnlyFields() + ); } + } - setReadOnlyFields = () => this.props.setReadOnlyFields([...this.updatableFields.filter(f => this.isReadOnly(f))]); + setReadOnlyFields = () => + this.props.setReadOnlyFields([ + ...this.updatableFields.filter((f) => this.isReadOnly(f)), + ]); - isReadOnly = field => { - const { rights, isUpdatable, isApprovable } = this.props; - switch (field) { - case 'policyHolder': - return this.state.isPolicyHolderSet; - case 'code': - case 'dateValidFrom': - case 'dateValidTo': - if (rights.includes(RIGHT_POLICYHOLDERCONTRACT_APPROVE)) { - return isApprovable ? true : !isUpdatable; - } else return true; - default: - if ( - [ - RIGHT_POLICYHOLDERCONTRACT_UPDATE, - RIGHT_POLICYHOLDERCONTRACT_APPROVE, - RIGHT_PORTALPOLICYHOLDERCONTRACT_UPDATE - ].some(right => rights.includes(right)) - ) { - return isApprovable ? false : !isUpdatable; - } else return true; - } + isReadOnly = (field) => { + const { rights, isUpdatable, isApprovable } = this.props; + switch (field) { + case "policyHolder": + return this.state.isPolicyHolderSet; + case "code": + case "dateValidFrom": + case "dateValidTo": + if (rights.includes(RIGHT_POLICYHOLDERCONTRACT_APPROVE)) { + return isApprovable ? true : !isUpdatable; + } else return true; + default: + if ( + [ + RIGHT_POLICYHOLDERCONTRACT_UPDATE, + RIGHT_POLICYHOLDERCONTRACT_APPROVE, + RIGHT_PORTALPOLICYHOLDERCONTRACT_UPDATE, + ].some((right) => rights.includes(right)) + ) { + return isApprovable ? false : !isUpdatable; + } else return true; } + }; - render() { - const { intl, classes, edited, mandatoryFieldsEmpty, readOnlyFields, isAmendment, isPolicyHolderPredefined } = this.props; - return ( - - - - - - - - - - - - - - {mandatoryFieldsEmpty && - -
- -
- -
- } - - - this.updateAttribute('code', v)} - readOnly={readOnlyFields.includes('code') || isAmendment} - /> - - - this.updateAttribute('policyHolder', v)} - readOnly={readOnlyFields.includes('policyHolder') || isAmendment || isPolicyHolderPredefined} - /> - - - - - - - - - - - - - - - - - - - - - this.updateAttribute('paymentReference', v)} - readOnly={readOnlyFields.includes('paymentReference')} - /> - - - - - - this.updateAttribute('dateValidFrom', v)} - readOnly={readOnlyFields.includes('dateValidFrom') || isAmendment} - /> - - - this.updateAttribute('dateValidTo', v)} - readOnly={readOnlyFields.includes('dateValidTo')} - /> - - -
- ) - } + shouldValidate = (input) => { + const { savedContractCode } = this.props; + return input !== savedContractCode; + }; + + render() { + const { + intl, + classes, + edited, + mandatoryFieldsEmpty, + readOnlyFields, + isAmendment, + isPolicyHolderPredefined, + isCodeValid, + isCodeValidating, + codeValidationError, + } = this.props; + return ( + + + + + + + + + + + + + + {mandatoryFieldsEmpty && ( + +
+ +
+ +
+ )} + + + this.updateAttribute("code", v)} + readOnly={readOnlyFields.includes("code") || isAmendment} + /> + + + this.updateAttribute("policyHolder", v)} + readOnly={ + readOnlyFields.includes("policyHolder") || + isAmendment || + isPolicyHolderPredefined + } + /> + + + + + + + + + + + + + + + + + + + + + this.updateAttribute("paymentReference", v)} + readOnly={readOnlyFields.includes("paymentReference")} + /> + + + + + + this.updateAttribute("dateValidFrom", v)} + readOnly={readOnlyFields.includes("dateValidFrom") || isAmendment} + /> + + + this.updateAttribute("dateValidTo", v)} + readOnly={readOnlyFields.includes("dateValidTo")} + /> + + +
+ ); + } } -export default withModulesManager(injectIntl(withTheme(withStyles(styles)(ContractHeadPanel)))) +const mapStateToProps = (store) => ({ + isCodeValid: store.contract.validationFields?.contractCode?.isValid, + isCodeValidating: store.contract.validationFields?.contractCode?.isValidating, + codeValidationError: + store.contract.validationFields?.contractCode?.validationError, + savedContractCode: store.contract.contract?.code, +}); + +export default withModulesManager( + injectIntl( + connect(mapStateToProps)(withTheme(withStyles(styles)(ContractHeadPanel))) + ) +); diff --git a/src/constants.js b/src/constants.js index 5674b49..553f1ef 100644 --- a/src/constants.js +++ b/src/constants.js @@ -41,3 +41,5 @@ export const RIGHT_PORTALPAYMENT_SEARCH = 154501 export const RIGHT_INSUREEPOLICY_SEARCH = 101500 export const RIGHT_PORTALINSUREEPOLICY_SEARCH = 154901 export const APPLY_DEFAULT_VALIDITY_FILTER = "applyDefaultValidityFilter"; +export const MODULE_NAME = "contact"; +export const CONTRACT_ROUTE_CONTRACT = "contract.route.contract"; diff --git a/src/pages/ContractsPage.js b/src/pages/ContractsPage.js index 02b076e..b566eb1 100644 --- a/src/pages/ContractsPage.js +++ b/src/pages/ContractsPage.js @@ -1,58 +1,117 @@ import React, { Component } from "react"; -import { Helmet, withModulesManager, formatMessage, withTooltip, historyPush, decodeId } from "@openimis/fe-core"; +import { bindActionCreators } from "redux"; +import { + Helmet, + withModulesManager, + formatMessage, + withTooltip, + historyPush, + decodeId, + clearCurrentPaginationPage, +} from "@openimis/fe-core"; import { injectIntl } from "react-intl"; import { withTheme, withStyles } from "@material-ui/core/styles"; import { connect } from "react-redux"; -import { RIGHT_POLICYHOLDERCONTRACT_SEARCH, RIGHT_POLICYHOLDERCONTRACT_CREATE, - RIGHT_POLICYHOLDERCONTRACT_UPDATE, RIGHT_POLICYHOLDERCONTRACT_APPROVE } from "../constants" +import { + RIGHT_POLICYHOLDERCONTRACT_SEARCH, + RIGHT_POLICYHOLDERCONTRACT_CREATE, + RIGHT_POLICYHOLDERCONTRACT_UPDATE, + RIGHT_POLICYHOLDERCONTRACT_APPROVE, + CONTRACT_ROUTE_CONTRACT, + MODULE_NAME, +} from "../constants"; import ContractSearcher from "../components/ContractSearcher"; import { Fab } from "@material-ui/core"; import AddIcon from "@material-ui/icons/Add"; -const styles = theme => ({ - page: theme.page, - fab: theme.fab -}) +const styles = (theme) => ({ + page: theme.page, + fab: theme.fab, +}); class ContractsPage extends Component { - onAdd = () => historyPush(this.props.modulesManager, this.props.history, "contract.route.contract"); + onAdd = () => + historyPush( + this.props.modulesManager, + this.props.history, + CONTRACT_ROUTE_CONTRACT + ); - contractUpdatePageUrl = contract => `${this.props.modulesManager.getRef("contract.route.contract")}${"/" + decodeId(contract.id)}`; + contractUpdatePageUrl = (contract) => + `${this.props.modulesManager.getRef(CONTRACT_ROUTE_CONTRACT)}${ + "/" + decodeId(contract.id) + }`; - onDoubleClick = (contract, newTab = false) => { - const { rights, modulesManager, history } = this.props; - if (rights.includes(RIGHT_POLICYHOLDERCONTRACT_UPDATE) || rights.includes(RIGHT_POLICYHOLDERCONTRACT_APPROVE)) { - historyPush(modulesManager, history, "contract.route.contract", [decodeId(contract.id)], newTab); - } - } - - render() { - const { intl, classes, rights } = this.props; - return ( - rights.includes(RIGHT_POLICYHOLDERCONTRACT_SEARCH) && ( -
- - - {rights.includes(RIGHT_POLICYHOLDERCONTRACT_CREATE) && withTooltip( -
- - - -
, - formatMessage(intl, "contract", "createButton.tooltip") - )} -
- ) - ) + onDoubleClick = (contract, newTab = false) => { + const { rights, modulesManager, history } = this.props; + if ( + rights.includes(RIGHT_POLICYHOLDERCONTRACT_UPDATE) || + rights.includes(RIGHT_POLICYHOLDERCONTRACT_APPROVE) + ) { + historyPush( + modulesManager, + history, + CONTRACT_ROUTE_CONTRACT, + [decodeId(contract.id)], + newTab + ); } + }; + + componentDidMount = () => { + const { module } = this.props; + if (module !== MODULE_NAME) this.props.clearCurrentPaginationPage(); + }; + + render() { + const { intl, classes, rights } = this.props; + return ( + rights.includes(RIGHT_POLICYHOLDERCONTRACT_SEARCH) && ( +
+ + + {rights.includes(RIGHT_POLICYHOLDERCONTRACT_CREATE) && + withTooltip( +
+ + + +
, + formatMessage(intl, "contract", "createButton.tooltip") + )} +
+ ) + ); + } } -const mapStateToProps = state => ({ - rights: !!state.core && !!state.core.user && !!state.core.user.i_user ? state.core.user.i_user.rights : [] +const mapStateToProps = (state) => ({ + rights: + !!state.core && !!state.core.user && !!state.core.user.i_user + ? state.core.user.i_user.rights + : [], + module: state.core?.savedPagination?.module, }); -export default withModulesManager(injectIntl(withTheme(withStyles(styles)(connect(mapStateToProps, null)(ContractsPage))))); +const mapDispatchToProps = (dispatch) => + bindActionCreators({ clearCurrentPaginationPage }, dispatch); + +export default withModulesManager( + injectIntl( + withTheme( + withStyles(styles)( + connect(mapStateToProps, mapDispatchToProps)(ContractsPage) + ) + ) + ) +); diff --git a/src/reducer.js b/src/reducer.js index 7a3182a..1b6062f 100644 --- a/src/reducer.js +++ b/src/reducer.js @@ -1,201 +1,288 @@ import { - formatServerError, formatGraphQLError, parseData, pageInfo, - dispatchMutationReq, dispatchMutationErr, dispatchMutationResp + formatServerError, + formatGraphQLError, + parseData, + pageInfo, + dispatchMutationReq, + dispatchMutationErr, + dispatchMutationResp, } from "@openimis/fe-core"; function reducer( - state = { - fetchingContracts: false, - errorContracts: null, + state = { + fetchingContracts: false, + errorContracts: null, + fetchedContracts: false, + contracts: [], + contractsPageInfo: {}, + contractsTotalCount: 0, + fetchingContract: false, + fetchedContract: false, + contract: {}, + errorContract: null, + fetchingContractDetails: false, + errorContractDetails: null, + fetchedContractDetails: false, + contractDetails: [], + contractDetailsPageInfo: {}, + contractDetailsTotalCount: 0, + fetchingContractContributionDetails: false, + errorContractContributionDetails: null, + fetchedContractContributionDetails: false, + contractContributionDetails: [], + contractContributionDetailsPageInfo: {}, + contractContributionDetailsTotalCount: 0, + fetchingInsureePolicies: false, + errorInsureePolicies: null, + fetchedInsureePolicies: false, + insureePolicies: [], + insureePoliciesPageInfo: {}, + insureePoliciesTotalCount: 0, + }, + action +) { + switch (action.type) { + case "CONTRACT_CONTRACTS_REQ": + return { + ...state, + fetchingContracts: true, fetchedContracts: false, contracts: [], contractsPageInfo: {}, contractsTotalCount: 0, + errorContracts: null, + }; + case "CONTRACT_CONTRACTS_RESP": + return { + ...state, + fetchingContracts: false, + fetchedContracts: true, + contracts: parseData(action.payload.data.contract), + contractsPageInfo: pageInfo(action.payload.data.contract), + contractsTotalCount: !!action.payload.data.contract + ? action.payload.data.contract.totalCount + : null, + errorContracts: formatGraphQLError(action.payload), + }; + case "CONTRACT_CONTRACTS_ERR": + return { + ...state, + fetchingContracts: false, + errorContracts: formatServerError(action.payload), + }; + case "CONTRACT_CONTRACT_REQ": + return { + ...state, + fetchingContract: true, + fetchedContract: false, + contract: [], + errorContract: null, + }; + case "CONTRACT_CONTRACT_RESP": + return { + ...state, + fetchingContract: false, + fetchedContract: true, + contract: parseData(action.payload.data.contract).find( + (contract) => !!contract + ), + errorContract: formatGraphQLError(action.payload), + }; + case "CONTRACT_CONTRACT_ERR": + return { + ...state, + fetchingContract: false, + errorContract: formatServerError(action.payload), + }; + case "CONTRACT_CONTRACT_CLEAR": + return { + ...state, fetchingContract: false, fetchedContract: false, contract: {}, errorContract: null, - fetchingContractDetails: false, - errorContractDetails: null, + }; + case "CONTRACT_CONTRACTDETAILS_REQ": + return { + ...state, + fetchingContractDetails: true, fetchedContractDetails: false, contractDetails: [], contractDetailsPageInfo: {}, contractDetailsTotalCount: 0, - fetchingContractContributionDetails: false, - errorContractContributionDetails: null, + errorContractDetails: null, + }; + case "CONTRACT_CONTRACTDETAILS_RESP": + return { + ...state, + fetchingContractDetails: false, + fetchedContractDetails: true, + contractDetails: parseData(action.payload.data.contractDetails), + contractDetailsPageInfo: pageInfo(action.payload.data.contractDetails), + contractDetailsTotalCount: !!action.payload.data.contractDetails + ? action.payload.data.contractDetails.totalCount + : null, + errorContractDetails: formatGraphQLError(action.payload), + }; + case "CONTRACT_CONTRACTDETAILS_ERR": + return { + ...state, + fetchingContractDetails: false, + errorContractDetails: formatServerError(action.payload), + }; + case "CONTRACT_CONTRACTCONTRIBUTIONDETAILS_REQ": + return { + ...state, + fetchingContractContributionDetails: true, fetchedContractContributionDetails: false, contractContributionDetails: [], contractContributionDetailsPageInfo: {}, contractContributionDetailsTotalCount: 0, - fetchingInsureePolicies: false, - errorInsureePolicies: null, + errorContractContributionDetails: null, + }; + case "CONTRACT_CONTRACTCONTRIBUTIONDETAILS_RESP": + return { + ...state, + fetchingContractContributionDetails: false, + fetchedContractContributionDetails: true, + contractContributionDetails: parseData( + action.payload.data.contractContributionPlanDetails + ), + contractContributionDetailsPageInfo: pageInfo( + action.payload.data.contractContributionPlanDetails + ), + contractContributionDetailsTotalCount: !!action.payload.data + .contractContributionPlanDetails + ? action.payload.data.contractContributionPlanDetails.totalCount + : null, + errorContractContributionDetails: formatGraphQLError(action.payload), + }; + case "CONTRACT_CONTRACTCONTRIBUTIONDETAILS_ERR": + return { + ...state, + fetchingContractContributionDetails: false, + errorContractContributionDetails: formatServerError(action.payload), + }; + case "CONTRACT_INSUREEPOLICIES_REQ": + return { + ...state, + fetchingInsureePolicies: true, fetchedInsureePolicies: false, insureePolicies: [], insureePoliciesPageInfo: {}, insureePoliciesTotalCount: 0, - }, - action -) { - switch(action.type) { - case "CONTRACT_CONTRACTS_REQ": - return { - ...state, - fetchingContracts: true, - fetchedContracts: false, - contracts: [], - contractsPageInfo: {}, - contractsTotalCount: 0, - errorContracts: null - }; - case "CONTRACT_CONTRACTS_RESP": - return { - ...state, - fetchingContracts: false, - fetchedContracts: true, - contracts: parseData(action.payload.data.contract), - contractsPageInfo: pageInfo(action.payload.data.contract), - contractsTotalCount: !!action.payload.data.contract ? action.payload.data.contract.totalCount : null, - errorContracts: formatGraphQLError(action.payload) - }; - case "CONTRACT_CONTRACTS_ERR": - return { - ...state, - fetchingContracts: false, - errorContracts: formatServerError(action.payload) - }; - case "CONTRACT_CONTRACT_REQ": - return { - ...state, - fetchingContract: true, - fetchedContract: false, - contract: [], - errorContract: null - }; - case "CONTRACT_CONTRACT_RESP": - return { - ...state, - fetchingContract: false, - fetchedContract: true, - contract: parseData(action.payload.data.contract).find(contract => !!contract), - errorContract: formatGraphQLError(action.payload) - }; - case "CONTRACT_CONTRACT_ERR": - return { - ...state, - fetchingContract: false, - errorContract: formatServerError(action.payload) - }; - case "CONTRACT_CONTRACTDETAILS_REQ": - return { - ...state, - fetchingContractDetails: true, - fetchedContractDetails: false, - contractDetails: [], - contractDetailsPageInfo: {}, - contractDetailsTotalCount: 0, - errorContractDetails: null - }; - case "CONTRACT_CONTRACTDETAILS_RESP": - return { - ...state, - fetchingContractDetails: false, - fetchedContractDetails: true, - contractDetails: parseData(action.payload.data.contractDetails), - contractDetailsPageInfo: pageInfo(action.payload.data.contractDetails), - contractDetailsTotalCount: !!action.payload.data.contractDetails ? action.payload.data.contractDetails.totalCount : null, - errorContractDetails: formatGraphQLError(action.payload) - }; - case "CONTRACT_CONTRACTDETAILS_ERR": - return { - ...state, - fetchingContractDetails: false, - errorContractDetails: formatServerError(action.payload) - }; - case "CONTRACT_CONTRACTCONTRIBUTIONDETAILS_REQ": - return { - ...state, - fetchingContractContributionDetails: true, - fetchedContractContributionDetails: false, - contractContributionDetails: [], - contractContributionDetailsPageInfo: {}, - contractContributionDetailsTotalCount: 0, - errorContractContributionDetails: null - }; - case "CONTRACT_CONTRACTCONTRIBUTIONDETAILS_RESP": - return { - ...state, - fetchingContractContributionDetails: false, - fetchedContractContributionDetails: true, - contractContributionDetails: parseData(action.payload.data.contractContributionPlanDetails), - contractContributionDetailsPageInfo: pageInfo(action.payload.data.contractContributionPlanDetails), - contractContributionDetailsTotalCount: !!action.payload.data.contractContributionPlanDetails ? action.payload.data.contractContributionPlanDetails.totalCount : null, - errorContractContributionDetails: formatGraphQLError(action.payload) - }; - case "CONTRACT_CONTRACTCONTRIBUTIONDETAILS_ERR": - return { - ...state, - fetchingContractContributionDetails: false, - errorContractContributionDetails: formatServerError(action.payload) - }; - case "CONTRACT_INSUREEPOLICIES_REQ": - return { - ...state, - fetchingInsureePolicies: true, - fetchedInsureePolicies: false, - insureePolicies: [], - insureePoliciesPageInfo: {}, - insureePoliciesTotalCount: 0, - errorInsureePolicies: null - }; - case "CONTRACT_INSUREEPOLICIES_RESP": - return { - ...state, - fetchingInsureePolicies: false, - fetchedInsureePolicies: true, - insureePolicies: parseData(action.payload.data.insureePolicy), - insureePoliciesPageInfo: pageInfo(action.payload.data.insureePolicy), - insureePoliciesTotalCount: !!action.payload.data.insureePolicy - ? action.payload.data.insureePolicy.totalCount - : null, - errorInsureePolicies: formatGraphQLError(action.payload) - }; - case "CONTRACT_INSUREEPOLICIES_ERR": - return { - ...state, - fetchingInsureePolicies: false, - errorInsureePolicies: formatServerError(action.payload) - }; - case "CONTRACT_MUTATION_REQ": - return dispatchMutationReq(state, action); - case "CONTRACT_MUTATION_ERR": - return dispatchMutationErr(state, action); - case "CONTRACT_CREATE_CONTRACT_RESP": - return dispatchMutationResp(state, "createContract", action); - case "CONTRACT_UPDATE_CONTRACT_RESP": - return dispatchMutationResp(state, "updateContract", action); - case "CONTRACT_DELETE_CONTRACT_RESP": - return dispatchMutationResp(state, "deleteContract", action); - case "CONTRACT_SUBMIT_CONTRACT_RESP": - return dispatchMutationResp(state, "submitContract", action); - case "CONTRACT_APPROVE_CONTRACT_RESP": - return dispatchMutationResp(state, "approveContract", action); - case "CONTRACT_APPROVE_CONTRACT_BULK_RESP": - return dispatchMutationResp(state, "approveBulkContract", action); - case "CONTRACT_COUNTER_CONTRACT_RESP": - return dispatchMutationResp(state, "counterContract", action); - case "CONTRACT_COUNTER_CONTRACT_BULK_RESP": - return dispatchMutationResp(state, "counterBulkContract", action); - case "CONTRACT_AMEND_CONTRACT_RESP": - return dispatchMutationResp(state, "amendContract", action); - case "CONTRACT_CREATE_CONTRACTDETAILS_RESP": - return dispatchMutationResp(state, "createContractDetails", action); - case "CONTRACT_UPDATE_CONTRACTDETAILS_RESP": - return dispatchMutationResp(state, "updateContractDetails", action); - case "CONTRACT_DELETE_CONTRACTDETAILS_RESP": - return dispatchMutationResp(state, "deleteContractDetails", action); - default: - return state; - } + errorInsureePolicies: null, + }; + case "CONTRACT_INSUREEPOLICIES_RESP": + return { + ...state, + fetchingInsureePolicies: false, + fetchedInsureePolicies: true, + insureePolicies: parseData(action.payload.data.insureePolicy), + insureePoliciesPageInfo: pageInfo(action.payload.data.insureePolicy), + insureePoliciesTotalCount: !!action.payload.data.insureePolicy + ? action.payload.data.insureePolicy.totalCount + : null, + errorInsureePolicies: formatGraphQLError(action.payload), + }; + case "CONTRACT_INSUREEPOLICIES_ERR": + return { + ...state, + fetchingInsureePolicies: false, + errorInsureePolicies: formatServerError(action.payload), + }; + case "CONTRACT_CODE_FIELDS_VALIDATION_REQ": + return { + ...state, + validationFields: { + ...state.validationFields, + contractCode: { + isValidating: true, + isValid: false, + validationError: null, + }, + }, + }; + case "CONTRACT_CODE_FIELDS_VALIDATION_RESP": + return { + ...state, + validationFields: { + ...state.validationFields, + contractCode: { + isValidating: false, + isValid: action.payload?.data?.validateContractCode.isValid, + validationError: formatGraphQLError(action.payload), + }, + }, + }; + case "CONTRACT_CODE_FIELDS_VALIDATION_ERR": + return { + ...state, + validationFields: { + ...state.validationFields, + contractCode: { + isValidating: false, + isValid: false, + validationError: formatServerError(action.payload), + }, + }, + }; + case "CONTRACT_CODE_FIELDS_VALIDATION_CLEAR": + return { + ...state, + validationFields: { + ...state.validationFields, + contractCode: { + isValidating: true, + isValid: false, + validationError: null, + }, + }, + }; + case "CONTRACT_CODE_FIELDS_VALIDATION_SET_VALID": + return { + ...state, + validationFields: { + ...state.validationFields, + contractCode: { + isValidating: false, + isValid: true, + validationError: null, + }, + }, + }; + + case "CONTRACT_MUTATION_REQ": + return dispatchMutationReq(state, action); + case "CONTRACT_MUTATION_ERR": + return dispatchMutationErr(state, action); + case "CONTRACT_CREATE_CONTRACT_RESP": + return dispatchMutationResp(state, "createContract", action); + case "CONTRACT_UPDATE_CONTRACT_RESP": + return dispatchMutationResp(state, "updateContract", action); + case "CONTRACT_DELETE_CONTRACT_RESP": + return dispatchMutationResp(state, "deleteContract", action); + case "CONTRACT_SUBMIT_CONTRACT_RESP": + return dispatchMutationResp(state, "submitContract", action); + case "CONTRACT_APPROVE_CONTRACT_RESP": + return dispatchMutationResp(state, "approveContract", action); + case "CONTRACT_APPROVE_CONTRACT_BULK_RESP": + return dispatchMutationResp(state, "approveBulkContract", action); + case "CONTRACT_COUNTER_CONTRACT_RESP": + return dispatchMutationResp(state, "counterContract", action); + case "CONTRACT_COUNTER_CONTRACT_BULK_RESP": + return dispatchMutationResp(state, "counterBulkContract", action); + case "CONTRACT_AMEND_CONTRACT_RESP": + return dispatchMutationResp(state, "amendContract", action); + case "CONTRACT_CREATE_CONTRACTDETAILS_RESP": + return dispatchMutationResp(state, "createContractDetails", action); + case "CONTRACT_UPDATE_CONTRACTDETAILS_RESP": + return dispatchMutationResp(state, "updateContractDetails", action); + case "CONTRACT_DELETE_CONTRACTDETAILS_RESP": + return dispatchMutationResp(state, "deleteContractDetails", action); + default: + return state; + } } export default reducer; diff --git a/src/translations/en.json b/src/translations/en.json index 6ff49b7..b73c36c 100644 --- a/src/translations/en.json +++ b/src/translations/en.json @@ -52,7 +52,7 @@ "contract.deleteButton.tooltip": "Delete", "contract.DeleteContract.mutationLabel": "Delete Contract {label}", "contract.deleteContract.confirm.title": "Are you sure you want to delete {label}?", - "contract.deleteContract.confirm.message": "Deleting data does not mean erasing it from OpenIMIS database. The data will only be deactivated from the viewed list.", + "contract.deleteContract.confirm.message": "Deleting data does not mean erasing it from openIMIS database. The data will only be deactivated from the viewed list.", "contract.contractDetails.label": "Contract Details", "contract.contractDetails.searcher.title": "{contractDetailsTotalCount} Contract Details Found", "contract.contractDetails.tabDisabledError": "To start creating new Contract Details, you need to fill in General Information fields and click Save.", @@ -104,5 +104,6 @@ "contract.contractContributionDetails.searcher.title": "{contractContributionDetailsTotalCount} Contract Contribution Details Found", "contract.insureePolicy.label": "Persons Covered", "contract.insureePolicy.searcher.title": "{insureePoliciesTotalCount} Persons Covered Found", - "contract.contract": "Contract" + "contract.contract": "Contract", + "contract.codeTaken": "Contract code already exists" }