diff --git a/src/entities/InterchangeSystem.ts b/src/entities/InterchangeSystem.ts index 03428a2..03cac46 100644 --- a/src/entities/InterchangeSystem.ts +++ b/src/entities/InterchangeSystem.ts @@ -14,6 +14,8 @@ import { ParkingRepositoryLoaderBuilderArguments } from "../loaders/parking/buildParkingRepositoryLoaderIfExists"; import { RedisParkingRepository } from "../repositories/parking/RedisParkingRepository"; +import { RedisShuttleRepository } from "../repositories/shuttle/RedisShuttleRepository"; +import { ShuttleGetterRepository } from "../repositories/shuttle/ShuttleGetterRepository"; export interface InterchangeSystemBuilderArguments { name: string; @@ -55,28 +57,13 @@ export class InterchangeSystem { static async build( args: InterchangeSystemBuilderArguments, ) { - const shuttleRepository = new UnoptimizedInMemoryShuttleRepository(); - const shuttleDataLoader = new ApiBasedShuttleRepositoryLoader( - args.passioSystemId, - args.id, - shuttleRepository - ); - const timedShuttleDataLoader = new TimedApiBasedRepositoryLoader( - shuttleDataLoader, - ); - await timedShuttleDataLoader.start(); + const { shuttleRepository, timedShuttleDataLoader } = await InterchangeSystem.buildRedisShuttleLoaderAndRepository(args); + timedShuttleDataLoader.start(); - const notificationRepository = new RedisNotificationRepository(); - await notificationRepository.connect(); - const notificationScheduler = new ETANotificationScheduler( - shuttleRepository, - notificationRepository, - new AppleNotificationSender(), - args.id, - ); + const { notificationScheduler, notificationRepository } = await InterchangeSystem.buildNotificationSchedulerAndRepository(shuttleRepository, args); notificationScheduler.startListeningForUpdates(); - let { parkingRepository, timedParkingLoader } = await this.buildRedisParkingLoaderAndRepository(args.parkingSystemId); + let { parkingRepository, timedParkingLoader } = await InterchangeSystem.buildRedisParkingLoaderAndRepository(args.parkingSystemId); timedParkingLoader?.start(); return new InterchangeSystem( @@ -91,49 +78,30 @@ export class InterchangeSystem { ); } - /** - * Construct an instance of the class where all composited - * classes are correctly linked, meant for unit tests, and server/app - * integration tests. - * @param args - */ - static buildForTesting( - args: InterchangeSystemBuilderArguments, - ) { - const shuttleRepository = new UnoptimizedInMemoryShuttleRepository(); + private static async buildRedisShuttleLoaderAndRepository(args: InterchangeSystemBuilderArguments) { + const shuttleRepository = new RedisShuttleRepository(); + await shuttleRepository.connect(); const shuttleDataLoader = new ApiBasedShuttleRepositoryLoader( args.passioSystemId, args.id, shuttleRepository ); - // Note that this loader should not be started, - // so the test data doesn't get overwritten - const timedShuttleLoader = new TimedApiBasedRepositoryLoader( - shuttleDataLoader, + const timedShuttleDataLoader = new TimedApiBasedRepositoryLoader( + shuttleDataLoader ); + return { shuttleRepository, timedShuttleDataLoader }; + } - const notificationRepository = new InMemoryNotificationRepository(); + private static async buildNotificationSchedulerAndRepository(shuttleRepository: ShuttleGetterRepository, args: InterchangeSystemBuilderArguments) { + const notificationRepository = new RedisNotificationRepository(); + await notificationRepository.connect(); const notificationScheduler = new ETANotificationScheduler( shuttleRepository, notificationRepository, - new AppleNotificationSender(false), - args.id, - ); - notificationScheduler.startListeningForUpdates(); - - let { parkingRepository, timedParkingLoader } = this.buildInMemoryParkingLoaderAndRepository(args.parkingSystemId); - // Timed parking loader is not started here - - return new InterchangeSystem( - args.name, - args.id, - timedShuttleLoader, - shuttleRepository, - notificationScheduler, - notificationRepository, - timedParkingLoader, - parkingRepository, + new AppleNotificationSender(), + args.id ); + return { notificationScheduler, notificationRepository }; } private static async buildRedisParkingLoaderAndRepository(id?: string) { @@ -161,6 +129,47 @@ export class InterchangeSystem { return { parkingRepository, timedParkingLoader }; } + /** + * Construct an instance of the class where all composited + * classes are correctly linked, meant for unit tests, and server/app + * integration tests. + * @param args + */ + static buildForTesting( + args: InterchangeSystemBuilderArguments, + ) { + const { shuttleRepository, timedShuttleLoader } = InterchangeSystem.buildInMemoryShuttleLoaderAndRepository(args); + // Timed shuttle loader is not started here + + const { notificationScheduler, notificationRepository } = InterchangeSystem.buildInMemoryNotificationSchedulerAndRepository(shuttleRepository, args); + notificationScheduler.startListeningForUpdates(); + + let { parkingRepository, timedParkingLoader } = this.buildInMemoryParkingLoaderAndRepository(args.parkingSystemId); + // Timed parking loader is not started here + + return new InterchangeSystem( + args.name, + args.id, + timedShuttleLoader, + shuttleRepository, + notificationScheduler, + notificationRepository, + timedParkingLoader, + parkingRepository, + ); + } + + private static buildInMemoryNotificationSchedulerAndRepository(shuttleRepository: UnoptimizedInMemoryShuttleRepository, args: InterchangeSystemBuilderArguments) { + const notificationRepository = new InMemoryNotificationRepository(); + const notificationScheduler = new ETANotificationScheduler( + shuttleRepository, + notificationRepository, + new AppleNotificationSender(false), + args.id + ); + return { notificationScheduler, notificationRepository }; + } + private static buildInMemoryParkingLoaderAndRepository(id?: string) { if (id === undefined) { return { parkingRepository: null, timedParkingLoader: null }; @@ -184,4 +193,19 @@ export class InterchangeSystem { return { parkingRepository, timedParkingLoader }; } + private static buildInMemoryShuttleLoaderAndRepository(args: InterchangeSystemBuilderArguments) { + const shuttleRepository = new UnoptimizedInMemoryShuttleRepository(); + const shuttleDataLoader = new ApiBasedShuttleRepositoryLoader( + args.passioSystemId, + args.id, + shuttleRepository + ); + // Note that this loader should not be started, + // so the test data doesn't get overwritten + const timedShuttleLoader = new TimedApiBasedRepositoryLoader( + shuttleDataLoader + ); + return { shuttleRepository, timedShuttleLoader }; + } + }