Skip to content

Commit

Permalink
fix: clean
Browse files Browse the repository at this point in the history
  • Loading branch information
Arnaud AMBROSELLI committed Apr 25, 2022
1 parent 283e7f0 commit 3eb0236
Show file tree
Hide file tree
Showing 13 changed files with 66 additions and 16 deletions.
9 changes: 7 additions & 2 deletions api/src/controllers/action.js
Original file line number Diff line number Diff line change
Expand Up @@ -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 },
Expand All @@ -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;
Expand Down
6 changes: 5 additions & 1 deletion api/src/controllers/comment.js
Original file line number Diff line number Diff line change
Expand Up @@ -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 },
Expand All @@ -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,
Expand Down
5 changes: 4 additions & 1 deletion api/src/controllers/organisation.js
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down
6 changes: 5 additions & 1 deletion api/src/controllers/passage.js
Original file line number Diff line number Diff line change
Expand Up @@ -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 },
Expand All @@ -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,
Expand Down
8 changes: 5 additions & 3 deletions api/src/controllers/person.js
Original file line number Diff line number Diff line change
Expand Up @@ -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 },
Expand All @@ -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),
Expand Down
6 changes: 5 additions & 1 deletion api/src/controllers/place.js
Original file line number Diff line number Diff line change
Expand Up @@ -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 },
Expand All @@ -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,
Expand Down
6 changes: 5 additions & 1 deletion api/src/controllers/relPersonPlace.js
Original file line number Diff line number Diff line change
Expand Up @@ -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 },
Expand All @@ -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,
Expand Down
6 changes: 5 additions & 1 deletion api/src/controllers/report.js
Original file line number Diff line number Diff line change
Expand Up @@ -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 } };

Expand All @@ -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,
Expand Down
6 changes: 5 additions & 1 deletion api/src/controllers/territory.js
Original file line number Diff line number Diff line change
Expand Up @@ -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 },
Expand All @@ -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,
Expand Down
6 changes: 5 additions & 1 deletion api/src/controllers/territoryObservation.js
Original file line number Diff line number Diff line change
Expand Up @@ -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 },
Expand All @@ -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,
Expand Down
2 changes: 1 addition & 1 deletion app/src/components/Loader.js
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down
8 changes: 7 additions & 1 deletion app/src/services/dataManagement.js
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
8 changes: 7 additions & 1 deletion dashboard/src/services/dataManagement.js
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down

0 comments on commit 3eb0236

Please sign in to comment.