Skip to content

Commit

Permalink
Merge branch 'master' into gfay_refacto_nest
Browse files Browse the repository at this point in the history
  • Loading branch information
MaGOs92 committed Nov 21, 2023
2 parents 6045e96 + b596787 commit aaaeb14
Show file tree
Hide file tree
Showing 12 changed files with 89 additions and 116 deletions.
2 changes: 1 addition & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -35,4 +35,4 @@ lerna-debug.log*
!.vscode/extensions.json

.env
contours-communes.json
contours-communes.json
2 changes: 1 addition & 1 deletion apps/legacy-api/models/base-locale.js
Original file line number Diff line number Diff line change
Expand Up @@ -124,7 +124,7 @@ function parseQueryFilters(query) {

if (query.email) {
if (typeof query.email === 'string' && checkValidEmail(query.email)) {
filters.emails = {$eq: query.email}
filters.emails = {$regex: new RegExp(`^${query.email}$`, 'i')}
} else {
throw createHttpError(400, 'La valeur du champ "email" est invalide')
}
Expand Down
35 changes: 0 additions & 35 deletions apps/legacy-api/routes/__tests__/base-locale.js
Original file line number Diff line number Diff line change
Expand Up @@ -95,41 +95,6 @@ test.serial('create a BaseLocale / invalid commune', async t => {
})
})

test.serial('get all BaseLocales', async t => {
const _idBalA = new mongo.ObjectId()
const _idBalB = new mongo.ObjectId()

await mongo.db.collection('bases_locales').insertOne({
_id: _idBalA,
nom: 'foo',
emails: ['[email protected]'],
commune: '27115',
token: 'coucou',
_created: new Date('2019-01-01'),
_updated: new Date('2019-01-01'),
_deleted: null
})

await mongo.db.collection('bases_locales').insertOne({
_id: _idBalB,
nom: 'fobaro',
emails: ['[email protected]'],
commune: '27115',
token: 'hello',
_created: new Date('2019-01-01'),
_updated: new Date('2019-01-01'),
_deleted: new Date('2020-01-01')
})

const {body, status} = await request(getApp())
.get('/bases-locales')

t.is(status, 200)
t.is(body.length, 2)
t.true(SAFE_FIELDS.every(k => k in body[0]))
t.is(Object.keys(body[0]).length, SAFE_FIELDS.length)
})

