Files
dinggadingga-backend/src/database/customerAddress.database.repository.ts

28 lines
1.1 KiB
TypeScript

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;
}
}