Compare commits

..

No commits in common. "996221b22174bfcdc9cc1121a41445047cb6a0dd" and "1adf24d105b4569c993f7f66ed7f819bbdfc498f" have entirely different histories.

9 changed files with 1093 additions and 4272 deletions

View File

@ -1,5 +0,0 @@
/** @type {import('ts-jest').JestConfigWithTsJest} */
module.exports = {
preset: 'ts-jest',
testEnvironment: 'node',
};

5152
package-lock.json generated

File diff suppressed because it is too large Load Diff

View File

@ -8,7 +8,6 @@
"html-minifier": "^4.0.0", "html-minifier": "^4.0.0",
"juice": "^9.0.0", "juice": "^9.0.0",
"ldbs-json": "^1.2.1", "ldbs-json": "^1.2.1",
"moment-timezone": "^0.5.43",
"mongodb": "^4.13.0", "mongodb": "^4.13.0",
"nodemailer": "^6.9.1", "nodemailer": "^6.9.1",
"redis": "^4.6.7", "redis": "^4.6.7",
@ -31,12 +30,9 @@
"author": "Fred Boniface", "author": "Fred Boniface",
"license": "GPL-3.0-or-later", "license": "GPL-3.0-or-later",
"devDependencies": { "devDependencies": {
"@owlboard/ts-types": "^0.0.3", "@owlboard/ts-types": "^0.0.1",
"@types/jest": "^29.5.3",
"eslint": "^8.39.0", "eslint": "^8.39.0",
"jest": "^29.6.2",
"prettier": "^2.8.8", "prettier": "^2.8.8",
"ts-jest": "^29.1.1",
"typescript": "^5.1.6" "typescript": "^5.1.6"
} }
} }

View File

