Skip to content

Commit

Permalink
feat: remove deprecated task that retrieved BAN owner addresses via A…
Browse files Browse the repository at this point in the history
…PI calls
  • Loading branch information
loicguillois committed Feb 5, 2025
1 parent 54b63d7 commit 3c0ad89
Show file tree
Hide file tree
Showing 6 changed files with 2 additions and 201 deletions.
3 changes: 0 additions & 3 deletions server/clevercloud/cron.json

This file was deleted.

5 changes: 1 addition & 4 deletions server/src/controllers/ownerController.ts
Original file line number Diff line number Diff line change
Expand Up @@ -104,10 +104,7 @@ async function create(
onConflict: ['id'],
merge: false
});
await banAddressesRepository.markAddressToBeNormalized(
owner.id,
AddressKinds.Owner
);

await eventRepository.insertOwnerEvent({
id: uuidv4(),
name: "Création d'un nouveau propriétaire",
Expand Down
5 changes: 0 additions & 5 deletions server/src/models/AddressApi.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,11 +6,6 @@ export interface AddressApi extends AddressDTO {
lastUpdatedAt?: string;
}

export interface AddressToNormalize
extends Pick<AddressApi, 'label' | 'refId' | 'addressKind'> {
geoCode: string;
}

export const formatAddressApi = (
addressApi?: AddressApi | null | undefined
) => {
Expand Down
88 changes: 1 addition & 87 deletions server/src/repositories/banAddressesRepository.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,7 @@
import { AddressKinds } from '@zerologementvacant/models';
import config from '~/infra/config';
import db from '~/infra/database';
import { createLogger } from '~/infra/logger';
import { AddressApi, AddressToNormalize } from '~/models/AddressApi';
import { Housing, housingTable } from './housingRepository';
import { AddressApi } from '~/models/AddressApi';

export const banAddressesTable = 'ban_addresses';
export const Addresses = (transaction = db) =>
Expand Down Expand Up @@ -77,87 +75,6 @@ const getByRefId = async (
return address ? parseAddressApi(address) : null;
};

async function listAddressesToNormalize(): Promise<AddressToNormalize[]> {
const housings = await Housing()
.select(
`${housingTable}.id`,
`${housingTable}.address_dgfip`,
`${housingTable}.geo_code`
)
.leftJoin<AddressDBO>(banAddressesTable, (join) => {
join
.on(`${housingTable}.id`, `${banAddressesTable}.ref_id`)
.andOnVal('address_kind', AddressKinds.Housing);
})
.where((where) => {
where
.whereNull('last_updated_at')
.orWhere(
'last_updated_at',
'<',
db.raw(`current_timestamp - interval '${config.ban.update.delay}'`)
);
})
.orderBy([{ column: 'last_updated_at', order: 'asc', nulls: 'first' }])
.limit(config.ban.update.pageSize);

return housings.map<AddressToNormalize>((housing) => ({
refId: housing.id,
addressKind: AddressKinds.Housing,
label: housing.address_dgfip.join(' '),
geoCode: housing.geo_code
}));
}

const upsertList = async (addresses: AddressApi[]): Promise<AddressApi[]> => {
logger.info('Upsert address list', addresses.length);

const upsertedAddresses = addresses
.filter((_) => _.refId)
.filter(
(value, index, self) =>
self.findIndex((_) => _.refId === value.refId) === index
)
.map((addressApi) => ({
...formatAddressApi(addressApi),
last_updated_at: new Date()
}));

if (!upsertedAddresses.length) {
return [];
}

try {
return db(banAddressesTable)
.insert(upsertedAddresses)
.onConflict(['ref_id', 'address_kind'])
.merge([
'house_number',
'street',
'postal_code',
'city',
'latitude',
'longitude',
'score',
'last_updated_at'
])
.returning('*');
} catch (err) {
console.error('Upserting addresses failed', err, addresses.length);
throw new Error('Upserting addresses failed');
}
};

const markAddressToBeNormalized = async (
addressId: string,
addressKind: AddressKinds
) => {
db(banAddressesTable)
.where('ref_id', addressId)
.andWhere('address_kind', addressKind)
.update({ last_updated_at: null });
};

export const parseAddressApi = (address: AddressDBO): AddressApi => ({
refId: address.ref_id,
addressKind: address.address_kind,
Expand Down Expand Up @@ -196,7 +113,4 @@ export default {
save,
saveMany,
getByRefId,
listAddressesToNormalize,
markAddressToBeNormalized,
upsertList
};
11 changes: 0 additions & 11 deletions server/src/repositories/test/banAddressesRepository.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -97,15 +97,4 @@ describe('BAN addresses repository', () => {
expect(actual.length).toBeGreaterThanOrEqual(addresses.length);
}, 10_000);
});

describe('listAddressesToNormalize', () => {
it('should list addresses to normalize', async () => {
const housings = Array.from({ length: 3 }, genHousingApi);
await Housing().insert(housings.map(formatHousingRecordApi));

const actual = await banAddressesRepository.listAddressesToNormalize();

expect(actual.length).toBeGreaterThanOrEqual(housings.length);
});
});
});
91 changes: 0 additions & 91 deletions server/src/tasks/addressTask.ts

This file was deleted.

0 comments on commit 3c0ad89

Please sign in to comment.