-
Notifications
You must be signed in to change notification settings - Fork 7
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
e885222
commit 3c64643
Showing
5 changed files
with
122 additions
and
0 deletions.
There are no files selected for viewing
14 changes: 14 additions & 0 deletions
14
db-migrations/migrations/20241206163355-add-right-on-postal-area-id-sequence.cjs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
'use strict' | ||
|
||
const {POSTGRES_BAN_USER} = process.env | ||
|
||
/** @type {import('sequelize-cli').Migration} */ | ||
module.exports = { | ||
async up(queryInterface) { | ||
await queryInterface.sequelize.query(`GRANT USAGE, SELECT, UPDATE ON SEQUENCE external.postal_area_id_seq TO "${POSTGRES_BAN_USER}";`) | ||
}, | ||
|
||
async down(queryInterface) { | ||
await queryInterface.sequelize.query(`REVOKE USAGE, SELECT, UPDATE ON SEQUENCE external.postal_area_id_seq FROM "${POSTGRES_BAN_USER}";`) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,44 @@ | ||
import {PostalArea, sequelize} from '../../util/sequelize.js' | ||
|
||
export const replacePostalAreasPerDistrictCog = async (cog, postalAreas) => { | ||
const formattedPostalAreas = postalAreas.map(({postalCode, geometry}) => ({ | ||
inseeCom: cog, | ||
postalCode, | ||
geometry: JSON.stringify(geometry), | ||
})) | ||
|
||
const transaction = await sequelize.transaction() | ||
|
||
try { | ||
const postalAreasDeletedCount = await PostalArea.destroy( | ||
{ | ||
where: {inseeCom: cog}, | ||
transaction, | ||
} | ||
) | ||
|
||
const insertQuery = ` | ||
INSERT INTO external.postal_area ("postalCode", "inseeCom", geometry, "createdAt", "updatedAt") | ||
VALUES ($1, $2, ST_SetSRID(ST_GeomFromGeoJSON($3), 2154), NOW(), NOW()) | ||
` | ||
|
||
const insertPromises = formattedPostalAreas.map(async ({postalCode, inseeCom, geometry}) => { | ||
const result = await sequelize.query(insertQuery, { | ||
bind: [postalCode, inseeCom, geometry], | ||
transaction, | ||
}) | ||
if (result[1] !== 1) { | ||
throw new Error(`Failed to insert postal area with postalCode: ${postalCode}`) | ||
} | ||
}) | ||
|
||
await Promise.all(insertPromises) | ||
|
||
await transaction.commit() | ||
|
||
return {postalAreasCreatedCount: formattedPostalAreas.length, postalAreasDeletedCount} | ||
} catch (error) { | ||
await transaction.rollback() | ||
throw error | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,37 @@ | ||
import 'dotenv/config.js' // eslint-disable-line import/no-unassigned-import | ||
import express from 'express' | ||
import auth from '../../middleware/auth.js' | ||
import {handleAPIResponse} from '../helper.js' | ||
import {replacePostalAreasPerDistrictCog} from './models.js' | ||
|
||
const app = new express.Router() | ||
app.use(express.json()) | ||
|
||
app.put('/district/cog/:cog', auth, async (req, res) => { | ||
try { | ||
const {cog} = req.params | ||
const postalAreas = req.body | ||
|
||
if (!cog) { | ||
handleAPIResponse(res, 400, 'COG code is required', {}) | ||
return | ||
} | ||
|
||
if (!Array.isArray(postalAreas) || postalAreas.length === 0) { | ||
handleAPIResponse(res, 400, 'An array of items is required', {}) | ||
return | ||
} | ||
|
||
const {postalAreasCreatedCount, postalAreasDeletedCount} = await replacePostalAreasPerDistrictCog(cog, postalAreas) | ||
|
||
handleAPIResponse(res, 200, 'Postal areas updated', { | ||
createdCount: postalAreasCreatedCount, | ||
deletedCount: postalAreasDeletedCount, | ||
}) | ||
} catch (error) { | ||
console.error(error) | ||
handleAPIResponse(res, 500, 'Internal server error', {}) | ||
} | ||
}) | ||
|
||
export default app |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters