Skip to content

Commit

Permalink
feat: refacto
Browse files Browse the repository at this point in the history
  • Loading branch information
alanlr committed Jul 9, 2024
1 parent 4df9c50 commit 5c7ddef
Showing 1 changed file with 11 additions and 8 deletions.
19 changes: 11 additions & 8 deletions server/src/modules/jobs/deca/hydrate-deca.ts
Original file line number Diff line number Diff line change
Expand Up @@ -33,8 +33,6 @@ const parseDate = (v: string) => {
return v ? new Date(v) : null;
};

const maxOldestDateForFetching = getMaxOldestDateForFetching();

// eslint-disable-next-line @typescript-eslint/no-explicit-any
export const buildDecaContract = (contrat: any) => {
return {
Expand Down Expand Up @@ -129,9 +127,7 @@ export const hydrateDeca = async ({ from, to }: { from?: string; to?: string })
yesterday.setMilliseconds(0);

// Récupération de la date début / fin
const dateDebutToFetch: Date = from
? new Date(`${from}T00:00:00.000Z`)
: (await getLastDecaCreatedDateInDb()) ?? maxOldestDateForFetching;
const dateDebutToFetch: Date = from ? new Date(`${from}T00:00:00.000Z`) : await getLastDecaCreatedDateInDb();
const dateFinToFetch = to ? new Date(`${to}T00:00:00.000Z`) : yesterday;

if (isAfter(dateDebutToFetch, dateFinToFetch)) {
Expand Down Expand Up @@ -291,14 +287,21 @@ const pushPeriod = async (periods: Array<{ dateDebut: string; dateFin: string }>
* Fonction de récupération de la dernière date de contrat Deca ajouté en base
* @returns
*/
export const getLastDecaCreatedDateInDb = async (): Promise<Date | null> => {
export const getLastDecaCreatedDateInDb = async () => {
const lastDecaItem = await getDbCollection("deca")
.find({ created_at: { $exists: true } })
.sort({ created_at: -1 })
.limit(1)
.toArray();
let lastCreatedAt = lastDecaItem[0]?.created_at ?? null;
// Si la dernière date est plus tard qu'hier, on prend d'avant hier en date de debut de référence
if (lastCreatedAt && isAfter(lastCreatedAt, addDays(new Date(), -1))) lastCreatedAt = addDays(new Date(), -2);
return lastCreatedAt;
if (lastCreatedAt) {
if (isAfter(lastCreatedAt, addDays(new Date(), -1))) {
lastCreatedAt = addDays(new Date(), -2);
}

return lastCreatedAt;
} else {
return getMaxOldestDateForFetching();
}
};

0 comments on commit 5c7ddef

Please sign in to comment.