This repository has been archived by the owner on Nov 14, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(simplification): fusion des scl en adc (#1443)
- Loading branch information
Showing
20 changed files
with
128 additions
and
110 deletions.
There are no files selected for viewing
2 changes: 1 addition & 1 deletion
2
packages/api/src/business/rules-demarches/axm/2000-01-01-pro.cas.json
Large diffs are not rendered by default.
Oops, something went wrong.
2 changes: 1 addition & 1 deletion
2
packages/api/src/business/rules-demarches/axm/2020-09-30-oct.cas.json
Large diffs are not rendered by default.
Oops, something went wrong.
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
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
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
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
2 changes: 1 addition & 1 deletion
2
packages/api/src/business/rules-demarches/prm/2019-10-31-oct.cas.json
Large diffs are not rendered by default.
Oops, something went wrong.
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
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
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
79 changes: 79 additions & 0 deletions
79
packages/api/src/knex/migrations/20240828134125_etapes-scl-deviennent-adc.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,79 @@ | ||
/* eslint-disable no-restricted-syntax */ | ||
/* eslint-disable sql/no-unsafe-query */ | ||
|
||
import { DemarcheId } from 'camino-common/src/demarche' | ||
import { EtapeDocumentId, EtapeId } from 'camino-common/src/etape' | ||
import { DocumentTypeId, DocumentsTypes } from 'camino-common/src/static/documentsTypes' | ||
import { Knex } from 'knex' | ||
import { LargeObjectId } from '../../database/largeobjects' | ||
import { isNotNullNorUndefinedNorEmpty, isNullOrUndefined } from 'camino-common/src/typescript-tools' | ||
import { CaminoDate } from 'camino-common/src/date' | ||
|
||
export const up = async (knex: Knex): Promise<void> => { | ||
const etapes: { rows: { id: EtapeId; titre_demarche_id: DemarcheId; archive: boolean; date: CaminoDate }[] } = await knex.raw( | ||
`SELECT id, titre_demarche_id, archive, date FROM titres_etapes WHERE type_id = 'scl'` | ||
) | ||
|
||
type EtapeDocumentRow = { | ||
id: EtapeDocumentId | ||
etape_document_type_id: DocumentTypeId | ||
etape_id: EtapeId | ||
largeobject_id: LargeObjectId | ||
} | ||
const etapesDocuments: { rows: EtapeDocumentRow[] } = await knex.raw( | ||
`SELECT id, etape_document_type_id, etape_id, largeobject_id | ||
FROM etapes_documents | ||
WHERE etape_id = ANY(?)`, | ||
[etapes.rows.map(({ id }) => id)] | ||
) | ||
const demarchesAvecAdc: { rows: { id: EtapeId; titre_demarche_id: DemarcheId }[] } = await knex.raw( | ||
`SELECT titre_demarche_id, id | ||
FROM titres_etapes | ||
WHERE type_id = 'adc' AND titre_demarche_id = ANY(?)`, | ||
[etapes.rows.map(({ titre_demarche_id }) => titre_demarche_id)] | ||
) | ||
|
||
const indexDesDemarchesAvecAdc = demarchesAvecAdc.rows.reduce<Record<DemarcheId, EtapeId>>((acc, row) => { | ||
acc[row.titre_demarche_id] = row.id | ||
|
||
return acc | ||
}, {}) | ||
const indexDesDocuments = etapesDocuments.rows.reduce<Record<EtapeId, EtapeDocumentRow[]>>((acc, row) => { | ||
if (isNullOrUndefined(acc[row.etape_id])) { | ||
acc[row.etape_id] = [] | ||
} | ||
|
||
acc[row.etape_id].push(row) | ||
|
||
return acc | ||
}, {}) | ||
|
||
const etapesASupprimer: EtapeId[] = [] | ||
for (let i = 0; i < etapes.rows.length; i += 1) { | ||
const etape = etapes.rows[i] | ||
|
||
let etapeIdDesAvis = etape.id | ||
if (etape.archive || isNullOrUndefined(indexDesDemarchesAvecAdc[etape.titre_demarche_id])) { | ||
await knex.raw(`UPDATE titres_etapes SET type_id = 'adc' WHERE id = ?`, [etape.id]) | ||
} else { | ||
etapeIdDesAvis = indexDesDemarchesAvecAdc[etape.titre_demarche_id] | ||
etapesASupprimer.push(etape.id) | ||
} | ||
|
||
if (isNotNullNorUndefinedNorEmpty(indexDesDocuments[etape.id])) { | ||
await Promise.all( | ||
indexDesDocuments[etape.id].map(document => | ||
knex.raw( | ||
`INSERT INTO etape_avis (id, avis_type_id, avis_statut_id, avis_visibility_id, etape_id, description, date, largeobject_id) VALUES (?, 'avisDUneCollectivite', 'Non renseigné', 'Administrations', ?, ?, ?, ?)`, | ||
[document.id, etapeIdDesAvis, DocumentsTypes[document.etape_document_type_id].nom, etape.date, document.largeobject_id] | ||
) | ||
) | ||
) | ||
} | ||
} | ||
|
||
await knex.raw('DELETE FROM etapes_documents WHERE id = ANY(?)', [etapesDocuments.rows.map(({ id }) => id)]) | ||
await knex.raw('DELETE FROM titres_etapes WHERE id = ANY(?)', [etapesASupprimer]) | ||
} | ||
|
||
export const down = (): void => {} |
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
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
Oops, something went wrong.