Skip to content

fix(TS): Rendre optionnelles les propriétés de BatchValue #67

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 14 commits into from
Jun 18, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 7 additions & 6 deletions dbmongo/js/compact/ava_tests.ts
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ const dates = [
ISODate("2016-01-01T00:00:00.000+0000"),
]
const batch: BatchValues = {
[batchKey]: {} as BatchValue, // TODO: rendre optionnelles les props de BatchValues, pour retirer ce cast
[batchKey]: {},
}

const importedData = {
Expand Down Expand Up @@ -73,7 +73,7 @@ const expectedFinalizeResultValue = {
}),
{}
),
} as BatchValue, // TODO: rendre optionnelles les props de BatchValues, pour retirer ce cast
},
},
scope,
index: { algo1: false, algo2: false }, // car il n'y a pas de données justifiant que l'établissement compte 10 employés ou pas
Expand Down Expand Up @@ -106,13 +106,14 @@ test.serial(
global.serie_periode = dates // used by complete_reporder(), which is called by finalize()
const finalizeResult = finalize(siret, expectedReduceResults)
const { reporder } = finalizeResult.batch[batchKey]
t.is(typeof reporder, "object")
// reporder contient une propriété par periode
t.is(Object.keys(reporder).length, dates.length)
Object.keys(reporder).forEach((periodKey) => {
t.is(typeof reporder[periodKey].random_order, "number")
t.is(Object.keys(reporder || {}).length, dates.length)
Object.keys(reporder || {}).forEach((periodKey) => {
t.is(typeof reporder?.[periodKey]?.random_order, "number")
})
// vérification de la structure complète, sans les nombres aléatoires
removeRandomOrder(finalizeResult.batch[batchKey].reporder) // will mutate finalizeResult
removeRandomOrder(reporder || {}) // will mutate finalizeResult
t.deepEqual(finalizeResult, expectedFinalizeResultValue)
}
)
2 changes: 1 addition & 1 deletion dbmongo/js/compact/complete_reporder.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ export function complete_reporder(

Object.keys(reporder).forEach((ro) => {
if (!missing[reporder[ro].periode.getTime()]) {
delete object.batch[batch].reporder[ro]
delete reporder[ro]
} else {
missing[reporder[ro].periode.getTime()] = false
}
Expand Down
27 changes: 15 additions & 12 deletions dbmongo/js/compact/currentState.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,22 +8,25 @@ export function currentState(batches: BatchValue[]): CurrentDataState {
const currentState: CurrentDataState = batches.reduce(
(m: CurrentDataState, batch: BatchValue) => {
//1. On supprime les clés de la mémoire
Object.keys((batch.compact || { delete: [] }).delete).forEach((type) => {
batch.compact.delete[type].forEach((key) => {
m[type].delete(key) // Should never fail or collection is corrupted
})
})
if (batch.compact) {
for (const type of Object.keys(batch.compact.delete)) {
batch.compact.delete[type].forEach((key) => {
m[type].delete(key) // Should never fail or collection is corrupted
})
}
}

//2. On ajoute les nouvelles clés
Object.keys(batch)
.filter((type) => type !== "compact")
.forEach((type) => {
m[type] = m[type] || new Set()
const neyKeyTypes = Object.keys(batch).filter(
(type) => type !== "compact"
) as (keyof BatchValue)[]
neyKeyTypes.forEach((type) => {
m[type] = m[type] || new Set()

Object.keys(batch[type as keyof BatchValue]).forEach((key) => {
m[type].add(key)
})
Object.keys(batch[type] || {}).forEach((key) => {
m[type].add(key)
})
})
return m
},
{}
Expand Down
55 changes: 21 additions & 34 deletions dbmongo/js/compact/reduce.ts
Original file line number Diff line number Diff line change
Expand Up @@ -95,18 +95,14 @@ export function reduce(
all_interesting_types.forEach((type) => {
// Le type compact gère les clés supprimées
if (type === "compact") {
if (reduced_value.batch[batch].compact.delete) {
Object.keys(reduced_value.batch[batch].compact.delete).forEach(
(delete_type) => {
reduced_value.batch[batch].compact.delete[delete_type].forEach(
(hash) => {
hashToDelete[delete_type] =
hashToDelete[delete_type] || new Set()
hashToDelete[delete_type].add(hash)
}
)
}
)
const compactDelete = reduced_value.batch[batch].compact?.delete
if (compactDelete) {
Object.keys(compactDelete).forEach((delete_type) => {
compactDelete[delete_type].forEach((hash) => {
hashToDelete[delete_type] = hashToDelete[delete_type] || new Set()
hashToDelete[delete_type].add(hash)
})
})
}
} else {
Object.keys(
Expand Down Expand Up @@ -187,13 +183,10 @@ export function reduce(
// -------------------------------
stock_types.forEach((type) => {
if (hashToDelete[type]) {
reduced_value.batch[batch].compact =
reduced_value.batch[batch].compact || {}
reduced_value.batch[batch].compact.delete =
reduced_value.batch[batch].compact.delete || {}
reduced_value.batch[batch].compact.delete[type] = [
...hashToDelete[type],
]
const reducedBatch = reduced_value.batch[batch]
reducedBatch.compact = reducedBatch.compact || { delete: {} }
reducedBatch.compact.delete = reducedBatch.compact.delete || {}
reducedBatch.compact.delete[type] = [...hashToDelete[type]]
}
})

Expand All @@ -209,7 +202,7 @@ export function reduce(
reduced_value.batch[batch][type] = [...hashToAdd[type]].reduce(
(typedBatchValues, hash) => ({
...typedBatchValues,
[hash]: reduced_value.batch[batch][type][hash],
[hash]: reduced_value.batch[batch][type]?.[hash],
}),
{}
)
Expand All @@ -223,25 +216,19 @@ export function reduce(
//types vides
Object.keys(reduced_value.batch[batch]).forEach((strType) => {
const type = strType as keyof BatchValue
if (Object.keys(reduced_value.batch[batch][type]).length === 0) {
if (Object.keys(reduced_value.batch[batch][type] || {}).length === 0) {
delete reduced_value.batch[batch][type]
}
})
//hash à supprimer vides (compact.delete)
if (
reduced_value.batch[batch].compact &&
reduced_value.batch[batch].compact.delete
) {
Object.keys(reduced_value.batch[batch].compact.delete).forEach(
(type) => {
if (reduced_value.batch[batch].compact.delete[type].length === 0) {
delete reduced_value.batch[batch].compact.delete[type]
}
const compactDelete = reduced_value.batch[batch].compact?.delete
if (compactDelete) {
Object.keys(compactDelete).forEach((type) => {
if (compactDelete[type].length === 0) {
delete compactDelete[type]
}
)
if (
Object.keys(reduced_value.batch[batch].compact.delete).length === 0
) {
})
if (Object.keys(compactDelete).length === 0) {
delete reduced_value.batch[batch].compact
}
}
Expand Down
10 changes: 5 additions & 5 deletions dbmongo/js/globals.ts
Original file line number Diff line number Diff line change
Expand Up @@ -32,11 +32,11 @@ type CompanyDataValuesWithFlags = CompanyDataValues & {
}

type BatchValue = {
reporder: { [periode: string]: RepOrder }
compact: { delete: { [dataType: string]: DataHash[] } }
effectif: { [dataHash: string]: Effectif }
apconso: { [key: string]: any } // TODO: définir type plus précisément
apdemande: { [key: string]: any } // TODO: définir type plus précisément
reporder?: { [periode: string]: RepOrder }
compact?: { delete: { [dataType: string]: DataHash[] } }
effectif?: { [dataHash: string]: Effectif }
apconso?: { [key: string]: any } // TODO: définir type plus précisément
apdemande?: { [key: string]: any } // TODO: définir type plus précisément
}

type DataHash = string
Expand Down
2 changes: 1 addition & 1 deletion dbmongo/js/public/ava_tests.ts
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@ global.serie_periode = dates // used by effectifs(), which is called by map()

const rawData = {
batch: {
[batchKey]: {} as BatchValue, // TODO: rendre optionnelles les props de BatchValues, pour retirer ce cast
[batchKey]: {},
},
scope,
index: { algo1: false, algo2: false }, // car il n'y a pas de données justifiant que l'établissement compte 10 employés ou pas
Expand Down
2 changes: 1 addition & 1 deletion dbmongo/js/reduce.algo2/ava_tests.ts
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,7 @@ global.naf = {} // used by map()
// même valeur en entrée que pour ../compact/ava_tests.ts
const rawData = {
batch: {
[batchKey]: {} as BatchValue, // TODO: rendre optionnelles les props de BatchValues, pour retirer ce cast
[batchKey]: {},
},
scope,
index: { algo1: false, algo2: false },
Expand Down
63 changes: 33 additions & 30 deletions dbmongo/lib/engine/jsFunctions.go
Original file line number Diff line number Diff line change
Expand Up @@ -202,7 +202,7 @@ function complete_reporder(siret, object) {
const reporder = object.batch[batch].reporder || {};
Object.keys(reporder).forEach((ro) => {
if (!missing[reporder[ro].periode.getTime()]) {
delete object.batch[batch].reporder[ro];
delete reporder[ro];
}
else {
missing[reporder[ro].periode.getTime()] = false;
Expand Down Expand Up @@ -230,17 +230,18 @@ function currentState(batches) {
"use strict";
const currentState = batches.reduce((m, batch) => {
//1. On supprime les clés de la mémoire
Object.keys((batch.compact || { delete: [] }).delete).forEach((type) => {
batch.compact.delete[type].forEach((key) => {
m[type].delete(key); // Should never fail or collection is corrupted
});
});
if (batch.compact) {
for (const type of Object.keys(batch.compact.delete)) {
batch.compact.delete[type].forEach((key) => {
m[type].delete(key); // Should never fail or collection is corrupted
});
}
}
//2. On ajoute les nouvelles clés
Object.keys(batch)
.filter((type) => type !== "compact")
.forEach((type) => {
const neyKeyTypes = Object.keys(batch).filter((type) => type !== "compact");
neyKeyTypes.forEach((type) => {
m[type] = m[type] || new Set();
Object.keys(batch[type]).forEach((key) => {
Object.keys(batch[type] || {}).forEach((key) => {
m[type].add(key);
});
});
Expand Down Expand Up @@ -325,6 +326,7 @@ function reduce(key, values) {
// suivants.
const modified_batches = batches.filter((batch) => batch >= batchKey);
modified_batches.forEach((batch) => {
var _a;
reduced_value.batch[batch] = reduced_value.batch[batch] || {};
// Les types où il y a potentiellement des suppressions
let stock_types = completeTypes[batch].filter((type) => (memory[type] || new Set()).size > 0);
Expand All @@ -343,13 +345,14 @@ function reduce(key, values) {
const hashToDelete = {};
const hashToAdd = {};
all_interesting_types.forEach((type) => {
var _a;
// Le type compact gère les clés supprimées
if (type === "compact") {
if (reduced_value.batch[batch].compact.delete) {
Object.keys(reduced_value.batch[batch].compact.delete).forEach((delete_type) => {
reduced_value.batch[batch].compact.delete[delete_type].forEach((hash) => {
hashToDelete[delete_type] =
hashToDelete[delete_type] || new Set();
const compactDelete = (_a = reduced_value.batch[batch].compact) === null || _a === void 0 ? void 0 : _a.delete;
if (compactDelete) {
Object.keys(compactDelete).forEach((delete_type) => {
compactDelete[delete_type].forEach((hash) => {
hashToDelete[delete_type] = hashToDelete[delete_type] || new Set();
hashToDelete[delete_type].add(hash);
});
});
Expand Down Expand Up @@ -416,13 +419,10 @@ function reduce(key, values) {
// -------------------------------
stock_types.forEach((type) => {
if (hashToDelete[type]) {
reduced_value.batch[batch].compact =
reduced_value.batch[batch].compact || {};
reduced_value.batch[batch].compact.delete =
reduced_value.batch[batch].compact.delete || {};
reduced_value.batch[batch].compact.delete[type] = [
...hashToDelete[type],
];
const reducedBatch = reduced_value.batch[batch];
reducedBatch.compact = reducedBatch.compact || { delete: {} };
reducedBatch.compact.delete = reducedBatch.compact.delete || {};
reducedBatch.compact.delete[type] = [...hashToDelete[type]];
}
});
const typesToAdd = Object.keys(hashToAdd).filter((type) => type !== "compact");
Expand All @@ -431,7 +431,10 @@ function reduce(key, values) {
delete reduced_value.batch[batch][type];
}
else {
reduced_value.batch[batch][type] = [...hashToAdd[type]].reduce((typedBatchValues, hash) => (Object.assign(Object.assign({}, typedBatchValues), { [hash]: reduced_value.batch[batch][type][hash] })), {});
reduced_value.batch[batch][type] = [...hashToAdd[type]].reduce((typedBatchValues, hash) => {
var _a;
return (Object.assign(Object.assign({}, typedBatchValues), { [hash]: (_a = reduced_value.batch[batch][type]) === null || _a === void 0 ? void 0 : _a[hash] }));
}, {});
}
});
// 6. nettoyage
Expand All @@ -440,19 +443,19 @@ function reduce(key, values) {
//types vides
Object.keys(reduced_value.batch[batch]).forEach((strType) => {
const type = strType;
if (Object.keys(reduced_value.batch[batch][type]).length === 0) {
if (Object.keys(reduced_value.batch[batch][type] || {}).length === 0) {
delete reduced_value.batch[batch][type];
}
});
//hash à supprimer vides (compact.delete)
if (reduced_value.batch[batch].compact &&
reduced_value.batch[batch].compact.delete) {
Object.keys(reduced_value.batch[batch].compact.delete).forEach((type) => {
if (reduced_value.batch[batch].compact.delete[type].length === 0) {
delete reduced_value.batch[batch].compact.delete[type];
const compactDelete = (_a = reduced_value.batch[batch].compact) === null || _a === void 0 ? void 0 : _a.delete;
if (compactDelete) {
Object.keys(compactDelete).forEach((type) => {
if (compactDelete[type].length === 0) {
delete compactDelete[type];
}
});
if (Object.keys(reduced_value.batch[batch].compact.delete).length === 0) {
if (Object.keys(compactDelete).length === 0) {
delete reduced_value.batch[batch].compact;
}
}
Expand Down