Skip to content

Commit

Permalink
feat: fakeData refresh date (#156)
Browse files Browse the repository at this point in the history
  • Loading branch information
juliengrach-cassation authored Dec 23, 2024
1 parent b4013ed commit 707af96
Showing 1 changed file with 123 additions and 0 deletions.
123 changes: 123 additions & 0 deletions seeds/refreshDate.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,123 @@
const { MongoClient } = require("mongodb");
if (!process.env.NODE_ENV) require("dotenv").config();

async function refreshDocuments(db, date) {
const decisions = await db.collection("documents").find();

return decisions
.map(({ _id, decisionMetadata, nlpVersions }) => {
return db.collection("documents").updateOne(
{ _id },
{
$set: {
creationDate: date.getTime(),
"decisionMetadata.date": new Date(
`${date.toISOString().slice(0, 4)}-${
date.toISOString().slice(5, 7) - 1
}-${new Date(decisionMetadata.date).toISOString().slice(8)}`
).getTime(),
updateDate: date.getTime(),
nlpVersions: Object.entries(nlpVersions).reduce(
(acc, [key, value]) => {
return value?.date
? {
...acc,
[key]: {
...value,
date: date.toISOString().replace("T", " ").slice(0, -5),
},
}
: { ...acc, [key]: value };
},
{}
),
},
}
);
})
.toArray();
}

async function refreshAssignations(db, date) {
const decisions = await db.collection("assignations").find();

return decisions
.map(({ _id }) => {
return db.collection("assignations").updateOne(
{ _id },
{
$set: {
assignationDate: date.getTime(),
},
}
);
})
.toArray();
}

async function refreshStatistics(db, date) {
const decisions = await db.collection("statistics").find();

return decisions
.map(({ _id, treatmentDate, decisionDate }) => {
return db.collection("statistics").updateOne(
{ _id },
{
$set: {
decisionDate: new Date(
`${date.toISOString().slice(0, 4)}-${
date.toISOString().slice(5, 7) - 1
}-${new Date(decisionDate).toISOString().slice(8)}`
).getTime(),
treatmentDate: treatmentDate ? date.getTime() : null,
},
}
);
})
.toArray();
}

async function refreshTreatments(db, date) {
const decisions = await db.collection("treatments").find();

return decisions
.map(({ _id }) => {
return db.collection("treatments").updateOne(
{ _id },
{
$set: {
lastUpdateDate: date.getTime(),
},
}
);
})
.toArray();
}

async function main() {
const client = new MongoClient(process.env.LABEL_DB_URL, {
useUnifiedTopology: true,
});
await client.connect();

const db = client.db(process.env.LABEL_DB_NAME);

const input = process.argv[2];
const date = new Date(input * 1000);
if (!(date instanceof Date) || isNaN(date.valueOf()))
throw new Error(
`script.js [date]: waiting for an unix epoch date valid (input: ${input})`
);

const resDocuments = await refreshDocuments(db, date);
const resAssignations = await refreshAssignations(db, date);
const resStatistics = await refreshStatistics(db, date)
const resTreatments = await refreshTreatments(db, date)

return Promise.all([...resDocuments, ...resAssignations, ...resStatistics, ...resTreatments]);
}

main()
.then((_) => console.log(`update successfull: ${_.length} documents`))
.catch(console.error)
.finally((_) => process.exit());

0 comments on commit 707af96

Please sign in to comment.