-
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.
Merge pull request #462 from BaseAdresseNationale/jugurtha/api-pour-g…
…enerer-et-recuperer-un-certificat Jugurtha/api_certificat
- Loading branch information
Showing
8 changed files
with
256 additions
and
3 deletions.
There are no files selected for viewing
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
84 changes: 84 additions & 0 deletions
84
db-migrations/migrations/20240808162138-init-certificate-table.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,84 @@ | ||
'use strict' | ||
|
||
require('dotenv').config() | ||
|
||
const {POSTGRES_BAN_USER} = process.env | ||
|
||
/** @type {import('sequelize-cli').Migration} */ | ||
module.exports = { | ||
async up(queryInterface, Sequelize) { | ||
try { | ||
// Create ban schema if not exists | ||
await queryInterface.sequelize.query('CREATE SCHEMA IF NOT EXISTS ban;') | ||
|
||
// Grant permissions to ban user on schema ban | ||
await queryInterface.sequelize.query( | ||
`GRANT USAGE ON SCHEMA ban TO "${POSTGRES_BAN_USER}";` | ||
) | ||
|
||
// Create Certificate Table if not exists | ||
await queryInterface.createTable( | ||
'certificate', | ||
{ | ||
id: { | ||
type: Sequelize.UUID, | ||
defaultValue: Sequelize.UUIDV4, | ||
allowNull: false, | ||
primaryKey: true, | ||
}, | ||
// eslint-disable-next-line camelcase | ||
address_id: { | ||
type: Sequelize.UUID, | ||
allowNull: false, | ||
references: { | ||
model: { | ||
tableName: 'address', | ||
schema: 'ban', | ||
}, | ||
key: 'id', | ||
}, | ||
onUpdate: 'CASCADE', | ||
onDelete: 'CASCADE', | ||
}, | ||
// eslint-disable-next-line camelcase | ||
full_address: { | ||
type: Sequelize.JSONB, | ||
allowNull: false, | ||
}, | ||
// eslint-disable-next-line camelcase | ||
cadastre_ids: { | ||
type: Sequelize.ARRAY(Sequelize.STRING), | ||
allowNull: true, | ||
}, | ||
createdAt: { | ||
type: Sequelize.DATE, | ||
defaultValue: Sequelize.NOW, | ||
}, | ||
}, | ||
{ | ||
schema: 'ban', | ||
timestamps: false, | ||
ifNotExists: true, | ||
} | ||
) | ||
|
||
// Grant permissions to ban user on the certificate table | ||
await queryInterface.sequelize.query( | ||
`GRANT SELECT, INSERT, UPDATE, DELETE ON TABLE ban.certificate TO "${POSTGRES_BAN_USER}";` | ||
) | ||
} catch (error) { | ||
console.error(error) | ||
} | ||
}, | ||
|
||
async down(queryInterface) { | ||
try { | ||
// Drop the Certificate table | ||
await queryInterface.sequelize.query( | ||
'DROP TABLE IF EXISTS ban.certificate CASCADE;' | ||
) | ||
} catch (error) { | ||
console.error(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,56 @@ | ||
import {Certificate, sequelize} from '../../util/sequelize.js' | ||
|
||
const getDataForCertificateQuery = ` | ||
SELECT | ||
a.id as "addressID", | ||
a.number as "addressNumber", | ||
a.suffix as "addressSuffix", | ||
ct.labels[1]->>'value' as "commonToponymDefaultLabel", | ||
d.labels[1]->>'value' as "districtDefaultLabel", | ||
d.meta->'insee'->>'cog' as "districtCog", | ||
d.config as "districtConfig", | ||
a.meta->'cadastre'->'ids' as "cadastreIDs", | ||
a.certified, | ||
a."isActive" | ||
FROM | ||
"ban"."address" AS a | ||
JOIN | ||
"ban"."district" AS d ON a."districtID" = d.id | ||
LEFT JOIN | ||
"ban"."common_toponym" AS ct ON ct.id = a."mainCommonToponymID" | ||
WHERE | ||
a.id = :addressId | ||
and a.certified=true | ||
and a."isActive"=true | ||
and jsonb_array_length(a.meta->'cadastre'->'ids') > 0 | ||
` | ||
|
||
export const getCertificate = certificateID => Certificate.findByPk(certificateID, {raw: true}) | ||
|
||
export const getCertificates = certificateIDs => Certificate.findAll({ | ||
where: {id: certificateIDs}, | ||
raw: true | ||
}) | ||
|
||
export const getCertificatesByAddress = addressID => Certificate.findAll({ | ||
where: {address_id: addressID}, // eslint-disable-line camelcase | ||
raw: true | ||
}) | ||
|
||
export const setCertificate = async certificate => Certificate.create(certificate) | ||
|
||
export const getDataForCertificate = async addressId => { | ||
try { | ||
const [data] = await sequelize.query(getDataForCertificateQuery, { | ||
replacements: {addressId}, | ||
raw: true, | ||
}) | ||
|
||
console.log(`Data for certificate: ${JSON.stringify(data)}`) | ||
return data[0] | ||
} catch (error) { | ||
console.error(`Error executing query: ${error.message}`) | ||
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,58 @@ | ||
import 'dotenv/config.js' // eslint-disable-line import/no-unassigned-import | ||
import express from 'express' | ||
import auth from '../../middleware/auth.js' | ||
import { | ||
getCertificate, | ||
setCertificate, | ||
getDataForCertificate | ||
} from './models.js' | ||
import {formatDataForCertificate} from './utils.js' | ||
|
||
const app = new express.Router() | ||
app.use(express.json()) | ||
|
||
app.get('/:id', async (req, res) => { | ||
const {id} = req.params | ||
try { | ||
const certificate = await getCertificate(id) | ||
if (certificate) { | ||
res.status(200).json(certificate) | ||
} else { | ||
res.status(404).json({message: 'Certificate not found'}) | ||
} | ||
} catch (error) { | ||
console.error(`Error retrieving certificate: ${error.message}`) | ||
res.status(500).json({message: 'Internal server error'}) | ||
} | ||
}) | ||
|
||
app.post('/', auth, async (req, res) => { | ||
try { | ||
const {addressID} = req.body | ||
|
||
if (!addressID) { | ||
return res.status(400).json({message: 'addressID is required'}) | ||
} | ||
|
||
const data = await getDataForCertificate(addressID) | ||
|
||
if (!data) { | ||
return res.status(400).json({message: 'Address is not certified, not active, or has no parcels.'}) | ||
} | ||
|
||
const {districtConfig} = data | ||
if (!districtConfig.certificate) { | ||
return res.status(400).json({message: 'District has not activated the certificate config.'}) | ||
} | ||
|
||
const certificate = await formatDataForCertificate(data) | ||
const newCertificate = await setCertificate(certificate) | ||
|
||
res.status(201).json(newCertificate) | ||
} catch (error) { | ||
console.error(`Error creating certificate: ${error.message}`) | ||
res.status(500).json({message: '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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
export const formatDataForCertificate = data => { | ||
const fullAddress = { | ||
number: data.addressNumber, | ||
commonToponymDefaultLabel: data.commonToponymDefaultLabel, | ||
suffix: data.addressSuffix, | ||
districtDefaultLabel: data.districtDefaultLabel, | ||
cog: data.districtCog, | ||
} | ||
|
||
return { | ||
address_id: data.addressID, // eslint-disable-line camelcase | ||
full_address: fullAddress, // eslint-disable-line camelcase | ||
cadastre_ids: data.cadastreIDs, // eslint-disable-line camelcase | ||
} | ||
} |
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
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