forked from ReliefApplications/ems-backend
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge remote-tracking branch 'origin/next-oort' into feat/HIT-261-uni…
…que-check
- Loading branch information
Showing
24 changed files
with
1,303 additions
and
1,288 deletions.
There are no files selected for viewing
57 changes: 57 additions & 0 deletions
57
migrations/1710988222735-move-idshape-from-form-to-resource.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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! | ||
*/ | ||
}; |
Oops, something went wrong.