Skip to content

Commit

Permalink
Merge remote-tracking branch 'origin/next-oort' into feat/HIT-261-uni…
Browse files Browse the repository at this point in the history
…que-check
  • Loading branch information
matheus-relief committed Apr 2, 2024
2 parents 064c16a + aa73871 commit 8fec00d
Show file tree
Hide file tree
Showing 24 changed files with 1,303 additions and 1,288 deletions.
57 changes: 57 additions & 0 deletions migrations/1710988222735-move-idshape-from-form-to-resource.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
import { DEFAULT_INCREMENTAL_ID_SHAPE, Form, Resource } from '@models';
import { startDatabaseForMigration } from '../src/utils/migrations/database.helper';
import { logger } from '@services/logger.service';
import { isEqual } from 'lodash';
import { updateIncrementalIds } from '@utils/form';

type FormWithIdShape = Form & { idShape: Resource['idShape'] };

/**
* Update resource, by populating idShape with customized form idShape
*
* @param form form to use to update resource
*/
const updateFormResource = async (form: FormWithIdShape) => {
await Resource.findByIdAndUpdate(form.resource, {
idShape: form.idShape,
});
form.idShape = null;
await form.save();
logger.info(`[${form.resource.name}]: updated custom idShape.`);
};

/**
* Sample function of up migration
*
* @returns just migrate data.
*/
export const up = async () => {
await startDatabaseForMigration();
// Update dashboard pages
const forms = (await Form.find().select(
'idShape resource'
)) as FormWithIdShape[];

const updatedResources: string[] = [];

for (const form of forms) {
if (form.idShape && !isEqual(form.idShape, DEFAULT_INCREMENTAL_ID_SHAPE)) {
await updateFormResource(form);
if (!updatedResources.includes(form.resource.toString())) {
await updateIncrementalIds(form.resource, form.idShape, true);
updatedResources.push(form.resource.toString());
}
}
}
};

/**
* Sample function of down migration
*
* @returns just migrate data.
*/
export const down = async () => {
/*
Code you downgrade script here!
*/
};
Loading

0 comments on commit 8fec00d

Please sign in to comment.