Skip to content

Commit

Permalink
dismiss
Browse files Browse the repository at this point in the history
  • Loading branch information
folland87 committed Feb 5, 2025
1 parent 51efd02 commit 7ffd12d
Show file tree
Hide file tree
Showing 4 changed files with 51 additions and 10 deletions.
28 changes: 28 additions & 0 deletions src/api/commons/queries/sirene-updates.query.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,42 @@ import currentLegalCategoryQuery from './current-legal-category.query';
import currentLocalisationQuery from './current-localisation.query';
import currentNameQuery from './current-name.query';

const currentSiretQuery = [
{
$lookup: {
from: 'identifiers',
let: { item: '$id' },
pipeline: [{
$match: {
$expr: {
$and: [
{ $eq: ['$resourceId', '$$item'] },
{ $eq: ['$type', 'siret'] },
{ $ne: ['$active', false] },
],
},
},
}],
as: 'currentSiret',
},
},
{
$set: {
currentSiret: { $arrayElemAt: ['$currentSiret', 0] }
}
}
]

const structQuery = [
...currentLegalCategoryQuery,
...currentLocalisationQuery,
...currentNameQuery,
...currentSiretQuery,
{
$project: {
_id: 0,
id: 1,
currentSiret: { $ifNull: ['$currentSiret', null] },
closureDate: { $ifNull: ['$closureDate', null] },
creationDate: { $ifNull: ['$creationDate', null] },
currentLocalisation: { $ifNull: ['$currentLocalisation', {}] },
Expand Down
17 changes: 17 additions & 0 deletions src/api/sirene/updates/updates.routes.js
Original file line number Diff line number Diff line change
@@ -1,12 +1,29 @@
import express from "express";
import { ObjectId } from "mongodb";

import controllers from "../../commons/middlewares/crud.middlewares";

import readQuery from "../../commons/queries/sirene-updates.query";
import { sireneUpdatesRepository as repository } from "../../commons/repositories";
import { patchContext } from "../../commons/middlewares/context.middlewares";
import { db } from "../../../services/mongo.service";

const router = new express.Router();

router.route("/sirene/updates").get(controllers.list(repository, readQuery));
router.route("/sirene/updates/:id")
.patch(async (req, res) => {
const { id } = req.params;
const { status } = req.body;
console.log('HERE', status, id)

const { value } = await db.collection("sirene_updates").findOneAndUpdate(
{ _id: new ObjectId(id) },
{ $set: { status } },
{ returnDocument: "after" },
);

res.json(value);
})

export default router;
9 changes: 5 additions & 4 deletions src/jobs/sirene/api.js
Original file line number Diff line number Diff line change
Expand Up @@ -38,11 +38,12 @@ const fetchPage = async (endpoint, params, cursor) => {

const json = await response.json();

if (json?.header?.statut !== 200) throw new Error(json?.header?.message);
if (json?.header?.statut !== 200 && json?.header?.statut !== 404) throw new Error(json?.header?.message);

const data = json?.[API_CONFIG[endpoint]] ?? [];

return {
data: json[API_CONFIG[endpoint]] || [],
data,
nextCursor: json.header.curseurSuivant,
total: json.header.total
};
Expand All @@ -63,7 +64,7 @@ async function* sirenePageGenerator(endpoint, params, cursor = '*', total = null
yield {
data,
progress: {
total: total ?? pageTotal,
total: total ?? pageTotal ?? 0,
cursor,
}
};
Expand All @@ -90,7 +91,7 @@ const fetchSireneApi = async (endpoint, params) => {
results.push(...data);
processedCount += data.length;
console.log(
`Processed ${processedCount.toLocaleString()}/${progress.total.toLocaleString()} records`
`Processed ${processedCount?.toLocaleString()}/${progress.total?.toLocaleString()} records`
);
}

Expand Down
7 changes: 1 addition & 6 deletions src/jobs/sirene/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -46,12 +46,7 @@ export async function monitorSiren(job) {
const from = await getLastExecutionDate();
const until = now.toISOString().slice(0, 19);

if (!from) {
return {
status: "failed",
message: "No previous execution"
};
}
if (!from) job.fail("No previous execution")

const siretStockFromPaysage = await getSiretStockFromPaysage();
const sirenUpdatesMap = await fetchLegalUnitUpdates(from, until);
Expand Down

0 comments on commit 7ffd12d

Please sign in to comment.