Skip to content

Commit

Permalink
Merge pull request #252 from BaseAdresseNationale/antoineludeau/fixes…
Browse files Browse the repository at this point in the history
…-and-improvements

Fixes and improvements
  • Loading branch information
antoineludeau authored Sep 4, 2023
2 parents 9b81164 + 7d1defb commit 03d0d35
Show file tree
Hide file tree
Showing 8 changed files with 31 additions and 26 deletions.
4 changes: 2 additions & 2 deletions lib/api/address/__mocks__/address-models.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,10 @@ export async function getAddresses(addressIDs) {
return bddAddressMock.filter(({id}) => addressIDs.includes(id))
}

export async function getAllAddressIDsFromCommune(districtID) {
export async function getAllAddressIDsFromDistrict(districtID) {
return bddAddressMock.filter(({districtID: districtIDAddress}) => districtIDAddress === districtID).map(({id}) => id)
}

export async function getAllAddressIDsOutsideCommune(addressIDs, districtID) {
export async function getAllAddressIDsOutsideDistrict(addressIDs, districtID) {
return bddAddressMock.filter(({id, districtID: districtIDAddress}) => addressIDs.includes(id) && districtIDAddress !== districtID).map(({id}) => id)
}
8 changes: 4 additions & 4 deletions lib/api/address/models.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,13 +9,13 @@ export async function getAddresses(addressIDs) {
return Address.findAll({where: {id: addressIDs}, raw: true})
}

export async function getAllAddressIDsFromCommune(districtID) {
const addresses = await Address.findAll({where: {districtID}, raw: true})
export async function getAllAddressIDsFromDistrict(districtID) {
const addresses = await Address.findAll({where: {districtID}, attributes: ['id'], raw: true})
return addresses.map(address => address.id)
}

export async function getAllAddressIDsOutsideCommune(addressIDs, districtID) {
const addresses = await Address.findAll({where: {id: addressIDs, districtID: {[Op.ne]: districtID}}, raw: true})
export async function getAllAddressIDsOutsideDistrict(addressIDs, districtID) {
const addresses = await Address.findAll({where: {id: addressIDs, districtID: {[Op.ne]: districtID}}, attributes: ['id'], raw: true})
return addresses.map(address => address.id)
}

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

const getExistingAddressIDs = async addressIDs => {
Expand Down Expand Up @@ -75,16 +75,16 @@ export const checkAddressesRequest = async (addresses, actionType) => {
}

export const getDeltaReport = async (addressIDs, districtID) => {
const allAddressIDsFromCommune = await getAllAddressIDsFromCommune(districtID)
const allAddressIDsFromCommuneSet = new Set(allAddressIDsFromCommune)
const allAddressIDsFromCommune = await getAllAddressIDsFromDistrict(districtID)
const allAddressIDsFromDistrictSet = new Set(allAddressIDsFromCommune)
const addressIDsSet = new Set(addressIDs)

let idsToCreate = []
const idsToUpdate = []
const idsToDelete = []

for (const id of addressIDs) {
if (allAddressIDsFromCommuneSet.has(id)) {
if (allAddressIDsFromDistrictSet.has(id)) {
idsToUpdate.push(id)
} else {
idsToCreate.push(id)
Expand All @@ -97,7 +97,7 @@ export const getDeltaReport = async (addressIDs, districtID) => {
}
}

const idsUnauthorized = await getAllAddressIDsOutsideCommune(idsToCreate, districtID)
const idsUnauthorized = await getAllAddressIDsOutsideDistrict(idsToCreate, districtID)
const idsUnauthorizedSet = new Set(idsUnauthorized)
if (idsUnauthorized.length > 0) {
idsToCreate = idsToCreate.filter(id => !idsUnauthorizedSet.has(id))
Expand Down
4 changes: 2 additions & 2 deletions lib/api/common-toponym/__mocks__/common-toponym-models.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,10 @@ export async function getCommonToponyms(commonToponymIDs) {
return bddCommonToponymMock.filter(({id}) => commonToponymIDs.includes(id))
}

export async function getAllCommonToponymIDsFromCommune(districtID) {
export async function getAllCommonToponymIDsFromDistrict(districtID) {
return bddCommonToponymMock.filter(({districtID: districtIDCommonToponym}) => districtIDCommonToponym === districtID).map(({id}) => id)
}

export async function getAllCommonToponymIDsOutsideCommune(commonToponymIDs, districtID) {
export async function getAllCommonToponymIDsOutsideDistrict(commonToponymIDs, districtID) {
return bddCommonToponymMock.filter(({id, districtID: districtIDCommonToponym}) => commonToponymIDs.includes(id) && districtIDCommonToponym !== districtID).map(({id}) => id)
}
16 changes: 11 additions & 5 deletions lib/api/common-toponym/models.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,13 +9,17 @@ export async function getCommonToponyms(commonToponymIDs) {
return CommonToponym.findAll({where: {id: commonToponymIDs}, raw: true})
}

export async function getAllCommonToponymIDsFromCommune(districtID) {
const commonToponyms = await CommonToponym.findAll({where: {districtID}, raw: true})
export async function getAllCommonToponymIDsFromDistrict(districtID) {
const commonToponyms = await CommonToponym.findAll(
{where: {districtID}, attributes: ['id'], raw: true}
)
return commonToponyms.map(commonToponym => commonToponym.id)
}

export async function getAllCommonToponymIDsOutsideCommune(commonToponymIDs, districtID) {
const commonToponyms = await CommonToponym.findAll({where: {id: commonToponymIDs, districtID: {[Op.ne]: districtID}}, raw: true})
export async function getAllCommonToponymIDsOutsideDistrict(commonToponymIDs, districtID) {
const commonToponyms = await CommonToponym.findAll(
{where: {id: commonToponymIDs, districtID: {[Op.ne]: districtID}}, attributes: ['id'], raw: true}
)
return commonToponyms.map(commonToponym => commonToponym.id)
}

Expand All @@ -24,7 +28,9 @@ export async function setCommonToponyms(commonToponyms) {
}

export async function updateCommonToponyms(commonToponyms) {
const bulkOperations = commonToponyms.map(commonToponym => CommonToponym.update(commonToponym, {where: {id: commonToponym.id}}))
const bulkOperations = commonToponyms.map(commonToponym =>
CommonToponym.update(commonToponym, {where: {id: commonToponym.id}})
)
return Promise.all(bulkOperations)
}

Expand Down
10 changes: 5 additions & 5 deletions lib/api/common-toponym/utils.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import {checkDataFormat, dataValidationReportFrom, checkIdsIsUniq, checkIdsIsVacant, checkIdsIsAvailable, checkDataShema, checkIdsShema, checkIfDistrictsExist} from '../helper.js'
import {banID} from '../schema.js'
import {getCommonToponyms, getAllCommonToponymIDsFromCommune, getAllCommonToponymIDsOutsideCommune} from './models.js'
import {getCommonToponyms, getAllCommonToponymIDsFromDistrict, getAllCommonToponymIDsOutsideDistrict} from './models.js'
import {banCommonToponymSchema} from './schema.js'

const getExistingCommonToponymIDs = async commonToponymIDs => {
Expand Down Expand Up @@ -67,16 +67,16 @@ export const checkCommonToponymsRequest = async (commonToponyms, actionType) =>
}

export const getDeltaReport = async (commonToponymIDs, districtID) => {
const allCommonToponymIDsFromCommune = await getAllCommonToponymIDsFromCommune(districtID)
const allCommonToponymIDsFromCommuneSet = new Set(allCommonToponymIDsFromCommune)
const allCommonToponymIDsFromCommune = await getAllCommonToponymIDsFromDistrict(districtID)
const allCommonToponymIDsFromDistrictSet = new Set(allCommonToponymIDsFromCommune)
const commonToponymIDsSet = new Set(commonToponymIDs)

let idsToCreate = []
const idsToUpdate = []
const idsToDelete = []

for (const id of commonToponymIDs) {
if (allCommonToponymIDsFromCommuneSet.has(id)) {
if (allCommonToponymIDsFromDistrictSet.has(id)) {
idsToUpdate.push(id)
} else {
idsToCreate.push(id)
Expand All @@ -89,7 +89,7 @@ export const getDeltaReport = async (commonToponymIDs, districtID) => {
}
}

const idsUnauthorized = await getAllCommonToponymIDsOutsideCommune(idsToCreate, districtID)
const idsUnauthorized = await getAllCommonToponymIDsOutsideDistrict(idsToCreate, districtID)
const idsUnauthorizedSet = new Set(idsUnauthorized)
if (idsUnauthorized.length > 0) {
idsToCreate = idsToCreate.filter(id => !idsUnauthorizedSet.has(id))
Expand Down
2 changes: 1 addition & 1 deletion lib/api/consumers/clean-job-status-consumer.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ export default async function cleanJobStatusConsumer() {
const limitValidDate = new Date(Date.now() - ms(JOB_STATUS_LIMIT_DURATION))
const jobStatus = await getAllJobStatusOlderThanDate(limitValidDate)
if (jobStatus.length > 0) {
await deleteJobStatus(jobStatus)
await deleteJobStatus(jobStatus.map(({id}) => id))
}
} catch (error) {
console.error(error)
Expand Down
3 changes: 1 addition & 2 deletions lib/api/job-status/models.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,6 @@ export async function setJobStatus(statusID, content) {
return JobStatus.create({id: statusID, ...content})
}

export async function deleteJobStatus(jobStatus) {
const jobStatusIDs = jobStatus.map(status => status.id)
export async function deleteJobStatus(jobStatusIDs) {
return JobStatus.destroy({where: {id: jobStatusIDs}})
}

0 comments on commit 03d0d35

Please sign in to comment.