Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: correct line number for GIS import #3552

Merged
merged 3 commits into from
Oct 1, 2024
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 8 additions & 3 deletions app/backend/lib/gis-upload.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import formidable, { File } from 'formidable';
import Ajv, { ErrorObject } from 'ajv';
import fs from 'fs';
import RateLimit from 'express-rate-limit';
import jsonSourceMap from 'json-source-map';
import schema from './gis-schema.json';
import { performQuery } from './graphql';
import getAuthRole from '../../utils/getAuthRole';
Expand Down Expand Up @@ -35,19 +36,23 @@ const FIELD_NAME_POSITION = 2;

const formatAjv = (data: Record<string, any>, errors: ErrorObject[]) => {
const reply = [];
const sourceMap = jsonSourceMap.stringify(data, null, 2);
errors.forEach((e) => {
const parts = e.instancePath.split('/');
const ccbcNumber = data?.[parts[LINE_NUMBER_POSITION]]?.ccbc_number ?? null;
const errorPointer = sourceMap.pointers[e.instancePath];
if (parts.length > MIN_PATH_DEPTH) {
const item = {
line: parseInt(parts[LINE_NUMBER_POSITION], 10) + 1,
ccbc_number: data[parts[LINE_NUMBER_POSITION]].ccbc_number,
line: (errorPointer?.key?.line || 0) + 1,
ccbc_number: ccbcNumber,
message: `${parts[FIELD_NAME_POSITION]} ${e.message}`,
};
reply.push(item);
} else {
// errors on root level
const item = {
line: 1,
line: e.keyword === 'required' ? null : 1,
ccbc_number: ccbcNumber,
message: e.message,
};
reply.push(item);
Expand Down
1 change: 1 addition & 0 deletions app/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -82,6 +82,7 @@
"js-cookie": "^3.0.5",
"json-diff": "^1.0.6",
"json-schema": "^0.4.0",
"json-source-map": "^0.6.1",
"jsonlint": "^1.6.3",
"jsonwebtoken": "^9.0.2",
"lightship": "7.2.0",
Expand Down
8 changes: 7 additions & 1 deletion app/pages/analyst/gis/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -166,11 +166,17 @@ const GisTab = () => {
const col = err?.posiition
? `and column ${err?.posiition}`
: '';
const erroneousCcbcNumber = err?.ccbc_number
? `for ${err?.ccbc_number}`
: '';
const errorAt = err?.line
? `at line ${err?.line}`
: erroneousCcbcNumber;
return (
<div
// eslint-disable-next-line react/no-array-index-key
key={index}
>{`Parsing error: ${err?.message} at line ${err?.line} ${col}`}</div>
>{`Parsing error: ${err?.message} ${errorAt} ${col}`}</div>
);
})}{' '}
Please check your file and try again.
Expand Down
113 changes: 56 additions & 57 deletions app/tests/backend/lib/gis-upload.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,9 +18,8 @@ function FormDataMock() {
}

global.FormData = jest.fn(() => {
return FormDataMock();
}
) as jest.Mock;
return FormDataMock();
}) as jest.Mock;

jest.setTimeout(10000000);

Expand Down Expand Up @@ -55,18 +54,18 @@ describe('The GIS import', () => {

mocked(performQuery).mockImplementation(async () => {
return {
data: { createGisData: {gisData:{rowId:1}}}
data: { createGisData: { gisData: { rowId: 1 } } },
};
});

const response = await request(app)
.post('/api/analyst/gis')
.set("Content-Type", "application/json")
.post('/api/analyst/gis')
.set('Content-Type', 'application/json')
.set('Connection', 'keep-alive')
.field("data", JSON.stringify({ name: "gis-data" }))
.attach("gis-data", `${__dirname}/gis-data-200.json`)
.field('data', JSON.stringify({ name: 'gis-data' }))
.attach('gis-data', `${__dirname}/gis-data-200.json`)
.expect(200);

expect(response.status).toBe(200);
});

Expand All @@ -80,26 +79,27 @@ describe('The GIS import', () => {

mocked(performQuery).mockImplementation(async () => {
return {
data: { createGisData: {gisData:{rowId:1}}}
data: { createGisData: { gisData: { rowId: 1 } } },
};
});
const expected={
"errors": [
const expected = {
errors: [
{
"line": 1,
"message": "must be array"
}
]
line: 1,
message: 'must be array',
ccbc_number: null,
},
],
};

const response = await request(app)
.post('/api/analyst/gis')
.set("Content-Type", "application/json")
.post('/api/analyst/gis')
.set('Content-Type', 'application/json')
.set('Connection', 'keep-alive')
.field("data", JSON.stringify({ name: "gis-data" }))
.attach("gis-data", `${__dirname}/gis-data-400a.json`)
.field('data', JSON.stringify({ name: 'gis-data' }))
.attach('gis-data', `${__dirname}/gis-data-400a.json`)
.expect(400);
expect(response.status).toBe(400);
expect(response.status).toBe(400);
expect(response.body).toEqual(expected);
});

