import { Logger } from '@nestjs/common'; /** * Runs `action` on each item, one at a time: a failing item is logged and skipped, the others are still processed. * @param items * @param action * @param logger * @param describe label of an item in the error log, e.g. "Hiboutik brand 12". * @returns the number of failed items. */ export async function forEachSafely( items: T[], action: (item: T) => Promise, logger: Logger, describe: (item: T) => string, ): Promise { let nbFailed = 0; for (const item of items) { try { await action(item); } catch (error) { nbFailed++; logger.error(`Failed to sync ${describe(item)}`, (error as Error)?.stack); } } return nbFailed; }