feat(sync): synchronize Hiboutik data through POST /sync
POST /sync starts the full synchronization in the background (202, or 409 when one is already running). Entities are synced in dependency order: brands, categories, suppliers, products (with versions and stocks), customers. Each row is synced independently so that a failure is logged without stopping the rest. Replaces the default AppService.
This commit is contained in:
24
src/utils/sync.ts
Normal file
24
src/utils/sync.ts
Normal file
@@ -0,0 +1,24 @@
|
||||
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 describe label of an item in the error log, e.g. "Hiboutik brand 12".
|
||||
* @returns the number of failed items.
|
||||
*/
|
||||
export async function forEachSafely<T>(
|
||||
items: T[],
|
||||
action: (item: T) => Promise<unknown>,
|
||||
logger: Logger,
|
||||
describe: (item: T) => string,
|
||||
): Promise<number> {
|
||||
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;
|
||||
}
|
||||
Reference in New Issue
Block a user