Skip to content

Commit

Permalink
Modified delta-report route behavior adding hash
Browse files Browse the repository at this point in the history
  • Loading branch information
antoineludeau committed Nov 29, 2023
1 parent 9835808 commit 41b5090
Show file tree
Hide file tree
Showing 8 changed files with 38 additions and 34 deletions.
2 changes: 1 addition & 1 deletion lib/api/address/__mocks__/address-models.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ export async function getAddresses(addressIDs) {
return bddAddressMock.filter(({id}) => addressIDs.includes(id))
}

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

Expand Down
6 changes: 3 additions & 3 deletions lib/api/address/models.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,9 @@ export const getAddress = addressID => Address.findByPk(addressID, {raw: true})

export const getAddresses = addressIDs => Address.findAll({where: {id: addressIDs}, raw: true})

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

export const getAllAddressIDsOutsideDistrict = async (addressIDs, districtID) => {
Expand Down
6 changes: 3 additions & 3 deletions lib/api/address/routes.js
Original file line number Diff line number Diff line change
Expand Up @@ -189,14 +189,14 @@ app.post('/delete', auth, analyticsMiddleware, async (req, res) => {
app.post('/delta-report', auth, analyticsMiddleware, async (req, res) => {
let response
try {
const {addressIDs, districtID} = req.body
const {data, districtID} = req.body

if (!addressIDs || !districtID) {
if (!data || !districtID) {
res.status(404).send('Wrong request format')
return
}

const deltaReport = await getDeltaReport(addressIDs, districtID)
const deltaReport = await getDeltaReport(data, districtID)
response = {
date: new Date(),
status: 'success',
Expand Down
22 changes: 12 additions & 10 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, getAllAddressIDsFromDistrict, getAllAddressIDsOutsideDistrict} from './models.js'
import {getAddresses, getAllAddressIDsWithHashFromDistrict, getAllAddressIDsOutsideDistrict} from './models.js'
import {banAddressSchema} from './schema.js'

const getExistingAddressIDs = async addressIDs => {
Expand Down Expand Up @@ -82,25 +82,27 @@ export const checkAddressesRequest = async (addresses, actionType) => {
return report
}

export const getDeltaReport = async (addressIDs, districtID) => {
const allAddressIDsFromCommune = await getAllAddressIDsFromDistrict(districtID)
const allAddressIDsFromDistrictSet = new Set(allAddressIDsFromCommune)
const addressIDsSet = new Set(addressIDs)
export const getDeltaReport = async (addressIDsWithHash, districtID) => {
const addressIDsWithHashMap = new Map(addressIDsWithHash.map(({id, hash}) => [id, hash]))
const allAddressIDsWithHashFromDistrict = await getAllAddressIDsWithHashFromDistrict(districtID)
const allAddressIDsWithHashFromDistrictMap = new Map(allAddressIDsWithHashFromDistrict.map(({id, hash}) => [id, hash]))

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

for (const id of addressIDs) {
if (allAddressIDsFromDistrictSet.has(id)) {
idsToUpdate.push(id)
for (const [id, hash] of addressIDsWithHashMap) {
if (allAddressIDsWithHashFromDistrictMap.has(id)) {
if (allAddressIDsWithHashFromDistrictMap.get(id) !== hash) {
idsToUpdate.push(id)
}
} else {
idsToCreate.push(id)
}
}

for (const id of allAddressIDsFromCommune) {
if (!addressIDsSet.has(id)) {
for (const id of allAddressIDsWithHashFromDistrictMap.keys()) {
if (!addressIDsWithHashMap.has(id)) {
idsToDelete.push(id)
}
}
Expand Down
2 changes: 1 addition & 1 deletion lib/api/common-toponym/__mocks__/common-toponym-models.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ export async function getCommonToponyms(commonToponymIDs) {
return bddCommonToponymMock.filter(({id}) => commonToponymIDs.includes(id))
}

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

Expand Down
6 changes: 3 additions & 3 deletions lib/api/common-toponym/models.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,11 @@ export const getCommonToponym = commonToponymID => CommonToponym.findByPk(common

export const getCommonToponyms = commonToponymIDs => CommonToponym.findAll({where: {id: commonToponymIDs}, raw: true})

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

export const getAllCommonToponymIDsOutsideDistrict = async (commonToponymIDs, districtID) => {
Expand Down
6 changes: 3 additions & 3 deletions lib/api/common-toponym/routes.js
Original file line number Diff line number Diff line change
Expand Up @@ -189,14 +189,14 @@ app.post('/delete', auth, analyticsMiddleware, async (req, res) => {
app.post('/delta-report', auth, analyticsMiddleware, async (req, res) => {
let response
try {
const {commonToponymIDs, districtID} = req.body
const {data, districtID} = req.body

if (!commonToponymIDs || !districtID) {
if (!data || !districtID) {
res.status(404).send('Wrong request format')
return
}

const deltaReport = await getDeltaReport(commonToponymIDs, districtID)
const deltaReport = await getDeltaReport(data, districtID)
response = {
date: new Date(),
status: 'success',
Expand Down
22 changes: 12 additions & 10 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, getAllCommonToponymIDsFromDistrict, getAllCommonToponymIDsOutsideDistrict} from './models.js'
import {getCommonToponyms, getAllCommonToponymIDsWithHashFromDistrict, getAllCommonToponymIDsOutsideDistrict} from './models.js'
import {banCommonToponymSchema} from './schema.js'

const getExistingCommonToponymIDs = async commonToponymIDs => {
Expand Down Expand Up @@ -74,25 +74,27 @@ export const checkCommonToponymsRequest = async (commonToponyms, actionType) =>
return report
}

export const getDeltaReport = async (commonToponymIDs, districtID) => {
const allCommonToponymIDsFromCommune = await getAllCommonToponymIDsFromDistrict(districtID)
const allCommonToponymIDsFromDistrictSet = new Set(allCommonToponymIDsFromCommune)
const commonToponymIDsSet = new Set(commonToponymIDs)
export const getDeltaReport = async (commonToponymIDsWithHash, districtID) => {
const commonToponymIDsWithHashMap = new Map(commonToponymIDsWithHash.map(({id, hash}) => [id, hash]))
const allCommonToponymIDsWithHashFromDistrict = await getAllCommonToponymIDsWithHashFromDistrict(districtID)
const allCommonToponymIDsWithHashFromDistrictMap = new Map(allCommonToponymIDsWithHashFromDistrict.map(({id, hash}) => [id, hash]))

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

for (const id of commonToponymIDs) {
if (allCommonToponymIDsFromDistrictSet.has(id)) {
idsToUpdate.push(id)
for (const [id, hash] of commonToponymIDsWithHashMap) {
if (allCommonToponymIDsWithHashFromDistrictMap.has(id)) {
if (allCommonToponymIDsWithHashFromDistrictMap.get(id) !== hash) {
idsToUpdate.push(id)
}
} else {
idsToCreate.push(id)
}
}

for (const id of allCommonToponymIDsFromCommune) {
if (!commonToponymIDsSet.has(id)) {
for (const id of allCommonToponymIDsWithHashFromDistrictMap.keys()) {
if (!commonToponymIDsWithHashMap.has(id)) {
idsToDelete.push(id)
}
}
Expand Down

0 comments on commit 41b5090

Please sign in to comment.