feat(sync): harden the Hiboutik sync, share rate limits across instances, update dependencies (#4)
This commit is contained in:
@@ -1,6 +1,6 @@
|
||||
import { Injectable } from '@nestjs/common';
|
||||
import { PrismaService } from '../../prisma/prisma.service';
|
||||
import { ProductVersion } from '../../prisma/generated/client';
|
||||
import { Prisma, ProductVersion } from '../../prisma/generated/client';
|
||||
import { ProductVersionCreateInput } from '../../prisma/generated/models/ProductVersion';
|
||||
|
||||
/** Database access to product versions (one per Hiboutik size), identified by their product and `hiboutikSizeId`. */
|
||||
@@ -14,9 +14,16 @@ export class ProductVersionDatabaseRepository {
|
||||
});
|
||||
}
|
||||
|
||||
/** Creates or updates the version of `productId` with the same `hiboutikSizeId`. */
|
||||
async upsert(productId: string, productVersion: ProductVersionCreateInput): Promise<ProductVersion> {
|
||||
return this.prisma.productVersion.upsert({
|
||||
/**
|
||||
* Creates or updates the version of `productId` with the same `hiboutikSizeId`.
|
||||
* @param db transaction client, to run it inside a transaction.
|
||||
*/
|
||||
async upsert(
|
||||
productId: string,
|
||||
productVersion: ProductVersionCreateInput,
|
||||
db: Prisma.TransactionClient = this.prisma,
|
||||
): Promise<ProductVersion> {
|
||||
return db.productVersion.upsert({
|
||||
where: { productId_hiboutikSizeId: { productId: productId, hiboutikSizeId: productVersion.hiboutikSizeId } },
|
||||
create: productVersion,
|
||||
update: productVersion,
|
||||
@@ -26,9 +33,10 @@ export class ProductVersionDatabaseRepository {
|
||||
/**
|
||||
* Deletes the versions of a product that no longer exist in Hiboutik.
|
||||
* Order lines keep their own snapshot of the product, their link is set to null.
|
||||
* @param db transaction client, to run it inside a transaction.
|
||||
*/
|
||||
async deleteMissing(productId: string, keptHiboutikSizeIds: number[]): Promise<number> {
|
||||
const result = await this.prisma.productVersion.deleteMany({
|
||||
async deleteMissing(productId: string, keptHiboutikSizeIds: number[], db: Prisma.TransactionClient = this.prisma): Promise<number> {
|
||||
const result = await db.productVersion.deleteMany({
|
||||
where: { productId, hiboutikSizeId: { notIn: keptHiboutikSizeIds } },
|
||||
});
|
||||
return result.count;
|
||||
|
||||
Reference in New Issue
Block a user