Skip to content

Commit

Permalink
changed migration (#3996)
Browse files Browse the repository at this point in the history
  • Loading branch information
thetif authored Apr 21, 2022
1 parent e2df045 commit c35c52e
Showing 1 changed file with 49 additions and 49 deletions.
98 changes: 49 additions & 49 deletions api/mongo-migrations/1647374289696-apd-schema.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,12 @@ const logger = require('../logger')('mongoose-migrate/migrate-apd-schema');
/**
* Update the APD schema to more closely match the front end nav/page sections
*/
async function up () {
async function up() {
require('../models/apd'); // eslint-disable-line global-require

// Grab all APDs
const apds = await this('APD').find({}).lean();

const convertContractorResources = contractorResources =>
contractorResources.map(contractorResource => {
return {
Expand All @@ -19,58 +19,58 @@ async function up () {
useHourly: contractorResource.hourly.useHourly
};
});

// Create new object by sections/new schema
const updatedApds = apds.map(apd => {
if(apd.stateProfile) {
return {
id: apd._id,
createdAt: apd.createdAt,
updatedAt: apd.updatedAt,
years: apd.years,
stateId: apd.stateId,
status: apd.status,
name: apd.name,
apdOverview: {
programOverview: apd.programOverview,
narrativeHIT: apd.narrativeHIT,
narrativeHIE: apd.narrativeHIE,
narrativeMMIS: apd.narrativeMMIS
},
keyStatePersonnel: {
medicaidDirector: apd.stateProfile.medicaidDirector,
medicaidOffice: apd.stateProfile.medicaidOffice,
keyPersonnel: apd.keyPersonnel
},
previousActivities: {
previousActivitySummary: apd.previousActivitySummary,
actualExpenditures: apd.previousActivityExpenses
},
activities: apd.activities.map(activity => ({
...activity,
contractorResources: convertContractorResources(activity.contractorResources)
})),
proposedBudget: {
incentivePayments: apd.incentivePayments
},
assurancesAndCompliances: apd.federalCitations
}
}
});
const updatedApds = apds
.filter(apd => apd.stateProfile)
.map(apd => ({
id: apd._id,
createdAt: apd.createdAt,
updatedAt: apd.updatedAt,
years: apd.years,
stateId: apd.stateId,
status: apd.status,
name: apd.name,
apdOverview: {
programOverview: apd.programOverview,
narrativeHIT: apd.narrativeHIT,
narrativeHIE: apd.narrativeHIE,
narrativeMMIS: apd.narrativeMMIS
},
keyStatePersonnel: {
medicaidDirector: apd.stateProfile.medicaidDirector,
medicaidOffice: apd.stateProfile.medicaidOffice,
keyPersonnel: apd.keyPersonnel
},
previousActivities: {
previousActivitySummary: apd.previousActivitySummary,
actualExpenditures: apd.previousActivityExpenses
},
activities: apd.activities.map(activity => ({
...activity,
contractorResources: convertContractorResources(
activity.contractorResources
)
})),
proposedBudget: {
incentivePayments: apd.incentivePayments
},
assurancesAndCompliances: apd.federalCitations
}));

// Update them into the database
await Promise.all(updatedApds.map(async (apd) => {
await this('APD').replaceOne( { _id: apd.id }, { ...apd });
})).catch(err => {
logger.error(err)
await Promise.all(
updatedApds.map(async apd => {
await this('APD').replaceOne({ _id: apd.id }, { ...apd });
})
).catch(err => {
logger.error(err);
});
}

/**
* Make any changes that UNDO the up function side effects here (if possible)
*/
async function down () {

}
async function down() {}

module.exports = { up, down };
module.exports = { up, down };

0 comments on commit c35c52e

Please sign in to comment.