Skip to content

Commit 4829bad

Browse files
committed
feat(update-identifiers): add jobs for update identifiers
1 parent 87006c4 commit 4829bad

10 files changed

Lines changed: 818 additions & 195 deletions

File tree

Lines changed: 94 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,94 @@
1+
import currentLegalCategoryQuery from './current-legal-category.query';
2+
import currentLocalisationQuery from './current-localisation.query';
3+
import currentNameQuery from './current-name.query';
4+
5+
const currentIdentifiersQuery = [
6+
{
7+
$lookup: {
8+
from: 'identifiers',
9+
let: { item: '$id' },
10+
pipeline: [
11+
{
12+
$match: {
13+
$expr: {
14+
$and: [
15+
{ $eq: ['$resourceId', '$$item'] },
16+
{ $ne: ['$active', false] },
17+
],
18+
},
19+
},
20+
},
21+
{
22+
$project: {
23+
_id: 0,
24+
type: 1,
25+
value: 1,
26+
},
27+
},
28+
],
29+
as: 'currentIdentifiers',
30+
},
31+
},
32+
];
33+
34+
const structQuery = [
35+
...currentLegalCategoryQuery,
36+
...currentLocalisationQuery,
37+
...currentNameQuery,
38+
...currentIdentifiersQuery,
39+
{
40+
$project: {
41+
_id: 0,
42+
id: 1,
43+
closureDate: { $ifNull: ['$closureDate', null] },
44+
creationDate: { $ifNull: ['$creationDate', null] },
45+
currentLocalisation: { $ifNull: ['$currentLocalisation', {}] },
46+
currentName: { $ifNull: ['$currentName', {}] },
47+
displayName: '$currentName.usualName',
48+
emails: { $ifNull: ['$emails', []] },
49+
href: { $concat: ['/structures/', '$id'] },
50+
legalcategory: { $ifNull: ['$legalcategory', {}] },
51+
structureStatus: { $ifNull: ['$structureStatus', 'active'] },
52+
currentIdentifiers: { $ifNull: ['$currentIdentifiers', []] },
53+
},
54+
},
55+
];
56+
57+
export default [
58+
{
59+
$group: {
60+
_id: '$paysage',
61+
id: { $first: '$paysage' },
62+
suggestions: {
63+
$push: {
64+
_id: '$_id',
65+
type: '$type',
66+
value: '$value',
67+
sourceType: '$sourceType',
68+
sourceValue: '$sourceValue',
69+
status: '$status',
70+
lastChecked: '$lastChecked',
71+
},
72+
},
73+
lastModificationDate: { $max: '$lastChecked' },
74+
},
75+
},
76+
{
77+
$lookup: {
78+
from: 'structures',
79+
localField: '_id',
80+
foreignField: 'id',
81+
pipeline: structQuery,
82+
as: 'paysageData',
83+
},
84+
},
85+
{
86+
$project: {
87+
_id: 0,
88+
id: '$_id',
89+
lastModificationDate: 1,
90+
suggestions: 1,
91+
paysageData: { $arrayElemAt: ['$paysageData', 0] },
92+
},
93+
},
94+
];

