From 09269a95b726ac5b5c663eeb6f0af3d4a0487bbd Mon Sep 17 00:00:00 2001 From: Matthieu Bessat Date: Tue, 4 Aug 2020 22:53:42 +0200 Subject: [PATCH] fix: id cleanup in backend --- src/controllers/DelegateController.ts | 36 ++++++++++++++++++++++----- 1 file changed, 30 insertions(+), 6 deletions(-) diff --git a/src/controllers/DelegateController.ts b/src/controllers/DelegateController.ts index 6a156d1..d2cb545 100644 --- a/src/controllers/DelegateController.ts +++ b/src/controllers/DelegateController.ts @@ -49,10 +49,36 @@ export default class DelegateController { // only update proposedVersion let proposedVersion: any = req.body proposedVersion.tags = tags - + + if (Utils.isUsable(proposedVersion, 'pricing') && Array.isArray(proposedVersion.pricing)) { + proposedVersion.pricing = proposedVersion.pricing.map((i: any) => { + delete i._id + return i + }).filter((i: any) => i.isExample !== true) + } + + if (Utils.isUsable(proposedVersion, 'schedule') && Array.isArray(proposedVersion.schedule)) { + proposedVersion.schedule = proposedVersion.schedule.map((i: any) => { + delete i._id + if (Array.isArray(i.when) && i.when.length > 0) { + i.when = i.when.map((w: any) => { + delete w._id + return w + }) + } + return i + }).filter((i: any) => i.isExample !== true) + } + // remove useless isResponsable if (Utils.isUsable(proposedVersion, 'contacts.peoples') && Array.isArray(proposedVersion.contacts.peoples)) { - proposedVersion.contacts.peoples = proposedVersion.contacts.peoples.filter((p: any) => !p.isResponsable) + proposedVersion.contacts.peoples = proposedVersion.contacts.peoples + .filter((p: any) => !p.isResponsable) + // cleanup id + .map((p: any) => { + delete p._id + return p + }) } // sanitize short description @@ -69,10 +95,6 @@ export default class DelegateController { ) } - // validate contact.address - // validate all fields to not overflow - // validate the size of all the json, all the data recorded - // manage medias // delete media that are not used if (!Array.isArray(proposedVersion.gallery)) { @@ -119,6 +141,8 @@ export default class DelegateController { proposedVersion.pricing = [] } + // validate all fields to not overflow + // validate the size of all the json, all the data recorded // just before updating the whole organization, we want to check the size of the proposedVersion JSON String, just in case it is too large if (JSON.stringify(proposedVersion).length > 10000) { return res.status(413).json({