Skip to content

Commit

Permalink
Merge pull request #17 from icdocsoc/feat-skip-products-when-no-changes
Browse files Browse the repository at this point in the history
feat: skip product import from eactivities when no new purchases found (to avoid unecessary requests to eactivities)
  • Loading branch information
Gum-Joe authored Oct 23, 2024
2 parents 9be3d7f + a8c7efc commit b1fe4e8
Showing 1 changed file with 31 additions and 1 deletion.
32 changes: 31 additions & 1 deletion collection/lib/crud/loadSalesFromEActivites.ts
Original file line number Diff line number Diff line change
Expand Up @@ -109,9 +109,39 @@ export async function loadSalesFromEActivites(
if (typeof product.eActivitiesId !== "number") {
logger.warn(`Product ${product.id} has an invalid eActivities ID, so skipping!`);
}

// Filter out sales
const sales = allSales.filter((sale) => sale.ProductID === product.eActivitiesId);
if (sales.length === 0) {
logger.warn(`No sales found for product ${product.name} id ${product.id} - skipping`);
continue;
}

// If length of sales === number of sales for product, skip
const existingSales = await prisma.variant.findMany({
select: {
_count: {
select: {
OrderItem: true,
},
},
},
where: {
rootItemId: product.id,
},
});

const existingSalesCount = existingSales.reduce(
(acc, curr) => acc + curr._count.OrderItem,
0,
);

if (existingSalesCount === sales.length) {
logger.warn(
`All sales for product ${product.name} id ${product.id} already imported, skipping. (${existingSalesCount} sales already imported)`,
);
continue;
}

// Pull product data as well
let productData: Awaited<ReturnType<typeof eActivities.getProductById>>;
Expand Down

0 comments on commit b1fe4e8

Please sign in to comment.