mirror of
https://github.com/brendan-ch/project-inter-server.git
synced 2026-04-17 07:50:31 +00:00
update server context to only include the notification repository
This commit is contained in:
@@ -1,7 +1,8 @@
|
|||||||
import { ETANotificationScheduler } from "./notifications/schedulers/ETANotificationScheduler";
|
import { ETANotificationScheduler } from "./notifications/schedulers/ETANotificationScheduler";
|
||||||
import { ShuttleGetterSetterRepository } from "./repositories/ShuttleGetterSetterRepository";
|
import { ShuttleGetterSetterRepository } from "./repositories/ShuttleGetterSetterRepository";
|
||||||
|
import { NotificationRepository } from "./repositories/NotificationRepository";
|
||||||
|
|
||||||
export interface ServerContext {
|
export interface ServerContext {
|
||||||
shuttleRepository: ShuttleGetterSetterRepository;
|
shuttleRepository: ShuttleGetterSetterRepository;
|
||||||
notificationService: ETANotificationScheduler;
|
notificationRepository: NotificationRepository;
|
||||||
}
|
}
|
||||||
|
|||||||
26
src/index.ts
26
src/index.ts
@@ -9,6 +9,7 @@ import { ETANotificationScheduler } from "./notifications/schedulers/ETANotifica
|
|||||||
import { loadShuttleTestData } from "./loaders/loadShuttleTestData";
|
import { loadShuttleTestData } from "./loaders/loadShuttleTestData";
|
||||||
import { AppleNotificationSender } from "./notifications/senders/AppleNotificationSender";
|
import { AppleNotificationSender } from "./notifications/senders/AppleNotificationSender";
|
||||||
import { InMemoryNotificationRepository } from "./repositories/InMemoryNotificationRepository";
|
import { InMemoryNotificationRepository } from "./repositories/InMemoryNotificationRepository";
|
||||||
|
import { NotificationRepository } from "./repositories/NotificationRepository";
|
||||||
|
|
||||||
const typeDefs = readFileSync("./schema.graphqls", "utf8");
|
const typeDefs = readFileSync("./schema.graphqls", "utf8");
|
||||||
|
|
||||||
@@ -19,25 +20,32 @@ async function main() {
|
|||||||
introspection: process.env.NODE_ENV !== "production",
|
introspection: process.env.NODE_ENV !== "production",
|
||||||
});
|
});
|
||||||
|
|
||||||
const repository = new UnoptimizedInMemoryShuttleRepository();
|
const shuttleRepository = new UnoptimizedInMemoryShuttleRepository();
|
||||||
|
|
||||||
|
let notificationRepository: NotificationRepository;
|
||||||
let notificationService: ETANotificationScheduler;
|
let notificationService: ETANotificationScheduler;
|
||||||
if (process.argv.length > 2 && process.argv[2] == "integration-testing") {
|
if (process.argv.length > 2 && process.argv[2] == "integration-testing") {
|
||||||
console.log("Using integration testing setup")
|
console.log("Using integration testing setup")
|
||||||
await loadShuttleTestData(repository);
|
await loadShuttleTestData(shuttleRepository);
|
||||||
|
|
||||||
const appleNotificationSender = new AppleNotificationSender(false);
|
const appleNotificationSender = new AppleNotificationSender(false);
|
||||||
const inMemoryNotificationRepository = new InMemoryNotificationRepository();
|
notificationRepository = new InMemoryNotificationRepository();
|
||||||
notificationService = new ETANotificationScheduler(
|
notificationService = new ETANotificationScheduler(
|
||||||
repository,
|
shuttleRepository,
|
||||||
inMemoryNotificationRepository,
|
notificationRepository,
|
||||||
appleNotificationSender
|
appleNotificationSender
|
||||||
);
|
);
|
||||||
} else {
|
} else {
|
||||||
const repositoryDataUpdater = new TimedApiBasedShuttleRepositoryLoader(
|
const repositoryDataUpdater = new TimedApiBasedShuttleRepositoryLoader(
|
||||||
repository
|
shuttleRepository,
|
||||||
);
|
);
|
||||||
await repositoryDataUpdater.start();
|
await repositoryDataUpdater.start();
|
||||||
notificationService = new ETANotificationScheduler(repository);
|
|
||||||
|
notificationRepository = new InMemoryNotificationRepository();
|
||||||
|
notificationService = new ETANotificationScheduler(
|
||||||
|
shuttleRepository,
|
||||||
|
notificationRepository
|
||||||
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
const { url } = await startStandaloneServer(server, {
|
const { url } = await startStandaloneServer(server, {
|
||||||
@@ -46,8 +54,8 @@ async function main() {
|
|||||||
},
|
},
|
||||||
context: async () => {
|
context: async () => {
|
||||||
return {
|
return {
|
||||||
repository,
|
shuttleRepository,
|
||||||
notificationService,
|
notificationRepository,
|
||||||
}
|
}
|
||||||
},
|
},
|
||||||
});
|
});
|
||||||
|
|||||||
@@ -30,7 +30,7 @@ export const MutationResolvers: Resolvers<ServerContext> = {
|
|||||||
: ETANotificationScheduler.defaultSecondsThresholdForNotificationToFire,
|
: ETANotificationScheduler.defaultSecondsThresholdForNotificationToFire,
|
||||||
}
|
}
|
||||||
|
|
||||||
await context.notificationService.scheduleNotification(notificationData);
|
await context.notificationRepository.scheduleNotification(notificationData);
|
||||||
|
|
||||||
const response: NotificationResponse = {
|
const response: NotificationResponse = {
|
||||||
message: "Notification scheduled",
|
message: "Notification scheduled",
|
||||||
@@ -40,8 +40,8 @@ export const MutationResolvers: Resolvers<ServerContext> = {
|
|||||||
return response;
|
return response;
|
||||||
},
|
},
|
||||||
cancelNotification: async (_parent, args, context, _info) => {
|
cancelNotification: async (_parent, args, context, _info) => {
|
||||||
if (context.notificationService.isNotificationScheduled(args.input)) {
|
if (context.notificationRepository.isNotificationScheduled(args.input)) {
|
||||||
await context.notificationService.cancelNotificationIfExists(args.input);
|
await context.notificationRepository.cancelNotificationIfExists(args.input);
|
||||||
return {
|
return {
|
||||||
success: true,
|
success: true,
|
||||||
message: "Notification cancelled",
|
message: "Notification cancelled",
|
||||||
|
|||||||
@@ -18,11 +18,11 @@ export const QueryResolvers: Resolvers<ServerContext> = {
|
|||||||
},
|
},
|
||||||
isNotificationScheduled: async (_parent, args, contextValue, _info) => {
|
isNotificationScheduled: async (_parent, args, contextValue, _info) => {
|
||||||
const notificationData = args.input;
|
const notificationData = args.input;
|
||||||
return contextValue.notificationService.isNotificationScheduled(notificationData);
|
return contextValue.notificationRepository.isNotificationScheduled(notificationData);
|
||||||
},
|
},
|
||||||
secondsThresholdForNotification: async (_parent, args, contextValue, _info) => {
|
secondsThresholdForNotification: async (_parent, args, contextValue, _info) => {
|
||||||
const notificationData = args.input;
|
const notificationData = args.input;
|
||||||
return contextValue.notificationService.getSecondsThresholdForScheduledNotification(notificationData);
|
return contextValue.notificationRepository.getSecondsThresholdForScheduledNotification(notificationData);
|
||||||
},
|
},
|
||||||
},
|
},
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -44,7 +44,7 @@ describe("MutationResolvers", () => {
|
|||||||
const notificationResponse = response.body.singleResult.data?.scheduleNotification as any;
|
const notificationResponse = response.body.singleResult.data?.scheduleNotification as any;
|
||||||
expect(notificationResponse.success).toBe(false);
|
expect(notificationResponse.success).toBe(false);
|
||||||
|
|
||||||
expect(context.notificationService.isNotificationScheduled(notificationInput)).toBe(false);
|
expect(context.notificationRepository.isNotificationScheduled(notificationInput)).toBe(false);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
@@ -72,7 +72,7 @@ describe("MutationResolvers", () => {
|
|||||||
expect(notificationResponse?.success).toBe(true);
|
expect(notificationResponse?.success).toBe(true);
|
||||||
expect(notificationResponse?.data).toEqual(expectedNotificationData);
|
expect(notificationResponse?.data).toEqual(expectedNotificationData);
|
||||||
|
|
||||||
expect(context.notificationService.getSecondsThresholdForScheduledNotification(expectedNotificationData)).toBe(240);
|
expect(context.notificationRepository.getSecondsThresholdForScheduledNotification(expectedNotificationData)).toBe(240);
|
||||||
});
|
});
|
||||||
|
|
||||||
it("adds a notification with the default seconds threshold if none is provided", async () => {
|
it("adds a notification with the default seconds threshold if none is provided", async () => {
|
||||||
@@ -93,7 +93,7 @@ describe("MutationResolvers", () => {
|
|||||||
const notificationResponse = response.body.singleResult.data?.scheduleNotification as any;
|
const notificationResponse = response.body.singleResult.data?.scheduleNotification as any;
|
||||||
expect(notificationResponse?.success).toBe(true);
|
expect(notificationResponse?.success).toBe(true);
|
||||||
|
|
||||||
expect(context.notificationService.getSecondsThresholdForScheduledNotification(notificationInput)).toBe(180);
|
expect(context.notificationRepository.getSecondsThresholdForScheduledNotification(notificationInput)).toBe(180);
|
||||||
});
|
});
|
||||||
|
|
||||||
it("fails if the shuttle ID doesn't exist", async () => {
|
it("fails if the shuttle ID doesn't exist", async () => {
|
||||||
@@ -150,7 +150,7 @@ describe("MutationResolvers", () => {
|
|||||||
stopId: stop.id,
|
stopId: stop.id,
|
||||||
secondsThreshold: 180,
|
secondsThreshold: 180,
|
||||||
}
|
}
|
||||||
await context.notificationService.scheduleNotification(notificationInput);
|
await context.notificationRepository.scheduleNotification(notificationInput);
|
||||||
|
|
||||||
const notificationLookup = {
|
const notificationLookup = {
|
||||||
...notificationInput
|
...notificationInput
|
||||||
@@ -166,7 +166,7 @@ describe("MutationResolvers", () => {
|
|||||||
expect(notificationResponse.success).toBe(true);
|
expect(notificationResponse.success).toBe(true);
|
||||||
expect(notificationResponse.data).toEqual(notificationLookup);
|
expect(notificationResponse.data).toEqual(notificationLookup);
|
||||||
|
|
||||||
expect(context.notificationService.isNotificationScheduled(notificationLookup)).toBe(false);
|
expect(context.notificationRepository.isNotificationScheduled(notificationLookup)).toBe(false);
|
||||||
});
|
});
|
||||||
|
|
||||||
it("fails if the notification doesn't exist", async () => {
|
it("fails if the notification doesn't exist", async () => {
|
||||||
|
|||||||
@@ -112,7 +112,7 @@ describe("QueryResolvers", () => {
|
|||||||
deviceId: "1",
|
deviceId: "1",
|
||||||
secondsThreshold: 240,
|
secondsThreshold: 240,
|
||||||
};
|
};
|
||||||
await context.notificationService.scheduleNotification(notification);
|
await context.notificationRepository.scheduleNotification(notification);
|
||||||
|
|
||||||
const notificationLookup: any = {
|
const notificationLookup: any = {
|
||||||
...notification,
|
...notification,
|
||||||
|
|||||||
@@ -5,6 +5,7 @@ import { UnoptimizedInMemoryShuttleRepository } from "../../src/repositories/Uno
|
|||||||
import { beforeEach } from "@jest/globals";
|
import { beforeEach } from "@jest/globals";
|
||||||
import { ServerContext } from "../../src/ServerContext";
|
import { ServerContext } from "../../src/ServerContext";
|
||||||
import { ETANotificationScheduler } from "../../src/notifications/schedulers/ETANotificationScheduler";
|
import { ETANotificationScheduler } from "../../src/notifications/schedulers/ETANotificationScheduler";
|
||||||
|
import { InMemoryNotificationRepository } from "../../src/repositories/InMemoryNotificationRepository";
|
||||||
|
|
||||||
|
|
||||||
function setUpTestServer() {
|
function setUpTestServer() {
|
||||||
@@ -26,7 +27,7 @@ export function setupTestServerContext() {
|
|||||||
|
|
||||||
beforeEach(() => {
|
beforeEach(() => {
|
||||||
context.shuttleRepository = new UnoptimizedInMemoryShuttleRepository();
|
context.shuttleRepository = new UnoptimizedInMemoryShuttleRepository();
|
||||||
context.notificationService = new ETANotificationScheduler(context.repository);
|
context.notificationRepository = new InMemoryNotificationRepository();
|
||||||
});
|
});
|
||||||
|
|
||||||
return context as ServerContext;
|
return context as ServerContext;
|
||||||
|
|||||||
Reference in New Issue
Block a user