src/api/commons/repositories.js

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -119,6 +119,10 @@ export const sireneUpdatesRepository = new BaseMongoRepository({
119119
db,
120120
collection: '_sirene_updates',
121121
});
122+
export const identifierUpdatesStructuresRepository = new BaseMongoRepository({
123+
db,
124+
collection: '_identifier_updates_structures',
125+
});
122126
export const socialmediasRepository = new BaseMongoRepository({
123127
db,
124128
collection: 'socialmedias',

src/api/identifiers/identifier.routes.js

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,9 +3,11 @@ import controllers from '../commons/middlewares/crud.middlewares';
33
import { identifiersRepository as repository } from '../commons/repositories';
44
import { identifiers as resource } from '../resources';
55
import { readQuery } from '../commons/queries/identifiers.query';
6+
import updates from './updates/updates.structures.routes';
67

78
const router = new express.Router();
89

10+
router.use(updates);
911
router.route(`/${resource}`)
1012
.get(controllers.list(repository, readQuery));
1113

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
import express from 'express';
2+
import { ObjectId } from 'mongodb';
3+
4+
import controllers from '../../commons/middlewares/crud.middlewares';
5+
6+
import readQuery from '../../commons/queries/structures.identifiers.updates.query';
7+
import { identifierUpdatesStructuresRepository as repository } from '../../commons/repositories';
8+
import { db } from '../../../services/mongo.service';
9+
10+
const router = new express.Router();
11+
12+
router.route('/structures-identifier/updates').get(controllers.list(repository, readQuery));
13+
router.route('/structures-identifier/updates/:id')
14+
.patch(async (req, res) => {
15+
const { id } = req.params;
16+
const { status } = req.body;
17+
18+
const { value } = await db.collection('_identifier_updates_structures').findOneAndUpdate(
19+
{ _id: new ObjectId(id) },
20+
{ $set: { status } },
21+
{ returnDocument: 'after' },
22+
);
23+
24+
res.json(value);
25+
});
26+
27+
export default router;

src/config.js

Lines changed: 35 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -2,45 +2,48 @@ const production = {
22
jwtSecret: process.env.JWT_SECRET,
33
defaultAccountConfirmation: false,
44
totpWindow: [20, 0],
5-
accessTokenExpiresIn: process.env.ACCESS_TOKEN_EXPRIES_IN || "10d",
6-
refreshTokenExpiresIn: process.env.REFRESH_TOKEN_EXPRIES_IN || "20d",
7-
otpHeader: "x-paysage-otp",
8-
otpMethodHeader: "x-paysage-otp-method",
9-
systemName: "paysage",
5+
accessTokenExpiresIn: process.env.ACCESS_TOKEN_EXPRIES_IN || '10d',
6+
refreshTokenExpiresIn: process.env.REFRESH_TOKEN_EXPRIES_IN || '20d',
7+
otpHeader: 'x-paysage-otp',
8+
otpMethodHeader: 'x-paysage-otp-method',
9+
systemName: 'paysage',
1010
mongo: {
11-
mongoUri: process.env.MONGO_URI || "mongodb://localhost:27017",
12-
mongoDbName: process.env.MONGO_DBNAME || "paysage",
11+
mongoUri: process.env.MONGO_URI || 'mongodb://localhost:27017',
12+
mongoDbName: process.env.MONGO_DBNAME || 'paysage',
1313
},
1414
elastic: {
1515
node: process.env.ES_NODE,
1616
username: process.env.ES_USERNAME,
1717
password: process.env.ES_PASSWORD,
18-
index: "paysage",
18+
index: 'paysage',
1919
},
2020
objectStorage: {
2121
credentials: {
22-
version: "v3",
23-
keystoneAuthVersion: "v3",
24-
provider: "openstack",
22+
version: 'v3',
23+
keystoneAuthVersion: 'v3',
24+
provider: 'openstack',
2525
authUrl: process.env.OVH_AUTH_URL,
2626
username: process.env.OVH_USERNAME,
2727
password: process.env.OVH_PASSWORD,
2828
tenantId: process.env.OVH_TENANT_ID,
2929
tenantName: process.env.OVH_TENANT_NAME,
30-
domainName: "Default",
31-
projectDomainName: "Default",
30+
domainName: 'Default',
31+
projectDomainName: 'Default',
3232
region: process.env.OVH_REGION,
3333
},
34-
container: "paysage",
34+
container: 'paysage',
3535
},
3636
logger: {
37-
logLevel: "info",
37+
logLevel: 'info',
3838
},
39-
hostname: "https://api.paysage.dataesr.ovh",
39+
hostname: 'https://api.paysage.dataesr.ovh',
4040
sirene: {
41-
apiUrl: "https://api.insee.fr/api-sirene/3.11",
41+
apiUrl: 'https://api.insee.fr/api-sirene/3.11',
4242
apiKey: process.env.SIREN_API_KEY,
43-
taskName: "sync-sirene",
43+
taskName: 'sync-sirene',
44+
},
45+
identifiers: {
46+
taskName: 'updates-structures-identifiers',
4447
},
4548
};
4649

@@ -49,56 +52,56 @@ const staging = {
4952
defaultAccountConfirmation: true,
5053
objectStorage: {
5154
...production.objectStorage,
52-
container: "paysage-staging",
55+
container: 'paysage-staging',
5356
},
5457
elastic: {
5558
...production.elastic,
56-
index: "paysage-staging",
59+
index: 'paysage-staging',
5760
},
58-
hostname: "https://paysage-api.staging.dataesr.ovh",
61+
hostname: 'https://paysage-api.staging.dataesr.ovh',
5962
};
6063

6164
const testing = {
6265
...production,
63-
jwtSecret: "VerYvErySecrREt",
66+
jwtSecret: 'VerYvErySecrREt',
6467
defaultAccountConfirmation: true,
6568
mongo: {
6669
...production.mongo,
67-
mongoDbName: "paysage-test",
70+
mongoDbName: 'paysage-test',
6871
},
6972
elastic: {
7073
...production.elastic,
71-
index: "paysage-test",
74+
index: 'paysage-test',
7275
},
7376
objectStorage: {
7477
...production.objectStorage,
75-
container: "paysage-test",
78+
container: 'paysage-test',
7679
},
7780
logger: {
78-
logLevel: "error",
81+
logLevel: 'error',
7982
},
8083
};
8184

8285
const development = {
8386
...production,
84-
jwtSecret: "VerYvErySecrREt",
87+
jwtSecret: 'VerYvErySecrREt',
8588
defaultAccountConfirmation: true,
8689
mongo: {
8790
...production.mongo,
88-
mongoDbName: process.env.MONGO_DBNAME || "paysage-dev",
91+
mongoDbName: process.env.MONGO_DBNAME || 'paysage-dev',
8992
},
9093
elastic: {
9194
...production.elastic,
92-
index: "paysage-dev",
95+
index: 'paysage-dev',
9396
},
9497
objectStorage: {
9598
...production.objectStorage,
96-
container: "paysage-dev",
99+
container: 'paysage-dev',
97100
},
98101
logger: {
99-
logLevel: "debug",
102+
logLevel: 'debug',
100103
},
101-
hostname: "http://localhost:3000",
104+
hostname: 'http://localhost:3000',
102105
};
103106

104107
const configs = {

0 commit comments

Comments
 (0)