Working on staffStation.utils and added tests

Signed-off-by: Fred Boniface <fred@fjla.uk>
This commit is contained in:
Fred Boniface 2023-08-01 21:34:30 +01:00
parent 658b0996bc
commit 996221b221
10 changed files with 3524 additions and 8 deletions

2
.npmrc
View File

@ -1 +1 @@
registry=https://git.fjla.uk/api/packages/OwlBoard/npm/
@owlboard:registry=https://git.fjla.uk/api/packages/OwlBoard/npm/

5
jest.config.js Normal file
View File

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

3327
package-lock.json generated

File diff suppressed because it is too large Load Diff

View File

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

View File

@ -20,6 +20,17 @@ function cleanNrcc(input: string) { // Remove newlines and then <p> tags from in
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
let split = mail.split('@');
return split[1];

View File

@ -1,10 +1,32 @@
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
export function transform(input: Object): StaffLdb {
export function transform(input: any): StaffLdb | null {
const data = input.GetBoardResult
let output: StaffLdb
try {
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[] {
@ -15,15 +37,41 @@ function transformNrcc(input: any): NrccMessage[] {
for (const item of input?.message) {
let message: NrccMessage = {
severity: item?.severity,
xhtmlMessage: item?.xhtmlMessage
xhtmlMessage: removeNewlineAndPTag(item?.xhtmlMessage)
}
output.push(message)
}
return output
}
function transformTrainServices(input: Object): TrainServices[] {
function transformTrainServices(input: any): TrainServices[] {
let services: any = input.service
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
}
@ -41,3 +89,8 @@ function transformLocation(input: any): ServiceLocation[] {
}
return output
}
function transformUnspecifiedDateTime(input: string): Date {
const date = tz(input, "Europe/London");
return date.toDate()
}

View File

@ -0,0 +1,21 @@
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

@ -0,0 +1,55 @@
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

@ -0,0 +1,40 @@
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

@ -107,8 +107,8 @@
"skipLibCheck": true /* Skip type checking all .d.ts files. */
},
"include": [
"./src",
"./test",
"src",
"test",
"./*",
"./config"
]