From a7d094badd152b7ba0e697ff760d4662cc50b985 Mon Sep 17 00:00:00 2001 From: Aashish Dhakal <85501584+dhakalaashish@users.noreply.github.com> Date: Fri, 2 Feb 2024 12:16:31 +0545 Subject: [PATCH 1/2] Limit site area to 1 million hectares during creation --- apps/server/src/server/api/routers/site.ts | 55 ++++++++++--------- .../src/server/api/zodSchemas/site.schema.ts | 6 -- 2 files changed, 29 insertions(+), 32 deletions(-) diff --git a/apps/server/src/server/api/routers/site.ts b/apps/server/src/server/api/routers/site.ts index f90cb5992..c5d3a67be 100644 --- a/apps/server/src/server/api/routers/site.ts +++ b/apps/server/src/server/api/routers/site.ts @@ -14,32 +14,6 @@ export const siteRouter = createTRPCRouter({ .input(createSiteSchema) .mutation(async ({ctx, input}) => { const userId = ctx.user!.id; - // const userPlan = ctx.user!.plan ? ctx.user!.plan as UserPlan : 'basic'; - // const siteCount = await ctx.prisma.site.count({ - // where:{ - // userId, - // } - // }) - // if(userPlan === 'basic'){ - // if (siteCount >= 20){ - // return { - // status: 'error', - // httpStatus: 403, - // code: 'FORBIDDEN', - // message: `You've exceeded the fair site use limits of FireAlert. Please contact info@plant-for-the-planet to remove these limits for your account.`, - // }; - // } - // } - // if(userPlan === 'custom'){ - // if (siteCount >= 50){ - // return { - // status: 'error', - // httpStatus: 403, - // code: 'FORBIDDEN', - // message: `You've exceeded the fair site use limits of FireAlert. You cannot create any more sites.`, - // }; - // } - // } try { const origin = 'firealert'; const lastUpdated = new Date(); @@ -54,6 +28,35 @@ export const siteRouter = createTRPCRouter({ radius = input.radius; } + // Convert geometry to GeoJSON string for PostGIS function + const geometryGeoJSON = JSON.stringify(input.geometry); + + // Calculate detection area using PostGIS + const result = await ctx.prisma.$queryRaw`SELECT ST_Area( + ST_Transform( + ST_Buffer( + ST_Transform( + ST_SetSRID( + ST_GeomFromGeoJSON(${geometryGeoJSON}::text), + 4326 + ), + 3857 + ), + ${input.radius} + ), + 3857 + ) + ) AS area`; + + const detectionArea = result[0].area; // Assuming result is an array with the area as its first item + + // Check if the detection area exceeds 1 million hectares (10,000 square kilometers) + if (detectionArea > 1e8) { + throw new TRPCError({ + code: 'BAD_REQUEST', + message: 'Site area exceeds the maximum allowed size of 1 million hectares.', + }); + } const site = await ctx.prisma.site.create({ data: { origin: origin, diff --git a/apps/server/src/server/api/zodSchemas/site.schema.ts b/apps/server/src/server/api/zodSchemas/site.schema.ts index 9f7ed01c1..420bb934d 100644 --- a/apps/server/src/server/api/zodSchemas/site.schema.ts +++ b/apps/server/src/server/api/zodSchemas/site.schema.ts @@ -11,12 +11,6 @@ const PolygonSchema = z.object({ coordinates: z.array(z.array(z.union([z.tuple([z.number(), z.number()]), z.tuple([z.number(), z.number(), z.optional(z.number())])]))) }); - -// const MultiPolygonSchema = z.object({ -// type: z.literal("MultiPolygon"), -// coordinates: z.array(z.array(z.union([z.tuple([z.number(), z.number()]), z.tuple([z.number(), z.number(), z.optional(z.number())])]))) -// }); - const MultiPolygonSchema = z.object({ type: z.literal("MultiPolygon"), coordinates: z.array( From 340c8ac5cd01a59553328c0ef3c96884cd3563a4 Mon Sep 17 00:00:00 2001 From: Aashish Dhakal <85501584+dhakalaashish@users.noreply.github.com> Date: Mon, 19 Feb 2024 17:58:28 +0545 Subject: [PATCH 2/2] limit area to be 1e10 meters --- apps/server/src/server/api/routers/site.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/apps/server/src/server/api/routers/site.ts b/apps/server/src/server/api/routers/site.ts index c5d3a67be..33bd08ba6 100644 --- a/apps/server/src/server/api/routers/site.ts +++ b/apps/server/src/server/api/routers/site.ts @@ -51,7 +51,7 @@ export const siteRouter = createTRPCRouter({ const detectionArea = result[0].area; // Assuming result is an array with the area as its first item // Check if the detection area exceeds 1 million hectares (10,000 square kilometers) - if (detectionArea > 1e8) { + if (detectionArea > 1e10) { throw new TRPCError({ code: 'BAD_REQUEST', message: 'Site area exceeds the maximum allowed size of 1 million hectares.',