test.serial('get a BaseLocal / with admin token', async t => {
const _id = new mongo.ObjectId()
await mongo.db.collection('bases_locales').insertOne({
Expand Down
5 changes: 0 additions & 5 deletions apps/legacy-api/routes/__tests__/stats.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@ const request = require('supertest')
const express = require('express')
const {MongoMemoryServer} = require('mongodb-memory-server')
const mongo = require('../../util/mongo')
const {prepareContoursCommunes} = require('../../util/contours-communes')
const routes = require('../stats')

function getApp() {
Expand All @@ -21,10 +20,6 @@ test.before('start server', async () => {
await mongo.connect(mongod.getUri())
})

test.before('prepare contours communes', async () => {
await prepareContoursCommunes()
})

test.after.always('cleanup', async () => {
await mongo.disconnect()
await mongod.stop()
Expand Down
8 changes: 0 additions & 8 deletions apps/legacy-api/routes/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -96,14 +96,6 @@ app.param('toponymeId', w(async (req, res, next) => {

// DONE
app.route('/bases-locales')
.get(w(async (req, res) => {
const basesLocales = await BaseLocale.fetchAll()
const basesLocalesExpanded = await Promise.all(
basesLocales.map(baseLocale => expandBalWithNumeros(baseLocale))
)

res.send(basesLocalesExpanded.map(bal => BaseLocale.filterSensitiveFields(bal)))
}))
.post(w(async (req, res) => {
const baseLocale = await BaseLocale.create(req.body)
const baseLocaleExpanded = await expandBalWithNumeros(baseLocale)
Expand Down
32 changes: 21 additions & 11 deletions apps/legacy-api/sync/index.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
const {setTimeout} = require('timers/promises')
const bluebird = require('bluebird')
const createError = require('http-errors')
const hasha = require('hasha')
const {sub} = require('date-fns')
Expand All @@ -8,6 +7,7 @@ const {sendMail} = require('../util/sendmail')
const createPublicationNotificationEmail = require('../emails/bal-publication-notification')
const BaseLocale = require('../models/base-locale')
const {fetchHabilitation} = require('../habilitations')
const {getMongoCache, setMongoCache} = require('../tasks/mongo-cache')
const {getCurrentRevision, publishNewRevision, getCurrentRevisions} = require('./api-depot')

async function detectOutdated() {
Expand Down Expand Up @@ -54,7 +54,11 @@ async function updateSyncInfo(balId) {

const currentRevision = await getCurrentRevision(codeCommune)

if (currentRevision._id !== baseLocale.sync.lastUploadedRevisionId) {
if (!currentRevision) {
console.error(`Comportement inattendu : pas de révision courante pour la commune ${codeCommune}`)
}

if (currentRevision?._id !== baseLocale.sync.lastUploadedRevisionId) {
return applyAndReturnSync(baseLocale, {
status: 'conflict',
isPaused: true
Expand Down Expand Up @@ -199,8 +203,9 @@ async function updateConflictStatus(codeCommune) {
return
}

await Promise.all(basesLocales.map(async baseLocale => {
for (const baseLocale of basesLocales) {
if (currentRevision._id === baseLocale.sync.lastUploadedRevisionId) {
/* eslint-disable-next-line no-await-in-loop */
await mongo.db.collection('bases_locales').updateOne(
{_id: baseLocale._id, status: 'replaced'},
{
Expand All @@ -211,6 +216,7 @@ async function updateConflictStatus(codeCommune) {
}
)
} else {
/* eslint-disable-next-line no-await-in-loop */
await mongo.db.collection('bases_locales').updateOne(
{_id: baseLocale._id, status: 'published'},
{
Expand All @@ -221,24 +227,28 @@ async function updateConflictStatus(codeCommune) {
}
)
}
}))
}
}

let detectConflictPublishedSince = new Date('2000-01-01')

async function detectConflict() {
const futurePublishedSince = new Date()

const detectConflictPublishedSince = await getMongoCache('detectConflictPublishedSince')
const currentRevisions = await getCurrentRevisions(detectConflictPublishedSince)
const revisedCommunes = currentRevisions.map(r => r.codeCommune)

detectConflictPublishedSince = futurePublishedSince
await setMongoCache('detectConflictPublishedSince', futurePublishedSince)

await setTimeout(5000)

await bluebird.map(revisedCommunes, async codeCommune => {
await updateConflictStatus(codeCommune)
}, {concurrency: 10})
for (const codeCommune of revisedCommunes) {
try {
/* eslint-disable-next-line no-await-in-loop */
await updateConflictStatus(codeCommune)
} catch (error) {
console.error(`Unable to detect conflict for ${codeCommune}`)
console.error(error)
}
}
}

async function syncOutdated() {
Expand Down
17 changes: 17 additions & 0 deletions apps/legacy-api/tasks/mongo-cache.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
const mongo = require('../util/mongo')

async function getMongoCache(name) {
const cache = await mongo.db.collection('_mongo-cache').findOne({name})

return cache?.value
}

function setMongoCache(name, value) {
return mongo.db.collection('_mongo-cache').updateOne(
{name},
{$set: {value}},
{upsert: true}
)
}

module.exports = {getMongoCache, setMongoCache}
27 changes: 27 additions & 0 deletions apps/legacy-api/tasks/task-queue.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
class TaskQueue {
constructor() {
this.queue = []
this.isTaskRunning = false
}

pushTask(task) {
this.queue.push(task)
if (!this.isTaskRunning) {
this.runTaskQueue()
}
}

async runTaskQueue() {
this.isTaskRunning = true

while (this.queue.length > 0) {
const task = this.queue.shift()
// eslint-disable-next-line no-await-in-loop
await task()
}

this.isTaskRunning = false
}
}

module.exports = {TaskQueue}
41 changes: 0 additions & 41 deletions apps/legacy-api/util/contours-communes.js

This file was deleted.

21 changes: 21 additions & 0 deletions migrations/2023-10-12-init-mongo-cache.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
#!/usr/bin/env node

require('dotenv').config()
const {sub} = require('date-fns')
const mongo = require('../lib/util/mongo')
const {setMongoCache} = require('../lib/tasks/mongo-cache')

async function main() {
await mongo.connect()

const initCacheTs = sub(new Date(), {hours: 1})
await setMongoCache('detectConflictPublishedSince', initCacheTs)

await mongo.disconnect()
process.exit()
}

main().catch(error => {
console.error(error)
process.exit(1)
})
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,7 @@
"class-validator": "^0.14.0",
"compression": "^1.7.4",
"cors": "^2.8.5",
"cron": "^3.1.1",
"csv-write-stream": "^2.0.0",
"date-fns": "^2.30.0",
"dotenv": "^16.3.1",
Expand Down
14 changes: 0 additions & 14 deletions scripts/update-contours-communes.js

This file was deleted.

0 comments on commit aaaeb14

Please sign in to comment.