Skip to content

Commit

Permalink
chore: clean migrations
Browse files Browse the repository at this point in the history
  • Loading branch information
Arnaud AMBROSELLI committed Oct 2, 2023
1 parent 7d970cc commit 2cd2614
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 655 deletions.
303 changes: 12 additions & 291 deletions api/src/controllers/migration.js
Original file line number Diff line number Diff line change
Expand Up @@ -38,311 +38,32 @@ router.put(

try {
await sequelize.transaction(async (tx) => {
if (req.params.migrationName === "reports-from-real-date-to-date-id") {
/*
// Example of migration:
if (req.params.migrationName === "migration-name") {
try {
z.array(z.string().regex(looseUuidRegex)).parse(req.body.thingsIdsToDestroy);
z.array(
z.object({
_id: z.string().regex(looseUuidRegex),
encrypted: z.string(),
encryptedEntityKey: z.string(),
})
).parse(req.body.reportsToMigrate);
).parse(req.body.thingsToUpdate);
} catch (e) {
const error = new Error(`Invalid request in reports-from-real-date-to-date-id migration: ${e}`);
error.status = 400;
throw error;
}
for (const { _id, encrypted, encryptedEntityKey } of req.body.reportsToMigrate) {
const report = await Report.findOne({ where: { _id, organisation: req.user.organisation }, transaction: tx });
if (report) {
report.set({ encrypted, encryptedEntityKey });
await report.save();
}
}
}

if (req.params.migrationName === "clean-reports-with-no-team-nor-date") {
try {
z.array(z.string().regex(looseUuidRegex)).parse(req.body.reportIdsToDelete);
} catch (e) {
const error = new Error(`Invalid request in reports-from-real-date-to-date-id migration: ${e}`);
error.status = 400;
throw error;
}
for (const _id of req.body.reportIdsToDelete) {
await Report.destroy({ where: { _id, organisation: req.user.organisation }, transaction: tx });
}
}

if (req.params.migrationName === "clean-duplicated-reports-4") {
try {
z.object({
reportIdsToDelete: z.array(z.string().regex(looseUuidRegex)),
consolidatedReports: z.array(
z.object({
_id: z.string().regex(looseUuidRegex),
encrypted: z.string(),
encryptedEntityKey: z.string(),
})
),
}).parse(req.body);
} catch (e) {
const error = new Error(`Invalid request in clean-duplicated-reports-4: ${e}`);
error.status = 400;
throw error;
}
for (const { _id, encrypted, encryptedEntityKey } of req.body.consolidatedReports) {
const report = await Report.findOne({ where: { _id, organisation: req.user.organisation }, transaction: tx });
if (report) {
report.set({ encrypted, encryptedEntityKey });
await report.save();
}
}
for (const _id of req.body.reportIdsToDelete) {
await Report.destroy({ where: { _id, organisation: req.user.organisation }, transaction: tx });
}
}
if (req.params.migrationName === "update-outOfActiveListReason-and-healthInsurances-to-multi-choice") {
try {
z.array(
z.object({
_id: z.string().regex(looseUuidRegex),
encrypted: z.string(),
encryptedEntityKey: z.string(),
})
).parse(req.body.personsToUpdate);
} catch (e) {
const error = new Error(`Invalid request in reports-from-real-date-to-date-id migration: ${e}`);
error.status = 400;
throw error;
}
for (const { _id, encrypted, encryptedEntityKey } of req.body.personsToUpdate) {
const person = await Person.findOne({ where: { _id, organisation: req.user.organisation }, transaction: tx });
if (person) {
person.set({ encrypted, encryptedEntityKey });
await person.save();
}
}

if (Array.isArray(organisation?.fieldsPersonsCustomizableOptions)) {
organisation.set({
fieldsPersonsCustomizableOptions: organisation?.fieldsPersonsCustomizableOptions.map((field) => {
if (field.name !== "outOfActiveListReason") return field;
return {
name: "outOfActiveListReasons",
type: "multi-choice",
label: "Motif(s) de sortie de file active",
options: field.options,
showInStats: true,
enabled: true,
};
}),
});
}
await organisation.save({ transaction: tx });
}
if (req.params.migrationName === "action-with-multiple-team") {
try {
z.array(
z.object({
_id: z.string().regex(looseUuidRegex),
encrypted: z.string(),
encryptedEntityKey: z.string(),
})
).parse(req.body.actionsToUpdate);
} catch (e) {
const error = new Error(`Invalid request in action-with-multiple-team migration: ${e}`);
error.status = 400;
throw error;
}
for (const { _id, encrypted, encryptedEntityKey } of req.body.actionsToUpdate) {
const action = await Action.findOne({ where: { _id, organisation: req.user.organisation }, transaction: tx });
if (action) {
action.set({ encrypted, encryptedEntityKey });
await action.save();
}
}
}

if (req.params.migrationName === "services-in-services-table") {
try {
z.array(
z.object({
_id: z.string().regex(looseUuidRegex),
encrypted: z.string(),
encryptedEntityKey: z.string(),
})
).parse(req.body.reportsToUpdate);
z.array(
z.object({
team: z.string().regex(looseUuidRegex),
date: z.string().regex(dateRegex),
service: z.string(),
count: z.number(),
})
).parse(req.body.servicesToSaveInDB);
} catch (e) {
const error = new Error(`Invalid request in services-in-services-table migration: ${e}`);
const error = new Error(`Invalid request in migration-name: ${e}`);
error.status = 400;
throw error;
}

// Get all teams (to remove services with a team that doesn't exist anymore)
const teams = await Team.findAll({ where: { organisation: req.user.organisation }, transaction: tx });

// Filter invalid services
const servicesToSaveInDB = [...req.body.servicesToSaveInDB].filter(
(e) => e.count > 0 && Boolean(e.service) && Boolean(e.date) && Boolean(e.team) && teams.find((t) => t._id === e.team)
);

// Update services that already exists (when there is both a service and a report for the same date)
const servicesInDB = await Service.findAll({
where: { organisation: req.user.organisation },
transaction: tx,
});
for (const serviceInDB of servicesInDB) {
const index = servicesToSaveInDB.findIndex(
(service) => service.service === serviceInDB.service && service.date === serviceInDB.date && service.team === serviceInDB.team
);
if (index !== -1) {
const service = servicesToSaveInDB[index];
serviceInDB.set({ count: service.count + serviceInDB.count });
await serviceInDB.save();
servicesToSaveInDB.splice(index, 1);
}
}

// Create services entries.
await Service.bulkCreate(
servicesToSaveInDB.map((service) => ({ ...service, organisation: req.user.organisation })),
{ transaction: tx }
);
for (const { _id, encrypted, encryptedEntityKey } of req.body.reportsToUpdate) {
const report = await Report.findOne({ where: { _id, organisation: req.user.organisation }, transaction: tx });
if (report) {
report.set({ encrypted, encryptedEntityKey });
await report.save();
}
}
}

if (req.params.migrationName === "clean-reports-with-services") {
try {
z.array(
z.object({
_id: z.string().regex(looseUuidRegex),
encrypted: z.string(),
encryptedEntityKey: z.string(),
})
).parse(req.body.reportsToUpdate);
} catch (e) {
const error = new Error(`Invalid request in clean-reports-with-services migration: ${e}`);
error.status = 400;
throw error;
}

for (const { _id, encrypted, encryptedEntityKey } of req.body.reportsToUpdate) {
const report = await Report.findOne({ where: { _id, organisation: req.user.organisation }, transaction: tx });
if (report) {
report.set({ encrypted, encryptedEntityKey });
await report.save();
}
}
}

if (req.params.migrationName === "comments-reset-person-id") {
try {
z.array(
z.object({
_id: z.string().regex(looseUuidRegex),
encrypted: z.string(),
encryptedEntityKey: z.string(),
})
).parse(req.body.commentsToUpdate);
} catch (e) {
const error = new Error(`Invalid request in comments-reset-person-id migration: ${e}`);
error.status = 400;
throw error;
}
for (const { _id, encrypted, encryptedEntityKey } of req.body.commentsToUpdate) {
const comment = await Comment.findOne({ where: { _id, organisation: req.user.organisation }, transaction: tx });
if (comment) {
comment.set({ encrypted, encryptedEntityKey });
await comment.save();
}
}
}
if (req.params.migrationName === "remove-medical-docs-from-persons") {
try {
z.array(
z.object({
_id: z.string().regex(looseUuidRegex),
encrypted: z.string(),
encryptedEntityKey: z.string(),
})
).parse(req.body.personsToUpdate);
} catch (e) {
const error = new Error(`Invalid request in remove-medical-docs-from-persons: ${e}`);
error.status = 400;
throw error;
}
for (const { _id, encrypted, encryptedEntityKey } of req.body.personsToUpdate) {
const person = await Person.findOne({ where: { _id, organisation: req.user.organisation }, transaction: tx });
if (person) {
person.set({ encrypted, encryptedEntityKey });
await person.save();
}
}
}

if (req.params.migrationName === "retrieve-docs-from-persons-backup") {
try {
z.array(
z.object({
_id: z.string().regex(looseUuidRegex),
encrypted: z.string(),
encryptedEntityKey: z.string(),
})
).parse(req.body.personsToUpdate);
} catch (e) {
const error = new Error(`Invalid request in retrieve-docs-from-persons-backup: ${e}`);
error.status = 400;
throw error;
}
for (const { _id, encrypted, encryptedEntityKey } of req.body.personsToUpdate) {
const person = await Person.findOne({ where: { _id, organisation: req.user.organisation }, transaction: tx });
if (person) {
person.set({ encrypted, encryptedEntityKey });
await person.save();
}
}
}

if (req.params.migrationName === "fix-family-relation-user-deleted") {
try {
z.object({
groupIdsToDestroy: z.array(z.string().regex(looseUuidRegex)),
groupsToUpdate: z.array(
z.object({
_id: z.string().regex(looseUuidRegex),
encrypted: z.string(),
encryptedEntityKey: z.string(),
})
),
}).parse(req.body);
} catch (e) {
const error = new Error(`Invalid request in fix-family-relation-user-deleted migration: ${e}`);
error.status = 400;
throw error;
for (const _id of req.body.thingsIdsToDestroy) {
await Thing.destroy({ where: { _id, organisation: req.user.organisation }, transaction: tx });
}
for (const { _id, encrypted, encryptedEntityKey } of req.body.groupsToUpdate) {
const group = await Group.findOne({ where: { _id, organisation: req.user.organisation }, transaction: tx });
if (group) {
group.set({ encrypted, encryptedEntityKey });
await group.save();
}
for (const { _id, encrypted, encryptedEntityKey } of req.body.thingsToUpdate) {
await Thing.update({ encrypted, encryptedEntityKey }, { where: { _id }, transaction: tx, paranoid: false });
}
}
// End of example of migration.
*/

if (req.params.migrationName === "integrate-comments-in-actions-history") {
try {
Expand Down
Loading

0 comments on commit 2cd2614

Please sign in to comment.