mirror of
https://github.com/brendan-ch/project-inter-server.git
synced 2026-04-17 16:00:32 +00:00
Implement mocking of environment file
This commit is contained in:
@@ -2,7 +2,7 @@ import { ParkingGetterSetterRepository } from "./ParkingGetterSetterRepository";
|
|||||||
import { IParkingStructure } from "../../entities/ParkingRepositoryEntities";
|
import { IParkingStructure } from "../../entities/ParkingRepositoryEntities";
|
||||||
import { HistoricalParkingAverageQueryResult, HistoricalParkingAverageQueryArguments } from "./ParkingGetterRepository";
|
import { HistoricalParkingAverageQueryResult, HistoricalParkingAverageQueryArguments } from "./ParkingGetterRepository";
|
||||||
import { BaseRedisRepository } from "../BaseRedisRepository";
|
import { BaseRedisRepository } from "../BaseRedisRepository";
|
||||||
import { PARKING_LOGGING_INTERVAL_MS } from "./ParkingRepositoryConstants";
|
import { PARKING_LOGGING_INTERVAL_MS } from "../../environment";
|
||||||
|
|
||||||
export type ParkingStructureID = string;
|
export type ParkingStructureID = string;
|
||||||
|
|
||||||
|
|||||||
@@ -2,34 +2,53 @@ import { Resolvers } from "../generated/graphql";
|
|||||||
import { ServerContext } from "../ServerContext";
|
import { ServerContext } from "../ServerContext";
|
||||||
import { HistoricalParkingAverageQueryArguments } from "../repositories/parking/ParkingGetterRepository";
|
import { HistoricalParkingAverageQueryArguments } from "../repositories/parking/ParkingGetterRepository";
|
||||||
import { GraphQLError } from "graphql/error";
|
import { GraphQLError } from "graphql/error";
|
||||||
|
import {
|
||||||
|
PARKING_HISTORICAL_AVERAGE_MAXIMUM_TIMESPAN,
|
||||||
|
PARKING_HISTORICAL_AVERAGE_MINIMUM_INTERVAL
|
||||||
|
} from "../environment";
|
||||||
|
|
||||||
export const ParkingStructureResolvers: Resolvers<ServerContext> = {
|
export const ParkingStructureResolvers: Resolvers<ServerContext> = {
|
||||||
ParkingStructure: {
|
ParkingStructure: {
|
||||||
historicalAverages: async (parent, args, contextValue, _info) => {
|
historicalAverages: async (parent, args, contextValue, _info) => {
|
||||||
|
/**
|
||||||
|
* @param errorMessage
|
||||||
|
*/
|
||||||
|
function throwBadUserInputError(errorMessage: string) {
|
||||||
|
throw new GraphQLError(errorMessage, {
|
||||||
|
extensions: {
|
||||||
|
code: 'BAD_USER_INPUT',
|
||||||
|
}
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
const system = contextValue.findSystemById(parent.systemId);
|
const system = contextValue.findSystemById(parent.systemId);
|
||||||
|
|
||||||
if (!args.input?.intervalMs) {
|
if (!args.input?.intervalMs) {
|
||||||
throw new GraphQLError('No interval provided', {
|
throwBadUserInputError('No interval provided');
|
||||||
extensions: {
|
return null;
|
||||||
code: 'BAD_USER_INPUT',
|
|
||||||
},
|
|
||||||
});
|
|
||||||
}
|
}
|
||||||
const historicalParkingAverageQueryArguments: HistoricalParkingAverageQueryArguments = {
|
const queryArguments: HistoricalParkingAverageQueryArguments = {
|
||||||
from: new Date(args.input.from),
|
from: new Date(args.input.from),
|
||||||
intervalMs: args.input.intervalMs,
|
intervalMs: args.input.intervalMs,
|
||||||
to: new Date(args.input.to),
|
to: new Date(args.input.to),
|
||||||
}
|
}
|
||||||
if (Number.isNaN(historicalParkingAverageQueryArguments.from.getTime()
|
if (Number.isNaN(queryArguments.from.getTime()
|
||||||
|| Number.isNaN(historicalParkingAverageQueryArguments.to.getTime()))) {
|
|| Number.isNaN(queryArguments.to.getTime()))) {
|
||||||
throw new GraphQLError('One or more invalid dates provided', {
|
throwBadUserInputError('One or more incorrect dates provided');
|
||||||
extensions: {
|
}
|
||||||
code: 'BAD_USER_INPUT',
|
if (queryArguments.from.getTime() > queryArguments.to.getTime()) {
|
||||||
},
|
throwBadUserInputError("`from` date can't be greater than the `to` date");
|
||||||
});
|
|
||||||
}
|
}
|
||||||
|
|
||||||
const parkingAverages = await system?.parkingRepository?.getHistoricalAveragesOfParkingStructureCounts(parent.id, historicalParkingAverageQueryArguments);
|
// Limit queries for improved performance
|
||||||
|
if (queryArguments.to.getTime() - queryArguments.from.getTime() > PARKING_HISTORICAL_AVERAGE_MAXIMUM_TIMESPAN) {
|
||||||
|
throwBadUserInputError('Maximum timespan exceeded');
|
||||||
|
}
|
||||||
|
if (queryArguments.intervalMs < PARKING_HISTORICAL_AVERAGE_MINIMUM_INTERVAL) {
|
||||||
|
throwBadUserInputError('Provided interval is less than minimum interval');
|
||||||
|
}
|
||||||
|
|
||||||
|
const parkingAverages = await system?.parkingRepository?.getHistoricalAveragesOfParkingStructureCounts(parent.id, queryArguments);
|
||||||
if (!parkingAverages) {
|
if (!parkingAverages) {
|
||||||
return null;
|
return null;
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -5,6 +5,8 @@ import { generateParkingStructures } from "../testHelpers/mockDataGenerators";
|
|||||||
import { HistoricalParkingAverageQueryInput } from "../../src/generated/graphql";
|
import { HistoricalParkingAverageQueryInput } from "../../src/generated/graphql";
|
||||||
import assert = require("node:assert");
|
import assert = require("node:assert");
|
||||||
|
|
||||||
|
jest.mock("../../src/environment");
|
||||||
|
|
||||||
describe("ParkingStructureResolver", () => {
|
describe("ParkingStructureResolver", () => {
|
||||||
const holder = setupTestServerHolder();
|
const holder = setupTestServerHolder();
|
||||||
const context = setupTestServerContext();
|
const context = setupTestServerContext();
|
||||||
|
|||||||
Reference in New Issue
Block a user