Expand All @@ -113,38 +113,38 @@ describe('The GIS import', () => {

mocked(performQuery).mockImplementation(async () => {
return {
data: { createGisData: {gisData:{rowId:1}}}
data: { createGisData: { gisData: { rowId: 1 } } },
};
});

const expected={
"errors": [
const expected = {
errors: [
{
"line": 10,
"position": 26,
"message": "Value expected"
line: 10,
position: 26,
message: 'Value expected',
},
{
"line": 5,
"position": 20,
"message": "Expected comma"
line: 5,
position: 20,
message: 'Expected comma',
},
{
"line": 2,
"position": 17,
"message": "Value expected"
}
]
line: 2,
position: 17,
message: 'Value expected',
},
],
};

const response = await request(app)
.post('/api/analyst/gis')
.set("Content-Type", "application/json")
.post('/api/analyst/gis')
.set('Content-Type', 'application/json')
.set('Connection', 'keep-alive')
.field("data", JSON.stringify({ name: "gis-data" }))
.attach("gis-data", `${__dirname}/gis-data-400b.json`)
.field('data', JSON.stringify({ name: 'gis-data' }))
.attach('gis-data', `${__dirname}/gis-data-400b.json`)
.expect(400);
expect(response.status).toBe(400);
expect(response.status).toBe(400);
expect(response.body).toEqual(expected);
});

Expand All @@ -158,20 +158,19 @@ describe('The GIS import', () => {

mocked(performQuery).mockImplementation(async () => {
return {
data: { createGisData: {gisData:{rowId:1}}}
data: { createGisData: { gisData: { rowId: 1 } } },
};
});

const response = await request(app)
.post('/api/analyst/gis')
.set("Content-Type", "application/json")
.post('/api/analyst/gis')
.set('Content-Type', 'application/json')
.set('Connection', 'keep-alive')
.field("data", JSON.stringify({ name: "gis-data" }))
.attach("gis-data", `${__dirname}/gis-data-400.json`)
.field('data', JSON.stringify({ name: 'gis-data' }))
.attach('gis-data', `${__dirname}/gis-data-400.json`)
.expect(400);


expect(response.status).toBe(400);
expect(response.status).toBe(400);
});

it('should return details about validation errors', async () => {
Expand All @@ -184,26 +183,26 @@ describe('The GIS import', () => {

mocked(performQuery).mockImplementation(async () => {
return {
data: { createGisData: {gisData:{rowId:1}}}
data: { createGisData: { gisData: { rowId: 1 } } },
};
});

const response = await request(app)
.post('/api/analyst/gis')
.set("Content-Type", "application/json")
.post('/api/analyst/gis')
.set('Content-Type', 'application/json')
.set('Connection', 'keep-alive')
.field("data", JSON.stringify({ name: "gis-data" }))
.attach("gis-data", `${__dirname}/gis-data-errors.json`)
.field('data', JSON.stringify({ name: 'gis-data' }))
.attach('gis-data', `${__dirname}/gis-data-errors.json`)
.expect(400);

expect(response.status).toBe(400);
const {errors} = response.body;
expect(response.status).toBe(400);

const { errors } = response.body;
expect(errors).toBeTruthy();

expect(errors.length).toBe(2);
const first = errors[0];
expect(first.line).toBe(1);
expect(first.line).toBe(6);
expect(first.ccbc_number).toBe('CCBC-010001');
expect(first.message).toBe('GIS_TOTAL_HH must be number');
});
Expand Down
5 changes: 5 additions & 0 deletions app/yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -10507,6 +10507,11 @@ json-schema@0.4.0, json-schema@^0.4.0:
resolved "https://registry.yarnpkg.com/json-schema/-/json-schema-0.4.0.tgz#f7de4cf6efab838ebaeb3236474cbba5a1930ab5"
integrity sha512-es94M3nTIfsEPisRafak+HDLfHXnKBhV3vU5eqPcS3flIWqcxJWgXHXiey3YrpaNsanY5ei1VoYEbOzijuq9BA==

json-source-map@^0.6.1:
version "0.6.1"
resolved "https://registry.yarnpkg.com/json-source-map/-/json-source-map-0.6.1.tgz#e0b1f6f4ce13a9ad57e2ae165a24d06e62c79a0f"
integrity sha512-1QoztHPsMQqhDq0hlXY5ZqcEdUzxQEIxgFkKl4WUp2pgShObl+9ovi4kRh2TfvAfxAoHOJ9vIMEqk3k4iex7tg==

json-stable-stringify-without-jsonify@^1.0.1:
version "1.0.1"
resolved "https://registry.yarnpkg.com/json-stable-stringify-without-jsonify/-/json-stable-stringify-without-jsonify-1.0.1.tgz#9db7b59496ad3f3cfef30a75142d2d930ad72651"
Expand Down
Loading