export async function findRecentLocalisation()

in lambda/rest-endpoints/src/curation.ts [129:176]


export async function findRecentLocalisation(
	territory: string,
	cutoffInDays: number,
	startDate: Date,
	curatedRecipes?: Set<string>,
	cutoff?: number,
) {
	for (let i = 0; i < cutoffInDays; i++) {
		const testDate = subDays(startDate, i);

		const maybeLocalisation = await retrieveLocalisationInsert(
			territory,
			testDate,
		);
		if (maybeLocalisation) {
			//if we have been given a set of curated recipes, ensure that they are not in the returned data
			if (curatedRecipes) {
				const deduplicatedLocalisation: FeastAppContainer = {
					...maybeLocalisation,
					//filter out anything that is a recipe specifier and also exists in curatedRecipes
					items: maybeLocalisation.items.filter(
						(row) =>
							// eslint-disable-next-line no-prototype-builtins -- how else to do this?
							(row.hasOwnProperty('recipe') &&
								!curatedRecipes.has((row as Recipe).recipe.id)) ||
							// eslint-disable-next-line no-prototype-builtins -- how else to do this?
							!row.hasOwnProperty('recipe'),
					),
				};
				return !!cutoff && deduplicatedLocalisation.items.length > cutoff
					? {
							...deduplicatedLocalisation,
							items: deduplicatedLocalisation.items.slice(0, cutoff),
					  }
					: deduplicatedLocalisation;
			} else {
				//we have not been asked to de-duplicate
				return !!cutoff && maybeLocalisation.items.length > cutoff
					? {
							...maybeLocalisation,
							items: maybeLocalisation.items.slice(0, cutoff),
					  }
					: maybeLocalisation;
			}
		}
	}
	return undefined;
}