From 3eb02365f02fdba2a52781a04e00d4eafa071c61 Mon Sep 17 00:00:00 2001 From: Arnaud AMBROSELLI Date: Mon, 25 Apr 2022 15:25:06 +0200 Subject: [PATCH] fix: clean --- api/src/controllers/action.js | 9 +++++++-- api/src/controllers/comment.js | 6 +++++- api/src/controllers/organisation.js | 5 ++++- api/src/controllers/passage.js | 6 +++++- api/src/controllers/person.js | 8 +++++--- api/src/controllers/place.js | 6 +++++- api/src/controllers/relPersonPlace.js | 6 +++++- api/src/controllers/report.js | 6 +++++- api/src/controllers/territory.js | 6 +++++- api/src/controllers/territoryObservation.js | 6 +++++- app/src/components/Loader.js | 2 +- app/src/services/dataManagement.js | 8 +++++++- dashboard/src/services/dataManagement.js | 8 +++++++- 13 files changed, 66 insertions(+), 16 deletions(-) diff --git a/api/src/controllers/action.js b/api/src/controllers/action.js index 473ba729f5..1f0c5a5f7f 100644 --- a/api/src/controllers/action.js +++ b/api/src/controllers/action.js @@ -70,12 +70,14 @@ router.get( z.optional(z.string().regex(positiveIntegerRegex)).parse(req.query.limit); z.optional(z.string().regex(positiveIntegerRegex)).parse(req.query.page); z.optional(z.string().regex(positiveIntegerRegex)).parse(req.query.lastRefresh); + z.optional(z.string().regex(positiveIntegerRegex)).parse(req.query.updatedAfter); + z.optional(z.string().regex(positiveIntegerRegex)).parse(req.query.deletedAfter); } catch (e) { const error = new Error(`Invalid request in action get: ${e}`); error.status = 400; return next(error); } - const { limit, page, lastRefresh } = req.query; + const { limit, page, lastRefresh, updatedAfter, deletedAfter } = req.query; const query = { where: { organisation: req.user.organisation }, @@ -93,7 +95,10 @@ router.get( query.where[Op.or] = [{ updatedAt: { [Op.gte]: new Date(Number(lastRefresh)) } }, { deletedAt: { [Op.gte]: new Date(Number(lastRefresh)) } }]; query.paranoid = false; } - + if (updatedAfter && deletedAfter) { + query.where[Op.or] = [{ updatedAt: { [Op.gte]: new Date(Number(updatedAfter)) } }, { deletedAt: { [Op.gte]: new Date(Number(deletedAfter)) } }]; + query.paranoid = false; + } const sortDoneOrCancel = (a, b) => { if (!a.dueAt) return -1; if (!b.dueAt) return 1; diff --git a/api/src/controllers/comment.js b/api/src/controllers/comment.js index 30d1b0b94d..fb215ad3e2 100644 --- a/api/src/controllers/comment.js +++ b/api/src/controllers/comment.js @@ -62,7 +62,7 @@ router.get( error.status = 400; return next(error); } - const { limit, page, lastRefresh } = req.query; + const { limit, page, lastRefresh, updatedAfter, deletedAfter } = req.query; const query = { where: { organisation: req.user.organisation }, @@ -76,6 +76,10 @@ router.get( query.where[Op.or] = [{ updatedAt: { [Op.gte]: new Date(Number(lastRefresh)) } }, { deletedAt: { [Op.gte]: new Date(Number(lastRefresh)) } }]; query.paranoid = false; } + if (updatedAfter && deletedAfter) { + query.where[Op.or] = [{ updatedAt: { [Op.gte]: new Date(Number(updatedAfter)) } }, { deletedAt: { [Op.gte]: new Date(Number(deletedAfter)) } }]; + query.paranoid = false; + } const data = await Comment.findAll({ ...query, diff --git a/api/src/controllers/organisation.js b/api/src/controllers/organisation.js index dba09bc42e..d0589efa5c 100644 --- a/api/src/controllers/organisation.js +++ b/api/src/controllers/organisation.js @@ -43,12 +43,15 @@ router.get( query.where[Op.or] = [{ updatedAt: { [Op.gte]: new Date(Number(lastRefresh)) } }, { deletedAt: { [Op.gte]: new Date(Number(lastRefresh)) } }]; query.paranoid = false; } + if (updatedAfter && deletedAfter) { + query.where[Op.or] = [{ updatedAt: { [Op.gte]: new Date(Number(updatedAfter)) } }, { deletedAt: { [Op.gte]: new Date(Number(deletedAfter)) } }]; + query.paranoid = false; + } const places = await Place.count(query); const relsPersonPlace = await RelPersonPlace.count(query); const actions = await Action.count(query); const persons = await Person.count(query); - console.log(query, new Date(Number(lastRefresh)), persons); const comments = await Comment.count(query); const passages = await Passage.count(query); const reports = await Report.count(query); diff --git a/api/src/controllers/passage.js b/api/src/controllers/passage.js index 685044ea2e..68afae022d 100644 --- a/api/src/controllers/passage.js +++ b/api/src/controllers/passage.js @@ -62,7 +62,7 @@ router.get( error.status = 400; return next(error); } - const { limit, page, lastRefresh } = req.query; + const { limit, page, lastRefresh, updatedAfter, deletedAfter } = req.query; const query = { where: { organisation: req.user.organisation }, @@ -76,6 +76,10 @@ router.get( query.where[Op.or] = [{ updatedAt: { [Op.gte]: new Date(Number(lastRefresh)) } }, { deletedAt: { [Op.gte]: new Date(Number(lastRefresh)) } }]; query.paranoid = false; } + if (updatedAfter && deletedAfter) { + query.where[Op.or] = [{ updatedAt: { [Op.gte]: new Date(Number(updatedAfter)) } }, { deletedAt: { [Op.gte]: new Date(Number(deletedAfter)) } }]; + query.paranoid = false; + } const data = await Passage.findAll({ ...query, diff --git a/api/src/controllers/person.js b/api/src/controllers/person.js index b2cc0f5b16..1c4aaaa532 100644 --- a/api/src/controllers/person.js +++ b/api/src/controllers/person.js @@ -211,7 +211,7 @@ router.get( error.status = 400; return next(error); } - const { limit, page, lastRefresh } = req.query; + const { limit, page, lastRefresh, updatedAfter, deletedAfter } = req.query; const query = { where: { organisation: req.user.organisation }, @@ -224,14 +224,16 @@ router.get( query.where[Op.or] = [{ updatedAt: { [Op.gte]: new Date(Number(lastRefresh)) } }, { deletedAt: { [Op.gte]: new Date(Number(lastRefresh)) } }]; query.paranoid = false; } + if (updatedAfter && deletedAfter) { + query.where[Op.or] = [{ updatedAt: { [Op.gte]: new Date(Number(updatedAfter)) } }, { deletedAt: { [Op.gte]: new Date(Number(deletedAfter)) } }]; + query.paranoid = false; + } const data = await Person.findAll({ ...query, attributes: ["_id", "encrypted", "encryptedEntityKey", "organisation", "createdAt", "updatedAt", "deletedAt"], }); - console.log({ data }); - return res.status(200).send({ ok: true, hasMore: data.length === Number(limit), diff --git a/api/src/controllers/place.js b/api/src/controllers/place.js index d69f5b4be8..da2f74ae75 100644 --- a/api/src/controllers/place.js +++ b/api/src/controllers/place.js @@ -61,7 +61,7 @@ router.get( error.status = 400; return next(error); } - const { limit, page, lastRefresh } = req.query; + const { limit, page, lastRefresh, updatedAfter, deletedAfter } = req.query; const query = { where: { organisation: req.user.organisation }, @@ -75,6 +75,10 @@ router.get( query.where[Op.or] = [{ updatedAt: { [Op.gte]: new Date(Number(lastRefresh)) } }, { deletedAt: { [Op.gte]: new Date(Number(lastRefresh)) } }]; query.paranoid = false; } + if (updatedAfter && deletedAfter) { + query.where[Op.or] = [{ updatedAt: { [Op.gte]: new Date(Number(updatedAfter)) } }, { deletedAt: { [Op.gte]: new Date(Number(deletedAfter)) } }]; + query.paranoid = false; + } const data = await Place.findAll({ ...query, diff --git a/api/src/controllers/relPersonPlace.js b/api/src/controllers/relPersonPlace.js index 37482501b2..40ca90f30d 100644 --- a/api/src/controllers/relPersonPlace.js +++ b/api/src/controllers/relPersonPlace.js @@ -62,7 +62,7 @@ router.get( error.status = 400; return next(error); } - const { limit, page, lastRefresh } = req.query; + const { limit, page, lastRefresh, updatedAfter, deletedAfter } = req.query; const query = { where: { organisation: req.user.organisation }, @@ -76,6 +76,10 @@ router.get( query.where[Op.or] = [{ updatedAt: { [Op.gte]: new Date(Number(lastRefresh)) } }, { deletedAt: { [Op.gte]: new Date(Number(lastRefresh)) } }]; query.paranoid = false; } + if (updatedAfter && deletedAfter) { + query.where[Op.or] = [{ updatedAt: { [Op.gte]: new Date(Number(updatedAfter)) } }, { deletedAt: { [Op.gte]: new Date(Number(deletedAfter)) } }]; + query.paranoid = false; + } const data = await RelPersonPlace.findAll({ ...query, diff --git a/api/src/controllers/report.js b/api/src/controllers/report.js index c71b90a4f0..45ad81dec6 100644 --- a/api/src/controllers/report.js +++ b/api/src/controllers/report.js @@ -23,7 +23,7 @@ router.get( error.status = 400; return next(error); } - const { limit, page, lastRefresh } = req.query; + const { limit, page, lastRefresh, updatedAfter, deletedAfter } = req.query; const query = { where: { organisation: req.user.organisation } }; @@ -34,6 +34,10 @@ router.get( query.where[Op.or] = [{ updatedAt: { [Op.gte]: new Date(Number(lastRefresh)) } }, { deletedAt: { [Op.gte]: new Date(Number(lastRefresh)) } }]; query.paranoid = false; } + if (updatedAfter && deletedAfter) { + query.where[Op.or] = [{ updatedAt: { [Op.gte]: new Date(Number(updatedAfter)) } }, { deletedAt: { [Op.gte]: new Date(Number(deletedAfter)) } }]; + query.paranoid = false; + } const data = await Report.findAll({ ...query, diff --git a/api/src/controllers/territory.js b/api/src/controllers/territory.js index 45f49e133d..aad3e353d5 100644 --- a/api/src/controllers/territory.js +++ b/api/src/controllers/territory.js @@ -60,7 +60,7 @@ router.get( error.status = 400; return next(error); } - const { limit, page, lastRefresh } = req.query; + const { limit, page, lastRefresh, updatedAfter, deletedAfter } = req.query; const query = { where: { organisation: req.user.organisation }, @@ -74,6 +74,10 @@ router.get( query.where[Op.or] = [{ updatedAt: { [Op.gte]: new Date(Number(lastRefresh)) } }, { deletedAt: { [Op.gte]: new Date(Number(lastRefresh)) } }]; query.paranoid = false; } + if (updatedAfter && deletedAfter) { + query.where[Op.or] = [{ updatedAt: { [Op.gte]: new Date(Number(updatedAfter)) } }, { deletedAt: { [Op.gte]: new Date(Number(deletedAfter)) } }]; + query.paranoid = false; + } const data = await Territory.findAll({ ...query, diff --git a/api/src/controllers/territoryObservation.js b/api/src/controllers/territoryObservation.js index 715c375f62..9e6c774613 100644 --- a/api/src/controllers/territoryObservation.js +++ b/api/src/controllers/territoryObservation.js @@ -59,7 +59,7 @@ router.get( error.status = 400; return next(error); } - const { limit, page, lastRefresh } = req.query; + const { limit, page, lastRefresh, updatedAfter, deletedAfter } = req.query; const query = { where: { organisation: req.user.organisation }, @@ -73,6 +73,10 @@ router.get( query.where[Op.or] = [{ updatedAt: { [Op.gte]: new Date(Number(lastRefresh)) } }, { deletedAt: { [Op.gte]: new Date(Number(lastRefresh)) } }]; query.paranoid = false; } + if (updatedAfter && deletedAfter) { + query.where[Op.or] = [{ updatedAt: { [Op.gte]: new Date(Number(updatedAfter)) } }, { deletedAt: { [Op.gte]: new Date(Number(deletedAfter)) } }]; + query.paranoid = false; + } const data = await TerritoryObservation.findAll({ ...query, diff --git a/app/src/components/Loader.js b/app/src/components/Loader.js index 570565b1c7..935706e25a 100644 --- a/app/src/components/Loader.js +++ b/app/src/components/Loader.js @@ -55,7 +55,7 @@ const mergeItems = (oldItems, newItems) => { const Loader = () => { const [picture, setPicture] = useState([picture1, picture3, picture2][randomIntFromInterval(0, 2)]); - const [lastRefresh, setLastRefresh] = useStorage('last-refresh', 0); + const [lastRefresh, setLastRefresh] = useStorage('last-refresh--cache-version-2022-04-25', 0); const [loading, setLoading] = useRecoilState(loadingState); const [progress, setProgress] = useRecoilState(progressState); const [fullScreen, setFullScreen] = useRecoilState(loaderFullScreenState); diff --git a/app/src/services/dataManagement.js b/app/src/services/dataManagement.js index e2feaf8b42..17d28f8a8d 100644 --- a/app/src/services/dataManagement.js +++ b/app/src/services/dataManagement.js @@ -34,7 +34,13 @@ export async function getData({ collectionName, data = [], isInitialization = fa data = (await MMKV.getMapAsync(collectionName)) || []; } - const response = await API.get({ path: `/${collectionName}`, batch: 1000, setProgress, query: { lastRefresh }, setBatchData }); + const response = await API.get({ + path: `/${collectionName}`, + batch: 1000, + setProgress, + query: { updatedAfter: lastRefresh, deletedAfter: lastRefresh }, + setBatchData, + }); if (!response.ok) throw { message: `Error getting ${collectionName} data`, response }; // avoid sending data if no new data, to avoid big useless `map` calculations in selectors diff --git a/dashboard/src/services/dataManagement.js b/dashboard/src/services/dataManagement.js index a59e905ae2..c04731cc9d 100644 --- a/dashboard/src/services/dataManagement.js +++ b/dashboard/src/services/dataManagement.js @@ -41,7 +41,13 @@ export async function getData({ setBatchData = null, lastRefresh = 0, }) { - const response = await API.get({ path: `/${collectionName}`, batch: 1000, setProgress, query: { lastRefresh }, setBatchData }); + const response = await API.get({ + path: `/${collectionName}`, + batch: 1000, + setProgress, + query: { updatedAfter: lastRefresh, deletedAfter: lastRefresh }, + setBatchData, + }); if (!response.ok) console.log({ message: `Error getting ${collectionName} data`, response }); if (response.ok && response.decryptedData && response.decryptedData.length) { data = mergeNewUpdatedData(response.decryptedData, data);