import EventPublisher from '../application/eventPublisher' ; import { IUserRepo } from '../domain/user/IUserRepo' ; import dynamoDBUserRepoCreator from './repos/userDynamoRepo' ; import mongoUserRepoCreator from './repos/userMongoRepo' ; import brokerFactory from '@micheleangioni/node-messagebrokers' ; import config from '../config/index' ; import dynamodbInitializer from './dynamo' ; import mongoInitializer from './mongo' ; import Logger from './logger' ; import isRunningInLocalStack from './utils/isRunningInLocalStack' ; if ( isRunningInLocalStack && ! process . env . SNS_ENDPOINT ) { process . env . SNS_ENDPOINT = 'http://localstack:4566' ; } else if ( process . env . ENV === 'development' && ! process . env . SNS_ENDPOINT ) { process . env . SNS_ENDPOINT = 'http://localhost:4566' ; } export default async ( accountId ) => { const logger = new Logger ( ) ; let userRepo ; if ( ! process . env . DB || process . env . DB === 'dynamo' ) { const { User } = dynamodbInitializer ( ) ; userRepo = dynamoDBUserRepoCreator ( User ) ; } else if ( process . env . DB === 'mongo' ) { const { User } = await mongoInitializer ( ) ; userRepo = mongoUserRepoCreator ( User ) ; } else { throw new Error ( `${ process . env . DB } is not a valid database` ) ; } const snsBroker = brokerFactory ( config . sns . topics , { awsAccountId : accountId === 'offlineContext_accountId' ? '000000000000' : accountId , } ) ; await snsBroker . init ( { createTopics : false , } ) ; const eventPublisher = new EventPublisher ( snsBroker , logger ) ; return { eventPublisher , logger , userRepo , } ; } ;