Skip to content

Commit

Permalink
Added patch routes and schemas for address, common toponym and district
Browse files Browse the repository at this point in the history
  • Loading branch information
antoineludeau committed Oct 6, 2023
1 parent 7f0ac0b commit c1e4126
Show file tree
Hide file tree
Showing 11 changed files with 214 additions and 18 deletions.
27 changes: 27 additions & 0 deletions lib/api/address/routes.js
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,33 @@ app.route('/')

res.send(response)
})
.patch(auth, analyticsMiddleware, async (req, res) => {
let response
try {
const addresses = req.body
const statusID = nanoid()

await apiQueue.add(
{dataType: 'address', jobType: 'patch', data: addresses, statusID},
{jobId: statusID, removeOnComplete: true}
)
response = {
date: new Date(),
status: 'success',
message: `Check the status of your request : ${BAN_API_URL}/job-status/${statusID}`,
response: {statusID},
}
} catch (error) {
response = {
date: new Date(),
status: 'error',
message: error,
response: {},
}
}

res.send(response)
})

app.route('/:addressID')
.get(analyticsMiddleware, async (req, res) => {
Expand Down
24 changes: 23 additions & 1 deletion lib/api/address/schema.js
Original file line number Diff line number Diff line change
Expand Up @@ -19,10 +19,32 @@ export const banAddressSchema = object({
districtID: banID.required(),
number: number().positive().integer().required(),
suffix: string().trim(),
labels: array().of(labelSchema).default(null).nullable(),
labels: array().of(labelSchema),
certified: boolean(),
positions: array().of(positionSchema).required(),
updateDate: date().required(),
meta: metaSchema
})

export const banAddressSchemaForPatch = object({
id: banID.required(),
mainCommonToponymID: banID,
secondaryCommonToponymIDs: array().of(banID),
districtID: banID,
number: number().positive().integer(),
suffix: string().trim(),
labels: array().of(labelSchema),
certified: boolean().default(false),
positions: array().of(positionSchema),
updateDate: date(),
meta: metaSchema
})

export const addressDefaultOptionalValues = {
secondaryCommonToponymIDs: [],
suffix: '',
labels: [],
certified: false,
meta: {},
}

6 changes: 4 additions & 2 deletions lib/api/address/utils.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import {checkDataFormat, dataValidationReportFrom, checkIdsIsUniq, checkIdsIsVacant, checkIdsIsAvailable, checkDataShema, checkIdsShema, checkIfCommonToponymsExist, checkIfDistrictsExist} from '../helper.js'
import {banID} from '../schema.js'
import {getAddresses, getAllAddressIDsFromDistrict, getAllAddressIDsOutsideDistrict} from './models.js'
import {banAddressSchema} from './schema.js'
import {banAddressSchema, banAddressSchemaForPatch} from './schema.js'

const getExistingAddressIDs = async addressIDs => {
const existingAddresses = await getAddresses(addressIDs)
Expand All @@ -24,6 +24,7 @@ export const checkAddressesIDsRequest = async (addressIDs, actionType, defaultRe
)
break
case 'update':
case 'patch':
report = (
checkIdsIsUniq('Shared IDs in request', addressIDs)
|| await checkIdsIsAvailable('Some unknown IDs', addressIDs, getExistingAddressIDs)
Expand All @@ -49,13 +50,14 @@ export const checkAddressesRequest = async (addresses, actionType) => {
switch (actionType) {
case 'insert':
case 'update':
case 'patch':
report = checkDataFormat(
`The request require an Array of address but receive ${typeof addresses}`,
'No address send to job',
addresses
)
|| await checkAddressesIDsRequest(addresses.map(address => address.id), actionType, false)
|| await checkDataShema('Invalid format', addresses, banAddressSchema)
|| await checkDataShema('Invalid format', addresses, actionType === 'patch' ? banAddressSchemaForPatch : banAddressSchema)
|| await checkIfCommonToponymsExist(addresses.reduce((acc, {mainCommonToponymID, secondaryCommonToponymIDs}) => {
const ids = [mainCommonToponymID]
if (secondaryCommonToponymIDs) {
Expand Down
27 changes: 27 additions & 0 deletions lib/api/common-toponym/routes.js
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,33 @@ app.route('/')

res.send(response)
})
.patch(auth, analyticsMiddleware, async (req, res) => {
let response
try {
const commonToponyms = req.body
const statusID = nanoid()

await apiQueue.add(
{dataType: 'commonToponym', jobType: 'patch', data: commonToponyms, statusID},
{jobId: statusID, removeOnComplete: true}
)
response = {
date: new Date(),
status: 'success',
message: `Check the status of your request : ${BAN_API_URL}/job-status/${statusID}`,
response: {statusID},
}
} catch (error) {
response = {
date: new Date(),
status: 'error',
message: error,
response: {},
}
}

res.send(response)
})

app.route('/:commonToponymID')
.get(analyticsMiddleware, async (req, res) => {
Expand Down
14 changes: 14 additions & 0 deletions lib/api/common-toponym/schema.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,3 +13,17 @@ export const banCommonToponymSchema = object({
updateDate: date().required(),
meta: metaSchema
})

export const banCommonToponymSchemaForPatch = object({
id: banID.required(),
districtID: banID,
labels: array().of(labelSchema),
geometry: geometrySchema,
updateDate: date(),
meta: metaSchema
})

export const commonToponymDefaultOptionalValues = {
geometry: {},
meta: {},
}
6 changes: 4 additions & 2 deletions lib/api/common-toponym/utils.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import {checkDataFormat, dataValidationReportFrom, checkIdsIsUniq, checkIdsIsVacant, checkIdsIsAvailable, checkDataShema, checkIdsShema, checkIfDistrictsExist} from '../helper.js'
import {banID} from '../schema.js'
import {getCommonToponyms, getAllCommonToponymIDsFromDistrict, getAllCommonToponymIDsOutsideDistrict} from './models.js'
import {banCommonToponymSchema} from './schema.js'
import {banCommonToponymSchema, banCommonToponymSchemaForPatch} from './schema.js'

const getExistingCommonToponymIDs = async commonToponymIDs => {
const existingCommonToponyms = await getCommonToponyms(commonToponymIDs)
Expand All @@ -24,6 +24,7 @@ export const checkCommonToponymsIDsRequest = async (commonToponymIDs, actionType
)
break
case 'update':
case 'patch':
report = (
checkIdsIsUniq('Shared IDs in request', commonToponymIDs)
|| await checkIdsIsAvailable('Some unknown IDs', commonToponymIDs, getExistingCommonToponymIDs)
Expand All @@ -49,13 +50,14 @@ export const checkCommonToponymsRequest = async (commonToponyms, actionType) =>
switch (actionType) {
case 'insert':
case 'update':
case 'patch':
report = checkDataFormat(
`The request require an Array of common toponym but receive ${typeof commonToponyms}`,
'No common toponym send to job',
commonToponyms
)
|| await checkCommonToponymsIDsRequest(commonToponyms.map(commonToponym => commonToponym.id), actionType, false)
|| await checkDataShema('Invalid common toponym format', commonToponyms, banCommonToponymSchema)
|| await checkDataShema('Invalid common toponym format', commonToponyms, actionType === 'patch' ? banCommonToponymSchemaForPatch : banCommonToponymSchema)
|| await checkIfDistrictsExist(commonToponyms.map(({districtID}) => districtID))
|| dataValidationReportFrom(true)
break
Expand Down
68 changes: 58 additions & 10 deletions lib/api/consumers/api-consumer.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,10 @@ import {setCommonToponyms, updateCommonToponyms, deleteCommonToponyms, getAllDis
import {checkCommonToponymsRequest, checkCommonToponymsIDsRequest} from '../common-toponym/utils.js'
import {setDistricts, updateDistricts, deleteDistricts} from '../district/models.js'
import {checkDistrictsRequest, checkDistrictsIDsRequest} from '../district/utils.js'
import {dataValidationReportFrom, addOrUpdateJob} from '../helper.js'
import {dataValidationReportFrom, formatObjectWithDefaults, addOrUpdateJob} from '../helper.js'
import {addressDefaultOptionalValues} from '../address/schema.js'
import {commonToponymDefaultOptionalValues} from '../common-toponym/schema.js'
import {districtDefaultOptionalValues} from '../district/schema.js'

const exportToExploitationDBQueue = queue('export-to-exploitation-db')
const exportToExploitationDBJobDelay = process.env.EXPORT_TO_EXPLOITATION_DB_JOB_DELAY || 10_000
Expand Down Expand Up @@ -60,6 +63,7 @@ const addressConsumer = async (jobType, payload, statusID) => {
switch (jobType) {
case 'insert':
case 'update':
case 'patch':
return checkAddressesRequest(payload, jobType)
case 'delete':
return checkAddressesIDsRequest(payload, jobType)
Expand All @@ -72,10 +76,23 @@ const addressConsumer = async (jobType, payload, statusID) => {
const addressesCount = payload.length
if (requestDataValidationReport.isValid) {
switch (jobType) {
case 'insert':
await setAddresses(payload)
case 'insert': {
const formattedAddresses = payload.map(address => (
formatObjectWithDefaults(address, addressDefaultOptionalValues)
))
await setAddresses(formattedAddresses)
break
case 'update':
}

case 'update': {
const formattedAddresses = payload.map(address => (
formatObjectWithDefaults(address, addressDefaultOptionalValues)
))
await updateAddresses(formattedAddresses)
break
}

case 'patch':
await updateAddresses(payload)
break
case 'delete':
Expand Down Expand Up @@ -108,6 +125,7 @@ const commonToponymConsumer = async (jobType, payload, statusID) => {
switch (jobType) {
case 'insert':
case 'update':
case 'patch':
return checkCommonToponymsRequest(payload, jobType)
case 'delete':
return checkCommonToponymsIDsRequest(payload, jobType)
Expand All @@ -120,10 +138,23 @@ const commonToponymConsumer = async (jobType, payload, statusID) => {
const commonToponymsCount = payload.length
if (requestDataValidationReport.isValid) {
switch (jobType) {
case 'insert':
await setCommonToponyms(payload)
case 'insert': {
const formattedCommonToponyms = payload.map(commonToponym => (
formatObjectWithDefaults(commonToponym, commonToponymDefaultOptionalValues)
))
await setCommonToponyms(formattedCommonToponyms)
break
case 'update':
}

case 'update': {
const formattedCommonToponyms = payload.map(commonToponym => (
formatObjectWithDefaults(commonToponym, commonToponymDefaultOptionalValues)
))
await updateCommonToponyms(formattedCommonToponyms)
break
}

case 'patch':
await updateCommonToponyms(payload)
break
case 'delete':
Expand Down Expand Up @@ -156,6 +187,7 @@ const districtConsumer = async (jobType, payload, statusID) => {
switch (jobType) {
case 'insert':
case 'update':
case 'patch':
return checkDistrictsRequest(payload, jobType)
case 'delete':
return checkDistrictsIDsRequest(payload, jobType)
Expand All @@ -168,10 +200,23 @@ const districtConsumer = async (jobType, payload, statusID) => {
const districtsCount = payload.length
if (requestDataValidationReport.isValid) {
switch (jobType) {
case 'insert':
await setDistricts(payload)
case 'insert': {
const formattedDistricts = payload.map(district => (
formatObjectWithDefaults(district, districtDefaultOptionalValues)
))
await setDistricts(formattedDistricts)
break
case 'update':
}

case 'update': {
const formattedDistricts = payload.map(district => (
formatObjectWithDefaults(district, districtDefaultOptionalValues)
))
await updateDistricts(formattedDistricts)
break
}

case 'patch':
await updateDistricts(payload)
break
case 'delete':
Expand Down Expand Up @@ -205,6 +250,7 @@ export const extractRelatedDistrictIDs = async (dataType, jobType, payload) => {
switch (jobType) {
case 'insert':
case 'update':
case 'patch':
return getAllDistrictIDsFromAddresses(payload.map(({id}) => id))
case 'delete':
return getAllDistrictIDsFromAddresses(payload)
Expand All @@ -217,6 +263,7 @@ export const extractRelatedDistrictIDs = async (dataType, jobType, payload) => {
switch (jobType) {
case 'insert':
case 'update':
case 'patch':
return getAllDistrictIDsFromCommonToponyms(payload.map(({id}) => id))
case 'delete':
return getAllDistrictIDsFromCommonToponyms(payload)
Expand All @@ -229,6 +276,7 @@ export const extractRelatedDistrictIDs = async (dataType, jobType, payload) => {
switch (jobType) {
case 'insert':
case 'update':
case 'patch':
return payload.map(({id}) => id)
case 'delete':
return payload
Expand Down
27 changes: 27 additions & 0 deletions lib/api/district/routes.js
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,33 @@ app.route('/')

res.send(response)
})
.patch(auth, analyticsMiddleware, async (req, res) => {
let response
try {
const districts = req.body
const statusID = nanoid()

await apiQueue.add(
{dataType: 'district', jobType: 'patch', data: districts, statusID},
{jobId: statusID, removeOnComplete: true}
)
response = {
date: new Date(),
status: 'success',
message: `Check the status of your request : ${BAN_API_URL}/job-status/${statusID}`,
response: {statusID},
}
} catch (error) {
response = {
date: new Date(),
status: 'error',
message: error,
response: {},
}
}

res.send(response)
})

app.route('/:districtID')
.get(analyticsMiddleware, async (req, res) => {
Expand Down
12 changes: 12 additions & 0 deletions lib/api/district/schema.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,3 +16,15 @@ export const banDistrictSchema = object({
config: configSchema,
meta: metaSchema
})

export const banDistrictSchemaForPatch = object({
id: banID.required(),
labels: array().of(labelSchema),
updateDate: date(),
config: configSchema,
meta: metaSchema
})

export const districtDefaultOptionalValues = {
config: {},
}
Loading

0 comments on commit c1e4126

Please sign in to comment.