From f0b382ce2cfd45a8a4f19ca661b1e97d1f5371e8 Mon Sep 17 00:00:00 2001 From: Hugo SUBTIL <ext.sopra.husubtil@grandlyon.com> Date: Tue, 21 Dec 2021 16:59:33 +0100 Subject: [PATCH] feat: migrate null values to 0 for equipments --- ...095148120-remove-equipments-null-values.ts | 56 +++++++++++++++++++ .../services/aptic-structures.service.ts | 10 ++-- 2 files changed, 61 insertions(+), 5 deletions(-) create mode 100644 src/migrations/scripts/1640095148120-remove-equipments-null-values.ts diff --git a/src/migrations/scripts/1640095148120-remove-equipments-null-values.ts b/src/migrations/scripts/1640095148120-remove-equipments-null-values.ts new file mode 100644 index 000000000..400d666b2 --- /dev/null +++ b/src/migrations/scripts/1640095148120-remove-equipments-null-values.ts @@ -0,0 +1,56 @@ +import { Db } from 'mongodb'; +import { getDb } from '../migrations-utils/db'; + +export const up = async () => { + const db: Db = await getDb(); + + const cursor = db.collection('structures').find({}); + let document; + while ((document = await cursor.next())) { + const newDoc = document; + if (document.nbComputers === null) { + newDoc.nbComputers = 0; + } + if (document.nbPrinters === null) { + newDoc.nbPrinters = 0; + } + if (document.nbScanners === null) { + newDoc.nbScanners = 0; + } + if (document.nbTablets === null) { + newDoc.nbTablets = 0; + } + if (document.nbNumericTerminal === null) { + newDoc.nbNumericTerminal = 0; + } + await db.collection('structures').updateOne({ _id: document._id }, [{ $set: newDoc }]); + } + console.log(`Update done`); +}; + +export const down = async () => { + const db: Db = await getDb(); + const cursor = db.collection('structures').find({}); + + let document; + while ((document = await cursor.next())) { + const newDoc = document; + if (document.nbComputers === 0) { + newDoc.nbComputers = null; + } + if (document.nbPrinters === 0) { + newDoc.nbPrinters = null; + } + if (document.nbScanners === 0) { + newDoc.nbScanners = null; + } + if (document.nbTablets === 0) { + newDoc.nbTablets = null; + } + if (document.nbNumericTerminal === 0) { + newDoc.nbNumericTerminal = null; + } + await db.collection('structures').updateOne({ _id: document._id }, [{ $set: newDoc }]); + } + console.log(`Update done`); +}; diff --git a/src/structures/services/aptic-structures.service.ts b/src/structures/services/aptic-structures.service.ts index 766a7513f..1833e57fb 100644 --- a/src/structures/services/aptic-structures.service.ts +++ b/src/structures/services/aptic-structures.service.ts @@ -80,11 +80,11 @@ export class ApticStructuresService { createdStructure.publics = ['toutPublic']; createdStructure.accountVerified = true; createdStructure.freeWorkShop = false; - createdStructure.nbComputers = null; - createdStructure.nbPrinters = null; - createdStructure.nbScanners = null; - createdStructure.nbTablets = null; - createdStructure.nbNumericTerminal = null; + createdStructure.nbComputers = 0; + createdStructure.nbPrinters = 0; + createdStructure.nbScanners = 0; + createdStructure.nbTablets = 0; + createdStructure.nbNumericTerminal = 0; // Address createdStructure.coord = [structure.address.gpsLng, structure.address.gpsLat]; createdStructure.address = this.formatAddress(structure); -- GitLab