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 { 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 { const result = await this.prisma.customerAddress.deleteMany({ where: { customerId, hiboutikId: { notIn: keptHiboutikIds } }, }); return result.count; } }