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

Adjust overlaps check for zones and communities in /company route #154

Merged
merged 12 commits into from
Sep 26, 2024
15 changes: 15 additions & 0 deletions src/lib/sqlHelpers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -33,3 +33,18 @@ export const covers = (
): RawBuilder<boolean> => {
return sql<boolean>`ST_Covers(zone.area, ST_SetSRID(ST_MakePoint(${coordinates!.lng}, ${coordinates!.lat}),${SRID}))`;
};

export const intersects = async (compulsory: number, community: number): Promise<boolean> => {
return (
(await db
.selectFrom('zone as compulsory_area')
.where('compulsory_area.id', '=', compulsory)
.innerJoin(
(eb) => eb.selectFrom('zone').where('id', '=', community).selectAll().as('community'),
(join) => join.onTrue()
)
.where(sql<boolean>`ST_Area(ST_Intersection(compulsory_area.area, community.area)) >= 1`)
.selectAll()
.executeTakeFirst()) != undefined
);
};
36 changes: 13 additions & 23 deletions src/routes/(user)/company/+page.server.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,8 @@ import type { PageServerLoad, Actions } from './$types.js';
import { fail } from '@sveltejs/kit';
import { db } from '$lib/database';
import { AddressGuess, geoCode } from '$lib/api.js';
import { sql } from 'kysely';
import { covers } from '$lib/sqlHelpers.js';
import type { Coordinates } from '$lib/location.js';
import { covers, intersects } from '$lib/sqlHelpers.js';

export const load: PageServerLoad = async (event) => {
const companyId = event.locals.user?.company;
Expand Down Expand Up @@ -72,32 +72,13 @@ export const actions = {
return fail(400, { error: 'Die Addresse konnte nicht gefunden werden.' });
}

if (
!(await db
.selectFrom('zone')
.where((eb) =>
eb.and([eb('zone.id', '=', community_area), covers(eb, bestAddressGuess!.pos)])
)
.executeTakeFirst())
) {
if (!(await contains(community_area, bestAddressGuess.pos))) {
return fail(400, {
error: 'Die Addresse liegt nicht in der ausgewählten Gemeinde.'
});
}

if (
!(await db
.selectFrom('zone as compulsory_area')
.where('compulsory_area.id', '=', zone)
.innerJoin(
(eb) =>
eb.selectFrom('zone').where('id', '=', community_area).selectAll().as('community'),
(join) => join.onTrue()
)
.where(sql<boolean>`ST_Intersects(compulsory_area.area, community.area)`)
.selectAll()
.executeTakeFirst())
) {
if (!(await intersects(zone, community_area))) {
return fail(400, {
error: 'Die Gemeinde liegt nicht im Pflichtfahrgebiet.'
});
Expand All @@ -119,3 +100,12 @@ export const actions = {
return { success: true };
}
} satisfies Actions;

const contains = async (community: number, coordinates: Coordinates): Promise<boolean> => {
return (
(await db
.selectFrom('zone')
.where((eb) => eb.and([eb('zone.id', '=', community), covers(eb, coordinates!)]))
.executeTakeFirst()) != undefined
);
};
29 changes: 29 additions & 0 deletions src/routes/(user)/company/intersects.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
import { db } from '$lib/database';
import { sql } from 'kysely';
import { describe, expect, it } from 'vitest';
import { intersects } from '$lib/sqlHelpers';

describe('blacklisting test', () => {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
describe('blacklisting test', () => {
describe('intersects test', () => {

it('2 methods for checking zone overlaps yield same results', async () => {
const firstCommunityId = 7;
const lastCommunityId = 86;
for (let compulsory = 1; compulsory != firstCommunityId; ++compulsory) {
for (let community = firstCommunityId; community != lastCommunityId + 1; ++community) {
const area = await db
.selectFrom('zone as compulsory')
.where('compulsory.id', '=', compulsory)
.innerJoin(
(eb) => eb.selectFrom('zone').where('id', '=', community).selectAll().as('community'),
(join) => join.onTrue()
)
.select([
sql<string>`ST_AsGeoJSON(ST_Intersection(compulsory.area, community.area))`.as('area')
])
.executeTakeFirst();
const method1Result = area!.area.includes('Polygon') && !area!.area.includes('[]');
const method2Result = await intersects(compulsory, community);
expect(method1Result).toBe(method2Result);
}
}
});
});