import { DomainEvents } from '@server/shared/src/core/domain/events/DomainEvents' ; import { prisma } from '@infra/prisma/client' ; import { Contact } from '@modules/contacts/domain/Contact' ; import { ContactEmail } from '@modules/contacts/domain/ContactEmail' ; import { ContactSubscriptions } from '@modules/contacts/domain/ContactSubscriptions' ; import { ContactMap } from '@modules/contacts/mappers/ContactMap' ; import { IContactRepo } from '@modules/contacts/repositories/IContactRepo' ; import { IContactSubscriptionsRepo } from '@modules/contacts/repositories/IContactSubscriptionsRepo' ; export class PrismaContactRepo implements IContactRepo { private contactSubscriptionsRepo : IContactSubscriptionsRepo ; constructor ( contactSubscriptionRepo ) { this . contactSubscriptionsRepo = contactSubscriptionRepo ; } private saveSubscriptions ( subscriptions ) { return this . contactSubscriptionsRepo . saveBulk ( subscriptions ) ; } async findByEmail ( email ) { const rawContact = await prisma . contact . findOne ( { where : { email : email instanceof ContactEmail ? email . value : email , } , } ) ; if ( ! rawContact ) { return null ; } return ContactMap . toDomain ( rawContact ) ; } async getContacts ( ) { const rawContacts = await prisma . contact . findMany ( ) ; const domainContacts = rawContacts . map ( ( contact ) => ContactMap . toDomain ( contact ) ) ; return domainContacts ; } async save ( contact ) { const contactData = ContactMap . toPersistence ( contact ) ; await prisma . contact . upsert ( { where : { email : contact . email . value } , update : contactData , create : contactData , } ) ; await this . saveSubscriptions ( contact . subscriptions ) ; DomainEvents . dispatchEventsForAggregate ( contact . id ) ; } }