@ -20,17 +20,6 @@ function cleanNrcc(input: string) { // Remove newlines and then <p> tags from in
return cleanInput; return cleanInput;
} }
export function removeNewlineAndPTag(input: string): string {
const regex = /[\n\r]|<\/?p[^>]*>/g;
return input.replace(regex, (match) => {
if (match === "\n" || match === "\r") {
return "";
} else {
return "";
}
})
}
async function getDomainFromEmail(mail: string) { // Needs testing async function getDomainFromEmail(mail: string) { // Needs testing
let split = mail.split('@'); let split = mail.split('@');
return split[1]; return split[1];

View File

@ -1,32 +1,10 @@
import type { StaffLdb, NrccMessage, TrainServices, import type { StaffLdb, NrccMessage, TrainServices,
ServiceLocation } from '@owlboard/ts-types'; ServiceLocation } from '@owlboard/ts-types'
import { tz } from 'moment-timezone';
import { removeNewlineAndPTag } from '../../sanitizer.utils';
/// I do not yet have a type defined for any of the input object /// I do not yet have a type defined for any of the input object
export function transform(input: any): StaffLdb | null { export function transform(input: Object): StaffLdb {
const data = input.GetBoardResult
let output: StaffLdb let output: StaffLdb
try { return output
output = {
generatedAt: transformDateTime(data?.generatedAt) || new Date(),
locationName: data?.locationName || "Not Found",
stationManagerCode: data?.stationManagerCode || "UK",
nrccMessages: transformNrcc(data?.nrccMessages),
trainServices: transformTrainServices(data?.trainServices),
busServices: transformTrainServices(data?.busServices),
ferryServices: transformTrainServices(data?.ferryServices)
}
return output
} catch (err) {
console.log('Unable to parse data, assuming no data: ' + err)
}
return null
}
function transformDateTime(input: string): Date {
return new Date(input)
} }
function transformNrcc(input: any): NrccMessage[] { function transformNrcc(input: any): NrccMessage[] {
@ -35,47 +13,21 @@ function transformNrcc(input: any): NrccMessage[] {
input.message = [input.message] input.message = [input.message]
} }
for (const item of input?.message) { for (const item of input?.message) {
let message: NrccMessage = { let message = {
severity: item?.severity, severity: item?.severity,
xhtmlMessage: removeNewlineAndPTag(item?.xhtmlMessage) xhtmlMessage: item?.xhtmlMessage
} }
output.push(message) output.push(message)
} }
return output return output
} }
function transformTrainServices(input: any): TrainServices[] { function transformTrainServices(input: Object): TrainServices[] {
let services: any = input.service let output: TrainServices[]
let output: TrainServices[] = []
if (!Array.isArray(input.service)) {
services = [input.service]
}
const trainServices: TrainServices = {
rid: services?.rid,
uid: services?.uid,
trainid: services?.trainid,
operatorCode: services?.operatorCode || 'UK',
platform: services?.platform || '',
platformIsHidden: services?.platformIsHidden || '',
serviceIsSupressed: services?.serviceIsSupressed || '',
origin: transformLocation(services?.origin),
destination: transformLocation(services?.destination),
isCancelled: services?.isCancelled || '',
cancelReason: services?.cancelReason,
delayReason: services?.delayReason,
arrivalType: services?.arrivalType,
departureType: services?.departureType,
sta: transformUnspecifiedDateTime(services?.sta),
eta: transformUnspecifiedDateTime(services?.eta),
ata: transformUnspecifiedDateTime(services?.ata),
std: transformUnspecifiedDateTime(services?.std),
etd: transformUnspecifiedDateTime(services?.etd),
atd: transformUnspecifiedDateTime(services?.atd),
}
return output return output
} }
function transformLocation(input: any): ServiceLocation[] { function transformLocation(input: any): ServiceLocation {
let output: ServiceLocation[] = [] let output: ServiceLocation[] = []
if (!Array.isArray(input)) { if (!Array.isArray(input)) {
input = [input] input = [input]
@ -88,9 +40,4 @@ function transformLocation(input: any): ServiceLocation[] {
output.push(location) output.push(location)
} }
return output return output
}
function transformUnspecifiedDateTime(input: string): Date {
const date = tz(input, "Europe/London");
return date.toDate()
} }

View File

@ -1,21 +0,0 @@
import { transform } from "../../../../src/utils/translators/ldb/staffStation";
import { inputs } from "./stationInputs.test.data";
import { outputs } from "./stationOutputs.test.data";
describe('transform', () => {
test('Should return null for empty input', () => {
const input = {};
expect(transform(input)).toBeNull();
});
for (const testNo in inputs) {
test(`Should correctly transform data ${testNo + 1}`, () => {
const input = inputs[testNo]
const expectedOutput = outputs[testNo]
expect(transform(input)).toEqual(expectedOutput);
});
}
});

View File

@ -1,55 +0,0 @@
export const inputs: any[] = [
{
GetBoardResult: {
generatedAt: '2023-08-01T20:37:05.559123+01:00',
locationName: 'Railway Station',
crs: 'RLY',
stationManager: 'Network Rail',
stationManagerCode: 'RT',
nrccMessages: {
message: {
severity: "minor",
xhtmlMessage: '\n<p>Minor Alert</p>',
type: "station"
}
},
isTruncated: 'true',
trainServices: {
service: [
{
rid: "202308017159276",
uid: "G59276",
trainuid: "1M83",
sdd: "2023-08-01",
operator: "CrossCountry",
operatorCode: "XC",
sta: "2023-08-01T20:24:00",
ata: "2023-08-01T20:27:22",
arrivalType: "Actual",
std: "2023-08-01T20:35:00",
etd: "2023-08-01T20:35:00",
departureType: "Estimated",
departureSource: "Darwin",
platform: "5",
origin: {
location: {
locationName: "Plymouth",
crs: "PLY",
tiploc: "PLYMTH"
}
},
destination: {
location: {
locationName: "Birmingham New Street",
crs: "BHM",
tiploc: "BHAMNWS"
}
},
category: "XX",
activities: "T",
}
]
}
}
},
]

View File

@ -1,40 +0,0 @@
export const outputs: any[] = [
{
generatedAt: expect.any(Date),
locationName: "Railway Station",
stationManagerCode: "RT",
nrccMessages: [
{
severity: "minor",
xhtmlMessage: "Minor Alert"
}
],
trainServices: [
{
rid: "202308017159276",
uid: "G59276",
trainuid: "1M83",
operatorCode: "XC",
sta: expect.any(Date),
ata: expect.any(Date),
arrivalType: "Actual",
std: expect.any(Date),
etd: expect.any(Date),
departureType: "Estimated",
platform: "5",
origin: [
{
tiploc: "PLYMTH",
locationName: "Plymouth"
}
],
destination: [
{
tiploc: "BHAMNWS",
locationName: "Birmingham New Street"
}
]
}
]
},
]

View File

@ -27,7 +27,7 @@
/* Modules */ /* Modules */
"module": "commonjs", /* Specify what module code is generated. */ "module": "commonjs", /* Specify what module code is generated. */
// "rootDir": "./", /* Specify the root folder within your source files. */ // "rootDir": "./", /* Specify the root folder within your source files. */
"moduleResolution": "node16", /* Specify how TypeScript looks up a file from a given module specifier. */ // "moduleResolution": "node10", /* Specify how TypeScript looks up a file from a given module specifier. */
// "baseUrl": "./", /* Specify the base directory to resolve non-relative module names. */ // "baseUrl": "./", /* Specify the base directory to resolve non-relative module names. */
// "paths": {}, /* Specify a set of entries that re-map imports to additional lookup locations. */ // "paths": {}, /* Specify a set of entries that re-map imports to additional lookup locations. */
// "rootDirs": [], /* Allow multiple folders to be treated as one when resolving modules. */ // "rootDirs": [], /* Allow multiple folders to be treated as one when resolving modules. */
@ -107,8 +107,8 @@
"skipLibCheck": true /* Skip type checking all .d.ts files. */ "skipLibCheck": true /* Skip type checking all .d.ts files. */
}, },
"include": [ "include": [
"src", "./src",
"test", "./test",
"./*", "./*",
"./config" "./config"
] ]