import { PgUser } from '@/infra/repos/postgres/entities' ; import { PgRepository } from '@/infra/repos/postgres/repository' ; import { LoadUserAccount , SaveFacebookAccount } from '@/domain/contracts/repos' ; export class PgUserAccountRepository extends PgRepository implements LoadUserAccount , SaveFacebookAccount { async load ( { email } ) { const pgUserRepo = this . getRepository ( PgUser ) ; const pgUser = await pgUserRepo . findOne ( { email } ) ; if ( pgUser !== undefined ) { return { id : pgUser . id . toString ( ) , name : pgUser . name ?? undefined } ; } } async saveWithFacebook ( { id , name , email , facebookId } ) { const pgUserRepo = this . getRepository ( PgUser ) ; let resultId ; if ( id === undefined ) { const pgUser = await pgUserRepo . save ( { email , name , facebookId } ) ; resultId = pgUser . id . toString ( ) ; } else { resultId = id ; await pgUserRepo . update ( { id : parseInt ( id ) } , { name , facebookId } ) ; } return { id : resultId } ; } }