feat(sync): synchronize customer adresses and orders from Hiboutik sales (#1)
This commit is contained in:
27
src/database/customerAddress.database.repository.ts
Normal file
27
src/database/customerAddress.database.repository.ts
Normal file
@@ -0,0 +1,27 @@
|
||||
import { Injectable } from '@nestjs/common';
|
||||
import { PrismaService } from '../../prisma/prisma.service';
|
||||
import { CustomerAddress } from '../../prisma/generated/client';
|
||||
import { CustomerAddressCreateInput } from '../../prisma/generated/models/CustomerAddress';
|
||||
|
||||
/** Database access to customer addresses. Rows are matched with Hiboutik by `hiboutikId`. */
|
||||
@Injectable()
|
||||
export class CustomerAddressDatabaseRepository {
|
||||
constructor(private readonly prisma: PrismaService) {}
|
||||
|
||||
/** Creates or updates the address with the same `hiboutikId`. */
|
||||
async upsert(address: CustomerAddressCreateInput): Promise<CustomerAddress> {
|
||||
return this.prisma.customerAddress.upsert({
|
||||
where: { hiboutikId: address.hiboutikId },
|
||||
create: address,
|
||||
update: address,
|
||||
});
|
||||
}
|
||||
|
||||
/** Deletes the addresses of a customer that no longer exist in Hiboutik (all of them if `keptHiboutikIds` is empty). */
|
||||
async deleteMissing(customerId: string, keptHiboutikIds: number[]): Promise<number> {
|
||||
const result = await this.prisma.customerAddress.deleteMany({
|
||||
where: { customerId, hiboutikId: { notIn: keptHiboutikIds } },
|
||||
});
|
||||
return result.count;
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user