Skip to content

Commit

Permalink
feat: maj import declare
Browse files Browse the repository at this point in the history
Signed-off-by: Maud Royer <[email protected]>
  • Loading branch information
jillro committed Jul 12, 2024
1 parent c1a4ba3 commit 9a01e59
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 3 deletions.
27 changes: 25 additions & 2 deletions bin/import-pac-declare.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,11 +8,12 @@ const getStream = require('get-stream')
const gdal = require('gdal-async')
const { fromCodePacStrict } = require('@agencebio/rosetta-cultures')

const { createOrUpdateOperatorRecord } = require('../lib/providers/cartobio')
const { createOrUpdateOperatorRecord, getRecord, patchFeatureCollection, deleteRecord } = require('../lib/providers/cartobio')

Check failure on line 11 in bin/import-pac-declare.js

View workflow job for this annotation

GitHub Actions / node-test

'deleteRecord' is assigned a value but never used
const { CertificationState, EtatProduction } = require('../lib/enums.js')
const pool = require('../lib/db')
const { unzipGeographicalContent, detectSrs, wgs84 } = require('../lib/providers/gdal')
const { getRandomFeatureId } = require('../lib/outputs/features')
const { normalizeRecord } = require('../lib/outputs/record')

/**
* Ce script lit la grande FeatureCollection des parcellaires déclarés de l'ASP
Expand Down Expand Up @@ -143,6 +144,22 @@ if (process.argv.length < 4) {
})
}

let firstRecord = null
const currentRecord = await pool.query(/* sql */
`
SELECT cartobio_operators.record_id
FROM cartobio_operators
LEFT JOIN cartobio_parcelles cp on cartobio_operators.record_id = cp.record_id
WHERE numerobio = $1
AND numero_pacage = $2 LIMIT 1
`,
[operator.numerobio, pacage]
)

if (currentRecord.rows.length) {
firstRecord = await getRecord(currentRecord.rows[0].record_id)
}

/**
* @type {import('../outputs/types/record').NormalizedRecord}
*/
Expand All @@ -162,7 +179,13 @@ if (process.argv.length < 4) {
}
}

await createOrUpdateOperatorRecord(record, { copyParcellesData: true }, client)
const newRecord = normalizeRecord(await createOrUpdateOperatorRecord(record, { copyParcellesData: true }, client))
if (firstRecord) {
await patchFeatureCollection({ record: firstRecord }, newRecord.parcelles.features)
await pool.query(`
DELETE FROM cartobio_operators WHERE record_id = $1
`, [newRecord.record_id])
}
i++
progress.increment()
}
Expand Down
2 changes: 1 addition & 1 deletion lib/providers/cartobio.js
Original file line number Diff line number Diff line change
Expand Up @@ -85,7 +85,7 @@ const recordFields = /* sqlFragment */`cartobio_operators.record_id, numerobio,
* @param {CartoBioUser} [context.user]
* @param {Boolean} [context.copyParcellesData]
* @param {import('pg').PoolClient} [customClient]
* @returns {Promise<DBOperatorRecord>}
* @returns {Promise<DBOperatorRecordWithParcelles>}
*/
async function createOrUpdateOperatorRecord (record, context = {}, customClient) {
const certificationState = record.certification_state || CertificationState.OPERATOR_DRAFT
Expand Down

0 comments on commit 9a01e59

Please sign in to comment.